2 Commits

Author SHA1 Message Date
Josh Baker
5a69e67cfd GetMany result value missing, fixes #48 2017-09-25 04:39:06 -07:00
Josh Baker
3c91814cf6 GetMany result incorrect, fixes #47 2017-09-25 04:37:57 -07:00
2 changed files with 36 additions and 1 deletions

View File

@@ -1790,7 +1790,6 @@ next_key:
usedPaths++
continue
}
// try to match the key to the path
// this is spaghetti code but the idea is to minimize
// calls and variable assignments when comparing the
@@ -1810,6 +1809,9 @@ next_key:
}
}
if len(paths[j]) <= len(key) || kplen != 0 {
if len(paths[j]) != i {
goto nomatch
}
// matched and at the end of the path
goto match_atend
}
@@ -1848,6 +1850,9 @@ next_key:
nomatch: // noop label
}
if !hasMatch && i < len(json) && json[i] == '}' {
return i + 1, true
}
if !parsedVal {
if hasMatch {
// we found a match and the value has not been parsed yet.

View File

@@ -1071,3 +1071,33 @@ func TestValidRandom(t *testing.T) {
validpayload(b[:n], 0)
}
}
func TestGetMany47(t *testing.T) {
json := `{"bar": {"id": 99, "mybar": "my mybar" }, "foo": {"myfoo": [605]}}`
paths := []string{"foo.myfoo", "bar.id", "bar.mybar", "bar.mybarx"}
expected := []string{"[605]", "99", "my mybar", ""}
results := GetMany(json, paths...)
if len(expected) != len(results) {
t.Fatalf("expected %v, got %v", len(expected), len(results))
}
for i, path := range paths {
if results[i].String() != expected[i] {
t.Fatalf("expected '%v', got '%v' for path '%v'", expected[i], results[i].String(), path)
}
}
}
func TestGetMany48(t *testing.T) {
json := `{"bar": {"id": 99, "xyz": "my xyz"}, "foo": {"myfoo": [605]}}`
paths := []string{"foo.myfoo", "bar.id", "bar.xyz", "bar.abc"}
expected := []string{"[605]", "99", "my xyz", ""}
results := GetMany(json, paths...)
if len(expected) != len(results) {
t.Fatalf("expected %v, got %v", len(expected), len(results))
}
for i, path := range paths {
if results[i].String() != expected[i] {
t.Fatalf("expected '%v', got '%v' for path '%v'", expected[i], results[i].String(), path)
}
}
}