From cced0fa71920fb537b36a139dacd950a0f8f41f4 Mon Sep 17 00:00:00 2001 From: thirstycoda Date: Sun, 28 Oct 2018 00:20:01 +0100 Subject: [PATCH] Added not like operator support to query --- README.md | 3 ++- gjson.go | 4 +++- gjson_test.go | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 70240d9..a7484c6 100644 --- a/README.md +++ b/README.md @@ -88,13 +88,14 @@ The dot and wildcard characters can be escaped with '\\'. ``` You can also query an array for the first match by using `#[...]`, or find all matches with `#[...]#`. -Queries support the `==`, `!=`, `<`, `<=`, `>`, `>=` comparison operators and the simple pattern matching `%` operator. +Queries support the `==`, `!=`, `<`, `<=`, `>`, `>=` comparison operators and the simple pattern matching `%` (like) and `!%` (not like) operators. ``` friends.#[last=="Murphy"].first >> "Dale" friends.#[last=="Murphy"]#.first >> ["Dale","Jane"] friends.#[age>45]#.last >> ["Craig","Murphy"] friends.#[first%"D*"].last >> "Murphy" +friends.#[first!%"D*"].last >> "Craig" ``` ## JSON Lines diff --git a/gjson.go b/gjson.go index 74515ed..2c2e5d8 100644 --- a/gjson.go +++ b/gjson.go @@ -755,7 +755,7 @@ func parseArrayPath(path string) (r arrayPathResult) { if i < len(path) { s = i if path[i] == '!' { - if i < len(path)-1 && path[i+1] == '=' { + if i < len(path)-1 && (path[i+1] == '=' || path[i+1] == '%') { i++ } } else if path[i] == '<' || path[i] == '>' { @@ -1099,6 +1099,8 @@ func queryMatches(rp *arrayPathResult, value Result) bool { return value.Str >= rpv case "%": return match.Match(value.Str, rpv) + case "!%": + return !match.Match(value.Str, rpv) } case Number: rpvn, _ := strconv.ParseFloat(rpv, 64) diff --git a/gjson_test.go b/gjson_test.go index 6333be7..c994cef 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -403,6 +403,10 @@ func TestBasic2(t *testing.T) { if mtok.String() != "aaaa" { t.Fatalf("expected %v, got %v", "aaaa", mtok.String()) } + mtok = get(basicJSON, `loggy.programmers.#[firstName !% "Bre*"].email`) + if mtok.String() != "bbbb" { + t.Fatalf("expected %v, got %v", "bbbb", mtok.String()) + } mtok = get(basicJSON, `loggy.programmers.#[firstName == "Brett"].email`) if mtok.String() != "aaaa" { t.Fatalf("expected %v, got %v", "aaaa", mtok.String())