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

Array strings #180

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,13 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
pathFlags[pi] = true

if of != -1 {
v, dt, _, e := Get(value[of:])
cb(pi, v, dt, e)
if dataType == String {
// the double-quotes were stripped, so we cannot call Get again.
cb(pi, value[of:], dataType, nil)
} else {
v, dt, _, e := Get(value[of:])
cb(pi, v, dt, e)
}
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,8 @@ var testJson = []byte(`{
"intPtr": 10,
"a\n":{
"b\n":99
}
},
"arrString": ["a","b","c"]}
}`)

func TestEachKey(t *testing.T) {
Expand All @@ -1721,6 +1722,7 @@ func TestEachKey(t *testing.T) {
{"arr", "["}, // issue#177 Invalid arguments
{"a\n", "b\n"}, // issue#165
{"nested", "b"}, // Should find repeated key
{"arrString", "[1]"},
}

keysFound := 0
Expand All @@ -1731,35 +1733,35 @@ func TestEachKey(t *testing.T) {
switch idx {
case 0:
if string(value) != "Name" {
t.Error("Should find 1 key", string(value))
t.Error("Should find 0 key", string(value))
}
case 1:
if string(value) != "Order" {
t.Errorf("Should find 2 key")
t.Errorf("Should find 1 key")
}
case 2:
if string(value) != "test" {
t.Errorf("Should find 3 key")
t.Errorf("Should find 2 key")
}
case 3:
if string(value) != "2" {
t.Errorf("Should find 4 key")
t.Errorf("Should find 3 key")
}
case 4:
if string(value) != "test2" {
t.Error("Should find 5 key", string(value))
t.Error("Should find 4 key", string(value))
}
case 5:
if string(value) != "4" {
t.Errorf("Should find 6 key")
t.Errorf("Should find 5 key")
}
case 6:
if string(value) != "2" {
t.Errorf("Should find 7 key")
t.Errorf("Should find 6 key")
}
case 7:
if string(value) != "4" {
t.Error("Should find 8 key", string(value))
t.Error("Should find 7 key", string(value))
}
case 8:
t.Errorf("Found key #8 that should not be found")
Expand All @@ -1771,19 +1773,23 @@ func TestEachKey(t *testing.T) {
t.Errorf("Found key #10 that should not be found")
case 11:
if string(value) != "99" {
t.Error("Should find 10 key", string(value))
t.Error("Should find 11 key", string(value))
}
case 12:
if string(value) != "2" {
t.Error("Should find 12 key", string(value))
}
case 13:
if string(value) != "b" {
t.Errorf("Should find 11 key")
}
default:
t.Errorf("Should find only 10 keys, got %v key", idx)
t.Errorf("Only %v keys specified, got key for non-existent index %v", len(paths), idx)
}
}, paths...)

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

Expand Down