Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed repeated paths from incorrectly incrementing data offset. #215

Merged
merged 1 commit into from Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions parser.go
Expand Up @@ -445,11 +445,10 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str

match = pi

i++
pathsMatched++
pathFlags |= bitwiseFlags[pi+1]

v, dt, _, e := Get(data[i:])
v, dt, _, e := Get(data[i+1:])
cb(pi, v, dt, e)

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

offset = nT+1
offset = nT + 1

if len(keys) > 0 {
if offset = searchKeys(data, keys...); offset == -1 {
Expand Down
19 changes: 12 additions & 7 deletions parser_test.go
Expand Up @@ -1420,9 +1420,9 @@ func TestArrayEachWithWhiteSpace(t *testing.T) {
keys []string
}
tests := []struct {
name string
args args
wantErr bool
name string
args args
wantErr bool
}{
{"Array with white space", args{[]byte(` ["AAA", "BBB", "CCC"]`), funcSuccess, []string{}}, false},
{"Array with only one character after white space", args{[]byte(` 1`), funcError, []string{}}, true},
Expand Down Expand Up @@ -1675,8 +1675,9 @@ func TestEachKey(t *testing.T) {
{"arrInt", "[3]"},
{"arrInt", "[5]"}, // Should not find last key
{"nested"},
{"arr", "["}, // issue#177 Invalid arguments
{"a\n", "b\n"}, // issue#165
{"arr", "["}, // issue#177 Invalid arguments
{"a\n", "b\n"}, // issue#165
{"nested", "b"}, // Should find repeated key
}

keysFound := 0
Expand Down Expand Up @@ -1729,13 +1730,17 @@ func TestEachKey(t *testing.T) {
if string(value) != "99" {
t.Error("Should find 10 key", string(value))
}
case 12:
if string(value) != "2" {
t.Errorf("Should find 11 key")
}
default:
t.Errorf("Should find only 10 keys, got %v key", idx)
}
}, paths...)

if keysFound != 10 {
t.Errorf("Should find 10 keys: %d", keysFound)
if keysFound != 11 {
t.Errorf("Should find 11 keys: %d", keysFound)
}
}

Expand Down