From b52f2371d1d8b1ce8e0b54f25d4f2d0b2006c125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 29 Nov 2021 20:01:43 +0100 Subject: [PATCH] Option to match path traversal --- example_test.go | 2 +- parser.go | 6 +++++- parser_test.go | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/example_test.go b/example_test.go index cb6681f..1039978 100644 --- a/example_test.go +++ b/example_test.go @@ -35,7 +35,7 @@ func Example_selectorFromPath() { // // Selector spec from a path, hopefully within that rootCid // The 001 is deliberate: making sure index navigation still works - selectorSpec, err := textselector.SelectorSpecFromPath("Links/1/Hash/Links/001/Hash", nil) + selectorSpec, err := textselector.SelectorSpecFromPath("Links/1/Hash/Links/001/Hash", false, nil) if err != nil { log.Fatal(err) } diff --git a/parser.go b/parser.go index 94595b7..ae24e72 100644 --- a/parser.go +++ b/parser.go @@ -34,7 +34,7 @@ PathValidCharset. The parsing rules are: - Any other valid segment is treated as a key within a map, or (if applicable) as an index within an array */ -func SelectorSpecFromPath(path Expression, optionalSubselectorAtTarget builder.SelectorSpec) (builder.SelectorSpec, error) { +func SelectorSpecFromPath(path Expression, matchPath bool, optionalSubselectorAtTarget builder.SelectorSpec) (builder.SelectorSpec, error) { if path == "/" { return nil, fmt.Errorf("a standalone '/' is not a valid path") @@ -69,6 +69,10 @@ func SelectorSpecFromPath(path Expression, optionalSubselectorAtTarget builder.S ss = ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) { efsb.Insert(segments[i], ss) }) + + if matchPath { + ss = ssb.ExploreUnion(ssb.Matcher(), ss) + } } return ss, nil diff --git a/parser_test.go b/parser_test.go index 33c4a6a..59207f4 100644 --- a/parser_test.go +++ b/parser_test.go @@ -11,7 +11,7 @@ func TestBasic(t *testing.T) { { valid := textselector.Expression("/a/42/b/c/") - ss, err := textselector.SelectorSpecFromPath(valid, nil) + ss, err := textselector.SelectorSpecFromPath(valid, false, nil) if err != nil { t.Fatalf("Expected no error with valid path '%s'", valid) } @@ -27,7 +27,7 @@ func TestBasic(t *testing.T) { "x//", ";", } { - ss, err := textselector.SelectorSpecFromPath(invalid, nil) + ss, err := textselector.SelectorSpecFromPath(invalid, false, nil) if ss != nil { t.Fatalf("Expected nil selector with invalid path '%s'", invalid)