File tree 2 files changed +29
-7
lines changed
2 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -214,6 +214,7 @@ func searchKeys(data []byte, keys ...string) int {
214
214
i := 0
215
215
ln := len (data )
216
216
lk := len (keys )
217
+ lastMatched := true
217
218
218
219
if lk == 0 {
219
220
return 0
@@ -241,8 +242,8 @@ func searchKeys(data []byte, keys ...string) int {
241
242
242
243
i += valueOffset
243
244
244
- // if string is a key, and key level match
245
- if data [i ] == ':' && keyLevel == level - 1 {
245
+ // if string is a key
246
+ if data [i ] == ':' {
246
247
if level < 1 {
247
248
return - 1
248
249
}
@@ -261,17 +262,32 @@ func searchKeys(data []byte, keys ...string) int {
261
262
}
262
263
263
264
if equalStr (& keyUnesc , keys [level - 1 ]) {
264
- keyLevel ++
265
- // If we found all keys in path
266
- if keyLevel == lk {
267
- return i + 1
265
+ lastMatched = true
266
+
267
+ // if key level match
268
+ if keyLevel == level - 1 {
269
+ keyLevel ++
270
+ // If we found all keys in path
271
+ if keyLevel == lk {
272
+ return i + 1
273
+ }
268
274
}
275
+ } else {
276
+ lastMatched = false
269
277
}
270
278
} else {
271
279
i --
272
280
}
273
281
case '{' :
274
- level ++
282
+
283
+ // in case parent key is matched then only we will increase the level otherwise can directly
284
+ // can move to the end of this block
285
+ if ! lastMatched {
286
+ end := blockEnd (data [i :], '{' , '}' )
287
+ i += end - 1
288
+ } else {
289
+ level ++
290
+ }
275
291
case '}' :
276
292
level --
277
293
if level == keyLevel {
Original file line number Diff line number Diff line change @@ -644,6 +644,12 @@ var getTests = []GetTest{
644
644
isFound : true ,
645
645
data : `4` ,
646
646
},
647
+ { // Issue #148
648
+ desc : `missing key in different key same level` ,
649
+ json : `{"s":"s","ic":2,"r":{"o":"invalid"}}` ,
650
+ path : []string {"ic" , "o" },
651
+ isFound : false ,
652
+ },
647
653
648
654
// Error/invalid tests
649
655
{
You can’t perform that action at this time.
0 commit comments