Skip to content

Commit 3980c9c

Browse files
committed
Fixed repeated paths from incorrectly incrementing data offset.
1 parent cb835d4 commit 3980c9c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

parser.go

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

446446
match = pi
447447

448-
i++
449448
pathsMatched++
450449
pathFlags |= bitwiseFlags[pi+1]
451450

452-
v, dt, _, e := Get(data[i:])
451+
v, dt, _, e := Get(data[i+1:])
453452
cb(pi, v, dt, e)
454453

455454
if pathsMatched == len(paths) {
@@ -930,7 +929,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
930929
return -1, MalformedJsonError
931930
}
932931

933-
offset = nT+1
932+
offset = nT + 1
934933

935934
if len(keys) > 0 {
936935
if offset = searchKeys(data, keys...); offset == -1 {

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)