-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the `jp.PathMatch` function that compares a normalized JSONPath with a target JSONPath. - Added `jp.MatchHandler` a TokenHandler that can be used to build a path and data while processing a JSON document. - Added `oj.Match` and `sen.Match` functions.
- Loading branch information
Showing
11 changed files
with
633 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) 2024, Peter Ohler, All rights reserved. | ||
|
||
package jp | ||
|
||
// PathMatch returns true if the provided path would match the target | ||
// expression. The path argument is expected to be a normalized path with only | ||
// elements of Root ($), At (@), Child (string), or Nth (int). A Filter | ||
// fragment in the target expression will match any value in path since it | ||
// requires data from a JSON document to be evaluated. Slice fragments always | ||
// return true as long as the path element is an Nth. | ||
func PathMatch(target, path Expr) bool { | ||
if 0 < len(target) { | ||
switch target[0].(type) { | ||
case Root, At: | ||
target = target[1:] | ||
} | ||
} | ||
if 0 < len(path) { | ||
switch path[0].(type) { | ||
case Root, At: | ||
path = path[1:] | ||
} | ||
} | ||
for i, f := range target { | ||
if len(path) == 0 { | ||
return false | ||
} | ||
switch path[0].(type) { | ||
case Child, Nth: | ||
default: | ||
return false | ||
} | ||
switch tf := f.(type) { | ||
case Child, Nth: | ||
if tf != path[0] { | ||
return false | ||
} | ||
path = path[1:] | ||
case Bracket: | ||
// ignore and don't advance path | ||
case Wildcard: | ||
path = path[1:] | ||
case Union: | ||
var ok bool | ||
for _, u := range tf { | ||
check: | ||
switch tu := u.(type) { | ||
case string: | ||
if Child(tu) == path[0] { | ||
ok = true | ||
break check | ||
} | ||
case int64: | ||
if Nth(tu) == path[0] { | ||
ok = true | ||
break check | ||
} | ||
} | ||
} | ||
if !ok { | ||
return false | ||
} | ||
path = path[1:] | ||
case Slice: | ||
if _, ok := path[0].(Nth); !ok { | ||
return false | ||
} | ||
path = path[1:] | ||
case *Filter: | ||
// Assume a match since there is no data for comparison. | ||
path = path[1:] | ||
case Descent: | ||
rest := target[i+1:] | ||
for 0 < len(path) { | ||
if PathMatch(rest, path) { | ||
return true | ||
} | ||
path = path[1:] | ||
} | ||
return false | ||
default: | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// copyright (c) 2024, Peter Ohler, All rights reserved. | ||
|
||
package jp_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ohler55/ojg/jp" | ||
"github.com/ohler55/ojg/tt" | ||
) | ||
|
||
type matchData struct { | ||
target string | ||
path string | ||
expect bool | ||
} | ||
|
||
func TestPathMatchCheck(t *testing.T) { | ||
for i, md := range []*matchData{ | ||
{target: "$.a", path: "a", expect: true}, | ||
{target: "@.a", path: "a", expect: true}, | ||
{target: "a", path: "a", expect: true}, | ||
{target: "a", path: "$.a", expect: true}, | ||
{target: "a", path: "@.a", expect: true}, | ||
{target: "[1]", path: "[1]", expect: true}, | ||
{target: "[1]", path: "[0]", expect: false}, | ||
{target: "*", path: "[1]", expect: true}, | ||
{target: "[*]", path: "[1]", expect: true}, | ||
{target: "*", path: "a", expect: true}, | ||
{target: "[1,'a']", path: "a", expect: true}, | ||
{target: "[1,'a']", path: "[1]", expect: true}, | ||
{target: "[1,'a']", path: "b", expect: false}, | ||
{target: "[1,'a']", path: "[0]", expect: false}, | ||
{target: "$.x[1,'a']", path: "x[1]", expect: true}, | ||
{target: "..x", path: "a.b.x", expect: true}, | ||
{target: "..x", path: "a.b.c", expect: false}, | ||
{target: "x[1:5:2]", path: "x[2]", expect: true}, | ||
{target: "x[1:5:2]", path: "x.y", expect: false}, | ||
{target: "x[[email protected] == 2]", path: "x[2]", expect: true}, | ||
{target: "x.y.z", path: "x.y", expect: false}, | ||
} { | ||
tt.Equal(t, md.expect, jp.PathMatch(jp.MustParseString(md.target), jp.MustParseString(md.path)), | ||
"%d: %s %s", i, md.target, md.path) | ||
} | ||
} | ||
|
||
func TestPathMatchDoubleRoot(t *testing.T) { | ||
tt.Equal(t, false, jp.PathMatch(jp.R().R().C("a"), jp.C("a"))) | ||
tt.Equal(t, false, jp.PathMatch(jp.A().A().C("a"), jp.C("a"))) | ||
tt.Equal(t, false, jp.PathMatch(jp.C("a"), jp.R().R().C("a"))) | ||
tt.Equal(t, false, jp.PathMatch(jp.C("a"), jp.A().A().C("a"))) | ||
} | ||
|
||
func TestPathMatchSkipBracket(t *testing.T) { | ||
tt.Equal(t, true, jp.PathMatch(jp.B().C("a"), jp.C("a"))) | ||
} |
Oops, something went wrong.