Files
CLIProxyAPI/internal/util/sjson_delete.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson
  and encoding/base64 with github.com/MoeForge/moego/base64 across the main
  module and examples
- Introduce cross-method *ojson.Doc request rewriting pipelines across
  thinking, signature, client/codex, runtime/executor/helps, pluginhost, and
  all provider executors with FinalizePayloadDoc as the final semantic barrier
- Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations)
  to batch edits on ojson.Doc
- Upgrade read-only byte paths to ojson.GetBytesNoCopy,
  ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit
  strings.Clone ownership protection across streaming and persistent state
- Use base64.StdEncoding.ValidateString for zero-allocation signature checks
  and base64.EncodeDataURL for single-allocation data URL construction
2026-11-03 03:05:04 +08:00

33 lines
1.1 KiB
Go

package util
import (
"github.com/MoeForge/moego/ojson"
)
// DeleteJSONBytesIfExists behaves like ojson.DeleteBytes but returns data
// unchanged when path is absent. ojson.DeleteBytes copies the document at
// least once even when there is nothing to delete (a partial prefix copy is
// built and discarded, then the whole input is copied again before sjson
// returns the original slice), which dominates allocation when callers
// defensively delete alias spellings that are usually missing. path must be a
// nonempty literal dotted path (gjson-compatible escapes like \. are
// supported); sjson's "-1" last-element delete and ":" forced-object-key syntax
// are not supported here. Behavior otherwise matches ojson.DeleteBytes.
func DeleteJSONBytesIfExists(data []byte, path string) ([]byte, error) {
if !GetGJSONBytesNoCopy(data, path).Exists() {
return data, nil
}
return ojson.DeleteBytes(data, path)
}
// DeleteJSONDocIfExists deletes path from doc only when the path exists.
func DeleteJSONDocIfExists(doc *ojson.Doc, path string) error {
if doc == nil {
return nil
}
if !doc.Get(path).Exists() {
return nil
}
return doc.Delete(path)
}