Skip to content

Commit 1f134a7

Browse files
committed
fixed the bug that searchKeys would return value != -1 when the keys located in different json entry
For example, this json object, when looking for keys [‘a’, ‘c’], searchKeys should return a negative value, use to return possible which could cause wrong data for Get API { "a": { "a": 1 }, "b": { "a": 3, "c": [1, 2] } }
1 parent f04e003 commit 1f134a7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

parser.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func searchKeys(data []byte, keys ...string) int {
166166
case '{':
167167
level++
168168
case '}':
169+
if keyLevel > 0 {
170+
keyLevel--
171+
}
169172
level--
170173
case '[':
171174
// Do not search for keys inside arrays

parser_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type GetTest struct {
4040

4141
var getTests = []GetTest{
4242
// Found key tests
43+
4344
GetTest{
4445
desc: "handling multiple nested keys with same name",
4546
json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`,
@@ -255,6 +256,12 @@ var getTests = []GetTest{
255256
path: []string{"key"},
256257
isFound: false,
257258
},
259+
GetTest{
260+
desc: "handling multiple keys with different name",
261+
json: `{"a":{"a":1},"b":{"a":3,"c":[1,2]}}`,
262+
path: []string{"a", "c"},
263+
isFound: false,
264+
},
258265

259266
// Error/invalid tests
260267
GetTest{

0 commit comments

Comments
 (0)