Go: Bump github.com/tidwall/gjson from 1.14.4 to 1.16.0 (#7089)

Bumps [github.com/tidwall/gjson](https://github.com/tidwall/gjson) from 1.14.4 to 1.16.0.
- [Commits](https://github.com/tidwall/gjson/compare/v1.14.4...v1.16.0)

---
updated-dependencies:
- dependency-name: github.com/tidwall/gjson
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
dependabot[bot]
2023-09-18 13:52:51 +02:00
committed by GitHub
parent 6bea0b56c6
commit 28def95001
6 changed files with 225 additions and 89 deletions

2
go.mod
View File

@@ -47,7 +47,7 @@ require (
github.com/spf13/afero v1.6.0 github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
github.com/tidwall/gjson v1.14.4 github.com/tidwall/gjson v1.16.0
github.com/zalando/go-keyring v0.2.3 github.com/zalando/go-keyring v0.2.3
golang.org/x/sync v0.3.0 golang.org/x/sync v0.3.0
golang.org/x/sys v0.12.0 golang.org/x/sys v0.12.0

4
go.sum generated
View File

@@ -1217,8 +1217,8 @@ github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I= github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=

View File

@@ -211,6 +211,7 @@ There are currently the following built-in modifiers:
- `@tostr`: Converts json to a string. Wraps a json string. - `@tostr`: Converts json to a string. Wraps a json string.
- `@fromstr`: Converts a string from json. Unwraps 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). - `@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 ### Modifier arguments

View File

@@ -137,12 +137,21 @@ next major release.*
The `~` (tilde) operator will convert a value to a boolean before comparison. 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: For example, using the following JSON:
```json ```json
{ {
"vals": [ "vals": [
{ "a": 1, "b": true }, { "a": 1, "b": "data" },
{ "a": 2, "b": true }, { "a": 2, "b": true },
{ "a": 3, "b": false }, { "a": 3, "b": false },
{ "a": 4, "b": "0" }, { "a": 4, "b": "0" },
@@ -157,15 +166,23 @@ For example, using the following JSON:
} }
``` ```
You can now query for all true(ish) or false(ish) values: To query for all true-ish or false-ish values:
``` ```
vals.#(b==~true)#.a >> [1,2,6,7,8] vals.#(b==~true)#.a >> [2,6,7,8]
vals.#(b==~false)#.a >> [3,4,5,9,10,11] vals.#(b==~false)#.a >> [3,4,5,9,10,11]
``` ```
The last value which was non-existent is treated as `false` 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 ### Dot vs Pipe
The `.` is standard separator, but it's also possible to use a `|`. The `.` is standard separator, but it's also possible to use a `|`.
@@ -241,6 +258,7 @@ There are currently the following built-in modifiers:
- `@tostr`: Converts json to a string. Wraps a json string. - `@tostr`: Converts json to a string. Wraps a json string.
- `@fromstr`: Converts a string from json. Unwraps 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). - `@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 #### Modifier arguments

View File

@@ -645,9 +645,9 @@ func tostr(json string) (raw string, str string) {
// Exists returns true if value exists. // Exists returns true if value exists.
// //
// if gjson.Get(json, "name.last").Exists(){ // if gjson.Get(json, "name.last").Exists(){
// println("value exists") // println("value exists")
// } // }
func (t Result) Exists() bool { func (t Result) Exists() bool {
return t.Type != Null || len(t.Raw) != 0 return t.Type != Null || len(t.Raw) != 0
} }
@@ -661,7 +661,6 @@ func (t Result) Exists() bool {
// nil, for JSON null // nil, for JSON null
// map[string]interface{}, for JSON objects // map[string]interface{}, for JSON objects
// []interface{}, for JSON arrays // []interface{}, for JSON arrays
//
func (t Result) Value() interface{} { func (t Result) Value() interface{} {
if t.Type == String { if t.Type == String {
return t.Str return t.Str
@@ -826,19 +825,28 @@ func parseArrayPath(path string) (r arrayPathResult) {
} }
// splitQuery takes a query and splits it into three parts: // splitQuery takes a query and splits it into three parts:
// path, op, middle, and right. //
// path, op, middle, and right.
//
// So for this query: // So for this query:
// #(first_name=="Murphy").last //
// #(first_name=="Murphy").last
//
// Becomes // Becomes
// first_name # path //
// =="Murphy" # middle // first_name # path
// .last # right // =="Murphy" # middle
// .last # right
//
// Or, // Or,
// #(service_roles.#(=="one")).cap //
// #(service_roles.#(=="one")).cap
//
// Becomes // Becomes
// service_roles.#(=="one") # path //
// # middle // service_roles.#(=="one") # path
// .cap # right // # middle
// .cap # right
func parseQuery(query string) ( func parseQuery(query string) (
path, op, value, remain string, i int, vesc, ok bool, path, op, value, remain string, i int, vesc, ok bool,
) { ) {
@@ -1251,15 +1259,74 @@ func matchLimit(str, pattern string) bool {
return matched return matched
} }
func falseish(t Result) bool {
switch t.Type {
case Null:
return true
case False:
return true
case String:
b, err := strconv.ParseBool(strings.ToLower(t.Str))
if err != nil {
return false
}
return !b
case Number:
return t.Num == 0
default:
return false
}
}
func trueish(t Result) bool {
switch t.Type {
case True:
return true
case String:
b, err := strconv.ParseBool(strings.ToLower(t.Str))
if err != nil {
return false
}
return b
case Number:
return t.Num != 0
default:
return false
}
}
func nullish(t Result) bool {
return t.Type == Null
}
func queryMatches(rp *arrayPathResult, value Result) bool { func queryMatches(rp *arrayPathResult, value Result) bool {
rpv := rp.query.value rpv := rp.query.value
if len(rpv) > 0 && rpv[0] == '~' { if len(rpv) > 0 {
// convert to bool if rpv[0] == '~' {
rpv = rpv[1:] // convert to bool
if value.Bool() { rpv = rpv[1:]
value = Result{Type: True} var ish, ok bool
} else { switch rpv {
value = Result{Type: False} case "*":
ish, ok = value.Exists(), true
case "null":
ish, ok = nullish(value), true
case "true":
ish, ok = trueish(value), true
case "false":
ish, ok = falseish(value), true
}
if ok {
rpv = "true"
if ish {
value = Result{Type: True}
} else {
value = Result{Type: False}
}
} else {
rpv = ""
value = Result{}
}
} }
} }
if !value.Exists() { if !value.Exists() {
@@ -1918,23 +1985,23 @@ type parseContext struct {
// the '#' character. // the '#' character.
// The dot and wildcard character can be escaped with '\'. // The dot and wildcard character can be escaped with '\'.
// //
// { // {
// "name": {"first": "Tom", "last": "Anderson"}, // "name": {"first": "Tom", "last": "Anderson"},
// "age":37, // "age":37,
// "children": ["Sara","Alex","Jack"], // "children": ["Sara","Alex","Jack"],
// "friends": [ // "friends": [
// {"first": "James", "last": "Murphy"}, // {"first": "James", "last": "Murphy"},
// {"first": "Roger", "last": "Craig"} // {"first": "Roger", "last": "Craig"}
// ] // ]
// } // }
// "name.last" >> "Anderson" // "name.last" >> "Anderson"
// "age" >> 37 // "age" >> 37
// "children" >> ["Sara","Alex","Jack"] // "children" >> ["Sara","Alex","Jack"]
// "children.#" >> 3 // "children.#" >> 3
// "children.1" >> "Alex" // "children.1" >> "Alex"
// "child*.2" >> "Jack" // "child*.2" >> "Jack"
// "c?ildren.0" >> "Sara" // "c?ildren.0" >> "Sara"
// "friends.#.first" >> ["James","Roger"] // "friends.#.first" >> ["James","Roger"]
// //
// This function expects that the json is well-formed, and does not validate. // 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. // Invalid json will not panic, but it may return back unexpected results.
@@ -2126,8 +2193,7 @@ func unescape(json string) string {
// The caseSensitive paramater is used when the tokens are Strings. // The caseSensitive paramater is used when the tokens are Strings.
// The order when comparing two different type is: // The order when comparing two different type is:
// //
// Null < False < Number < String < True < JSON // Null < False < Number < String < True < JSON
//
func (t Result) Less(token Result, caseSensitive bool) bool { func (t Result) Less(token Result, caseSensitive bool) bool {
if t.Type < token.Type { if t.Type < token.Type {
return true return true
@@ -2556,11 +2622,10 @@ func validnull(data []byte, i int) (outi int, ok bool) {
// Valid returns true if the input is valid json. // Valid returns true if the input is valid json.
// //
// if !gjson.Valid(json) { // if !gjson.Valid(json) {
// return errors.New("invalid json") // return errors.New("invalid json")
// } // }
// value := gjson.Get(json, "name.last") // value := gjson.Get(json, "name.last")
//
func Valid(json string) bool { func Valid(json string) bool {
_, ok := validpayload(stringBytes(json), 0) _, ok := validpayload(stringBytes(json), 0)
return ok return ok
@@ -2568,13 +2633,12 @@ func Valid(json string) bool {
// ValidBytes returns true if the input is valid json. // ValidBytes returns true if the input is valid json.
// //
// if !gjson.Valid(json) { // if !gjson.Valid(json) {
// return errors.New("invalid json") // return errors.New("invalid json")
// } // }
// value := gjson.Get(json, "name.last") // value := gjson.Get(json, "name.last")
// //
// If working with bytes, this method preferred over ValidBytes(string(data)) // If working with bytes, this method preferred over ValidBytes(string(data))
//
func ValidBytes(json []byte) bool { func ValidBytes(json []byte) bool {
_, ok := validpayload(json, 0) _, ok := validpayload(json, 0)
return ok return ok
@@ -2690,6 +2754,7 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
var parsedArgs bool var parsedArgs bool
switch pathOut[0] { switch pathOut[0] {
case '{', '[', '"': case '{', '[', '"':
// json arg
res := Parse(pathOut) res := Parse(pathOut)
if res.Exists() { if res.Exists() {
args = squash(pathOut) args = squash(pathOut)
@@ -2698,14 +2763,20 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
} }
} }
if !parsedArgs { if !parsedArgs {
idx := strings.IndexByte(pathOut, '|') // simple arg
if idx == -1 { i := 0
args = pathOut for ; i < len(pathOut); i++ {
pathOut = "" if pathOut[i] == '|' {
} else { break
args = pathOut[:idx] }
pathOut = pathOut[idx:] switch pathOut[i] {
case '{', '[', '"', '(':
s := squash(pathOut[i:])
i += len(s) - 1
}
} }
args = pathOut[:i]
pathOut = pathOut[i:]
} }
} }
return pathOut, fn(json, args), true return pathOut, fn(json, args), true
@@ -2725,19 +2796,24 @@ func unwrap(json string) string {
// DisableModifiers will disable the modifier syntax // DisableModifiers will disable the modifier syntax
var DisableModifiers = false var DisableModifiers = false
var modifiers = map[string]func(json, arg string) string{ var modifiers map[string]func(json, arg string) string
"pretty": modPretty,
"ugly": modUgly, func init() {
"reverse": modReverse, modifiers = map[string]func(json, arg string) string{
"this": modThis, "pretty": modPretty,
"flatten": modFlatten, "ugly": modUgly,
"join": modJoin, "reverse": modReverse,
"valid": modValid, "this": modThis,
"keys": modKeys, "flatten": modFlatten,
"values": modValues, "join": modJoin,
"tostr": modToStr, "valid": modValid,
"fromstr": modFromStr, "keys": modKeys,
"group": modGroup, "values": modValues,
"tostr": modToStr,
"fromstr": modFromStr,
"group": modGroup,
"dig": modDig,
}
} }
// AddModifier binds a custom modifier command to the GJSON syntax. // AddModifier binds a custom modifier command to the GJSON syntax.
@@ -2848,9 +2924,13 @@ func modReverse(json, arg string) string {
} }
// @flatten an array with child arrays. // @flatten an array with child arrays.
// [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]] //
// [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]]
//
// The {"deep":true} arg can be provide for deep flattening. // The {"deep":true} arg can be provide for deep flattening.
// [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7] //
// [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7]
//
// The original json is returned when the json is not an array. // The original json is returned when the json is not an array.
func modFlatten(json, arg string) string { func modFlatten(json, arg string) string {
res := Parse(json) res := Parse(json)
@@ -2895,7 +2975,8 @@ func modFlatten(json, arg string) string {
} }
// @keys extracts the keys from an object. // @keys extracts the keys from an object.
// {"first":"Tom","last":"Smith"} -> ["first","last"] //
// {"first":"Tom","last":"Smith"} -> ["first","last"]
func modKeys(json, arg string) string { func modKeys(json, arg string) string {
v := Parse(json) v := Parse(json)
if !v.Exists() { if !v.Exists() {
@@ -2922,7 +3003,8 @@ func modKeys(json, arg string) string {
} }
// @values extracts the values from an object. // @values extracts the values from an object.
// {"first":"Tom","last":"Smith"} -> ["Tom","Smith"] //
// {"first":"Tom","last":"Smith"} -> ["Tom","Smith"]
func modValues(json, arg string) string { func modValues(json, arg string) string {
v := Parse(json) v := Parse(json)
if !v.Exists() { if !v.Exists() {
@@ -2947,11 +3029,17 @@ func modValues(json, arg string) string {
} }
// @join multiple objects into a single object. // @join multiple objects into a single object.
// [{"first":"Tom"},{"last":"Smith"}] -> {"first","Tom","last":"Smith"} //
// [{"first":"Tom"},{"last":"Smith"}] -> {"first","Tom","last":"Smith"}
//
// The arg can be "true" to specify that duplicate keys should be preserved. // The arg can be "true" to specify that duplicate keys should be preserved.
// [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":37,"age":41} //
// [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":37,"age":41}
//
// Without preserved keys: // Without preserved keys:
// [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":41} //
// [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":41}
//
// The original json is returned when the json is not an object. // The original json is returned when the json is not an object.
func modJoin(json, arg string) string { func modJoin(json, arg string) string {
res := Parse(json) res := Parse(json)
@@ -3024,7 +3112,8 @@ func modValid(json, arg string) string {
} }
// @fromstr converts a string to json // @fromstr converts a string to json
// "{\"id\":1023,\"name\":\"alert\"}" -> {"id":1023,"name":"alert"} //
// "{\"id\":1023,\"name\":\"alert\"}" -> {"id":1023,"name":"alert"}
func modFromStr(json, arg string) string { func modFromStr(json, arg string) string {
if !Valid(json) { if !Valid(json) {
return "" return ""
@@ -3033,7 +3122,8 @@ func modFromStr(json, arg string) string {
} }
// @tostr converts a string to json // @tostr converts a string to json
// {"id":1023,"name":"alert"} -> "{\"id\":1023,\"name\":\"alert\"}" //
// {"id":1023,"name":"alert"} -> "{\"id\":1023,\"name\":\"alert\"}"
func modToStr(str, arg string) string { func modToStr(str, arg string) string {
return string(AppendJSONString(nil, str)) return string(AppendJSONString(nil, str))
} }
@@ -3210,11 +3300,11 @@ func revSquash(json string) string {
// Paths returns the original GJSON paths for a Result where the Result came // Paths returns the original GJSON paths for a Result where the Result came
// from a simple query path that returns an array, like: // from a simple query path that returns an array, like:
// //
// gjson.Get(json, "friends.#.first") // gjson.Get(json, "friends.#.first")
// //
// The returned value will be in the form of a JSON array: // The returned value will be in the form of a JSON array:
// //
// ["friends.0.first","friends.1.first","friends.2.first"] // ["friends.0.first","friends.1.first","friends.2.first"]
// //
// The param 'json' must be the original JSON used when calling Get. // The param 'json' must be the original JSON used when calling Get.
// //
@@ -3239,11 +3329,11 @@ func (t Result) Paths(json string) []string {
// Path returns the original GJSON path for a Result where the Result came // Path returns the original GJSON path for a Result where the Result came
// from a simple path that returns a single value, like: // from a simple path that returns a single value, like:
// //
// gjson.Get(json, "friends.#(last=Murphy)") // gjson.Get(json, "friends.#(last=Murphy)")
// //
// The returned value will be in the form of a JSON string: // The returned value will be in the form of a JSON string:
// //
// "friends.0" // "friends.0"
// //
// The param 'json' must be the original JSON used when calling Get. // The param 'json' must be the original JSON used when calling Get.
// //
@@ -3357,3 +3447,30 @@ func escapeComp(comp string) string {
} }
return comp return comp
} }
func parseRecursiveDescent(all []Result, parent Result, path string) []Result {
if res := parent.Get(path); res.Exists() {
all = append(all, res)
}
if parent.IsArray() || parent.IsObject() {
parent.ForEach(func(_, val Result) bool {
all = parseRecursiveDescent(all, val, path)
return true
})
}
return all
}
func modDig(json, arg string) string {
all := parseRecursiveDescent(nil, Parse(json), arg)
var out []byte
out = append(out, '[')
for i, res := range all {
if i > 0 {
out = append(out, ',')
}
out = append(out, res.Raw...)
}
out = append(out, ']')
return string(out)
}

2
vendor/modules.txt generated vendored
View File

@@ -817,7 +817,7 @@ github.com/spf13/pflag
## explicit; go 1.20 ## explicit; go 1.20
github.com/stretchr/testify/assert github.com/stretchr/testify/assert
github.com/stretchr/testify/require github.com/stretchr/testify/require
# github.com/tidwall/gjson v1.14.4 # github.com/tidwall/gjson v1.16.0
## explicit; go 1.12 ## explicit; go 1.12
github.com/tidwall/gjson github.com/tidwall/gjson
# github.com/tidwall/match v1.1.1 # github.com/tidwall/match v1.1.1