Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a69e67cfd | ||
|
|
3c91814cf6 |
7
gjson.go
7
gjson.go
@@ -1790,7 +1790,6 @@ next_key:
|
|||||||
usedPaths++
|
usedPaths++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to match the key to the path
|
// try to match the key to the path
|
||||||
// this is spaghetti code but the idea is to minimize
|
// this is spaghetti code but the idea is to minimize
|
||||||
// calls and variable assignments when comparing the
|
// 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]) <= len(key) || kplen != 0 {
|
||||||
|
if len(paths[j]) != i {
|
||||||
|
goto nomatch
|
||||||
|
}
|
||||||
// matched and at the end of the path
|
// matched and at the end of the path
|
||||||
goto match_atend
|
goto match_atend
|
||||||
}
|
}
|
||||||
@@ -1848,6 +1850,9 @@ next_key:
|
|||||||
nomatch: // noop label
|
nomatch: // noop label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !hasMatch && i < len(json) && json[i] == '}' {
|
||||||
|
return i + 1, true
|
||||||
|
}
|
||||||
if !parsedVal {
|
if !parsedVal {
|
||||||
if hasMatch {
|
if hasMatch {
|
||||||
// we found a match and the value has not been parsed yet.
|
// we found a match and the value has not been parsed yet.
|
||||||
|
|||||||
@@ -1071,3 +1071,33 @@ func TestValidRandom(t *testing.T) {
|
|||||||
validpayload(b[:n], 0)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user