MCPcopy
hub / github.com/tidwall/gjson / modReverse

Function modReverse

gjson.go:3005–3044  ·  view source on GitHub ↗

@reverse reverses array elements or root object members.

(json, arg string)

Source from the content-addressed store, hash-verified

3003
3004// @reverse reverses array elements or root object members.
3005func modReverse(json, arg string) string {
3006 res := Parse(json)
3007 if res.IsArray() {
3008 var values []Result
3009 res.ForEach(func(_, value Result) bool {
3010 values = append(values, value)
3011 return true
3012 })
3013 out := make([]byte, 0, len(json))
3014 out = append(out, '[')
3015 for i, j := len(values)-1, 0; i >= 0; i, j = i-1, j+1 {
3016 if j > 0 {
3017 out = append(out, ',')
3018 }
3019 out = append(out, values[i].Raw...)
3020 }
3021 out = append(out, ']')
3022 return bytesString(out)
3023 }
3024 if res.IsObject() {
3025 var keyValues []Result
3026 res.ForEach(func(key, value Result) bool {
3027 keyValues = append(keyValues, key, value)
3028 return true
3029 })
3030 out := make([]byte, 0, len(json))
3031 out = append(out, '{')
3032 for i, j := len(keyValues)-2, 0; i >= 0; i, j = i-2, j+1 {
3033 if j > 0 {
3034 out = append(out, ',')
3035 }
3036 out = append(out, keyValues[i+0].Raw...)
3037 out = append(out, ':')
3038 out = append(out, keyValues[i+1].Raw...)
3039 }
3040 out = append(out, '}')
3041 return bytesString(out)
3042 }
3043 return json
3044}
3045
3046// @flatten an array with child arrays.
3047//

Callers

nothing calls this directly

Calls 5

ParseFunction · 0.85
bytesStringFunction · 0.85
IsArrayMethod · 0.80
ForEachMethod · 0.80
IsObjectMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…