Skip to content

Commit

Permalink
Merge pull request #5 from ipld/feat/matchpath
Browse files Browse the repository at this point in the history
Option to match path traversal
  • Loading branch information
ribasushi authored Nov 29, 2021
2 parents 4c190a2 + b52f237 commit ce1872a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit ce1872a

Please # to comment.