Files
CLIProxyAPI/internal/runtime/executor/helps/payload_mutations.go
Luis Pater 53c1e7e2dd perf(util): introduce GetGJSONBytesNoCopy for efficient JSON parsing without data duplication
- Added `GetGJSONBytesNoCopy` in `internal/util` for safe, no-copy JSON parse operations leveraging `unsafe`.
- Replaced `gjson.GetBytes` with the new helper in key payload processing paths (`kimi_executor`, `vertex_payload_helpers`, etc.) to improve performance.
- Added unit tests for behavior validation, including edge cases with empty input.
2026-07-20 15:37:55 +08:00

82 lines
2.2 KiB
Go

package helps
import (
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// SetStringIfDifferent updates path only when its value is not already the
// canonical JSON string. Values with another JSON type are still normalized.
func SetStringIfDifferent(payload []byte, path, value string) []byte {
current := gjson.GetBytes(payload, path)
if current.Type == gjson.String && current.String() == value {
return payload
}
updated, errSet := sjson.SetBytes(payload, path, value)
if errSet != nil {
return payload
}
return updated
}
// SetBoolIfDifferent updates path only when its value is not already the
// canonical JSON boolean. Values with another JSON type are still normalized.
func SetBoolIfDifferent(payload []byte, path string, value bool) []byte {
current := gjson.GetBytes(payload, path)
if (value && current.Type == gjson.True) || (!value && current.Type == gjson.False) {
return payload
}
updated, errSet := sjson.SetBytes(payload, path, value)
if errSet != nil {
return payload
}
return updated
}
// SetRawIfDifferent updates path only when the existing raw JSON is identical.
func SetRawIfDifferent(payload []byte, path string, value []byte) []byte {
current := gjson.GetBytes(payload, path)
if current.Exists() && len(current.Indexes) == 0 && current.Raw == string(value) {
return payload
}
updated, errSet := sjson.SetRawBytes(payload, path, value)
if errSet != nil {
return payload
}
return updated
}
// JoinRawJSONArray joins validated raw JSON array items without re-encoding them.
func JoinRawJSONArray(items [][]byte) []byte {
size := len(items) + 1
for _, item := range items {
size += len(item)
}
out := make([]byte, 0, size)
out = append(out, '[')
for index, item := range items {
if index > 0 {
out = append(out, ',')
}
out = append(out, item...)
}
return append(out, ']')
}
// JoinRawJSONStrings joins raw JSON array items held as strings.
func JoinRawJSONStrings(items []string) []byte {
size := len(items) + 1
for _, item := range items {
size += len(item)
}
out := make([]byte, 0, size)
out = append(out, '[')
for index, item := range items {
if index > 0 {
out = append(out, ',')
}
out = append(out, item...)
}
return append(out, ']')
}