Skip to content

Commit 94ddf53

Browse files
authored
Merge pull request #215 from rrgilchrist/fix-repeat-paths
Fixed repeated paths from incorrectly incrementing data offset.
2 parents 49146d0 + 3980c9c commit 94ddf53

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

parser.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,10 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
437437

438438
match = pi
439439

440-
i++
441440
pathsMatched++
442441
pathFlags[pi] = true
443442

444-
v, dt, _, e := Get(data[i:])
443+
v, dt, _, e := Get(data[i+1:])
445444
cb(pi, v, dt, e)
446445

447446
if pathsMatched == len(paths) {

parser_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,9 +1420,9 @@ func TestArrayEachWithWhiteSpace(t *testing.T) {
14201420
keys []string
14211421
}
14221422
tests := []struct {
1423-
name string
1424-
args args
1425-
wantErr bool
1423+
name string
1424+
args args
1425+
wantErr bool
14261426
}{
14271427
{"Array with white space", args{[]byte(` ["AAA", "BBB", "CCC"]`), funcSuccess, []string{}}, false},
14281428
{"Array with only one character after white space", args{[]byte(` 1`), funcError, []string{}}, true},
@@ -1675,8 +1675,9 @@ func TestEachKey(t *testing.T) {
16751675
{"arrInt", "[3]"},
16761676
{"arrInt", "[5]"}, // Should not find last key
16771677
{"nested"},
1678-
{"arr", "["}, // issue#177 Invalid arguments
1679-
{"a\n", "b\n"}, // issue#165
1678+
{"arr", "["}, // issue#177 Invalid arguments
1679+
{"a\n", "b\n"}, // issue#165
1680+
{"nested", "b"}, // Should find repeated key
16801681
}
16811682

16821683
keysFound := 0
@@ -1729,13 +1730,17 @@ func TestEachKey(t *testing.T) {
17291730
if string(value) != "99" {
17301731
t.Error("Should find 10 key", string(value))
17311732
}
1733+
case 12:
1734+
if string(value) != "2" {
1735+
t.Errorf("Should find 11 key")
1736+
}
17321737
default:
17331738
t.Errorf("Should find only 10 keys, got %v key", idx)
17341739
}
17351740
}, paths...)
17361741

1737-
if keysFound != 10 {
1738-
t.Errorf("Should find 10 keys: %d", keysFound)
1742+
if keysFound != 11 {
1743+
t.Errorf("Should find 11 keys: %d", keysFound)
17391744
}
17401745
}
17411746

0 commit comments

Comments
 (0)