Skip to content

Commit 3f18356

Browse files
committed
Fix KeyEach when paths have same length and suffix
1 parent 07d4e00 commit 3f18356

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

parser.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func init() {
187187
}
188188

189189
func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int {
190-
var pathFlags int64
190+
var pathFlags, ignorePathFlags int64
191191
var level, pathsMatched, i int
192192
ln := len(data)
193193

@@ -214,7 +214,6 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
214214

215215
i += valueOffset
216216

217-
218217
// if string is a key, and key level match
219218
if data[i] == ':' {
220219
match := false
@@ -232,7 +231,7 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
232231
}
233232

234233
for pi, p := range paths {
235-
if len(p) < level || (pathFlags & bitwiseFlags[pi]) != 0 {
234+
if len(p) < level || (pathFlags & bitwiseFlags[pi]) != 0 || (ignorePathFlags & bitwiseFlags[pi] != 0) {
236235
continue
237236
}
238237

@@ -255,10 +254,13 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
255254
return i
256255
}
257256
}
257+
} else {
258+
ignorePathFlags |= bitwiseFlags[pi]
258259
}
259260
}
260261

261262
if !match {
263+
ignorePathFlags = 0
262264
tokenOffset := nextToken(data[i+1:])
263265
i += tokenOffset
264266

@@ -267,6 +269,11 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
267269
i += blockSkip + 1
268270
}
269271
}
272+
273+
switch data[i] {
274+
case '{', '}', '[', '"':
275+
i--
276+
}
270277
} else {
271278
i--
272279
}

parser_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ func TestEachKey(t *testing.T) {
637637
paths := [][]string{
638638
[]string{"name"},
639639
[]string{"nested", "a"},
640+
[]string{"nested2", "a"},
640641
[]string{"nested", "nested3", "b"},
641642
}
642643

@@ -655,16 +656,20 @@ func TestEachKey(t *testing.T) {
655656
t.Errorf("Should find 2 key")
656657
}
657658
case 2:
659+
if string(value) != "test2" {
660+
t.Error("Should find 3 key", string(value))
661+
}
662+
case 3:
658663
if string(value) != "4" {
659-
t.Errorf("Should find 3 key")
664+
t.Errorf("Should find 4 key")
660665
}
661666
default:
662-
t.Errorf("Should found only 3 keys")
667+
t.Errorf("Should found only 4 keys")
663668
}
664669
}, paths...)
665670

666-
if keysFound != 3 {
667-
t.Errorf("Should find 3 keys: %d", keysFound)
671+
if keysFound != 4 {
672+
t.Errorf("Should find 4 keys: %d", keysFound)
668673
}
669674
}
670675

0 commit comments

Comments
 (0)