Skip to content

Commit 52c6e14

Browse files
soniabhishekbuger
authored andcommitted
Fixing the issue of wrong get value (#149)
* Fixing the issue of wrong get value * adding test for failing case
1 parent 6acdf74 commit 52c6e14

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

parser.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func searchKeys(data []byte, keys ...string) int {
214214
i := 0
215215
ln := len(data)
216216
lk := len(keys)
217+
lastMatched := true
217218

218219
if lk == 0 {
219220
return 0
@@ -241,8 +242,8 @@ func searchKeys(data []byte, keys ...string) int {
241242

242243
i += valueOffset
243244

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] == ':' {
246247
if level < 1 {
247248
return -1
248249
}
@@ -261,17 +262,32 @@ func searchKeys(data []byte, keys ...string) int {
261262
}
262263

263264
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+
}
268274
}
275+
} else {
276+
lastMatched = false
269277
}
270278
} else {
271279
i--
272280
}
273281
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+
}
275291
case '}':
276292
level--
277293
if level == keyLevel {

parser_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,12 @@ var getTests = []GetTest{
644644
isFound: true,
645645
data: `4`,
646646
},
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+
},
647653

648654
// Error/invalid tests
649655
{

0 commit comments

Comments
 (0)