fix: avoid delete the key with empty value in object (lua table)
Signed-off-by: chaosi-zju <chaosi@zju.edu.cn>
This commit is contained in:
parent
bc9316705c
commit
e2babc3d24
4
go.mod
4
go.mod
|
@ -21,6 +21,8 @@ require (
|
|||
github.com/spf13/cobra v1.7.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/stretchr/testify v1.8.3
|
||||
github.com/tidwall/gjson v1.17.1
|
||||
github.com/tidwall/sjson v1.2.5
|
||||
github.com/vektra/mockery/v2 v2.10.0
|
||||
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64
|
||||
go.uber.org/mock v0.4.0
|
||||
|
@ -147,6 +149,8 @@ require (
|
|||
github.com/stoewer/go-strcase v1.2.0 // indirect
|
||||
github.com/stretchr/objx v0.5.0 // indirect
|
||||
github.com/subosito/gotenv v1.4.2 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/xlab/treeprint v1.2.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
|
||||
|
|
9
go.sum
9
go.sum
|
@ -746,7 +746,16 @@ github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
|
|||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
|
||||
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
|
||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
|
||||
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
|
||||
|
|
|
@ -63,6 +63,7 @@ spec:
|
|||
containers:
|
||||
- image: nginx:alpine
|
||||
name: nginx
|
||||
status: {}
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
|
@ -140,7 +140,7 @@ func (vm *VM) GetReplicas(obj *unstructured.Unstructured, script string) (replic
|
|||
replicaRequirementResult := results[1]
|
||||
requires = &workv1alpha2.ReplicaRequirements{}
|
||||
if replicaRequirementResult.Type() == lua.LTTable {
|
||||
err = ConvertLuaResultInto(replicaRequirementResult, requires)
|
||||
err = ConvertLuaResultInto(replicaRequirementResult.(*lua.LTable), requires)
|
||||
if err != nil {
|
||||
klog.Errorf("ConvertLuaResultToReplicaRequirements err %v", err.Error())
|
||||
return 0, nil, err
|
||||
|
@ -164,7 +164,7 @@ func (vm *VM) ReviseReplica(object *unstructured.Unstructured, replica int64, sc
|
|||
luaResult := results[0]
|
||||
reviseReplicaResult := &unstructured.Unstructured{}
|
||||
if luaResult.Type() == lua.LTTable {
|
||||
err := ConvertLuaResultInto(luaResult, reviseReplicaResult)
|
||||
err := ConvertLuaResultInto(luaResult.(*lua.LTable), reviseReplicaResult, object)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -237,7 +237,7 @@ func (vm *VM) Retain(desired *unstructured.Unstructured, observed *unstructured.
|
|||
luaResult := results[0]
|
||||
retainResult := &unstructured.Unstructured{}
|
||||
if luaResult.Type() == lua.LTTable {
|
||||
err := ConvertLuaResultInto(luaResult, retainResult)
|
||||
err := ConvertLuaResultInto(luaResult.(*lua.LTable), retainResult, desired, observed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ func (vm *VM) AggregateStatus(object *unstructured.Unstructured, items []workv1a
|
|||
luaResult := results[0]
|
||||
aggregateStatus := &unstructured.Unstructured{}
|
||||
if luaResult.Type() == lua.LTTable {
|
||||
err := ConvertLuaResultInto(luaResult, aggregateStatus)
|
||||
err := ConvertLuaResultInto(luaResult.(*lua.LTable), aggregateStatus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -293,7 +293,7 @@ func (vm *VM) ReflectStatus(object *unstructured.Unstructured, script string) (s
|
|||
}
|
||||
|
||||
status = &runtime.RawExtension{}
|
||||
err = ConvertLuaResultInto(luaStatusResult, status)
|
||||
err = ConvertLuaResultInto(luaStatusResult.(*lua.LTable), status)
|
||||
return status, err
|
||||
}
|
||||
|
||||
|
@ -309,7 +309,7 @@ func (vm *VM) GetDependencies(object *unstructured.Unstructured, script string)
|
|||
if luaResult.Type() != lua.LTTable {
|
||||
return nil, fmt.Errorf("expect the returned requires type is table but got %s", luaResult.Type())
|
||||
}
|
||||
err = ConvertLuaResultInto(luaResult, &dependencies)
|
||||
err = ConvertLuaResultInto(luaResult.(*lua.LTable), &dependencies)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -17,92 +17,225 @@ limitations under the License.
|
|||
package luavm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"k8s.io/apimachinery/pkg/conversion"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
luajson "layeh.com/gopher-json"
|
||||
)
|
||||
|
||||
// ConvertLuaResultInto convert lua result to obj
|
||||
func ConvertLuaResultInto(luaResult lua.LValue, obj interface{}) error {
|
||||
t, err := conversion.EnforcePtr(obj)
|
||||
// @param references: the method of encoding a lua table is flawed and needs to be referenced by the same Kind object.
|
||||
// take `Retain(desired, observed)` for example, we have problem to convert empty fields, but empty fields is most likely
|
||||
// from desired or observed object, so take {desired, observed} object as references to obtain more accurate encoding values.
|
||||
func ConvertLuaResultInto(luaResult *lua.LTable, obj interface{}, references ...any) error {
|
||||
objReflect, err := conversion.EnforcePtr(obj)
|
||||
if err != nil {
|
||||
return fmt.Errorf("obj is not pointer")
|
||||
}
|
||||
|
||||
// For example, `GetReplicas` returns requirement with empty:
|
||||
// {
|
||||
// nodeClaim: {},
|
||||
// resourceRequest: {
|
||||
// cpu: "100m"
|
||||
// }
|
||||
// }
|
||||
// Luajson encodes it to
|
||||
// {"nodeClaim": [], "resourceRequest": {"cpu": "100m"}}
|
||||
//
|
||||
// While go json fails to unmarshal `[]` to ReplicaRequirements.NodeClaim object.
|
||||
// ReplicaRequirements object.
|
||||
//
|
||||
// Here we handle it as follows:
|
||||
// 1. Walk the object (lua table), delete the key with empty value (`nodeClaim` in this example):
|
||||
// {
|
||||
// resourceRequest: {
|
||||
// cpu: "100m"
|
||||
// }
|
||||
// }
|
||||
// 2. Encode the object with luajson to be:
|
||||
// {"resourceRequest": {"cpu": "100m"}}
|
||||
// 4. Finally, unmarshal the new json to object, get
|
||||
// {
|
||||
// resourceRequest: {
|
||||
// cpu: "100m"
|
||||
// }
|
||||
// }
|
||||
isEmptyDic := func(v *lua.LTable) bool {
|
||||
count := 0
|
||||
v.ForEach(func(lua.LValue, lua.LValue) {
|
||||
count++
|
||||
})
|
||||
return count == 0
|
||||
}
|
||||
|
||||
var walkValue func(v lua.LValue)
|
||||
walkValue = func(v lua.LValue) {
|
||||
if t, ok := v.(*lua.LTable); ok {
|
||||
t.ForEach(func(key lua.LValue, value lua.LValue) {
|
||||
if tt, ok := value.(*lua.LTable); ok {
|
||||
if isEmptyDic(tt) {
|
||||
// set nil to delete key
|
||||
t.RawSetH(key, lua.LNil)
|
||||
} else {
|
||||
walkValue(value)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
walkValue(luaResult)
|
||||
|
||||
// 1. convert lua.LTable to json bytes
|
||||
jsonBytes, err := luajson.Encode(luaResult)
|
||||
if err != nil {
|
||||
return fmt.Errorf("json Encode obj eroor %v", err)
|
||||
return fmt.Errorf("encode lua result to json failed: %+v", err)
|
||||
}
|
||||
|
||||
// for lua an empty object by json encode be [] not {}
|
||||
if t.Kind() == reflect.Struct && len(jsonBytes) > 1 && jsonBytes[0] == '[' {
|
||||
jsonBytes[0], jsonBytes[len(jsonBytes)-1] = '{', '}'
|
||||
// 2. the json bytes above step got may not be expected in some special case, and should be adjusted manually.
|
||||
// because as for lua.LTable type, if a field is non-empty struct, it can be correctly encoded into struct format json;
|
||||
// but if a field is empty struct, it will be encoded into empty slice format as '[]' (root cause is empty lua.LTable
|
||||
// can not be distinguished from empty slice or empty struct).
|
||||
//
|
||||
// Supposing an object contains empty fileds, like following one has an empty slice field and an empty struct field.
|
||||
// e.g: struct{one-filed: {}, another-field: []}
|
||||
//
|
||||
// When it is converted to lua.LTable, empty slice and empty struct are all converted to lua.LTable{}, which can't be distinguished.
|
||||
// e.g: lua.LTable{one-filed: lua.LTable{}, another-field: lua.LTable{}}
|
||||
//
|
||||
// When the lua.LTable is encoded into json, the two empty field are all encoded into empty slice format as '[]'.
|
||||
// e.g: {one-filed: [], another-field: []}
|
||||
//
|
||||
// Actually, this json value is not the expected, so we need to do some extra processing.
|
||||
// we should convert `one-filed` back to {}, and keep `another-field` unchanged.
|
||||
// expected: {one-filed: {}, another-field: []}
|
||||
isStruct := objReflect.Kind() == reflect.Struct
|
||||
jsonBytes, err = convertEmptySliceToEmptyStructPartly(jsonBytes, isStruct, references)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert empty object back to empty slice failed: %+v", err)
|
||||
}
|
||||
|
||||
// 3. convert the converted json bytes to target object
|
||||
err = json.Unmarshal(jsonBytes, obj)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can not unmarshal %v to %#v:%v", string(jsonBytes), obj, err)
|
||||
return fmt.Errorf("json unmarshal obj failed: %+v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertEmptySliceToEmptyStructPartly convert certain json fields with `[]` value which originally is empty struct back to `{}`.
|
||||
// However, given a json bytes like {one-filed:[],another-field:[]}, the way to judge `one-filed:[]` should convert to `one-filed:{}`,
|
||||
// while `another-field:[]` should keep unchanged can be described as follows:
|
||||
//
|
||||
// Actually, empty struct/slice value are not generated out of thin air, take `Retain(desired, observed)` for example,
|
||||
// empty struct/slice value may come from three sources:
|
||||
// 1. originally from desired object
|
||||
// 2. originally from observed object
|
||||
// 3. user added in Custom ResourceInterpreter (the probability is low and not recommended)
|
||||
//
|
||||
// For the first two cases, we just need to iterate through the json bytes of `desired` and `observed` objects (called as references),
|
||||
// and record the empty slice fields in map `fieldOfEmptySlice`, record the empty struct fields in map `fieldOfEmptyStruct`,
|
||||
// finally iterate through the given json bytes:
|
||||
// 1. if an empty field ever existed in `fieldOfEmptySlice`, then it originally is slice, should keep unchanged.
|
||||
// 2. if an empty field ever existed in `fieldOfEmptyStruct`, then it originally is struct, should be change back to struct.
|
||||
// 3. if an empty field not ever existed in either map, is user added in Custom ResourceInterpreter,
|
||||
// we still remove this redundant field (compatible with previous).
|
||||
func convertEmptySliceToEmptyStructPartly(objBytes []byte, isStruct bool, references ...any) ([]byte, error) {
|
||||
// 1. if an empty lua object is originally struct type (not slice), just return {}; if slice, just return [].
|
||||
if bytes.Equal(objBytes, []byte("[]")) {
|
||||
if isStruct {
|
||||
return []byte("{}"), nil
|
||||
}
|
||||
return []byte("[]"), nil
|
||||
}
|
||||
|
||||
// 2. iterate through the json bytes of referenced objects and record the empty value fields.
|
||||
fieldOfEmptySlice, fieldOfEmptyStruct := sets.New[string](), sets.New[string]()
|
||||
for _, reference := range references {
|
||||
jsonBytes, err := json.Marshal(reference)
|
||||
if err != nil {
|
||||
return objBytes, fmt.Errorf("json marshal reference failed: %+v", err)
|
||||
}
|
||||
jsonVal := gjson.Parse(string(jsonBytes))
|
||||
|
||||
fieldOfEmptySliceTemp, fieldOfEmptyStructTemp := traverseToFindEmptyField(jsonVal, nil)
|
||||
fieldOfEmptySlice = fieldOfEmptySlice.Union(fieldOfEmptySliceTemp)
|
||||
fieldOfEmptyStruct = fieldOfEmptyStruct.Union(fieldOfEmptyStructTemp)
|
||||
}
|
||||
|
||||
// 3. iterate through the given json bytes, and check an empty slice field whether ever existed in referenced objects.
|
||||
objJSONStr := string(objBytes)
|
||||
objJSON := gjson.Parse(objJSONStr)
|
||||
fieldOfEmptySliceToStruct, fieldOfEmptySliceToDelete := traverseToFindEmptyFieldNeededModify(objJSON, nil, nil, fieldOfEmptySlice, fieldOfEmptyStruct)
|
||||
|
||||
// 4. if an empty slice field is originally a struct, change it back to {}; if originally not exist, delete the field.
|
||||
var err error
|
||||
for _, fieldPath := range fieldOfEmptySliceToStruct.UnsortedList() {
|
||||
objJSONStr, err = sjson.Set(objJSONStr, fieldPath, struct{}{})
|
||||
if err != nil {
|
||||
return objBytes, fmt.Errorf("sjson set empty slice to empty struct failed: %+v", err)
|
||||
}
|
||||
}
|
||||
for _, fieldPath := range fieldOfEmptySliceToDelete.UnsortedList() {
|
||||
objJSONStr, err = sjson.Delete(objJSONStr, fieldPath)
|
||||
if err != nil {
|
||||
return objBytes, fmt.Errorf("sjson delete empty slice field failed: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return []byte(objJSONStr), nil
|
||||
}
|
||||
|
||||
// traverseToFindEmptyField get the field path with empty values by traverse a gjson.Result
|
||||
//
|
||||
// e.g: root={"spec":{"aa":{},"bb":[],"cc":["x"],"dd":{"ee":{}}}}
|
||||
// we traverse the root and record every level filed name into `fieldPath` variable.
|
||||
// 1. when traverse to the field `spec.aa`, we got an empty struct, so add it into `fieldOfEmptyStruct` variable.
|
||||
// 2. when traverse to the field `spec.bb`, we got an empty slice, so add it into `fieldOfEmptySlice` variable.
|
||||
// 3. when traverse to the field `spec.dd.ee`, we got another empty struct, so add it into `fieldOfEmptyStruct` variable.
|
||||
//
|
||||
// So, finally, fieldOfEmptySlice={"spec.bb"}, fieldOfEmptyStruct={"spec.aa", "spec.dd.ee"}
|
||||
func traverseToFindEmptyField(root gjson.Result, fieldPath []string) (sets.Set[string], sets.Set[string]) {
|
||||
rootIsNotArray := !root.IsArray()
|
||||
fieldOfEmptySlice, fieldOfEmptyStruct := sets.New[string](), sets.New[string]()
|
||||
|
||||
root.ForEach(func(key, value gjson.Result) bool {
|
||||
curFieldPath := fieldPath
|
||||
// e.g: root={"rules":[{"ruleName":[]}}]}, when we traverse `rules` field, which is an array.
|
||||
// the first element is key=0, value={"ruleName":[]}
|
||||
// however, we expected to record the `ruleName` field path as `rules.ruleName`, rather than `rules.0.ruleName`.
|
||||
if rootIsNotArray {
|
||||
curFieldPath = append(fieldPath, key.String())
|
||||
}
|
||||
curFieldStr := strings.Join(curFieldPath, ".")
|
||||
|
||||
if value.IsArray() && len(value.Array()) == 0 {
|
||||
fieldOfEmptySlice.Insert(curFieldStr)
|
||||
} else if value.IsObject() && len(value.Map()) == 0 {
|
||||
fieldOfEmptyStruct.Insert(curFieldStr)
|
||||
} else if value.IsArray() || value.IsObject() {
|
||||
childEmptySlice, childEmptyStruct := traverseToFindEmptyField(value, curFieldPath)
|
||||
fieldOfEmptySlice = fieldOfEmptySlice.Union(childEmptySlice)
|
||||
fieldOfEmptyStruct = fieldOfEmptyStruct.Union(childEmptyStruct)
|
||||
}
|
||||
|
||||
return true // keep iterating
|
||||
})
|
||||
|
||||
return fieldOfEmptySlice, fieldOfEmptyStruct
|
||||
}
|
||||
|
||||
// traverseToFindEmptyFieldNeededModify find the field with empty values which needed to be modified by traverse a gjson.Result
|
||||
//
|
||||
// e.g: root={"spec":{"aa":[],"bb":[],"cc":["x"],"dd":{"ee":[],"ff":[]}}}
|
||||
// fieldOfEmptySlice={"spec.bb"}, fieldOfEmptyStruct={"spec.aa", "spec.dd.ee"}
|
||||
//
|
||||
// we traverse the root and record every level filed name into `fieldPath`、`fieldPathWithArrayIndex` variable,
|
||||
// then judge whether the field path exist in `fieldOfEmptySlice` or `fieldOfEmptyStruct` variable:
|
||||
// 1. when traverse to the field `spec.aa`, we got an empty slice, but this filed exists in `fieldOfEmptyStruct`,
|
||||
// so, it originally is struct, which needed to be modified back, we add it into `fieldOfEmptySliceToStruct` variable.
|
||||
// 2. when traverse to the field `spec.bb`, we got an empty slice, and it exists in `fieldOfEmptySlice`,
|
||||
// so, it originally is slice, which should keep unchanged.
|
||||
// 3. when traverse to the field `spec.dd.ee`, we got an empty slice, but it also exists in `fieldOfEmptyStruct`,
|
||||
// so, it originally is struct too, we add it into `fieldOfEmptySliceToStruct` variable.
|
||||
// 4. when traverse to the field `spec.dd.ff`, we got an empty slice, but it not exists in either map variable,
|
||||
// so, it orinally not exist, we can't judge whether it is struct, so we add it into `fieldOfEmptySliceToDelete` variable to remove it.
|
||||
//
|
||||
// So, finally, fieldOfEmptySliceToStruct={"spec.aa", "spec.dd.ee"}, fieldOfEmptySliceToDelete={"spec.dd.ff"}
|
||||
func traverseToFindEmptyFieldNeededModify(root gjson.Result, fieldPath, fieldPathWithArrayIndex []string, fieldOfEmptySlice, fieldOfEmptyStruct sets.Set[string]) (sets.Set[string], sets.Set[string]) {
|
||||
rootIsNotArray := !root.IsArray()
|
||||
fieldOfEmptySliceToStruct, fieldOfEmptySliceToDelete := sets.New[string](), sets.New[string]()
|
||||
|
||||
root.ForEach(func(key, value gjson.Result) bool {
|
||||
curFieldPath := fieldPath
|
||||
// e.g: root={"rules":[{"ruleName":[]}}]}, when we traverse `rules` field, which is an array.
|
||||
// the first element is key=0, value={"ruleName":[]}
|
||||
// we record `rules.ruleName` into curFieldPath, and record `rules.0.ruleName` into curFieldPathWithArrayIndex.
|
||||
if rootIsNotArray {
|
||||
curFieldPath = append(fieldPath, key.String())
|
||||
}
|
||||
curFieldPathWithArrayIndex := append(fieldPathWithArrayIndex, key.String())
|
||||
|
||||
if value.IsArray() && len(value.Array()) == 0 {
|
||||
curFieldPathStr := strings.Join(curFieldPath, ".")
|
||||
curFieldPathWithIndexStr := strings.Join(curFieldPathWithArrayIndex, ".")
|
||||
|
||||
if fieldOfEmptyStruct.Has(curFieldPathStr) {
|
||||
// if an empty object filed is originally an empty struct, we need to modify it back to struct.
|
||||
fieldOfEmptySliceToStruct.Insert(curFieldPathWithIndexStr)
|
||||
} else if !fieldOfEmptySlice.Has(curFieldPathStr) {
|
||||
// if an empty object filed is originally not exist (neither slice nor struct), we need to delete it.
|
||||
fieldOfEmptySliceToDelete.Insert(curFieldPathWithIndexStr)
|
||||
}
|
||||
} else if value.IsArray() || value.IsObject() {
|
||||
childEmptySliceToStruct, childEmptySliceToDelete := traverseToFindEmptyFieldNeededModify(value, curFieldPath,
|
||||
curFieldPathWithArrayIndex, fieldOfEmptySlice, fieldOfEmptyStruct)
|
||||
fieldOfEmptySliceToStruct = fieldOfEmptySliceToStruct.Union(childEmptySliceToStruct)
|
||||
fieldOfEmptySliceToDelete = fieldOfEmptySliceToDelete.Union(childEmptySliceToDelete)
|
||||
}
|
||||
|
||||
return true // keep iterating
|
||||
})
|
||||
|
||||
return fieldOfEmptySliceToStruct, fieldOfEmptySliceToDelete
|
||||
}
|
||||
|
||||
// ConvertLuaResultToInt convert lua result to int.
|
||||
func ConvertLuaResultToInt(luaResult lua.LValue) (int32, error) {
|
||||
if luaResult.Type() != lua.LTNumber {
|
||||
|
|
|
@ -17,13 +17,19 @@ limitations under the License.
|
|||
package luavm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
|
||||
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
|
||||
)
|
||||
|
||||
func TestConvertLuaResultInto(t *testing.T) {
|
||||
func TestConvertLuaResultToStruct(t *testing.T) {
|
||||
type barStruct struct {
|
||||
Bar string
|
||||
}
|
||||
|
@ -31,8 +37,19 @@ func TestConvertLuaResultInto(t *testing.T) {
|
|||
Bar barStruct
|
||||
}
|
||||
|
||||
type specStruct struct {
|
||||
EmptyMap map[string]string
|
||||
NonEmptyMap map[string]string
|
||||
EmptySlice []string
|
||||
NonEmptySlice []string
|
||||
OtherField string
|
||||
}
|
||||
type foolStruct struct {
|
||||
Spec specStruct
|
||||
}
|
||||
|
||||
type args struct {
|
||||
luaResult lua.LValue
|
||||
luaResult *lua.LTable
|
||||
obj interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
|
@ -42,7 +59,7 @@ func TestConvertLuaResultInto(t *testing.T) {
|
|||
want interface{}
|
||||
}{
|
||||
{
|
||||
name: "not a pointer",
|
||||
name: "obj is not a pointer",
|
||||
args: args{
|
||||
luaResult: &lua.LTable{},
|
||||
obj: struct{}{},
|
||||
|
@ -60,32 +77,9 @@ func TestConvertLuaResultInto(t *testing.T) {
|
|||
want: &fooStruct{},
|
||||
},
|
||||
{
|
||||
name: "empty table into slice",
|
||||
name: "table with empty table into struct",
|
||||
args: args{
|
||||
luaResult: &lua.LTable{},
|
||||
obj: &[]string{},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &[]string{},
|
||||
},
|
||||
{
|
||||
name: "non-empty table into slice",
|
||||
args: args{
|
||||
luaResult: func() lua.LValue {
|
||||
v := &lua.LTable{}
|
||||
v.Append(lua.LString("foo"))
|
||||
v.Append(lua.LString("bar"))
|
||||
return v
|
||||
}(),
|
||||
obj: &[]string{},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &[]string{"foo", "bar"},
|
||||
},
|
||||
{
|
||||
name: "table with empty table into slice",
|
||||
args: args{
|
||||
luaResult: func() lua.LValue {
|
||||
luaResult: func() *lua.LTable {
|
||||
v := &lua.LTable{}
|
||||
v.RawSetString("Bar", &lua.LTable{})
|
||||
return v
|
||||
|
@ -98,7 +92,7 @@ func TestConvertLuaResultInto(t *testing.T) {
|
|||
{
|
||||
name: "struct is not empty, and convert successfully",
|
||||
args: args{
|
||||
luaResult: func() lua.LValue {
|
||||
luaResult: func() *lua.LTable {
|
||||
bar := &lua.LTable{}
|
||||
bar.RawSetString("Bar", lua.LString("bar"))
|
||||
|
||||
|
@ -113,15 +107,345 @@ func TestConvertLuaResultInto(t *testing.T) {
|
|||
Bar: barStruct{Bar: "bar"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty table into slice",
|
||||
args: args{
|
||||
luaResult: &lua.LTable{},
|
||||
obj: &[]string{},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &[]string{},
|
||||
},
|
||||
{
|
||||
name: "non-empty table into slice",
|
||||
args: args{
|
||||
luaResult: func() *lua.LTable {
|
||||
v := &lua.LTable{}
|
||||
v.Append(lua.LString("foo"))
|
||||
v.Append(lua.LString("bar"))
|
||||
return v
|
||||
}(),
|
||||
obj: &[]string{},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &[]string{"foo", "bar"},
|
||||
},
|
||||
{
|
||||
name: "table into DependentObjectReference slice",
|
||||
args: args{
|
||||
luaResult: func() *lua.LTable {
|
||||
item := &lua.LTable{}
|
||||
item.RawSetString("apiVersion", lua.LString("demo"))
|
||||
item.RawSetString("kind", lua.LString("demo"))
|
||||
|
||||
v := &lua.LTable{}
|
||||
v.Append(item)
|
||||
return v
|
||||
}(),
|
||||
obj: &[]configv1alpha1.DependentObjectReference{},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &[]configv1alpha1.DependentObjectReference{{
|
||||
APIVersion: "demo",
|
||||
Kind: "demo",
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "convert struct with empty field",
|
||||
args: args{
|
||||
luaResult: func() *lua.LTable {
|
||||
innerMap := &lua.LTable{}
|
||||
innerMap.RawSetString("test-key", lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
innerSlice := &lua.LTable{}
|
||||
innerSlice.Append(lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
spec := &lua.LTable{}
|
||||
spec.RawSetH(lua.LString("EmptyMap"), &lua.LTable{})
|
||||
spec.RawSetH(lua.LString("NonEmptyMap"), innerMap)
|
||||
spec.RawSetH(lua.LString("EmptySlice"), &lua.LTable{})
|
||||
spec.RawSetH(lua.LString("NonEmptySlice"), innerSlice)
|
||||
spec.RawSetString("OtherField", lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
v := &lua.LTable{}
|
||||
v.RawSetString("Spec", spec)
|
||||
return v
|
||||
}(),
|
||||
obj: &foolStruct{},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &foolStruct{
|
||||
Spec: specStruct{
|
||||
NonEmptyMap: map[string]string{"test-key": `\"trap-string\":[]`},
|
||||
NonEmptySlice: []string{`\"trap-string\":[]`},
|
||||
OtherField: `\"trap-string\":[]`,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := ConvertLuaResultInto(tt.args.luaResult, tt.args.obj); (err != nil) != tt.wantErr {
|
||||
t.Errorf("ConvertLuaResultInto() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("ConvertLuaResultToStruct() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if got := tt.args.obj; !reflect.DeepEqual(tt.want, got) {
|
||||
t.Errorf("ConvertLuaResultToStruct() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertLuaResultToUnstruct(t *testing.T) {
|
||||
type args struct {
|
||||
luaResult *lua.LTable
|
||||
references []*unstructured.Unstructured
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *unstructured.Unstructured
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "convert unstructured with empty field not referring to references",
|
||||
args: args{
|
||||
luaResult: func() *lua.LTable {
|
||||
innerMap := &lua.LTable{}
|
||||
innerMap.RawSetString("test-key", lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
innerSlice := &lua.LTable{}
|
||||
innerSlice.Append(lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
spec := &lua.LTable{}
|
||||
spec.RawSetH(lua.LString("EmptyMap"), &lua.LTable{})
|
||||
spec.RawSetH(lua.LString("NonEmptyMap"), innerMap)
|
||||
spec.RawSetH(lua.LString("EmptySlice"), &lua.LTable{})
|
||||
spec.RawSetH(lua.LString("NonEmptySlice"), innerSlice)
|
||||
spec.RawSetString("OtherField", lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
v := &lua.LTable{}
|
||||
v.RawSetString("kind", lua.LString("demo"))
|
||||
v.RawSetString("spec", spec)
|
||||
return v
|
||||
}(),
|
||||
references: nil,
|
||||
},
|
||||
wantErr: false,
|
||||
want: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "demo",
|
||||
"spec": map[string]interface{}{
|
||||
"NonEmptyMap": map[string]interface{}{"test-key": `\"trap-string\":[]`},
|
||||
"NonEmptySlice": []interface{}{`\"trap-string\":[]`},
|
||||
"OtherField": `\"trap-string\":[]`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "convert unstructured with empty field referring to references",
|
||||
args: args{
|
||||
luaResult: func() *lua.LTable {
|
||||
innerMap := &lua.LTable{}
|
||||
innerMap.RawSetString("test-key", lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
innerSlice := &lua.LTable{}
|
||||
innerSlice.Append(lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
spec := &lua.LTable{}
|
||||
spec.RawSetH(lua.LString("EmptyMap"), &lua.LTable{})
|
||||
spec.RawSetH(lua.LString("NonEmptyMap"), innerMap)
|
||||
spec.RawSetH(lua.LString("EmptySlice"), &lua.LTable{})
|
||||
spec.RawSetH(lua.LString("NonEmptySlice"), innerSlice)
|
||||
spec.RawSetString("OtherField", lua.LString(`\"trap-string\":[]`))
|
||||
|
||||
v := &lua.LTable{}
|
||||
v.RawSetString("kind", lua.LString("demo"))
|
||||
v.RawSetString("spec", spec)
|
||||
return v
|
||||
}(),
|
||||
references: []*unstructured.Unstructured{{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "demo",
|
||||
"spec": map[string]interface{}{"EmptySlice": []interface{}{}, "EmptyMap": map[string]interface{}{}}},
|
||||
}},
|
||||
},
|
||||
wantErr: false,
|
||||
want: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "demo",
|
||||
"spec": map[string]interface{}{
|
||||
"EmptyMap": map[string]interface{}{},
|
||||
"NonEmptyMap": map[string]interface{}{"test-key": `\"trap-string\":[]`},
|
||||
"EmptySlice": []interface{}{},
|
||||
"NonEmptySlice": []interface{}{`\"trap-string\":[]`},
|
||||
"OtherField": `\"trap-string\":[]`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var got = &unstructured.Unstructured{}
|
||||
err := ConvertLuaResultInto(tt.args.luaResult, got, tt.args.references)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ConvertLuaResultInto() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ConvertLuaResultInto() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_traverseToFindEmptyField(t *testing.T) {
|
||||
type args struct {
|
||||
root gjson.Result
|
||||
fieldPath []string
|
||||
}
|
||||
type wants struct {
|
||||
fieldOfEmptySlice sets.Set[string]
|
||||
fieldOfEmptyStruct sets.Set[string]
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wants wants
|
||||
}{
|
||||
{
|
||||
name: "traverse to find empty field",
|
||||
args: args{
|
||||
root: gjson.Parse(`{"spec":[{"aa":{},"bb":[],"cc":["x"],"dd":{"ee":{}}}]}`),
|
||||
fieldPath: nil,
|
||||
},
|
||||
wants: wants{
|
||||
fieldOfEmptySlice: sets.New[string]("spec.bb"),
|
||||
fieldOfEmptyStruct: sets.New[string]("spec.aa", "spec.dd.ee"),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fieldOfEmptySlice, fieldOfEmptyStruct := traverseToFindEmptyField(tt.args.root, tt.args.fieldPath)
|
||||
|
||||
if got := fieldOfEmptySlice; !reflect.DeepEqual(tt.wants.fieldOfEmptySlice, got) {
|
||||
t.Errorf("traverseToFindEmptyField() got fieldOfEmptySlice = %v, want %v",
|
||||
got, tt.wants.fieldOfEmptySlice)
|
||||
}
|
||||
|
||||
if got := fieldOfEmptyStruct; !reflect.DeepEqual(tt.wants.fieldOfEmptyStruct, got) {
|
||||
t.Errorf("traverseToFindEmptyField() got fieldOfEmptyStruct = %v, want %v",
|
||||
got, tt.wants.fieldOfEmptyStruct)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_traverseToFindEmptyFieldNeededModify(t *testing.T) {
|
||||
type args struct {
|
||||
root gjson.Result
|
||||
fieldPath []string
|
||||
fieldPathWithArrayIndex []string
|
||||
fieldOfEmptySlice sets.Set[string]
|
||||
fieldOfEmptyStruct sets.Set[string]
|
||||
}
|
||||
type wants struct {
|
||||
fieldOfEmptySliceToStruct sets.Set[string]
|
||||
fieldOfEmptySliceToDelete sets.Set[string]
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wants wants
|
||||
}{
|
||||
{
|
||||
name: "traverse to find empty field needed modify",
|
||||
args: args{
|
||||
root: gjson.Parse(`{"spec":[{"aa":[],"bb":[],"cc":["x"],"dd":{"ee":[],"ff":[]}}]}`),
|
||||
fieldPath: nil,
|
||||
fieldPathWithArrayIndex: nil,
|
||||
fieldOfEmptySlice: sets.New[string]("spec.bb"),
|
||||
fieldOfEmptyStruct: sets.New[string]("spec.aa", "spec.dd.ee"),
|
||||
},
|
||||
wants: wants{
|
||||
fieldOfEmptySliceToStruct: sets.New[string]("spec.0.aa", "spec.0.dd.ee"),
|
||||
fieldOfEmptySliceToDelete: sets.New[string]("spec.0.dd.ff"),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fieldOfEmptySliceToStruct, fieldOfEmptySliceToDelete := traverseToFindEmptyFieldNeededModify(tt.args.root,
|
||||
tt.args.fieldPath, tt.args.fieldPathWithArrayIndex, tt.args.fieldOfEmptySlice, tt.args.fieldOfEmptyStruct)
|
||||
|
||||
if got := fieldOfEmptySliceToStruct; !reflect.DeepEqual(tt.wants.fieldOfEmptySliceToStruct, got) {
|
||||
t.Errorf("traverseToFindEmptyFieldNeededModify() got fieldOfEmptySliceToStruct = %v, want %v",
|
||||
got, tt.wants.fieldOfEmptySliceToStruct)
|
||||
}
|
||||
|
||||
if got := fieldOfEmptySliceToDelete; !reflect.DeepEqual(tt.wants.fieldOfEmptySliceToDelete, got) {
|
||||
t.Errorf("traverseToFindEmptyFieldNeededModify() got fieldOfEmptySliceToDelete = %v, want %v",
|
||||
got, tt.wants.fieldOfEmptySliceToDelete)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_convertEmptyObjectBackToEmptySlice(t *testing.T) {
|
||||
type args struct {
|
||||
objBytes []byte
|
||||
isStruct bool
|
||||
references []*unstructured.Unstructured
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
want []byte
|
||||
}{
|
||||
{
|
||||
name: "convert empty object back to empty slice",
|
||||
args: args{
|
||||
objBytes: []byte(`{"spec":[{"aa":[],"bb":[],"cc":["x"],"dd":{"ee":[],"ff":[]}}]}`),
|
||||
isStruct: true,
|
||||
references: func() []*unstructured.Unstructured {
|
||||
desired, observed := &unstructured.Unstructured{}, &unstructured.Unstructured{}
|
||||
_ = json.Unmarshal([]byte(`{"kind": "demo", "spec":[{"aa":{},"bb":[]}]}`), desired)
|
||||
_ = json.Unmarshal([]byte(`{"kind": "demo", "spec":[{"dd":{"ee":{}}}]}`), observed)
|
||||
return []*unstructured.Unstructured{desired, observed}
|
||||
}(),
|
||||
},
|
||||
wantErr: false,
|
||||
want: []byte(`{"spec":[{"aa":{},"bb":[],"cc":["x"],"dd":{"ee":{}}}]}`),
|
||||
},
|
||||
{
|
||||
name: "obj is not struct while objBytes is []",
|
||||
args: args{
|
||||
objBytes: []byte(`[]`),
|
||||
isStruct: false,
|
||||
references: func() []*unstructured.Unstructured {
|
||||
desired, observed := &unstructured.Unstructured{}, &unstructured.Unstructured{}
|
||||
_ = json.Unmarshal([]byte(`{"kind": "demo", "spec":[{"aa":{},"bb":[]}]}`), desired)
|
||||
_ = json.Unmarshal([]byte(`{"kind": "demo", "spec":[{"dd":{"ee":{}}}]}`), observed)
|
||||
return []*unstructured.Unstructured{desired, observed}
|
||||
}(),
|
||||
},
|
||||
wantErr: false,
|
||||
want: []byte(`[]`),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := convertEmptySliceToEmptyStructPartly(tt.args.objBytes, tt.args.isStruct, tt.args.references)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("convertEmptySliceToEmptyStructPartly() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("convertEmptySliceToEmptyStructPartly() got = %s, want %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,488 @@
|
|||
<p align="center">
|
||||
<img
|
||||
src="logo.png"
|
||||
width="240" height="78" border="0" alt="GJSON">
|
||||
<br>
|
||||
<a href="https://godoc.org/github.com/tidwall/gjson"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
|
||||
<a href="https://tidwall.com/gjson-play"><img src="https://img.shields.io/badge/%F0%9F%8F%90-playground-9900cc.svg?style=flat-square" alt="GJSON Playground"></a>
|
||||
<a href="SYNTAX.md"><img src="https://img.shields.io/badge/{}-syntax-33aa33.svg?style=flat-square" alt="GJSON Syntax"></a>
|
||||
|
||||
</p>
|
||||
|
||||
<p align="center">get json values quickly</a></p>
|
||||
|
||||
GJSON is a Go package that provides a [fast](#performance) and [simple](#get-a-value) way to get values from a json document.
|
||||
It has features such as [one line retrieval](#get-a-value), [dot notation paths](#path-syntax), [iteration](#iterate-through-an-object-or-array), and [parsing json lines](#json-lines).
|
||||
|
||||
Also check out [SJSON](https://github.com/tidwall/sjson) for modifying json, and the [JJ](https://github.com/tidwall/jj) command line tool.
|
||||
|
||||
This README is a quick overview of how to use GJSON, for more information check out [GJSON Syntax](SYNTAX.md).
|
||||
|
||||
GJSON is also available for [Python](https://github.com/volans-/gjson-py) and [Rust](https://github.com/tidwall/gjson.rs)
|
||||
|
||||
Getting Started
|
||||
===============
|
||||
|
||||
## Installing
|
||||
|
||||
To start using GJSON, install Go and run `go get`:
|
||||
|
||||
```sh
|
||||
$ go get -u github.com/tidwall/gjson
|
||||
```
|
||||
|
||||
This will retrieve the library.
|
||||
|
||||
## Get a value
|
||||
Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". When the value is found it's returned immediately.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/tidwall/gjson"
|
||||
|
||||
const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
|
||||
|
||||
func main() {
|
||||
value := gjson.Get(json, "name.last")
|
||||
println(value.String())
|
||||
}
|
||||
```
|
||||
|
||||
This will print:
|
||||
|
||||
```
|
||||
Prichard
|
||||
```
|
||||
*There's also the [GetMany](#get-multiple-values-at-once) function to get multiple values at once, and [GetBytes](#working-with-bytes) for working with JSON byte slices.*
|
||||
|
||||
## Path Syntax
|
||||
|
||||
Below is a quick overview of the path syntax, for more complete information please
|
||||
check out [GJSON Syntax](SYNTAX.md).
|
||||
|
||||
A path is a series of keys separated by a dot.
|
||||
A key may contain special wildcard characters '\*' and '?'.
|
||||
To access an array value use the index as the key.
|
||||
To get the number of elements in an array or to access a child path, use the '#' character.
|
||||
The dot and wildcard characters can be escaped with '\\'.
|
||||
|
||||
```json
|
||||
{
|
||||
"name": {"first": "Tom", "last": "Anderson"},
|
||||
"age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
|
||||
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
|
||||
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
|
||||
]
|
||||
}
|
||||
```
|
||||
```
|
||||
"name.last" >> "Anderson"
|
||||
"age" >> 37
|
||||
"children" >> ["Sara","Alex","Jack"]
|
||||
"children.#" >> 3
|
||||
"children.1" >> "Alex"
|
||||
"child*.2" >> "Jack"
|
||||
"c?ildren.0" >> "Sara"
|
||||
"fav\.movie" >> "Deer Hunter"
|
||||
"friends.#.first" >> ["Dale","Roger","Jane"]
|
||||
"friends.1.last" >> "Craig"
|
||||
```
|
||||
|
||||
You can also query an array for the first match by using `#(...)`, or find all
|
||||
matches with `#(...)#`. Queries support the `==`, `!=`, `<`, `<=`, `>`, `>=`
|
||||
comparison operators and the simple pattern matching `%` (like) and `!%`
|
||||
(not like) operators.
|
||||
|
||||
```
|
||||
friends.#(last=="Murphy").first >> "Dale"
|
||||
friends.#(last=="Murphy")#.first >> ["Dale","Jane"]
|
||||
friends.#(age>45)#.last >> ["Craig","Murphy"]
|
||||
friends.#(first%"D*").last >> "Murphy"
|
||||
friends.#(first!%"D*").last >> "Craig"
|
||||
friends.#(nets.#(=="fb"))#.first >> ["Dale","Roger"]
|
||||
```
|
||||
|
||||
*Please note that prior to v1.3.0, queries used the `#[...]` brackets. This was
|
||||
changed in v1.3.0 as to avoid confusion with the new
|
||||
[multipath](SYNTAX.md#multipaths) syntax. For backwards compatibility,
|
||||
`#[...]` will continue to work until the next major release.*
|
||||
|
||||
## Result Type
|
||||
|
||||
GJSON supports the json types `string`, `number`, `bool`, and `null`.
|
||||
Arrays and Objects are returned as their raw json types.
|
||||
|
||||
The `Result` type holds one of these:
|
||||
|
||||
```
|
||||
bool, for JSON booleans
|
||||
float64, for JSON numbers
|
||||
string, for JSON string literals
|
||||
nil, for JSON null
|
||||
```
|
||||
|
||||
To directly access the value:
|
||||
|
||||
```go
|
||||
result.Type // can be String, Number, True, False, Null, or JSON
|
||||
result.Str // holds the string
|
||||
result.Num // holds the float64 number
|
||||
result.Raw // holds the raw json
|
||||
result.Index // index of raw value in original json, zero means index unknown
|
||||
result.Indexes // indexes of all the elements that match on a path containing the '#' query character.
|
||||
```
|
||||
|
||||
There are a variety of handy functions that work on a result:
|
||||
|
||||
```go
|
||||
result.Exists() bool
|
||||
result.Value() interface{}
|
||||
result.Int() int64
|
||||
result.Uint() uint64
|
||||
result.Float() float64
|
||||
result.String() string
|
||||
result.Bool() bool
|
||||
result.Time() time.Time
|
||||
result.Array() []gjson.Result
|
||||
result.Map() map[string]gjson.Result
|
||||
result.Get(path string) Result
|
||||
result.ForEach(iterator func(key, value Result) bool)
|
||||
result.Less(token Result, caseSensitive bool) bool
|
||||
```
|
||||
|
||||
The `result.Value()` function returns an `interface{}` which requires type assertion and is one of the following Go types:
|
||||
|
||||
```go
|
||||
boolean >> bool
|
||||
number >> float64
|
||||
string >> string
|
||||
null >> nil
|
||||
array >> []interface{}
|
||||
object >> map[string]interface{}
|
||||
```
|
||||
|
||||
The `result.Array()` function returns back an array of values.
|
||||
If the result represents a non-existent value, then an empty array will be returned.
|
||||
If the result is not a JSON array, the return value will be an array containing one result.
|
||||
|
||||
### 64-bit integers
|
||||
|
||||
The `result.Int()` and `result.Uint()` calls are capable of reading all 64 bits, allowing for large JSON integers.
|
||||
|
||||
```go
|
||||
result.Int() int64 // -9223372036854775808 to 9223372036854775807
|
||||
result.Uint() uint64 // 0 to 18446744073709551615
|
||||
```
|
||||
|
||||
## Modifiers and path chaining
|
||||
|
||||
New in version 1.2 is support for modifier functions and path chaining.
|
||||
|
||||
A modifier is a path component that performs custom processing on the
|
||||
json.
|
||||
|
||||
Multiple paths can be "chained" together using the pipe character.
|
||||
This is useful for getting results from a modified query.
|
||||
|
||||
For example, using the built-in `@reverse` modifier on the above json document,
|
||||
we'll get `children` array and reverse the order:
|
||||
|
||||
```
|
||||
"children|@reverse" >> ["Jack","Alex","Sara"]
|
||||
"children|@reverse|0" >> "Jack"
|
||||
```
|
||||
|
||||
There are currently the following built-in modifiers:
|
||||
|
||||
- `@reverse`: Reverse an array or the members of an object.
|
||||
- `@ugly`: Remove all whitespace from a json document.
|
||||
- `@pretty`: Make the json document more human readable.
|
||||
- `@this`: Returns the current element. It can be used to retrieve the root element.
|
||||
- `@valid`: Ensure the json document is valid.
|
||||
- `@flatten`: Flattens an array.
|
||||
- `@join`: Joins multiple objects into a single object.
|
||||
- `@keys`: Returns an array of keys for an object.
|
||||
- `@values`: Returns an array of values for an object.
|
||||
- `@tostr`: Converts json to a string. Wraps a json string.
|
||||
- `@fromstr`: Converts a string from json. Unwraps a json string.
|
||||
- `@group`: Groups arrays of objects. See [e4fc67c](https://github.com/tidwall/gjson/commit/e4fc67c92aeebf2089fabc7872f010e340d105db).
|
||||
- `@dig`: Search for a value without providing its entire path. See [e8e87f2](https://github.com/tidwall/gjson/commit/e8e87f2a00dc41f3aba5631094e21f59a8cf8cbf).
|
||||
|
||||
### Modifier arguments
|
||||
|
||||
A modifier may accept an optional argument. The argument can be a valid JSON
|
||||
document or just characters.
|
||||
|
||||
For example, the `@pretty` modifier takes a json object as its argument.
|
||||
|
||||
```
|
||||
@pretty:{"sortKeys":true}
|
||||
```
|
||||
|
||||
Which makes the json pretty and orders all of its keys.
|
||||
|
||||
```json
|
||||
{
|
||||
"age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{"age": 44, "first": "Dale", "last": "Murphy"},
|
||||
{"age": 68, "first": "Roger", "last": "Craig"},
|
||||
{"age": 47, "first": "Jane", "last": "Murphy"}
|
||||
],
|
||||
"name": {"first": "Tom", "last": "Anderson"}
|
||||
}
|
||||
```
|
||||
|
||||
*The full list of `@pretty` options are `sortKeys`, `indent`, `prefix`, and `width`.
|
||||
Please see [Pretty Options](https://github.com/tidwall/pretty#customized-output) for more information.*
|
||||
|
||||
### Custom modifiers
|
||||
|
||||
You can also add custom modifiers.
|
||||
|
||||
For example, here we create a modifier that makes the entire json document upper
|
||||
or lower case.
|
||||
|
||||
```go
|
||||
gjson.AddModifier("case", func(json, arg string) string {
|
||||
if arg == "upper" {
|
||||
return strings.ToUpper(json)
|
||||
}
|
||||
if arg == "lower" {
|
||||
return strings.ToLower(json)
|
||||
}
|
||||
return json
|
||||
})
|
||||
```
|
||||
|
||||
```
|
||||
"children|@case:upper" >> ["SARA","ALEX","JACK"]
|
||||
"children|@case:lower|@reverse" >> ["jack","alex","sara"]
|
||||
```
|
||||
|
||||
## JSON Lines
|
||||
|
||||
There's support for [JSON Lines](http://jsonlines.org/) using the `..` prefix, which treats a multilined document as an array.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
{"name": "Gilbert", "age": 61}
|
||||
{"name": "Alexa", "age": 34}
|
||||
{"name": "May", "age": 57}
|
||||
{"name": "Deloise", "age": 44}
|
||||
```
|
||||
|
||||
```
|
||||
..# >> 4
|
||||
..1 >> {"name": "Alexa", "age": 34}
|
||||
..3 >> {"name": "Deloise", "age": 44}
|
||||
..#.name >> ["Gilbert","Alexa","May","Deloise"]
|
||||
..#(name="May").age >> 57
|
||||
```
|
||||
|
||||
The `ForEachLines` function will iterate through JSON lines.
|
||||
|
||||
```go
|
||||
gjson.ForEachLine(json, func(line gjson.Result) bool{
|
||||
println(line.String())
|
||||
return true
|
||||
})
|
||||
```
|
||||
|
||||
## Get nested array values
|
||||
|
||||
Suppose you want all the last names from the following json:
|
||||
|
||||
```json
|
||||
{
|
||||
"programmers": [
|
||||
{
|
||||
"firstName": "Janet",
|
||||
"lastName": "McLaughlin",
|
||||
}, {
|
||||
"firstName": "Elliotte",
|
||||
"lastName": "Hunter",
|
||||
}, {
|
||||
"firstName": "Jason",
|
||||
"lastName": "Harold",
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
You would use the path "programmers.#.lastName" like such:
|
||||
|
||||
```go
|
||||
result := gjson.Get(json, "programmers.#.lastName")
|
||||
for _, name := range result.Array() {
|
||||
println(name.String())
|
||||
}
|
||||
```
|
||||
|
||||
You can also query an object inside an array:
|
||||
|
||||
```go
|
||||
name := gjson.Get(json, `programmers.#(lastName="Hunter").firstName`)
|
||||
println(name.String()) // prints "Elliotte"
|
||||
```
|
||||
|
||||
## Iterate through an object or array
|
||||
|
||||
The `ForEach` function allows for quickly iterating through an object or array.
|
||||
The key and value are passed to the iterator function for objects.
|
||||
Only the value is passed for arrays.
|
||||
Returning `false` from an iterator will stop iteration.
|
||||
|
||||
```go
|
||||
result := gjson.Get(json, "programmers")
|
||||
result.ForEach(func(key, value gjson.Result) bool {
|
||||
println(value.String())
|
||||
return true // keep iterating
|
||||
})
|
||||
```
|
||||
|
||||
## Simple Parse and Get
|
||||
|
||||
There's a `Parse(json)` function that will do a simple parse, and `result.Get(path)` that will search a result.
|
||||
|
||||
For example, all of these will return the same result:
|
||||
|
||||
```go
|
||||
gjson.Parse(json).Get("name").Get("last")
|
||||
gjson.Get(json, "name").Get("last")
|
||||
gjson.Get(json, "name.last")
|
||||
```
|
||||
|
||||
## Check for the existence of a value
|
||||
|
||||
Sometimes you just want to know if a value exists.
|
||||
|
||||
```go
|
||||
value := gjson.Get(json, "name.last")
|
||||
if !value.Exists() {
|
||||
println("no last name")
|
||||
} else {
|
||||
println(value.String())
|
||||
}
|
||||
|
||||
// Or as one step
|
||||
if gjson.Get(json, "name.last").Exists() {
|
||||
println("has a last name")
|
||||
}
|
||||
```
|
||||
|
||||
## Validate JSON
|
||||
|
||||
The `Get*` and `Parse*` functions expects that the json is well-formed. Bad json will not panic, but it may return back unexpected results.
|
||||
|
||||
If you are consuming JSON from an unpredictable source then you may want to validate prior to using GJSON.
|
||||
|
||||
```go
|
||||
if !gjson.Valid(json) {
|
||||
return errors.New("invalid json")
|
||||
}
|
||||
value := gjson.Get(json, "name.last")
|
||||
```
|
||||
|
||||
## Unmarshal to a map
|
||||
|
||||
To unmarshal to a `map[string]interface{}`:
|
||||
|
||||
```go
|
||||
m, ok := gjson.Parse(json).Value().(map[string]interface{})
|
||||
if !ok {
|
||||
// not a map
|
||||
}
|
||||
```
|
||||
|
||||
## Working with Bytes
|
||||
|
||||
If your JSON is contained in a `[]byte` slice, there's the [GetBytes](https://godoc.org/github.com/tidwall/gjson#GetBytes) function. This is preferred over `Get(string(data), path)`.
|
||||
|
||||
```go
|
||||
var json []byte = ...
|
||||
result := gjson.GetBytes(json, path)
|
||||
```
|
||||
|
||||
If you are using the `gjson.GetBytes(json, path)` function and you want to avoid converting `result.Raw` to a `[]byte`, then you can use this pattern:
|
||||
|
||||
```go
|
||||
var json []byte = ...
|
||||
result := gjson.GetBytes(json, path)
|
||||
var raw []byte
|
||||
if result.Index > 0 {
|
||||
raw = json[result.Index:result.Index+len(result.Raw)]
|
||||
} else {
|
||||
raw = []byte(result.Raw)
|
||||
}
|
||||
```
|
||||
|
||||
This is a best-effort no allocation sub slice of the original json. This method utilizes the `result.Index` field, which is the position of the raw data in the original json. It's possible that the value of `result.Index` equals zero, in which case the `result.Raw` is converted to a `[]byte`.
|
||||
|
||||
## Performance
|
||||
|
||||
Benchmarks of GJSON alongside [encoding/json](https://golang.org/pkg/encoding/json/),
|
||||
[ffjson](https://github.com/pquerna/ffjson),
|
||||
[EasyJSON](https://github.com/mailru/easyjson),
|
||||
[jsonparser](https://github.com/buger/jsonparser),
|
||||
and [json-iterator](https://github.com/json-iterator/go)
|
||||
|
||||
```
|
||||
BenchmarkGJSONGet-16 11644512 311 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGJSONUnmarshalMap-16 1122678 3094 ns/op 1920 B/op 26 allocs/op
|
||||
BenchmarkJSONUnmarshalMap-16 516681 6810 ns/op 2944 B/op 69 allocs/op
|
||||
BenchmarkJSONUnmarshalStruct-16 697053 5400 ns/op 928 B/op 13 allocs/op
|
||||
BenchmarkJSONDecoder-16 330450 10217 ns/op 3845 B/op 160 allocs/op
|
||||
BenchmarkFFJSONLexer-16 1424979 2585 ns/op 880 B/op 8 allocs/op
|
||||
BenchmarkEasyJSONLexer-16 3000000 729 ns/op 501 B/op 5 allocs/op
|
||||
BenchmarkJSONParserGet-16 3000000 366 ns/op 21 B/op 0 allocs/op
|
||||
BenchmarkJSONIterator-16 3000000 869 ns/op 693 B/op 14 allocs/op
|
||||
```
|
||||
|
||||
JSON document used:
|
||||
|
||||
```json
|
||||
{
|
||||
"widget": {
|
||||
"debug": "on",
|
||||
"window": {
|
||||
"title": "Sample Konfabulator Widget",
|
||||
"name": "main_window",
|
||||
"width": 500,
|
||||
"height": 500
|
||||
},
|
||||
"image": {
|
||||
"src": "Images/Sun.png",
|
||||
"hOffset": 250,
|
||||
"vOffset": 250,
|
||||
"alignment": "center"
|
||||
},
|
||||
"text": {
|
||||
"data": "Click Here",
|
||||
"size": 36,
|
||||
"style": "bold",
|
||||
"vOffset": 100,
|
||||
"alignment": "center",
|
||||
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Each operation was rotated through one of the following search paths:
|
||||
|
||||
```
|
||||
widget.window.name
|
||||
widget.image.hOffset
|
||||
widget.text.onMouseUp
|
||||
```
|
||||
|
||||
*These benchmarks were run on a MacBook Pro 16" 2.4 GHz Intel Core i9 using Go 1.17 and can be found [here](https://github.com/tidwall/gjson-benchmarks).*
|
|
@ -0,0 +1,360 @@
|
|||
# GJSON Path Syntax
|
||||
|
||||
A GJSON Path is a text string syntax that describes a search pattern for quickly retreiving values from a JSON payload.
|
||||
|
||||
This document is designed to explain the structure of a GJSON Path through examples.
|
||||
|
||||
- [Path structure](#path-structure)
|
||||
- [Basic](#basic)
|
||||
- [Wildcards](#wildcards)
|
||||
- [Escape Character](#escape-character)
|
||||
- [Arrays](#arrays)
|
||||
- [Queries](#queries)
|
||||
- [Dot vs Pipe](#dot-vs-pipe)
|
||||
- [Modifiers](#modifiers)
|
||||
- [Multipaths](#multipaths)
|
||||
- [Literals](#literals)
|
||||
|
||||
The definitive implemenation is [github.com/tidwall/gjson](https://github.com/tidwall/gjson).
|
||||
Use the [GJSON Playground](https://gjson.dev) to experiment with the syntax online.
|
||||
|
||||
## Path structure
|
||||
|
||||
A GJSON Path is intended to be easily expressed as a series of components seperated by a `.` character.
|
||||
|
||||
Along with `.` character, there are a few more that have special meaning, including `|`, `#`, `@`, `\`, `*`, `!`, and `?`.
|
||||
|
||||
## Example
|
||||
|
||||
Given this JSON
|
||||
|
||||
```json
|
||||
{
|
||||
"name": {"first": "Tom", "last": "Anderson"},
|
||||
"age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
|
||||
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
|
||||
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
The following GJSON Paths evaluate to the accompanying values.
|
||||
|
||||
### Basic
|
||||
|
||||
In many cases you'll just want to retreive values by object name or array index.
|
||||
|
||||
```go
|
||||
name.last "Anderson"
|
||||
name.first "Tom"
|
||||
age 37
|
||||
children ["Sara","Alex","Jack"]
|
||||
children.0 "Sara"
|
||||
children.1 "Alex"
|
||||
friends.1 {"first": "Roger", "last": "Craig", "age": 68}
|
||||
friends.1.first "Roger"
|
||||
```
|
||||
|
||||
### Wildcards
|
||||
|
||||
A key may contain the special wildcard characters `*` and `?`.
|
||||
The `*` will match on any zero+ characters, and `?` matches on any one character.
|
||||
|
||||
```go
|
||||
child*.2 "Jack"
|
||||
c?ildren.0 "Sara"
|
||||
```
|
||||
|
||||
### Escape character
|
||||
|
||||
Special purpose characters, such as `.`, `*`, and `?` can be escaped with `\`.
|
||||
|
||||
```go
|
||||
fav\.movie "Deer Hunter"
|
||||
```
|
||||
|
||||
You'll also need to make sure that the `\` character is correctly escaped when hardcoding a path in your source code.
|
||||
|
||||
```go
|
||||
// Go
|
||||
val := gjson.Get(json, "fav\\.movie") // must escape the slash
|
||||
val := gjson.Get(json, `fav\.movie`) // no need to escape the slash
|
||||
```
|
||||
|
||||
```rust
|
||||
// Rust
|
||||
let val = gjson::get(json, "fav\\.movie") // must escape the slash
|
||||
let val = gjson::get(json, r#"fav\.movie"#) // no need to escape the slash
|
||||
```
|
||||
|
||||
|
||||
### Arrays
|
||||
|
||||
The `#` character allows for digging into JSON Arrays.
|
||||
|
||||
To get the length of an array you'll just use the `#` all by itself.
|
||||
|
||||
```go
|
||||
friends.# 3
|
||||
friends.#.age [44,68,47]
|
||||
```
|
||||
|
||||
### Queries
|
||||
|
||||
You can also query an array for the first match by using `#(...)`, or find all matches with `#(...)#`.
|
||||
Queries support the `==`, `!=`, `<`, `<=`, `>`, `>=` comparison operators,
|
||||
and the simple pattern matching `%` (like) and `!%` (not like) operators.
|
||||
|
||||
```go
|
||||
friends.#(last=="Murphy").first "Dale"
|
||||
friends.#(last=="Murphy")#.first ["Dale","Jane"]
|
||||
friends.#(age>45)#.last ["Craig","Murphy"]
|
||||
friends.#(first%"D*").last "Murphy"
|
||||
friends.#(first!%"D*").last "Craig"
|
||||
```
|
||||
|
||||
To query for a non-object value in an array, you can forgo the string to the right of the operator.
|
||||
|
||||
```go
|
||||
children.#(!%"*a*") "Alex"
|
||||
children.#(%"*a*")# ["Sara","Jack"]
|
||||
```
|
||||
|
||||
Nested queries are allowed.
|
||||
|
||||
```go
|
||||
friends.#(nets.#(=="fb"))#.first >> ["Dale","Roger"]
|
||||
```
|
||||
|
||||
*Please note that prior to v1.3.0, queries used the `#[...]` brackets. This was
|
||||
changed in v1.3.0 as to avoid confusion with the new [multipath](#multipaths)
|
||||
syntax. For backwards compatibility, `#[...]` will continue to work until the
|
||||
next major release.*
|
||||
|
||||
The `~` (tilde) operator will convert a value to a boolean before comparison.
|
||||
|
||||
Supported tilde comparison type are:
|
||||
|
||||
```
|
||||
~true Converts true-ish values to true
|
||||
~false Converts false-ish and non-existent values to true
|
||||
~null Converts null and non-existent values to true
|
||||
~* Converts any existing value to true
|
||||
```
|
||||
|
||||
For example, using the following JSON:
|
||||
|
||||
```json
|
||||
{
|
||||
"vals": [
|
||||
{ "a": 1, "b": "data" },
|
||||
{ "a": 2, "b": true },
|
||||
{ "a": 3, "b": false },
|
||||
{ "a": 4, "b": "0" },
|
||||
{ "a": 5, "b": 0 },
|
||||
{ "a": 6, "b": "1" },
|
||||
{ "a": 7, "b": 1 },
|
||||
{ "a": 8, "b": "true" },
|
||||
{ "a": 9, "b": false },
|
||||
{ "a": 10, "b": null },
|
||||
{ "a": 11 }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
To query for all true-ish or false-ish values:
|
||||
|
||||
```
|
||||
vals.#(b==~true)#.a >> [2,6,7,8]
|
||||
vals.#(b==~false)#.a >> [3,4,5,9,10,11]
|
||||
```
|
||||
|
||||
The last value which was non-existent is treated as `false`
|
||||
|
||||
To query for null and explicit value existence:
|
||||
|
||||
```
|
||||
vals.#(b==~null)#.a >> [10,11]
|
||||
vals.#(b==~*)#.a >> [1,2,3,4,5,6,7,8,9,10]
|
||||
vals.#(b!=~*)#.a >> [11]
|
||||
```
|
||||
|
||||
### Dot vs Pipe
|
||||
|
||||
The `.` is standard separator, but it's also possible to use a `|`.
|
||||
In most cases they both end up returning the same results.
|
||||
The cases where`|` differs from `.` is when it's used after the `#` for [Arrays](#arrays) and [Queries](#queries).
|
||||
|
||||
Here are some examples
|
||||
|
||||
```go
|
||||
friends.0.first "Dale"
|
||||
friends|0.first "Dale"
|
||||
friends.0|first "Dale"
|
||||
friends|0|first "Dale"
|
||||
friends|# 3
|
||||
friends.# 3
|
||||
friends.#(last="Murphy")# [{"first": "Dale", "last": "Murphy", "age": 44},{"first": "Jane", "last": "Murphy", "age": 47}]
|
||||
friends.#(last="Murphy")#.first ["Dale","Jane"]
|
||||
friends.#(last="Murphy")#|first <non-existent>
|
||||
friends.#(last="Murphy")#.0 []
|
||||
friends.#(last="Murphy")#|0 {"first": "Dale", "last": "Murphy", "age": 44}
|
||||
friends.#(last="Murphy")#.# []
|
||||
friends.#(last="Murphy")#|# 2
|
||||
```
|
||||
|
||||
Let's break down a few of these.
|
||||
|
||||
The path `friends.#(last="Murphy")#` all by itself results in
|
||||
|
||||
```json
|
||||
[{"first": "Dale", "last": "Murphy", "age": 44},{"first": "Jane", "last": "Murphy", "age": 47}]
|
||||
```
|
||||
|
||||
The `.first` suffix will process the `first` path on each array element *before* returning the results. Which becomes
|
||||
|
||||
```json
|
||||
["Dale","Jane"]
|
||||
```
|
||||
|
||||
But the `|first` suffix actually processes the `first` path *after* the previous result.
|
||||
Since the previous result is an array, not an object, it's not possible to process
|
||||
because `first` does not exist.
|
||||
|
||||
Yet, `|0` suffix returns
|
||||
|
||||
```json
|
||||
{"first": "Dale", "last": "Murphy", "age": 44}
|
||||
```
|
||||
|
||||
Because `0` is the first index of the previous result.
|
||||
|
||||
### Modifiers
|
||||
|
||||
A modifier is a path component that performs custom processing on the JSON.
|
||||
|
||||
For example, using the built-in `@reverse` modifier on the above JSON payload will reverse the `children` array:
|
||||
|
||||
```go
|
||||
children.@reverse ["Jack","Alex","Sara"]
|
||||
children.@reverse.0 "Jack"
|
||||
```
|
||||
|
||||
There are currently the following built-in modifiers:
|
||||
|
||||
- `@reverse`: Reverse an array or the members of an object.
|
||||
- `@ugly`: Remove all whitespace from JSON.
|
||||
- `@pretty`: Make the JSON more human readable.
|
||||
- `@this`: Returns the current element. It can be used to retrieve the root element.
|
||||
- `@valid`: Ensure the json document is valid.
|
||||
- `@flatten`: Flattens an array.
|
||||
- `@join`: Joins multiple objects into a single object.
|
||||
- `@keys`: Returns an array of keys for an object.
|
||||
- `@values`: Returns an array of values for an object.
|
||||
- `@tostr`: Converts json to a string. Wraps a json string.
|
||||
- `@fromstr`: Converts a string from json. Unwraps a json string.
|
||||
- `@group`: Groups arrays of objects. See [e4fc67c](https://github.com/tidwall/gjson/commit/e4fc67c92aeebf2089fabc7872f010e340d105db).
|
||||
- `@dig`: Search for a value without providing its entire path. See [e8e87f2](https://github.com/tidwall/gjson/commit/e8e87f2a00dc41f3aba5631094e21f59a8cf8cbf).
|
||||
|
||||
#### Modifier arguments
|
||||
|
||||
A modifier may accept an optional argument. The argument can be a valid JSON payload or just characters.
|
||||
|
||||
For example, the `@pretty` modifier takes a json object as its argument.
|
||||
|
||||
```
|
||||
@pretty:{"sortKeys":true}
|
||||
```
|
||||
|
||||
Which makes the json pretty and orders all of its keys.
|
||||
|
||||
```json
|
||||
{
|
||||
"age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{"age": 44, "first": "Dale", "last": "Murphy"},
|
||||
{"age": 68, "first": "Roger", "last": "Craig"},
|
||||
{"age": 47, "first": "Jane", "last": "Murphy"}
|
||||
],
|
||||
"name": {"first": "Tom", "last": "Anderson"}
|
||||
}
|
||||
```
|
||||
|
||||
*The full list of `@pretty` options are `sortKeys`, `indent`, `prefix`, and `width`.
|
||||
Please see [Pretty Options](https://github.com/tidwall/pretty#customized-output) for more information.*
|
||||
|
||||
#### Custom modifiers
|
||||
|
||||
You can also add custom modifiers.
|
||||
|
||||
For example, here we create a modifier which makes the entire JSON payload upper or lower case.
|
||||
|
||||
```go
|
||||
gjson.AddModifier("case", func(json, arg string) string {
|
||||
if arg == "upper" {
|
||||
return strings.ToUpper(json)
|
||||
}
|
||||
if arg == "lower" {
|
||||
return strings.ToLower(json)
|
||||
}
|
||||
return json
|
||||
})
|
||||
"children.@case:upper" ["SARA","ALEX","JACK"]
|
||||
"children.@case:lower.@reverse" ["jack","alex","sara"]
|
||||
```
|
||||
|
||||
*Note: Custom modifiers are not yet available in the Rust version*
|
||||
|
||||
### Multipaths
|
||||
|
||||
Starting with v1.3.0, GJSON added the ability to join multiple paths together
|
||||
to form new documents. Wrapping comma-separated paths between `[...]` or
|
||||
`{...}` will result in a new array or object, respectively.
|
||||
|
||||
For example, using the given multipath:
|
||||
|
||||
```
|
||||
{name.first,age,"the_murphys":friends.#(last="Murphy")#.first}
|
||||
```
|
||||
|
||||
Here we selected the first name, age, and the first name for friends with the
|
||||
last name "Murphy".
|
||||
|
||||
You'll notice that an optional key can be provided, in this case
|
||||
"the_murphys", to force assign a key to a value. Otherwise, the name of the
|
||||
actual field will be used, in this case "first". If a name cannot be
|
||||
determined, then "_" is used.
|
||||
|
||||
This results in
|
||||
|
||||
```json
|
||||
{"first":"Tom","age":37,"the_murphys":["Dale","Jane"]}
|
||||
```
|
||||
|
||||
### Literals
|
||||
|
||||
Starting with v1.12.0, GJSON added support of json literals, which provides a way for constructing static blocks of json. This is can be particularly useful when constructing a new json document using [multipaths](#multipaths).
|
||||
|
||||
A json literal begins with the '!' declaration character.
|
||||
|
||||
For example, using the given multipath:
|
||||
|
||||
```
|
||||
{name.first,age,"company":!"Happysoft","employed":!true}
|
||||
```
|
||||
|
||||
Here we selected the first name and age. Then add two new fields, "company" and "employed".
|
||||
|
||||
This results in
|
||||
|
||||
```json
|
||||
{"first":"Tom","age":37,"company":"Happysoft","employed":true}
|
||||
```
|
||||
|
||||
*See issue [#249](https://github.com/tidwall/gjson/issues/249) for additional context on JSON Literals.*
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,29 @@
|
|||
# Match
|
||||
|
||||
[](https://godoc.org/github.com/tidwall/match)
|
||||
|
||||
Match is a very simple pattern matcher where '*' matches on any
|
||||
number characters and '?' matches on any one character.
|
||||
|
||||
## Installing
|
||||
|
||||
```
|
||||
go get -u github.com/tidwall/match
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
match.Match("hello", "*llo")
|
||||
match.Match("jello", "?ello")
|
||||
match.Match("hello", "h*o")
|
||||
```
|
||||
|
||||
|
||||
## Contact
|
||||
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
## License
|
||||
|
||||
Redcon source code is available under the MIT [License](/LICENSE).
|
|
@ -0,0 +1,237 @@
|
|||
// Package match provides a simple pattern matcher with unicode support.
|
||||
package match
|
||||
|
||||
import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Match returns true if str matches pattern. This is a very
|
||||
// simple wildcard match where '*' matches on any number characters
|
||||
// and '?' matches on any one character.
|
||||
//
|
||||
// pattern:
|
||||
// { term }
|
||||
// term:
|
||||
// '*' matches any sequence of non-Separator characters
|
||||
// '?' matches any single non-Separator character
|
||||
// c matches character c (c != '*', '?', '\\')
|
||||
// '\\' c matches character c
|
||||
//
|
||||
func Match(str, pattern string) bool {
|
||||
if pattern == "*" {
|
||||
return true
|
||||
}
|
||||
return match(str, pattern, 0, nil, -1) == rMatch
|
||||
}
|
||||
|
||||
// MatchLimit is the same as Match but will limit the complexity of the match
|
||||
// operation. This is to avoid long running matches, specifically to avoid ReDos
|
||||
// attacks from arbritary inputs.
|
||||
//
|
||||
// How it works:
|
||||
// The underlying match routine is recursive and may call itself when it
|
||||
// encounters a sandwiched wildcard pattern, such as: `user:*:name`.
|
||||
// Everytime it calls itself a counter is incremented.
|
||||
// The operation is stopped when counter > maxcomp*len(str).
|
||||
func MatchLimit(str, pattern string, maxcomp int) (matched, stopped bool) {
|
||||
if pattern == "*" {
|
||||
return true, false
|
||||
}
|
||||
counter := 0
|
||||
r := match(str, pattern, len(str), &counter, maxcomp)
|
||||
if r == rStop {
|
||||
return false, true
|
||||
}
|
||||
return r == rMatch, false
|
||||
}
|
||||
|
||||
type result int
|
||||
|
||||
const (
|
||||
rNoMatch result = iota
|
||||
rMatch
|
||||
rStop
|
||||
)
|
||||
|
||||
func match(str, pat string, slen int, counter *int, maxcomp int) result {
|
||||
// check complexity limit
|
||||
if maxcomp > -1 {
|
||||
if *counter > slen*maxcomp {
|
||||
return rStop
|
||||
}
|
||||
*counter++
|
||||
}
|
||||
|
||||
for len(pat) > 0 {
|
||||
var wild bool
|
||||
pc, ps := rune(pat[0]), 1
|
||||
if pc > 0x7f {
|
||||
pc, ps = utf8.DecodeRuneInString(pat)
|
||||
}
|
||||
var sc rune
|
||||
var ss int
|
||||
if len(str) > 0 {
|
||||
sc, ss = rune(str[0]), 1
|
||||
if sc > 0x7f {
|
||||
sc, ss = utf8.DecodeRuneInString(str)
|
||||
}
|
||||
}
|
||||
switch pc {
|
||||
case '?':
|
||||
if ss == 0 {
|
||||
return rNoMatch
|
||||
}
|
||||
case '*':
|
||||
// Ignore repeating stars.
|
||||
for len(pat) > 1 && pat[1] == '*' {
|
||||
pat = pat[1:]
|
||||
}
|
||||
|
||||
// If this star is the last character then it must be a match.
|
||||
if len(pat) == 1 {
|
||||
return rMatch
|
||||
}
|
||||
|
||||
// Match and trim any non-wildcard suffix characters.
|
||||
var ok bool
|
||||
str, pat, ok = matchTrimSuffix(str, pat)
|
||||
if !ok {
|
||||
return rNoMatch
|
||||
}
|
||||
|
||||
// Check for single star again.
|
||||
if len(pat) == 1 {
|
||||
return rMatch
|
||||
}
|
||||
|
||||
// Perform recursive wildcard search.
|
||||
r := match(str, pat[1:], slen, counter, maxcomp)
|
||||
if r != rNoMatch {
|
||||
return r
|
||||
}
|
||||
if len(str) == 0 {
|
||||
return rNoMatch
|
||||
}
|
||||
wild = true
|
||||
default:
|
||||
if ss == 0 {
|
||||
return rNoMatch
|
||||
}
|
||||
if pc == '\\' {
|
||||
pat = pat[ps:]
|
||||
pc, ps = utf8.DecodeRuneInString(pat)
|
||||
if ps == 0 {
|
||||
return rNoMatch
|
||||
}
|
||||
}
|
||||
if sc != pc {
|
||||
return rNoMatch
|
||||
}
|
||||
}
|
||||
str = str[ss:]
|
||||
if !wild {
|
||||
pat = pat[ps:]
|
||||
}
|
||||
}
|
||||
if len(str) == 0 {
|
||||
return rMatch
|
||||
}
|
||||
return rNoMatch
|
||||
}
|
||||
|
||||
// matchTrimSuffix matches and trims any non-wildcard suffix characters.
|
||||
// Returns the trimed string and pattern.
|
||||
//
|
||||
// This is called because the pattern contains extra data after the wildcard
|
||||
// star. Here we compare any suffix characters in the pattern to the suffix of
|
||||
// the target string. Basically a reverse match that stops when a wildcard
|
||||
// character is reached. This is a little trickier than a forward match because
|
||||
// we need to evaluate an escaped character in reverse.
|
||||
//
|
||||
// Any matched characters will be trimmed from both the target
|
||||
// string and the pattern.
|
||||
func matchTrimSuffix(str, pat string) (string, string, bool) {
|
||||
// It's expected that the pattern has at least two bytes and the first byte
|
||||
// is a wildcard star '*'
|
||||
match := true
|
||||
for len(str) > 0 && len(pat) > 1 {
|
||||
pc, ps := utf8.DecodeLastRuneInString(pat)
|
||||
var esc bool
|
||||
for i := 0; ; i++ {
|
||||
if pat[len(pat)-ps-i-1] != '\\' {
|
||||
if i&1 == 1 {
|
||||
esc = true
|
||||
ps++
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if pc == '*' && !esc {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
sc, ss := utf8.DecodeLastRuneInString(str)
|
||||
if !((pc == '?' && !esc) || pc == sc) {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
str = str[:len(str)-ss]
|
||||
pat = pat[:len(pat)-ps]
|
||||
}
|
||||
return str, pat, match
|
||||
}
|
||||
|
||||
var maxRuneBytes = [...]byte{244, 143, 191, 191}
|
||||
|
||||
// Allowable parses the pattern and determines the minimum and maximum allowable
|
||||
// values that the pattern can represent.
|
||||
// When the max cannot be determined, 'true' will be returned
|
||||
// for infinite.
|
||||
func Allowable(pattern string) (min, max string) {
|
||||
if pattern == "" || pattern[0] == '*' {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
minb := make([]byte, 0, len(pattern))
|
||||
maxb := make([]byte, 0, len(pattern))
|
||||
var wild bool
|
||||
for i := 0; i < len(pattern); i++ {
|
||||
if pattern[i] == '*' {
|
||||
wild = true
|
||||
break
|
||||
}
|
||||
if pattern[i] == '?' {
|
||||
minb = append(minb, 0)
|
||||
maxb = append(maxb, maxRuneBytes[:]...)
|
||||
} else {
|
||||
minb = append(minb, pattern[i])
|
||||
maxb = append(maxb, pattern[i])
|
||||
}
|
||||
}
|
||||
if wild {
|
||||
r, n := utf8.DecodeLastRune(maxb)
|
||||
if r != utf8.RuneError {
|
||||
if r < utf8.MaxRune {
|
||||
r++
|
||||
if r > 0x7f {
|
||||
b := make([]byte, 4)
|
||||
nn := utf8.EncodeRune(b, r)
|
||||
maxb = append(maxb[:len(maxb)-n], b[:nn]...)
|
||||
} else {
|
||||
maxb = append(maxb[:len(maxb)-n], byte(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return string(minb), string(maxb)
|
||||
}
|
||||
|
||||
// IsPattern returns true if the string is a pattern.
|
||||
func IsPattern(str string) bool {
|
||||
for i := 0; i < len(str); i++ {
|
||||
if str[i] == '*' || str[i] == '?' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Josh Baker
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,122 @@
|
|||
# Pretty
|
||||
|
||||
[](https://pkg.go.dev/github.com/tidwall/pretty)
|
||||
|
||||
Pretty is a Go package that provides [fast](#performance) methods for formatting JSON for human readability, or to compact JSON for smaller payloads.
|
||||
|
||||
Getting Started
|
||||
===============
|
||||
|
||||
## Installing
|
||||
|
||||
To start using Pretty, install Go and run `go get`:
|
||||
|
||||
```sh
|
||||
$ go get -u github.com/tidwall/pretty
|
||||
```
|
||||
|
||||
This will retrieve the library.
|
||||
|
||||
## Pretty
|
||||
|
||||
Using this example:
|
||||
|
||||
```json
|
||||
{"name": {"first":"Tom","last":"Anderson"}, "age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter", "friends": [
|
||||
{"first": "Janet", "last": "Murphy", "age": 44}
|
||||
]}
|
||||
```
|
||||
|
||||
The following code:
|
||||
```go
|
||||
result = pretty.Pretty(example)
|
||||
```
|
||||
|
||||
Will format the json to:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": {
|
||||
"first": "Tom",
|
||||
"last": "Anderson"
|
||||
},
|
||||
"age": 37,
|
||||
"children": ["Sara", "Alex", "Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{
|
||||
"first": "Janet",
|
||||
"last": "Murphy",
|
||||
"age": 44
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Color
|
||||
|
||||
Color will colorize the json for outputing to the screen.
|
||||
|
||||
```json
|
||||
result = pretty.Color(json, nil)
|
||||
```
|
||||
|
||||
Will add color to the result for printing to the terminal.
|
||||
The second param is used for a customizing the style, and passing nil will use the default `pretty.TerminalStyle`.
|
||||
|
||||
## Ugly
|
||||
|
||||
The following code:
|
||||
```go
|
||||
result = pretty.Ugly(example)
|
||||
```
|
||||
|
||||
Will format the json to:
|
||||
|
||||
```json
|
||||
{"name":{"first":"Tom","last":"Anderson"},"age":37,"children":["Sara","Alex","Jack"],"fav.movie":"Deer Hunter","friends":[{"first":"Janet","last":"Murphy","age":44}]}```
|
||||
```
|
||||
|
||||
## Customized output
|
||||
|
||||
There's a `PrettyOptions(json, opts)` function which allows for customizing the output with the following options:
|
||||
|
||||
```go
|
||||
type Options struct {
|
||||
// Width is an max column width for single line arrays
|
||||
// Default is 80
|
||||
Width int
|
||||
// Prefix is a prefix for all lines
|
||||
// Default is an empty string
|
||||
Prefix string
|
||||
// Indent is the nested indentation
|
||||
// Default is two spaces
|
||||
Indent string
|
||||
// SortKeys will sort the keys alphabetically
|
||||
// Default is false
|
||||
SortKeys bool
|
||||
}
|
||||
```
|
||||
## Performance
|
||||
|
||||
Benchmarks of Pretty alongside the builtin `encoding/json` Indent/Compact methods.
|
||||
```
|
||||
BenchmarkPretty-16 1000000 1034 ns/op 720 B/op 2 allocs/op
|
||||
BenchmarkPrettySortKeys-16 586797 1983 ns/op 2848 B/op 14 allocs/op
|
||||
BenchmarkUgly-16 4652365 254 ns/op 240 B/op 1 allocs/op
|
||||
BenchmarkUglyInPlace-16 6481233 183 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkJSONIndent-16 450654 2687 ns/op 1221 B/op 0 allocs/op
|
||||
BenchmarkJSONCompact-16 685111 1699 ns/op 442 B/op 0 allocs/op
|
||||
```
|
||||
|
||||
*These benchmarks were run on a MacBook Pro 2.4 GHz 8-Core Intel Core i9.*
|
||||
|
||||
## Contact
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
## License
|
||||
|
||||
Pretty source code is available under the MIT [License](/LICENSE).
|
||||
|
|
@ -0,0 +1,674 @@
|
|||
package pretty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Options is Pretty options
|
||||
type Options struct {
|
||||
// Width is an max column width for single line arrays
|
||||
// Default is 80
|
||||
Width int
|
||||
// Prefix is a prefix for all lines
|
||||
// Default is an empty string
|
||||
Prefix string
|
||||
// Indent is the nested indentation
|
||||
// Default is two spaces
|
||||
Indent string
|
||||
// SortKeys will sort the keys alphabetically
|
||||
// Default is false
|
||||
SortKeys bool
|
||||
}
|
||||
|
||||
// DefaultOptions is the default options for pretty formats.
|
||||
var DefaultOptions = &Options{Width: 80, Prefix: "", Indent: " ", SortKeys: false}
|
||||
|
||||
// Pretty converts the input json into a more human readable format where each
|
||||
// element is on it's own line with clear indentation.
|
||||
func Pretty(json []byte) []byte { return PrettyOptions(json, nil) }
|
||||
|
||||
// PrettyOptions is like Pretty but with customized options.
|
||||
func PrettyOptions(json []byte, opts *Options) []byte {
|
||||
if opts == nil {
|
||||
opts = DefaultOptions
|
||||
}
|
||||
buf := make([]byte, 0, len(json))
|
||||
if len(opts.Prefix) != 0 {
|
||||
buf = append(buf, opts.Prefix...)
|
||||
}
|
||||
buf, _, _, _ = appendPrettyAny(buf, json, 0, true,
|
||||
opts.Width, opts.Prefix, opts.Indent, opts.SortKeys,
|
||||
0, 0, -1)
|
||||
if len(buf) > 0 {
|
||||
buf = append(buf, '\n')
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// Ugly removes insignificant space characters from the input json byte slice
|
||||
// and returns the compacted result.
|
||||
func Ugly(json []byte) []byte {
|
||||
buf := make([]byte, 0, len(json))
|
||||
return ugly(buf, json)
|
||||
}
|
||||
|
||||
// UglyInPlace removes insignificant space characters from the input json
|
||||
// byte slice and returns the compacted result. This method reuses the
|
||||
// input json buffer to avoid allocations. Do not use the original bytes
|
||||
// slice upon return.
|
||||
func UglyInPlace(json []byte) []byte { return ugly(json, json) }
|
||||
|
||||
func ugly(dst, src []byte) []byte {
|
||||
dst = dst[:0]
|
||||
for i := 0; i < len(src); i++ {
|
||||
if src[i] > ' ' {
|
||||
dst = append(dst, src[i])
|
||||
if src[i] == '"' {
|
||||
for i = i + 1; i < len(src); i++ {
|
||||
dst = append(dst, src[i])
|
||||
if src[i] == '"' {
|
||||
j := i - 1
|
||||
for ; ; j-- {
|
||||
if src[j] != '\\' {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (j-i)%2 != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func isNaNOrInf(src []byte) bool {
|
||||
return src[0] == 'i' || //Inf
|
||||
src[0] == 'I' || // inf
|
||||
src[0] == '+' || // +Inf
|
||||
src[0] == 'N' || // Nan
|
||||
(src[0] == 'n' && len(src) > 1 && src[1] != 'u') // nan
|
||||
}
|
||||
|
||||
func appendPrettyAny(buf, json []byte, i int, pretty bool, width int, prefix, indent string, sortkeys bool, tabs, nl, max int) ([]byte, int, int, bool) {
|
||||
for ; i < len(json); i++ {
|
||||
if json[i] <= ' ' {
|
||||
continue
|
||||
}
|
||||
if json[i] == '"' {
|
||||
return appendPrettyString(buf, json, i, nl)
|
||||
}
|
||||
|
||||
if (json[i] >= '0' && json[i] <= '9') || json[i] == '-' || isNaNOrInf(json[i:]) {
|
||||
return appendPrettyNumber(buf, json, i, nl)
|
||||
}
|
||||
if json[i] == '{' {
|
||||
return appendPrettyObject(buf, json, i, '{', '}', pretty, width, prefix, indent, sortkeys, tabs, nl, max)
|
||||
}
|
||||
if json[i] == '[' {
|
||||
return appendPrettyObject(buf, json, i, '[', ']', pretty, width, prefix, indent, sortkeys, tabs, nl, max)
|
||||
}
|
||||
switch json[i] {
|
||||
case 't':
|
||||
return append(buf, 't', 'r', 'u', 'e'), i + 4, nl, true
|
||||
case 'f':
|
||||
return append(buf, 'f', 'a', 'l', 's', 'e'), i + 5, nl, true
|
||||
case 'n':
|
||||
return append(buf, 'n', 'u', 'l', 'l'), i + 4, nl, true
|
||||
}
|
||||
}
|
||||
return buf, i, nl, true
|
||||
}
|
||||
|
||||
type pair struct {
|
||||
kstart, kend int
|
||||
vstart, vend int
|
||||
}
|
||||
|
||||
type byKeyVal struct {
|
||||
sorted bool
|
||||
json []byte
|
||||
buf []byte
|
||||
pairs []pair
|
||||
}
|
||||
|
||||
func (arr *byKeyVal) Len() int {
|
||||
return len(arr.pairs)
|
||||
}
|
||||
func (arr *byKeyVal) Less(i, j int) bool {
|
||||
if arr.isLess(i, j, byKey) {
|
||||
return true
|
||||
}
|
||||
if arr.isLess(j, i, byKey) {
|
||||
return false
|
||||
}
|
||||
return arr.isLess(i, j, byVal)
|
||||
}
|
||||
func (arr *byKeyVal) Swap(i, j int) {
|
||||
arr.pairs[i], arr.pairs[j] = arr.pairs[j], arr.pairs[i]
|
||||
arr.sorted = true
|
||||
}
|
||||
|
||||
type byKind int
|
||||
|
||||
const (
|
||||
byKey byKind = 0
|
||||
byVal byKind = 1
|
||||
)
|
||||
|
||||
type jtype int
|
||||
|
||||
const (
|
||||
jnull jtype = iota
|
||||
jfalse
|
||||
jnumber
|
||||
jstring
|
||||
jtrue
|
||||
jjson
|
||||
)
|
||||
|
||||
func getjtype(v []byte) jtype {
|
||||
if len(v) == 0 {
|
||||
return jnull
|
||||
}
|
||||
switch v[0] {
|
||||
case '"':
|
||||
return jstring
|
||||
case 'f':
|
||||
return jfalse
|
||||
case 't':
|
||||
return jtrue
|
||||
case 'n':
|
||||
return jnull
|
||||
case '[', '{':
|
||||
return jjson
|
||||
default:
|
||||
return jnumber
|
||||
}
|
||||
}
|
||||
|
||||
func (arr *byKeyVal) isLess(i, j int, kind byKind) bool {
|
||||
k1 := arr.json[arr.pairs[i].kstart:arr.pairs[i].kend]
|
||||
k2 := arr.json[arr.pairs[j].kstart:arr.pairs[j].kend]
|
||||
var v1, v2 []byte
|
||||
if kind == byKey {
|
||||
v1 = k1
|
||||
v2 = k2
|
||||
} else {
|
||||
v1 = bytes.TrimSpace(arr.buf[arr.pairs[i].vstart:arr.pairs[i].vend])
|
||||
v2 = bytes.TrimSpace(arr.buf[arr.pairs[j].vstart:arr.pairs[j].vend])
|
||||
if len(v1) >= len(k1)+1 {
|
||||
v1 = bytes.TrimSpace(v1[len(k1)+1:])
|
||||
}
|
||||
if len(v2) >= len(k2)+1 {
|
||||
v2 = bytes.TrimSpace(v2[len(k2)+1:])
|
||||
}
|
||||
}
|
||||
t1 := getjtype(v1)
|
||||
t2 := getjtype(v2)
|
||||
if t1 < t2 {
|
||||
return true
|
||||
}
|
||||
if t1 > t2 {
|
||||
return false
|
||||
}
|
||||
if t1 == jstring {
|
||||
s1 := parsestr(v1)
|
||||
s2 := parsestr(v2)
|
||||
return string(s1) < string(s2)
|
||||
}
|
||||
if t1 == jnumber {
|
||||
n1, _ := strconv.ParseFloat(string(v1), 64)
|
||||
n2, _ := strconv.ParseFloat(string(v2), 64)
|
||||
return n1 < n2
|
||||
}
|
||||
return string(v1) < string(v2)
|
||||
|
||||
}
|
||||
|
||||
func parsestr(s []byte) []byte {
|
||||
for i := 1; i < len(s); i++ {
|
||||
if s[i] == '\\' {
|
||||
var str string
|
||||
json.Unmarshal(s, &str)
|
||||
return []byte(str)
|
||||
}
|
||||
if s[i] == '"' {
|
||||
return s[1:i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendPrettyObject(buf, json []byte, i int, open, close byte, pretty bool, width int, prefix, indent string, sortkeys bool, tabs, nl, max int) ([]byte, int, int, bool) {
|
||||
var ok bool
|
||||
if width > 0 {
|
||||
if pretty && open == '[' && max == -1 {
|
||||
// here we try to create a single line array
|
||||
max := width - (len(buf) - nl)
|
||||
if max > 3 {
|
||||
s1, s2 := len(buf), i
|
||||
buf, i, _, ok = appendPrettyObject(buf, json, i, '[', ']', false, width, prefix, "", sortkeys, 0, 0, max)
|
||||
if ok && len(buf)-s1 <= max {
|
||||
return buf, i, nl, true
|
||||
}
|
||||
buf = buf[:s1]
|
||||
i = s2
|
||||
}
|
||||
} else if max != -1 && open == '{' {
|
||||
return buf, i, nl, false
|
||||
}
|
||||
}
|
||||
buf = append(buf, open)
|
||||
i++
|
||||
var pairs []pair
|
||||
if open == '{' && sortkeys {
|
||||
pairs = make([]pair, 0, 8)
|
||||
}
|
||||
var n int
|
||||
for ; i < len(json); i++ {
|
||||
if json[i] <= ' ' {
|
||||
continue
|
||||
}
|
||||
if json[i] == close {
|
||||
if pretty {
|
||||
if open == '{' && sortkeys {
|
||||
buf = sortPairs(json, buf, pairs)
|
||||
}
|
||||
if n > 0 {
|
||||
nl = len(buf)
|
||||
if buf[nl-1] == ' ' {
|
||||
buf[nl-1] = '\n'
|
||||
} else {
|
||||
buf = append(buf, '\n')
|
||||
}
|
||||
}
|
||||
if buf[len(buf)-1] != open {
|
||||
buf = appendTabs(buf, prefix, indent, tabs)
|
||||
}
|
||||
}
|
||||
buf = append(buf, close)
|
||||
return buf, i + 1, nl, open != '{'
|
||||
}
|
||||
if open == '[' || json[i] == '"' {
|
||||
if n > 0 {
|
||||
buf = append(buf, ',')
|
||||
if width != -1 && open == '[' {
|
||||
buf = append(buf, ' ')
|
||||
}
|
||||
}
|
||||
var p pair
|
||||
if pretty {
|
||||
nl = len(buf)
|
||||
if buf[nl-1] == ' ' {
|
||||
buf[nl-1] = '\n'
|
||||
} else {
|
||||
buf = append(buf, '\n')
|
||||
}
|
||||
if open == '{' && sortkeys {
|
||||
p.kstart = i
|
||||
p.vstart = len(buf)
|
||||
}
|
||||
buf = appendTabs(buf, prefix, indent, tabs+1)
|
||||
}
|
||||
if open == '{' {
|
||||
buf, i, nl, _ = appendPrettyString(buf, json, i, nl)
|
||||
if sortkeys {
|
||||
p.kend = i
|
||||
}
|
||||
buf = append(buf, ':')
|
||||
if pretty {
|
||||
buf = append(buf, ' ')
|
||||
}
|
||||
}
|
||||
buf, i, nl, ok = appendPrettyAny(buf, json, i, pretty, width, prefix, indent, sortkeys, tabs+1, nl, max)
|
||||
if max != -1 && !ok {
|
||||
return buf, i, nl, false
|
||||
}
|
||||
if pretty && open == '{' && sortkeys {
|
||||
p.vend = len(buf)
|
||||
if p.kstart > p.kend || p.vstart > p.vend {
|
||||
// bad data. disable sorting
|
||||
sortkeys = false
|
||||
} else {
|
||||
pairs = append(pairs, p)
|
||||
}
|
||||
}
|
||||
i--
|
||||
n++
|
||||
}
|
||||
}
|
||||
return buf, i, nl, open != '{'
|
||||
}
|
||||
func sortPairs(json, buf []byte, pairs []pair) []byte {
|
||||
if len(pairs) == 0 {
|
||||
return buf
|
||||
}
|
||||
vstart := pairs[0].vstart
|
||||
vend := pairs[len(pairs)-1].vend
|
||||
arr := byKeyVal{false, json, buf, pairs}
|
||||
sort.Stable(&arr)
|
||||
if !arr.sorted {
|
||||
return buf
|
||||
}
|
||||
nbuf := make([]byte, 0, vend-vstart)
|
||||
for i, p := range pairs {
|
||||
nbuf = append(nbuf, buf[p.vstart:p.vend]...)
|
||||
if i < len(pairs)-1 {
|
||||
nbuf = append(nbuf, ',')
|
||||
nbuf = append(nbuf, '\n')
|
||||
}
|
||||
}
|
||||
return append(buf[:vstart], nbuf...)
|
||||
}
|
||||
|
||||
func appendPrettyString(buf, json []byte, i, nl int) ([]byte, int, int, bool) {
|
||||
s := i
|
||||
i++
|
||||
for ; i < len(json); i++ {
|
||||
if json[i] == '"' {
|
||||
var sc int
|
||||
for j := i - 1; j > s; j-- {
|
||||
if json[j] == '\\' {
|
||||
sc++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if sc%2 == 1 {
|
||||
continue
|
||||
}
|
||||
i++
|
||||
break
|
||||
}
|
||||
}
|
||||
return append(buf, json[s:i]...), i, nl, true
|
||||
}
|
||||
|
||||
func appendPrettyNumber(buf, json []byte, i, nl int) ([]byte, int, int, bool) {
|
||||
s := i
|
||||
i++
|
||||
for ; i < len(json); i++ {
|
||||
if json[i] <= ' ' || json[i] == ',' || json[i] == ':' || json[i] == ']' || json[i] == '}' {
|
||||
break
|
||||
}
|
||||
}
|
||||
return append(buf, json[s:i]...), i, nl, true
|
||||
}
|
||||
|
||||
func appendTabs(buf []byte, prefix, indent string, tabs int) []byte {
|
||||
if len(prefix) != 0 {
|
||||
buf = append(buf, prefix...)
|
||||
}
|
||||
if len(indent) == 2 && indent[0] == ' ' && indent[1] == ' ' {
|
||||
for i := 0; i < tabs; i++ {
|
||||
buf = append(buf, ' ', ' ')
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < tabs; i++ {
|
||||
buf = append(buf, indent...)
|
||||
}
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// Style is the color style
|
||||
type Style struct {
|
||||
Key, String, Number [2]string
|
||||
True, False, Null [2]string
|
||||
Escape [2]string
|
||||
Append func(dst []byte, c byte) []byte
|
||||
}
|
||||
|
||||
func hexp(p byte) byte {
|
||||
switch {
|
||||
case p < 10:
|
||||
return p + '0'
|
||||
default:
|
||||
return (p - 10) + 'a'
|
||||
}
|
||||
}
|
||||
|
||||
// TerminalStyle is for terminals
|
||||
var TerminalStyle *Style
|
||||
|
||||
func init() {
|
||||
TerminalStyle = &Style{
|
||||
Key: [2]string{"\x1B[94m", "\x1B[0m"},
|
||||
String: [2]string{"\x1B[92m", "\x1B[0m"},
|
||||
Number: [2]string{"\x1B[93m", "\x1B[0m"},
|
||||
True: [2]string{"\x1B[96m", "\x1B[0m"},
|
||||
False: [2]string{"\x1B[96m", "\x1B[0m"},
|
||||
Null: [2]string{"\x1B[91m", "\x1B[0m"},
|
||||
Escape: [2]string{"\x1B[35m", "\x1B[0m"},
|
||||
Append: func(dst []byte, c byte) []byte {
|
||||
if c < ' ' && (c != '\r' && c != '\n' && c != '\t' && c != '\v') {
|
||||
dst = append(dst, "\\u00"...)
|
||||
dst = append(dst, hexp((c>>4)&0xF))
|
||||
return append(dst, hexp((c)&0xF))
|
||||
}
|
||||
return append(dst, c)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Color will colorize the json. The style parma is used for customizing
|
||||
// the colors. Passing nil to the style param will use the default
|
||||
// TerminalStyle.
|
||||
func Color(src []byte, style *Style) []byte {
|
||||
if style == nil {
|
||||
style = TerminalStyle
|
||||
}
|
||||
apnd := style.Append
|
||||
if apnd == nil {
|
||||
apnd = func(dst []byte, c byte) []byte {
|
||||
return append(dst, c)
|
||||
}
|
||||
}
|
||||
type stackt struct {
|
||||
kind byte
|
||||
key bool
|
||||
}
|
||||
var dst []byte
|
||||
var stack []stackt
|
||||
for i := 0; i < len(src); i++ {
|
||||
if src[i] == '"' {
|
||||
key := len(stack) > 0 && stack[len(stack)-1].key
|
||||
if key {
|
||||
dst = append(dst, style.Key[0]...)
|
||||
} else {
|
||||
dst = append(dst, style.String[0]...)
|
||||
}
|
||||
dst = apnd(dst, '"')
|
||||
esc := false
|
||||
uesc := 0
|
||||
for i = i + 1; i < len(src); i++ {
|
||||
if src[i] == '\\' {
|
||||
if key {
|
||||
dst = append(dst, style.Key[1]...)
|
||||
} else {
|
||||
dst = append(dst, style.String[1]...)
|
||||
}
|
||||
dst = append(dst, style.Escape[0]...)
|
||||
dst = apnd(dst, src[i])
|
||||
esc = true
|
||||
if i+1 < len(src) && src[i+1] == 'u' {
|
||||
uesc = 5
|
||||
} else {
|
||||
uesc = 1
|
||||
}
|
||||
} else if esc {
|
||||
dst = apnd(dst, src[i])
|
||||
if uesc == 1 {
|
||||
esc = false
|
||||
dst = append(dst, style.Escape[1]...)
|
||||
if key {
|
||||
dst = append(dst, style.Key[0]...)
|
||||
} else {
|
||||
dst = append(dst, style.String[0]...)
|
||||
}
|
||||
} else {
|
||||
uesc--
|
||||
}
|
||||
} else {
|
||||
dst = apnd(dst, src[i])
|
||||
}
|
||||
if src[i] == '"' {
|
||||
j := i - 1
|
||||
for ; ; j-- {
|
||||
if src[j] != '\\' {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (j-i)%2 != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if esc {
|
||||
dst = append(dst, style.Escape[1]...)
|
||||
} else if key {
|
||||
dst = append(dst, style.Key[1]...)
|
||||
} else {
|
||||
dst = append(dst, style.String[1]...)
|
||||
}
|
||||
} else if src[i] == '{' || src[i] == '[' {
|
||||
stack = append(stack, stackt{src[i], src[i] == '{'})
|
||||
dst = apnd(dst, src[i])
|
||||
} else if (src[i] == '}' || src[i] == ']') && len(stack) > 0 {
|
||||
stack = stack[:len(stack)-1]
|
||||
dst = apnd(dst, src[i])
|
||||
} else if (src[i] == ':' || src[i] == ',') && len(stack) > 0 && stack[len(stack)-1].kind == '{' {
|
||||
stack[len(stack)-1].key = !stack[len(stack)-1].key
|
||||
dst = apnd(dst, src[i])
|
||||
} else {
|
||||
var kind byte
|
||||
if (src[i] >= '0' && src[i] <= '9') || src[i] == '-' || isNaNOrInf(src[i:]) {
|
||||
kind = '0'
|
||||
dst = append(dst, style.Number[0]...)
|
||||
} else if src[i] == 't' {
|
||||
kind = 't'
|
||||
dst = append(dst, style.True[0]...)
|
||||
} else if src[i] == 'f' {
|
||||
kind = 'f'
|
||||
dst = append(dst, style.False[0]...)
|
||||
} else if src[i] == 'n' {
|
||||
kind = 'n'
|
||||
dst = append(dst, style.Null[0]...)
|
||||
} else {
|
||||
dst = apnd(dst, src[i])
|
||||
}
|
||||
if kind != 0 {
|
||||
for ; i < len(src); i++ {
|
||||
if src[i] <= ' ' || src[i] == ',' || src[i] == ':' || src[i] == ']' || src[i] == '}' {
|
||||
i--
|
||||
break
|
||||
}
|
||||
dst = apnd(dst, src[i])
|
||||
}
|
||||
if kind == '0' {
|
||||
dst = append(dst, style.Number[1]...)
|
||||
} else if kind == 't' {
|
||||
dst = append(dst, style.True[1]...)
|
||||
} else if kind == 'f' {
|
||||
dst = append(dst, style.False[1]...)
|
||||
} else if kind == 'n' {
|
||||
dst = append(dst, style.Null[1]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// Spec strips out comments and trailing commas and convert the input to a
|
||||
// valid JSON per the official spec: https://tools.ietf.org/html/rfc8259
|
||||
//
|
||||
// The resulting JSON will always be the same length as the input and it will
|
||||
// include all of the same line breaks at matching offsets. This is to ensure
|
||||
// the result can be later processed by a external parser and that that
|
||||
// parser will report messages or errors with the correct offsets.
|
||||
func Spec(src []byte) []byte {
|
||||
return spec(src, nil)
|
||||
}
|
||||
|
||||
// SpecInPlace is the same as Spec, but this method reuses the input json
|
||||
// buffer to avoid allocations. Do not use the original bytes slice upon return.
|
||||
func SpecInPlace(src []byte) []byte {
|
||||
return spec(src, src)
|
||||
}
|
||||
|
||||
func spec(src, dst []byte) []byte {
|
||||
dst = dst[:0]
|
||||
for i := 0; i < len(src); i++ {
|
||||
if src[i] == '/' {
|
||||
if i < len(src)-1 {
|
||||
if src[i+1] == '/' {
|
||||
dst = append(dst, ' ', ' ')
|
||||
i += 2
|
||||
for ; i < len(src); i++ {
|
||||
if src[i] == '\n' {
|
||||
dst = append(dst, '\n')
|
||||
break
|
||||
} else if src[i] == '\t' || src[i] == '\r' {
|
||||
dst = append(dst, src[i])
|
||||
} else {
|
||||
dst = append(dst, ' ')
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if src[i+1] == '*' {
|
||||
dst = append(dst, ' ', ' ')
|
||||
i += 2
|
||||
for ; i < len(src)-1; i++ {
|
||||
if src[i] == '*' && src[i+1] == '/' {
|
||||
dst = append(dst, ' ', ' ')
|
||||
i++
|
||||
break
|
||||
} else if src[i] == '\n' || src[i] == '\t' ||
|
||||
src[i] == '\r' {
|
||||
dst = append(dst, src[i])
|
||||
} else {
|
||||
dst = append(dst, ' ')
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
dst = append(dst, src[i])
|
||||
if src[i] == '"' {
|
||||
for i = i + 1; i < len(src); i++ {
|
||||
dst = append(dst, src[i])
|
||||
if src[i] == '"' {
|
||||
j := i - 1
|
||||
for ; ; j-- {
|
||||
if src[j] != '\\' {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (j-i)%2 != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if src[i] == '}' || src[i] == ']' {
|
||||
for j := len(dst) - 2; j >= 0; j-- {
|
||||
if dst[j] <= ' ' {
|
||||
continue
|
||||
}
|
||||
if dst[j] == ',' {
|
||||
dst[j] = ' '
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Josh Baker
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
|
@ -0,0 +1,278 @@
|
|||
<p align="center">
|
||||
<img
|
||||
src="logo.png"
|
||||
width="240" height="78" border="0" alt="SJSON">
|
||||
<br>
|
||||
<a href="https://godoc.org/github.com/tidwall/sjson"><img src="https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt="GoDoc"></a>
|
||||
</p>
|
||||
|
||||
<p align="center">set a json value quickly</p>
|
||||
|
||||
SJSON is a Go package that provides a [very fast](#performance) and simple way to set a value in a json document.
|
||||
For quickly retrieving json values check out [GJSON](https://github.com/tidwall/gjson).
|
||||
|
||||
For a command line interface check out [JJ](https://github.com/tidwall/jj).
|
||||
|
||||
Getting Started
|
||||
===============
|
||||
|
||||
Installing
|
||||
----------
|
||||
|
||||
To start using SJSON, install Go and run `go get`:
|
||||
|
||||
```sh
|
||||
$ go get -u github.com/tidwall/sjson
|
||||
```
|
||||
|
||||
This will retrieve the library.
|
||||
|
||||
Set a value
|
||||
-----------
|
||||
Set sets the value for the specified path.
|
||||
A path is in dot syntax, such as "name.last" or "age".
|
||||
This function expects that the json is well-formed and validated.
|
||||
Invalid json will not panic, but it may return back unexpected results.
|
||||
Invalid paths may return an error.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/tidwall/sjson"
|
||||
|
||||
const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
|
||||
|
||||
func main() {
|
||||
value, _ := sjson.Set(json, "name.last", "Anderson")
|
||||
println(value)
|
||||
}
|
||||
```
|
||||
|
||||
This will print:
|
||||
|
||||
```json
|
||||
{"name":{"first":"Janet","last":"Anderson"},"age":47}
|
||||
```
|
||||
|
||||
Path syntax
|
||||
-----------
|
||||
|
||||
A path is a series of keys separated by a dot.
|
||||
The dot and colon characters can be escaped with ``\``.
|
||||
|
||||
```json
|
||||
{
|
||||
"name": {"first": "Tom", "last": "Anderson"},
|
||||
"age":37,
|
||||
"children": ["Sara","Alex","Jack"],
|
||||
"fav.movie": "Deer Hunter",
|
||||
"friends": [
|
||||
{"first": "James", "last": "Murphy"},
|
||||
{"first": "Roger", "last": "Craig"}
|
||||
]
|
||||
}
|
||||
```
|
||||
```
|
||||
"name.last" >> "Anderson"
|
||||
"age" >> 37
|
||||
"children.1" >> "Alex"
|
||||
"friends.1.last" >> "Craig"
|
||||
```
|
||||
|
||||
The `-1` key can be used to append a value to an existing array:
|
||||
|
||||
```
|
||||
"children.-1" >> appends a new value to the end of the children array
|
||||
```
|
||||
|
||||
Normally number keys are used to modify arrays, but it's possible to force a numeric object key by using the colon character:
|
||||
|
||||
```json
|
||||
{
|
||||
"users":{
|
||||
"2313":{"name":"Sara"},
|
||||
"7839":{"name":"Andy"}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
A colon path would look like:
|
||||
|
||||
```
|
||||
"users.:2313.name" >> "Sara"
|
||||
```
|
||||
|
||||
Supported types
|
||||
---------------
|
||||
|
||||
Pretty much any type is supported:
|
||||
|
||||
```go
|
||||
sjson.Set(`{"key":true}`, "key", nil)
|
||||
sjson.Set(`{"key":true}`, "key", false)
|
||||
sjson.Set(`{"key":true}`, "key", 1)
|
||||
sjson.Set(`{"key":true}`, "key", 10.5)
|
||||
sjson.Set(`{"key":true}`, "key", "hello")
|
||||
sjson.Set(`{"key":true}`, "key", []string{"hello", "world"})
|
||||
sjson.Set(`{"key":true}`, "key", map[string]interface{}{"hello":"world"})
|
||||
```
|
||||
|
||||
When a type is not recognized, SJSON will fallback to the `encoding/json` Marshaller.
|
||||
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Set a value from empty document:
|
||||
```go
|
||||
value, _ := sjson.Set("", "name", "Tom")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"name":"Tom"}
|
||||
```
|
||||
|
||||
Set a nested value from empty document:
|
||||
```go
|
||||
value, _ := sjson.Set("", "name.last", "Anderson")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"name":{"last":"Anderson"}}
|
||||
```
|
||||
|
||||
Set a new value:
|
||||
```go
|
||||
value, _ := sjson.Set(`{"name":{"last":"Anderson"}}`, "name.first", "Sara")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"name":{"first":"Sara","last":"Anderson"}}
|
||||
```
|
||||
|
||||
Update an existing value:
|
||||
```go
|
||||
value, _ := sjson.Set(`{"name":{"last":"Anderson"}}`, "name.last", "Smith")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"name":{"last":"Smith"}}
|
||||
```
|
||||
|
||||
Set a new array value:
|
||||
```go
|
||||
value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.2", "Sara")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"friends":["Andy","Carol","Sara"]
|
||||
```
|
||||
|
||||
Append an array value by using the `-1` key in a path:
|
||||
```go
|
||||
value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.-1", "Sara")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"friends":["Andy","Carol","Sara"]
|
||||
```
|
||||
|
||||
Append an array value that is past the end:
|
||||
```go
|
||||
value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.4", "Sara")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"friends":["Andy","Carol",null,null,"Sara"]
|
||||
```
|
||||
|
||||
Delete a value:
|
||||
```go
|
||||
value, _ := sjson.Delete(`{"name":{"first":"Sara","last":"Anderson"}}`, "name.first")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"name":{"last":"Anderson"}}
|
||||
```
|
||||
|
||||
Delete an array value:
|
||||
```go
|
||||
value, _ := sjson.Delete(`{"friends":["Andy","Carol"]}`, "friends.1")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"friends":["Andy"]}
|
||||
```
|
||||
|
||||
Delete the last array value:
|
||||
```go
|
||||
value, _ := sjson.Delete(`{"friends":["Andy","Carol"]}`, "friends.-1")
|
||||
println(value)
|
||||
|
||||
// Output:
|
||||
// {"friends":["Andy"]}
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
Benchmarks of SJSON alongside [encoding/json](https://golang.org/pkg/encoding/json/),
|
||||
[ffjson](https://github.com/pquerna/ffjson),
|
||||
[EasyJSON](https://github.com/mailru/easyjson),
|
||||
and [Gabs](https://github.com/Jeffail/gabs)
|
||||
|
||||
```
|
||||
Benchmark_SJSON-8 3000000 805 ns/op 1077 B/op 3 allocs/op
|
||||
Benchmark_SJSON_ReplaceInPlace-8 3000000 449 ns/op 0 B/op 0 allocs/op
|
||||
Benchmark_JSON_Map-8 300000 21236 ns/op 6392 B/op 150 allocs/op
|
||||
Benchmark_JSON_Struct-8 300000 14691 ns/op 1789 B/op 24 allocs/op
|
||||
Benchmark_Gabs-8 300000 21311 ns/op 6752 B/op 150 allocs/op
|
||||
Benchmark_FFJSON-8 300000 17673 ns/op 3589 B/op 47 allocs/op
|
||||
Benchmark_EasyJSON-8 1500000 3119 ns/op 1061 B/op 13 allocs/op
|
||||
```
|
||||
|
||||
JSON document used:
|
||||
|
||||
```json
|
||||
{
|
||||
"widget": {
|
||||
"debug": "on",
|
||||
"window": {
|
||||
"title": "Sample Konfabulator Widget",
|
||||
"name": "main_window",
|
||||
"width": 500,
|
||||
"height": 500
|
||||
},
|
||||
"image": {
|
||||
"src": "Images/Sun.png",
|
||||
"hOffset": 250,
|
||||
"vOffset": 250,
|
||||
"alignment": "center"
|
||||
},
|
||||
"text": {
|
||||
"data": "Click Here",
|
||||
"size": 36,
|
||||
"style": "bold",
|
||||
"vOffset": 100,
|
||||
"alignment": "center",
|
||||
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Each operation was rotated though one of the following search paths:
|
||||
|
||||
```
|
||||
widget.window.name
|
||||
widget.image.hOffset
|
||||
widget.text.onMouseUp
|
||||
```
|
||||
|
||||
*These benchmarks were run on a MacBook Pro 15" 2.8 GHz Intel Core i7 using Go 1.7 and can be be found [here](https://github.com/tidwall/sjson-benchmarks)*.
|
||||
|
||||
## Contact
|
||||
Josh Baker [@tidwall](http://twitter.com/tidwall)
|
||||
|
||||
## License
|
||||
|
||||
SJSON source code is available under the MIT [License](/LICENSE).
|
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
|
@ -0,0 +1,737 @@
|
|||
// Package sjson provides setting json values.
|
||||
package sjson
|
||||
|
||||
import (
|
||||
jsongo "encoding/json"
|
||||
"sort"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type errorType struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (err *errorType) Error() string {
|
||||
return err.msg
|
||||
}
|
||||
|
||||
// Options represents additional options for the Set and Delete functions.
|
||||
type Options struct {
|
||||
// Optimistic is a hint that the value likely exists which
|
||||
// allows for the sjson to perform a fast-track search and replace.
|
||||
Optimistic bool
|
||||
// ReplaceInPlace is a hint to replace the input json rather than
|
||||
// allocate a new json byte slice. When this field is specified
|
||||
// the input json will not longer be valid and it should not be used
|
||||
// In the case when the destination slice doesn't have enough free
|
||||
// bytes to replace the data in place, a new bytes slice will be
|
||||
// created under the hood.
|
||||
// The Optimistic flag must be set to true and the input must be a
|
||||
// byte slice in order to use this field.
|
||||
ReplaceInPlace bool
|
||||
}
|
||||
|
||||
type pathResult struct {
|
||||
part string // current key part
|
||||
gpart string // gjson get part
|
||||
path string // remaining path
|
||||
force bool // force a string key
|
||||
more bool // there is more path to parse
|
||||
}
|
||||
|
||||
func isSimpleChar(ch byte) bool {
|
||||
switch ch {
|
||||
case '|', '#', '@', '*', '?':
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func parsePath(path string) (res pathResult, simple bool) {
|
||||
var r pathResult
|
||||
if len(path) > 0 && path[0] == ':' {
|
||||
r.force = true
|
||||
path = path[1:]
|
||||
}
|
||||
for i := 0; i < len(path); i++ {
|
||||
if path[i] == '.' {
|
||||
r.part = path[:i]
|
||||
r.gpart = path[:i]
|
||||
r.path = path[i+1:]
|
||||
r.more = true
|
||||
return r, true
|
||||
}
|
||||
if !isSimpleChar(path[i]) {
|
||||
return r, false
|
||||
}
|
||||
if path[i] == '\\' {
|
||||
// go into escape mode. this is a slower path that
|
||||
// strips off the escape character from the part.
|
||||
epart := []byte(path[:i])
|
||||
gpart := []byte(path[:i+1])
|
||||
i++
|
||||
if i < len(path) {
|
||||
epart = append(epart, path[i])
|
||||
gpart = append(gpart, path[i])
|
||||
i++
|
||||
for ; i < len(path); i++ {
|
||||
if path[i] == '\\' {
|
||||
gpart = append(gpart, '\\')
|
||||
i++
|
||||
if i < len(path) {
|
||||
epart = append(epart, path[i])
|
||||
gpart = append(gpart, path[i])
|
||||
}
|
||||
continue
|
||||
} else if path[i] == '.' {
|
||||
r.part = string(epart)
|
||||
r.gpart = string(gpart)
|
||||
r.path = path[i+1:]
|
||||
r.more = true
|
||||
return r, true
|
||||
} else if !isSimpleChar(path[i]) {
|
||||
return r, false
|
||||
}
|
||||
epart = append(epart, path[i])
|
||||
gpart = append(gpart, path[i])
|
||||
}
|
||||
}
|
||||
// append the last part
|
||||
r.part = string(epart)
|
||||
r.gpart = string(gpart)
|
||||
return r, true
|
||||
}
|
||||
}
|
||||
r.part = path
|
||||
r.gpart = path
|
||||
return r, true
|
||||
}
|
||||
|
||||
func mustMarshalString(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] < ' ' || s[i] > 0x7f || s[i] == '"' || s[i] == '\\' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// appendStringify makes a json string and appends to buf.
|
||||
func appendStringify(buf []byte, s string) []byte {
|
||||
if mustMarshalString(s) {
|
||||
b, _ := jsongo.Marshal(s)
|
||||
return append(buf, b...)
|
||||
}
|
||||
buf = append(buf, '"')
|
||||
buf = append(buf, s...)
|
||||
buf = append(buf, '"')
|
||||
return buf
|
||||
}
|
||||
|
||||
// appendBuild builds a json block from a json path.
|
||||
func appendBuild(buf []byte, array bool, paths []pathResult, raw string,
|
||||
stringify bool) []byte {
|
||||
if !array {
|
||||
buf = appendStringify(buf, paths[0].part)
|
||||
buf = append(buf, ':')
|
||||
}
|
||||
if len(paths) > 1 {
|
||||
n, numeric := atoui(paths[1])
|
||||
if numeric || (!paths[1].force && paths[1].part == "-1") {
|
||||
buf = append(buf, '[')
|
||||
buf = appendRepeat(buf, "null,", n)
|
||||
buf = appendBuild(buf, true, paths[1:], raw, stringify)
|
||||
buf = append(buf, ']')
|
||||
} else {
|
||||
buf = append(buf, '{')
|
||||
buf = appendBuild(buf, false, paths[1:], raw, stringify)
|
||||
buf = append(buf, '}')
|
||||
}
|
||||
} else {
|
||||
if stringify {
|
||||
buf = appendStringify(buf, raw)
|
||||
} else {
|
||||
buf = append(buf, raw...)
|
||||
}
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// atoui does a rip conversion of string -> unigned int.
|
||||
func atoui(r pathResult) (n int, ok bool) {
|
||||
if r.force {
|
||||
return 0, false
|
||||
}
|
||||
for i := 0; i < len(r.part); i++ {
|
||||
if r.part[i] < '0' || r.part[i] > '9' {
|
||||
return 0, false
|
||||
}
|
||||
n = n*10 + int(r.part[i]-'0')
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
|
||||
// appendRepeat repeats string "n" times and appends to buf.
|
||||
func appendRepeat(buf []byte, s string, n int) []byte {
|
||||
for i := 0; i < n; i++ {
|
||||
buf = append(buf, s...)
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// trim does a rip trim
|
||||
func trim(s string) string {
|
||||
for len(s) > 0 {
|
||||
if s[0] <= ' ' {
|
||||
s = s[1:]
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
for len(s) > 0 {
|
||||
if s[len(s)-1] <= ' ' {
|
||||
s = s[:len(s)-1]
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// deleteTailItem deletes the previous key or comma.
|
||||
func deleteTailItem(buf []byte) ([]byte, bool) {
|
||||
loop:
|
||||
for i := len(buf) - 1; i >= 0; i-- {
|
||||
// look for either a ',',':','['
|
||||
switch buf[i] {
|
||||
case '[':
|
||||
return buf, true
|
||||
case ',':
|
||||
return buf[:i], false
|
||||
case ':':
|
||||
// delete tail string
|
||||
i--
|
||||
for ; i >= 0; i-- {
|
||||
if buf[i] == '"' {
|
||||
i--
|
||||
for ; i >= 0; i-- {
|
||||
if buf[i] == '"' {
|
||||
i--
|
||||
if i >= 0 && buf[i] == '\\' {
|
||||
i--
|
||||
continue
|
||||
}
|
||||
for ; i >= 0; i-- {
|
||||
// look for either a ',','{'
|
||||
switch buf[i] {
|
||||
case '{':
|
||||
return buf[:i+1], true
|
||||
case ',':
|
||||
return buf[:i], false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
break loop
|
||||
}
|
||||
}
|
||||
return buf, false
|
||||
}
|
||||
|
||||
var errNoChange = &errorType{"no change"}
|
||||
|
||||
func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
|
||||
stringify, del bool) ([]byte, error) {
|
||||
var err error
|
||||
var res gjson.Result
|
||||
var found bool
|
||||
if del {
|
||||
if paths[0].part == "-1" && !paths[0].force {
|
||||
res = gjson.Get(jstr, "#")
|
||||
if res.Int() > 0 {
|
||||
res = gjson.Get(jstr, strconv.FormatInt(int64(res.Int()-1), 10))
|
||||
found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
res = gjson.Get(jstr, paths[0].gpart)
|
||||
}
|
||||
if res.Index > 0 {
|
||||
if len(paths) > 1 {
|
||||
buf = append(buf, jstr[:res.Index]...)
|
||||
buf, err = appendRawPaths(buf, res.Raw, paths[1:], raw,
|
||||
stringify, del)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf = append(buf, jstr[res.Index+len(res.Raw):]...)
|
||||
return buf, nil
|
||||
}
|
||||
buf = append(buf, jstr[:res.Index]...)
|
||||
var exidx int // additional forward stripping
|
||||
if del {
|
||||
var delNextComma bool
|
||||
buf, delNextComma = deleteTailItem(buf)
|
||||
if delNextComma {
|
||||
i, j := res.Index+len(res.Raw), 0
|
||||
for ; i < len(jstr); i, j = i+1, j+1 {
|
||||
if jstr[i] <= ' ' {
|
||||
continue
|
||||
}
|
||||
if jstr[i] == ',' {
|
||||
exidx = j + 1
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if stringify {
|
||||
buf = appendStringify(buf, raw)
|
||||
} else {
|
||||
buf = append(buf, raw...)
|
||||
}
|
||||
}
|
||||
buf = append(buf, jstr[res.Index+len(res.Raw)+exidx:]...)
|
||||
return buf, nil
|
||||
}
|
||||
if del {
|
||||
return nil, errNoChange
|
||||
}
|
||||
n, numeric := atoui(paths[0])
|
||||
isempty := true
|
||||
for i := 0; i < len(jstr); i++ {
|
||||
if jstr[i] > ' ' {
|
||||
isempty = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isempty {
|
||||
if numeric {
|
||||
jstr = "[]"
|
||||
} else {
|
||||
jstr = "{}"
|
||||
}
|
||||
}
|
||||
jsres := gjson.Parse(jstr)
|
||||
if jsres.Type != gjson.JSON {
|
||||
if numeric {
|
||||
jstr = "[]"
|
||||
} else {
|
||||
jstr = "{}"
|
||||
}
|
||||
jsres = gjson.Parse(jstr)
|
||||
}
|
||||
var comma bool
|
||||
for i := 1; i < len(jsres.Raw); i++ {
|
||||
if jsres.Raw[i] <= ' ' {
|
||||
continue
|
||||
}
|
||||
if jsres.Raw[i] == '}' || jsres.Raw[i] == ']' {
|
||||
break
|
||||
}
|
||||
comma = true
|
||||
break
|
||||
}
|
||||
switch jsres.Raw[0] {
|
||||
default:
|
||||
return nil, &errorType{"json must be an object or array"}
|
||||
case '{':
|
||||
end := len(jsres.Raw) - 1
|
||||
for ; end > 0; end-- {
|
||||
if jsres.Raw[end] == '}' {
|
||||
break
|
||||
}
|
||||
}
|
||||
buf = append(buf, jsres.Raw[:end]...)
|
||||
if comma {
|
||||
buf = append(buf, ',')
|
||||
}
|
||||
buf = appendBuild(buf, false, paths, raw, stringify)
|
||||
buf = append(buf, '}')
|
||||
return buf, nil
|
||||
case '[':
|
||||
var appendit bool
|
||||
if !numeric {
|
||||
if paths[0].part == "-1" && !paths[0].force {
|
||||
appendit = true
|
||||
} else {
|
||||
return nil, &errorType{
|
||||
"cannot set array element for non-numeric key '" +
|
||||
paths[0].part + "'"}
|
||||
}
|
||||
}
|
||||
if appendit {
|
||||
njson := trim(jsres.Raw)
|
||||
if njson[len(njson)-1] == ']' {
|
||||
njson = njson[:len(njson)-1]
|
||||
}
|
||||
buf = append(buf, njson...)
|
||||
if comma {
|
||||
buf = append(buf, ',')
|
||||
}
|
||||
|
||||
buf = appendBuild(buf, true, paths, raw, stringify)
|
||||
buf = append(buf, ']')
|
||||
return buf, nil
|
||||
}
|
||||
buf = append(buf, '[')
|
||||
ress := jsres.Array()
|
||||
for i := 0; i < len(ress); i++ {
|
||||
if i > 0 {
|
||||
buf = append(buf, ',')
|
||||
}
|
||||
buf = append(buf, ress[i].Raw...)
|
||||
}
|
||||
if len(ress) == 0 {
|
||||
buf = appendRepeat(buf, "null,", n-len(ress))
|
||||
} else {
|
||||
buf = appendRepeat(buf, ",null", n-len(ress))
|
||||
if comma {
|
||||
buf = append(buf, ',')
|
||||
}
|
||||
}
|
||||
buf = appendBuild(buf, true, paths, raw, stringify)
|
||||
buf = append(buf, ']')
|
||||
return buf, nil
|
||||
}
|
||||
}
|
||||
|
||||
func isOptimisticPath(path string) bool {
|
||||
for i := 0; i < len(path); i++ {
|
||||
if path[i] < '.' || path[i] > 'z' {
|
||||
return false
|
||||
}
|
||||
if path[i] > '9' && path[i] < 'A' {
|
||||
return false
|
||||
}
|
||||
if path[i] > 'z' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Set sets a json value for the specified path.
|
||||
// A path is in dot syntax, such as "name.last" or "age".
|
||||
// This function expects that the json is well-formed, and does not validate.
|
||||
// Invalid json will not panic, but it may return back unexpected results.
|
||||
// An error is returned if the path is not valid.
|
||||
//
|
||||
// A path is a series of keys separated by a dot.
|
||||
//
|
||||
// {
|
||||
// "name": {"first": "Tom", "last": "Anderson"},
|
||||
// "age":37,
|
||||
// "children": ["Sara","Alex","Jack"],
|
||||
// "friends": [
|
||||
// {"first": "James", "last": "Murphy"},
|
||||
// {"first": "Roger", "last": "Craig"}
|
||||
// ]
|
||||
// }
|
||||
// "name.last" >> "Anderson"
|
||||
// "age" >> 37
|
||||
// "children.1" >> "Alex"
|
||||
//
|
||||
func Set(json, path string, value interface{}) (string, error) {
|
||||
return SetOptions(json, path, value, nil)
|
||||
}
|
||||
|
||||
// SetBytes sets a json value for the specified path.
|
||||
// If working with bytes, this method preferred over
|
||||
// Set(string(data), path, value)
|
||||
func SetBytes(json []byte, path string, value interface{}) ([]byte, error) {
|
||||
return SetBytesOptions(json, path, value, nil)
|
||||
}
|
||||
|
||||
// SetRaw sets a raw json value for the specified path.
|
||||
// This function works the same as Set except that the value is set as a
|
||||
// raw block of json. This allows for setting premarshalled json objects.
|
||||
func SetRaw(json, path, value string) (string, error) {
|
||||
return SetRawOptions(json, path, value, nil)
|
||||
}
|
||||
|
||||
// SetRawOptions sets a raw json value for the specified path with options.
|
||||
// This furnction works the same as SetOptions except that the value is set
|
||||
// as a raw block of json. This allows for setting premarshalled json objects.
|
||||
func SetRawOptions(json, path, value string, opts *Options) (string, error) {
|
||||
var optimistic bool
|
||||
if opts != nil {
|
||||
optimistic = opts.Optimistic
|
||||
}
|
||||
res, err := set(json, path, value, false, false, optimistic, false)
|
||||
if err == errNoChange {
|
||||
return json, nil
|
||||
}
|
||||
return string(res), err
|
||||
}
|
||||
|
||||
// SetRawBytes sets a raw json value for the specified path.
|
||||
// If working with bytes, this method preferred over
|
||||
// SetRaw(string(data), path, value)
|
||||
func SetRawBytes(json []byte, path string, value []byte) ([]byte, error) {
|
||||
return SetRawBytesOptions(json, path, value, nil)
|
||||
}
|
||||
|
||||
type dtype struct{}
|
||||
|
||||
// Delete deletes a value from json for the specified path.
|
||||
func Delete(json, path string) (string, error) {
|
||||
return Set(json, path, dtype{})
|
||||
}
|
||||
|
||||
// DeleteBytes deletes a value from json for the specified path.
|
||||
func DeleteBytes(json []byte, path string) ([]byte, error) {
|
||||
return SetBytes(json, path, dtype{})
|
||||
}
|
||||
|
||||
type stringHeader struct {
|
||||
data unsafe.Pointer
|
||||
len int
|
||||
}
|
||||
|
||||
type sliceHeader struct {
|
||||
data unsafe.Pointer
|
||||
len int
|
||||
cap int
|
||||
}
|
||||
|
||||
func set(jstr, path, raw string,
|
||||
stringify, del, optimistic, inplace bool) ([]byte, error) {
|
||||
if path == "" {
|
||||
return []byte(jstr), &errorType{"path cannot be empty"}
|
||||
}
|
||||
if !del && optimistic && isOptimisticPath(path) {
|
||||
res := gjson.Get(jstr, path)
|
||||
if res.Exists() && res.Index > 0 {
|
||||
sz := len(jstr) - len(res.Raw) + len(raw)
|
||||
if stringify {
|
||||
sz += 2
|
||||
}
|
||||
if inplace && sz <= len(jstr) {
|
||||
if !stringify || !mustMarshalString(raw) {
|
||||
jsonh := *(*stringHeader)(unsafe.Pointer(&jstr))
|
||||
jsonbh := sliceHeader{
|
||||
data: jsonh.data, len: jsonh.len, cap: jsonh.len}
|
||||
jbytes := *(*[]byte)(unsafe.Pointer(&jsonbh))
|
||||
if stringify {
|
||||
jbytes[res.Index] = '"'
|
||||
copy(jbytes[res.Index+1:], []byte(raw))
|
||||
jbytes[res.Index+1+len(raw)] = '"'
|
||||
copy(jbytes[res.Index+1+len(raw)+1:],
|
||||
jbytes[res.Index+len(res.Raw):])
|
||||
} else {
|
||||
copy(jbytes[res.Index:], []byte(raw))
|
||||
copy(jbytes[res.Index+len(raw):],
|
||||
jbytes[res.Index+len(res.Raw):])
|
||||
}
|
||||
return jbytes[:sz], nil
|
||||
}
|
||||
return []byte(jstr), nil
|
||||
}
|
||||
buf := make([]byte, 0, sz)
|
||||
buf = append(buf, jstr[:res.Index]...)
|
||||
if stringify {
|
||||
buf = appendStringify(buf, raw)
|
||||
} else {
|
||||
buf = append(buf, raw...)
|
||||
}
|
||||
buf = append(buf, jstr[res.Index+len(res.Raw):]...)
|
||||
return buf, nil
|
||||
}
|
||||
}
|
||||
var paths []pathResult
|
||||
r, simple := parsePath(path)
|
||||
if simple {
|
||||
paths = append(paths, r)
|
||||
for r.more {
|
||||
r, simple = parsePath(r.path)
|
||||
if !simple {
|
||||
break
|
||||
}
|
||||
paths = append(paths, r)
|
||||
}
|
||||
}
|
||||
if !simple {
|
||||
if del {
|
||||
return []byte(jstr),
|
||||
&errorType{"cannot delete value from a complex path"}
|
||||
}
|
||||
return setComplexPath(jstr, path, raw, stringify)
|
||||
}
|
||||
njson, err := appendRawPaths(nil, jstr, paths, raw, stringify, del)
|
||||
if err != nil {
|
||||
return []byte(jstr), err
|
||||
}
|
||||
return njson, nil
|
||||
}
|
||||
|
||||
func setComplexPath(jstr, path, raw string, stringify bool) ([]byte, error) {
|
||||
res := gjson.Get(jstr, path)
|
||||
if !res.Exists() || !(res.Index != 0 || len(res.Indexes) != 0) {
|
||||
return []byte(jstr), errNoChange
|
||||
}
|
||||
if res.Index != 0 {
|
||||
njson := []byte(jstr[:res.Index])
|
||||
if stringify {
|
||||
njson = appendStringify(njson, raw)
|
||||
} else {
|
||||
njson = append(njson, raw...)
|
||||
}
|
||||
njson = append(njson, jstr[res.Index+len(res.Raw):]...)
|
||||
jstr = string(njson)
|
||||
}
|
||||
if len(res.Indexes) > 0 {
|
||||
type val struct {
|
||||
index int
|
||||
res gjson.Result
|
||||
}
|
||||
vals := make([]val, 0, len(res.Indexes))
|
||||
res.ForEach(func(_, vres gjson.Result) bool {
|
||||
vals = append(vals, val{res: vres})
|
||||
return true
|
||||
})
|
||||
if len(res.Indexes) != len(vals) {
|
||||
return []byte(jstr), errNoChange
|
||||
}
|
||||
for i := 0; i < len(res.Indexes); i++ {
|
||||
vals[i].index = res.Indexes[i]
|
||||
}
|
||||
sort.SliceStable(vals, func(i, j int) bool {
|
||||
return vals[i].index > vals[j].index
|
||||
})
|
||||
for _, val := range vals {
|
||||
vres := val.res
|
||||
index := val.index
|
||||
njson := []byte(jstr[:index])
|
||||
if stringify {
|
||||
njson = appendStringify(njson, raw)
|
||||
} else {
|
||||
njson = append(njson, raw...)
|
||||
}
|
||||
njson = append(njson, jstr[index+len(vres.Raw):]...)
|
||||
jstr = string(njson)
|
||||
}
|
||||
}
|
||||
return []byte(jstr), nil
|
||||
}
|
||||
|
||||
// SetOptions sets a json value for the specified path with options.
|
||||
// A path is in dot syntax, such as "name.last" or "age".
|
||||
// This function expects that the json is well-formed, and does not validate.
|
||||
// Invalid json will not panic, but it may return back unexpected results.
|
||||
// An error is returned if the path is not valid.
|
||||
func SetOptions(json, path string, value interface{},
|
||||
opts *Options) (string, error) {
|
||||
if opts != nil {
|
||||
if opts.ReplaceInPlace {
|
||||
// it's not safe to replace bytes in-place for strings
|
||||
// copy the Options and set options.ReplaceInPlace to false.
|
||||
nopts := *opts
|
||||
opts = &nopts
|
||||
opts.ReplaceInPlace = false
|
||||
}
|
||||
}
|
||||
jsonh := *(*stringHeader)(unsafe.Pointer(&json))
|
||||
jsonbh := sliceHeader{data: jsonh.data, len: jsonh.len, cap: jsonh.len}
|
||||
jsonb := *(*[]byte)(unsafe.Pointer(&jsonbh))
|
||||
res, err := SetBytesOptions(jsonb, path, value, opts)
|
||||
return string(res), err
|
||||
}
|
||||
|
||||
// SetBytesOptions sets a json value for the specified path with options.
|
||||
// If working with bytes, this method preferred over
|
||||
// SetOptions(string(data), path, value)
|
||||
func SetBytesOptions(json []byte, path string, value interface{},
|
||||
opts *Options) ([]byte, error) {
|
||||
var optimistic, inplace bool
|
||||
if opts != nil {
|
||||
optimistic = opts.Optimistic
|
||||
inplace = opts.ReplaceInPlace
|
||||
}
|
||||
jstr := *(*string)(unsafe.Pointer(&json))
|
||||
var res []byte
|
||||
var err error
|
||||
switch v := value.(type) {
|
||||
default:
|
||||
b, merr := jsongo.Marshal(value)
|
||||
if merr != nil {
|
||||
return nil, merr
|
||||
}
|
||||
raw := *(*string)(unsafe.Pointer(&b))
|
||||
res, err = set(jstr, path, raw, false, false, optimistic, inplace)
|
||||
case dtype:
|
||||
res, err = set(jstr, path, "", false, true, optimistic, inplace)
|
||||
case string:
|
||||
res, err = set(jstr, path, v, true, false, optimistic, inplace)
|
||||
case []byte:
|
||||
raw := *(*string)(unsafe.Pointer(&v))
|
||||
res, err = set(jstr, path, raw, true, false, optimistic, inplace)
|
||||
case bool:
|
||||
if v {
|
||||
res, err = set(jstr, path, "true", false, false, optimistic, inplace)
|
||||
} else {
|
||||
res, err = set(jstr, path, "false", false, false, optimistic, inplace)
|
||||
}
|
||||
case int8:
|
||||
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case int16:
|
||||
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case int32:
|
||||
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case int64:
|
||||
res, err = set(jstr, path, strconv.FormatInt(int64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case uint8:
|
||||
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case uint16:
|
||||
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case uint32:
|
||||
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case uint64:
|
||||
res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10),
|
||||
false, false, optimistic, inplace)
|
||||
case float32:
|
||||
res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64),
|
||||
false, false, optimistic, inplace)
|
||||
case float64:
|
||||
res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64),
|
||||
false, false, optimistic, inplace)
|
||||
}
|
||||
if err == errNoChange {
|
||||
return json, nil
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// SetRawBytesOptions sets a raw json value for the specified path with options.
|
||||
// If working with bytes, this method preferred over
|
||||
// SetRawOptions(string(data), path, value, opts)
|
||||
func SetRawBytesOptions(json []byte, path string, value []byte,
|
||||
opts *Options) ([]byte, error) {
|
||||
jstr := *(*string)(unsafe.Pointer(&json))
|
||||
vstr := *(*string)(unsafe.Pointer(&value))
|
||||
var optimistic, inplace bool
|
||||
if opts != nil {
|
||||
optimistic = opts.Optimistic
|
||||
inplace = opts.ReplaceInPlace
|
||||
}
|
||||
res, err := set(jstr, path, vstr, false, false, optimistic, inplace)
|
||||
if err == errNoChange {
|
||||
return json, nil
|
||||
}
|
||||
return res, err
|
||||
}
|
|
@ -459,6 +459,18 @@ github.com/stretchr/testify/require
|
|||
# github.com/subosito/gotenv v1.4.2
|
||||
## explicit; go 1.18
|
||||
github.com/subosito/gotenv
|
||||
# github.com/tidwall/gjson v1.17.1
|
||||
## explicit; go 1.12
|
||||
github.com/tidwall/gjson
|
||||
# github.com/tidwall/match v1.1.1
|
||||
## explicit; go 1.15
|
||||
github.com/tidwall/match
|
||||
# github.com/tidwall/pretty v1.2.0
|
||||
## explicit; go 1.16
|
||||
github.com/tidwall/pretty
|
||||
# github.com/tidwall/sjson v1.2.5
|
||||
## explicit; go 1.14
|
||||
github.com/tidwall/sjson
|
||||
# github.com/vektra/mockery/v2 v2.10.0
|
||||
## explicit; go 1.16
|
||||
github.com/vektra/mockery/v2
|
||||
|
|
Loading…
Reference in New Issue