end of path regression, fixes #21

thanks @Poorva17
This commit is contained in:
Josh Baker 2017-03-29 08:19:50 -07:00
parent 9944282cf6
commit e8d1a9ab93
2 changed files with 26 additions and 15 deletions

View File

@ -1761,12 +1761,11 @@ next_key:
goto match_not_atend goto match_not_atend
} }
} }
if len(paths[j]) > len(key) { if len(paths[j]) <= len(key) || kplen != 0 {
goto nomatch
}
// matched and at the end of the path // matched and at the end of the path
goto match_atend goto match_atend
} }
}
// no match, jump to the nomatch label // no match, jump to the nomatch label
goto nomatch goto nomatch
match_atend: match_atend:

View File

@ -576,31 +576,43 @@ func TestManyBasic(t *testing.T) {
testMany(true, `[world]`, strings.Repeat("a.", 70)+"hello") testMany(true, `[world]`, strings.Repeat("a.", 70)+"hello")
} }
const sampleJSON = `{ "name": "FirstName", "name1": "FirstName1", "address": "address1", "addressDetails": "address2", }` func testMany(t *testing.T, json string, paths, expected []string) {
func TestIssue20(t *testing.T) {
expectedValues := []string{"FirstName", "FirstName1", "address1", "address2"}
paths := []string{"name", "name1", "address", "addressDetails"}
var result []Result var result []Result
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
var which string var which string
if i == 0 { if i == 0 {
which = "Get" which = "Get"
result = nil result = nil
for j := 0; j < len(expectedValues); j++ { for j := 0; j < len(expected); j++ {
result = append(result, Get(sampleJSON, paths[j])) result = append(result, Get(json, paths[j]))
} }
} else if i == 1 { } else if i == 1 {
which = "GetMany" which = "GetMany"
result = GetMany(sampleJSON, paths...) result = GetMany(json, paths...)
} }
for j := 0; j < len(expectedValues); j++ { for j := 0; j < len(expected); j++ {
if result[j].String() != expectedValues[j] { if result[j].String() != expected[j] {
t.Fatalf("expected '%v', got '%v' for '%s'", expectedValues[j], result[j].String(), which) t.Fatalf("Using key '%s' for '%s'\nexpected '%v', got '%v'", paths[j], which, expected[j], result[j].String())
} }
} }
} }
} }
func TestIssue20(t *testing.T) {
json := `{ "name": "FirstName", "name1": "FirstName1", "address": "address1", "addressDetails": "address2", }`
paths := []string{"name", "name1", "address", "addressDetails"}
expected := []string{"FirstName", "FirstName1", "address1", "address2"}
t.Run("SingleMany", func(t *testing.T) { testMany(t, json, paths, expected) })
}
func TestIssue21(t *testing.T) {
json := `{ "Level1Field1":3,
"Level1Field4":4,
"Level1Field2":{ "Level2Field1":[ "value1", "value2" ],
"Level2Field2":{ "Level3Field1":[ { "key1":"value1" } ] } } }`
paths := []string{"Level1Field1", "Level1Field2.Level2Field1", "Level1Field2.Level2Field2.Level3Field1", "Level1Field4"}
expected := []string{"3", `[ "value1", "value2" ]`, `[ { "key1":"value1" } ]`, "4"}
t.Run("SingleMany", func(t *testing.T) { testMany(t, json, paths, expected) })
}
func TestRandomMany(t *testing.T) { func TestRandomMany(t *testing.T) {
var lstr string var lstr string