diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-10-06 13:34:26 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-10-06 13:34:26 +0200 |
| commit | d4565cd6f26529f465da84a6fe5afc5557961f39 (patch) | |
| tree | aebeda9fa9ab4be99aaafd5b23a94a9d0e7a76a0 | |
| parent | 27d300df663ce8c1adb1aa3a54511f331bc3ae14 (diff) | |
| download | go-gg-d4565cd6f26529f465da84a6fe5afc5557961f39.tar.gz | |
pathrouter: add CatchAllVariableIf, VariableIf
| -rw-r--r-- | CHANGELOG.md | 6 | ||||
| -rw-r--r-- | pathrouter/catchallvariable.go | 18 | ||||
| -rw-r--r-- | pathrouter/pathrouter_test.go | 33 | ||||
| -rw-r--r-- | pathrouter/variable.go | 16 |
4 files changed, 65 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a7fb566..a340601 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> SPDX-License-Identifier: Apache-2.0 --> +# v1.19.0 (TBD) + +Changes: + +- pathrouter: Add CatchAllVariableIf() and VariableIf(). + # v1.18.0 (2026-10-05) Changes: diff --git a/pathrouter/catchallvariable.go b/pathrouter/catchallvariable.go index 29947ab..9c316b2 100644 --- a/pathrouter/catchallvariable.go +++ b/pathrouter/catchallvariable.go @@ -19,12 +19,18 @@ import ( // CatchAllVariable() may appear at any point within the routing tree, // but it may not contain another CatchAllVariable() anywhere within it. func CatchAllVariable(name string, matcher Matcher) Matcher { - return catchAllVariable(name, matcher.downcast()) + return catchAllVariable(name, nil, matcher.downcast()) } -func catchAllVariable(name string, matcher realMatcher) Matcher { +// CatchAllVariableIf is like [CatchAllVariable], but only matches if the collected path elements are accepted by the given predicate. +// The predicate will receive the unescaped form of the collected path elements, matching the return value of later [VariableValue] calls. +func CatchAllVariableIf(name string, predicate func(string) bool, matcher Matcher) Matcher { + return catchAllVariable(name, predicate, matcher.downcast()) +} + +func catchAllVariable(name string, predicate func(string) bool, matcher realMatcher) Matcher { // NOTE: The specific behavior of CatchAllVariable() is why this package exists in the first place. - // I wanted to replace gorilla/mux with something more performance in Keppel, + // I wanted to replace gorilla/mux with something more performant in Keppel, // but all the fast routers do not accept catch-all variables in the middle of a path // like the OCI Distribution API requires (e.g. "/v2/*repo/manifests/:reference" with "repo" being a full path). @@ -43,12 +49,16 @@ func catchAllVariable(name string, matcher realMatcher) Matcher { if len(caughtPath) == 0 { continue } + variableValue := pathUnescape(strings.Join(caughtPath, "/")) + if predicate != nil && !predicate(variableValue) { + continue + } handlerFunc := matcher.accept(subpath, rc) if handlerFunc == nil { continue } - rc.vars[name] = pathUnescape(strings.Join(caughtPath, "/")) + rc.vars[name] = variableValue return handlerFunc } return nil diff --git a/pathrouter/pathrouter_test.go b/pathrouter/pathrouter_test.go index 08a7c69..3fc8fa7 100644 --- a/pathrouter/pathrouter_test.go +++ b/pathrouter/pathrouter_test.go @@ -9,6 +9,7 @@ import ( "net/http" "net/http/httptest" "regexp" + "strconv" "strings" "testing" @@ -140,6 +141,23 @@ func TestRouting(t *testing.T) { check("GET", "/long/path/but/no/match", "404: 404 page not found", nil) check("GET", "/shortpathbutnomatch", "404: 404 page not found", nil) + // chekc CatchAllVariableIf() + startsWithFooBar := func(path string) bool { return strings.HasPrefix(path, "foo/bar") } + m = pr.CatchAllVariableIf("path", startsWithFooBar, pr.Choice( + pr.Element("one", pr.Handlers(pr.ByMethod{ + http.MethodGet: h(http.StatusOK, "one $path"), + })), + pr.Element("two", pr.Element("two", pr.Handlers(pr.ByMethod{ + http.MethodGet: h(http.StatusOK, "two $path"), + }))), + )) + + check("GET", "/foo/bar/baz/one", "200: one foo/bar/baz", nil) + check("GET", "/foo/bar/baz/two/two", "200: two foo/bar/baz", nil) + + check("GET", "/foo/baz/one", "404: 404 page not found", nil) + check("GET", "/foo/baz/two/two", "404: 404 page not found", nil) + // check Variable() m = pr.Element("nice", pr.Element("objects", pr.Variable("id", pr.Here(pr.Handlers(pr.ByMethod{ http.MethodPut: h(http.StatusCreated, "created object $id"), @@ -148,9 +166,22 @@ func TestRouting(t *testing.T) { check("PUT", "/nice/objects", "404: 404 page not found", nil) check("PUT", "/nice/objects/", "404: 404 page not found", nil) check("PUT", "/nice/objects/42", "201: created object 42", nil) - check("PUT", "/nice/objects/4/2", "404: 404 page not found", nil) // variable can onlt catch one element + check("PUT", "/nice/objects/4/2", "404: 404 page not found", nil) // variable can only catch one element check("PUT", "/nice/objects/4%2F2", "201: created object 4/2", nil) check("PUT", "/nice/objects/4%2F2/", "201: created object 4/2", nil) + + // check VariableIf() + isUint32 := func(input string) bool { + _, err := strconv.ParseUint(input, 10, 32) + return err == nil + } + m = pr.Element("objects", pr.VariableIf("id", isUint32, pr.Here(pr.Handlers(pr.ByMethod{ + http.MethodPut: h(http.StatusCreated, "created object $id"), + })))) + + check("PUT", "/objects/42", "201: created object 42", nil) + check("PUT", "/objects/10000000000", "404: 404 page not found", nil) + check("PUT", "/objects/fortytwo", "404: 404 page not found", nil) } func TestPanics(t *testing.T) { diff --git a/pathrouter/variable.go b/pathrouter/variable.go index 78bf394..386fe5e 100644 --- a/pathrouter/variable.go +++ b/pathrouter/variable.go @@ -14,10 +14,16 @@ import ( // (to be retrieved inside the request handler using [VariableValue]), // and the remaining subpath will have to be accepted by the next matcher. func Variable(name string, matcher Matcher) Matcher { - return variable(name, matcher.downcast()) + return variable(name, nil, matcher.downcast()) } -func variable(name string, matcher realMatcher) Matcher { +// VariableIf is like [Variable], but only matches if the path element is accepted by the given predicate. +// The predicate will receive the unescaped form of the path element, matching the return value of later [VariableValue] calls. +func VariableIf(name string, predicate func(string) bool, matcher Matcher) Matcher { + return variable(name, predicate, matcher.downcast()) +} + +func variable(name string, predicate func(string) bool, matcher realMatcher) Matcher { return realMatcher{ minLength: matcher.minLength + 1, maxLength: options.Map(matcher.maxLength, increment), @@ -25,11 +31,15 @@ func variable(name string, matcher realMatcher) Matcher { if len(path) == 0 || path[0] == "" { return nil } + variableValue := pathUnescape(path[0]) + if predicate != nil && !predicate(variableValue) { + return nil + } handlerFunc := matcher.accept(path[1:], rc) if handlerFunc == nil { return nil } - rc.vars[name] = pathUnescape(path[0]) + rc.vars[name] = variableValue return handlerFunc }, } |
