From e8d1a9ab933a07aef3fa4a68f5b5218ec81b1c94 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Wed, 29 Mar 2017 08:19:50 -0700 Subject: [PATCH] end of path regression, fixes #21 thanks @Poorva17 --- gjson.go | 7 +++---- gjson_test.go | 34 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/gjson.go b/gjson.go index a6853e2..d1735f4 100644 --- a/gjson.go +++ b/gjson.go @@ -1761,11 +1761,10 @@ next_key: goto match_not_atend } } - if len(paths[j]) > len(key) { - goto nomatch + if len(paths[j]) <= len(key) || kplen != 0 { + // matched and at the end of the path + goto match_atend } - // matched and at the end of the path - goto match_atend } // no match, jump to the nomatch label goto nomatch diff --git a/gjson_test.go b/gjson_test.go index f8fa2d3..bf37305 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -576,31 +576,43 @@ func TestManyBasic(t *testing.T) { testMany(true, `[world]`, strings.Repeat("a.", 70)+"hello") } -const sampleJSON = `{ "name": "FirstName", "name1": "FirstName1", "address": "address1", "addressDetails": "address2", }` - -func TestIssue20(t *testing.T) { - expectedValues := []string{"FirstName", "FirstName1", "address1", "address2"} - paths := []string{"name", "name1", "address", "addressDetails"} +func testMany(t *testing.T, json string, paths, expected []string) { var result []Result for i := 0; i < 2; i++ { var which string if i == 0 { which = "Get" result = nil - for j := 0; j < len(expectedValues); j++ { - result = append(result, Get(sampleJSON, paths[j])) + for j := 0; j < len(expected); j++ { + result = append(result, Get(json, paths[j])) } } else if i == 1 { which = "GetMany" - result = GetMany(sampleJSON, paths...) + result = GetMany(json, paths...) } - for j := 0; j < len(expectedValues); j++ { - if result[j].String() != expectedValues[j] { - t.Fatalf("expected '%v', got '%v' for '%s'", expectedValues[j], result[j].String(), which) + for j := 0; j < len(expected); j++ { + if result[j].String() != expected[j] { + 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) { var lstr string