@@ -859,9 +859,9 @@ func TestIssue20(t *testing.T) {
859859}
860860
861861func TestIssue21 (t * testing.T ) {
862- json := `{ "Level1Field1":3,
863- "Level1Field4":4,
864- "Level1Field2":{ "Level2Field1":[ "value1", "value2" ],
862+ json := `{ "Level1Field1":3,
863+ "Level1Field4":4,
864+ "Level1Field2":{ "Level2Field1":[ "value1", "value2" ],
865865 "Level2Field2":{ "Level3Field1":[ { "key1":"value1" } ] } } }`
866866 paths := []string {"Level1Field1" , "Level1Field2.Level2Field1" ,
867867 "Level1Field2.Level2Field2.Level3Field1" , "Level1Field4" }
@@ -922,7 +922,7 @@ var complicatedJSON = `
922922 "nestedTagged": {
923923 "Green": "Green",
924924 "Map": {
925- "this": "that",
925+ "this": "that",
926926 "and": "the other thing"
927927 },
928928 "Ints": {
@@ -1291,10 +1291,10 @@ func TestArrayValues(t *testing.T) {
12911291 }
12921292 expect := strings .Join ([]string {
12931293 `gjson.Result{Type:3, Raw:"\"PERSON1\"", Str:"PERSON1", Num:0, ` +
1294- `Index:0}` ,
1294+ `Index:0, Indexes:[]int(nil) }` ,
12951295 `gjson.Result{Type:3, Raw:"\"PERSON2\"", Str:"PERSON2", Num:0, ` +
1296- `Index:0}` ,
1297- `gjson.Result{Type:2, Raw:"0", Str:"", Num:0, Index:0}` ,
1296+ `Index:0, Indexes:[]int(nil) }` ,
1297+ `gjson.Result{Type:2, Raw:"0", Str:"", Num:0, Index:0, Indexes:[]int(nil) }` ,
12981298 }, "\n " )
12991299 if output != expect {
13001300 t .Fatalf ("expected '%v', got '%v'" , expect , output )
@@ -1492,7 +1492,7 @@ func TestDeepSelectors(t *testing.T) {
14921492 }
14931493 },
14941494 {
1495- "first": "Roger", "last": "Craig",
1495+ "first": "Roger", "last": "Craig",
14961496 "extra": [40,50,60],
14971497 "details": {
14981498 "city": "Phoenix",
@@ -2119,4 +2119,80 @@ func TestModifierDoubleQuotes(t *testing.T) {
21192119 `{"name":"Product P4","value":"{\"productId\":\"1cc3\",\"vendorId\":\"20de\"}"},` +
21202120 `{"name":"Product P4","value":"{\"productId\":\"1dd3\",\"vendorId\":\"30de\"}"}` +
21212121 `]` )
2122+
2123+ }
2124+
2125+ func TestIndexes (t * testing.T ) {
2126+ var exampleJSON = `{
2127+ "vals": [
2128+ [1,66,{test: 3}],
2129+ [4,5,[6]]
2130+ ],
2131+ "objectArray":[
2132+ {"first": "Dale", "age": 44},
2133+ {"first": "Roger", "age": 68},
2134+ ]
2135+ }`
2136+
2137+ testCases := []struct {
2138+ path string
2139+ expected []string
2140+ }{
2141+ {
2142+ `vals.#.1` ,
2143+ []string {`6` , "5" },
2144+ },
2145+ {
2146+ `vals.#.2` ,
2147+ []string {"{" , "[" },
2148+ },
2149+ {
2150+ `objectArray.#(age>43)#.first` ,
2151+ []string {`"` , `"` },
2152+ },
2153+ {
2154+ `objectArray.@reverse.#.first` ,
2155+ nil ,
2156+ },
2157+ }
2158+
2159+ for _ , tc := range testCases {
2160+ r := Get (exampleJSON , tc .path )
2161+
2162+ assert (t , len (r .Indexes ) == len (tc .expected ))
2163+
2164+ for i , a := range r .Indexes {
2165+ assert (t , string (exampleJSON [a ]) == tc .expected [i ])
2166+ }
2167+ }
2168+ }
2169+
2170+ func TestHashtagIndexesMatchesRaw (t * testing.T ) {
2171+ var exampleJSON = `{
2172+ "objectArray":[
2173+ {"first": "Dale", "age": 44},
2174+ {"first": "Roger", "age": 68},
2175+ ]
2176+ }`
2177+ r := Get (exampleJSON , `objectArray.#(age>43)#.first` )
2178+ all := Get (exampleJSON , `@this` )
2179+ all .ForEach (func (_ , value Result ) bool {
2180+ if value .IsArray () {
2181+ value .ForEach (func (_ , v Result ) bool {
2182+ if v .IsArray () {
2183+ v .ForEach (func (_ , sv Result ) bool {
2184+ if sv .IsObject () {
2185+ assert (t , string (exampleJSON [r .Indexes [0 ]:r .Indexes [0 ]+ len (sv .Raw )]) == sv .Raw )
2186+ }
2187+ if sv .IsArray () {
2188+ assert (t , string (exampleJSON [r .Indexes [1 ]:r .Indexes [1 ]+ len (sv .Raw )]) == sv .Raw )
2189+ }
2190+ return true
2191+ })
2192+ }
2193+ return true
2194+ })
2195+ }
2196+ return true
2197+ })
21222198}
0 commit comments