From 3c91814cf699218e6e82820cfa554489ca987e91 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 25 Sep 2017 04:37:57 -0700 Subject: [PATCH] GetMany result incorrect, fixes #47 --- gjson.go | 4 +++- gjson_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gjson.go b/gjson.go index f9fe002..2679256 100644 --- a/gjson.go +++ b/gjson.go @@ -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 } diff --git a/gjson_test.go b/gjson_test.go index 57f0425..dfd0362 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -1071,3 +1071,18 @@ 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) + } + } +}