From 855ba5362604679f95d1ebff939f8078459486b5 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 6 Oct 2026 13:45:52 +0200 Subject: pathrouter: add Elements --- CHANGELOG.md | 2 +- pathrouter/element.go | 26 ++++++++++++++++++++++++++ pathrouter/pathrouter_test.go | 34 ++++++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a340601..587a645 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0 Changes: -- pathrouter: Add CatchAllVariableIf() and VariableIf(). +- pathrouter: Add CatchAllVariableIf(), VariableIf() and Elements(). # v1.18.0 (2026-10-05) diff --git a/pathrouter/element.go b/pathrouter/element.go index 6426c04..ef56468 100644 --- a/pathrouter/element.go +++ b/pathrouter/element.go @@ -5,6 +5,8 @@ package pathrouter import ( "net/http" + "slices" + "strings" "go.xyrillian.de/gg/options" ) @@ -12,6 +14,12 @@ import ( // Element is a [Matcher] that accepts subpaths with at least one path element. // The first path element must be equal to the given value, // and the remaining subpath will have to be accepted by the next matcher. +// +// To match a trailing slash in the request path, Element can be used with "/" as its argument: +// +// pathrouter.Element("/", pathrouter.Handlers(...)) +// +// But usually, when matching request paths with trailing slashes is desired, [Here] should be used instead. func Element(value string, matcher Matcher) Matcher { return element(value, matcher.downcast()) } @@ -36,3 +44,21 @@ func element(value string, matcher realMatcher) Matcher { }, } } + +// Elements is a shorthand for a chain of [Element] calls. For example, +// +// pathrouter.Element("v1", pathrouter.Element("admin", pathrouter.Element("warnings", innerMatcher))) +// +// can be shortened to +// +// pathrouter.Elements("v1/admin/warnings", innerMatcher))) +// +// Slashes in the provided value will be treated as delimiting separate path elements, so escaped slashes will not count. +// For instance, the example above might match a request path of "/v1/admin/warnings/253", but not "/v1/admin%2Fwarnings/253". +func Elements(value string, matcher Matcher) Matcher { + values := strings.Split(value, "/") + for _, value := range slices.Backward(values) { + matcher = Element(value, matcher) + } + return matcher +} diff --git a/pathrouter/pathrouter_test.go b/pathrouter/pathrouter_test.go index 3fc8fa7..8c352d1 100644 --- a/pathrouter/pathrouter_test.go +++ b/pathrouter/pathrouter_test.go @@ -96,6 +96,24 @@ func TestRouting(t *testing.T) { check("GET", "/foo/bar", "200: that's me", nil) check("PUT", "/foo/bar", "201: here I am", nil) check("DELETE", "/foo/bar", "202: goodbye", nil) + check("GET", "/foo%2Fbar", "404: 404 page not found", nil) + + // check Elements() + m = pr.Elements("foo/bar", pr.Handlers(pr.ByMethod{ + http.MethodGet: h(http.StatusOK, "that's me"), + http.MethodPut: h(http.StatusCreated, "here I am"), + http.MethodDelete: h(http.StatusAccepted, "goodbye"), + })) + check("GET", "/", "404: 404 page not found", nil) + check("GET", "/foo/", "404: 404 page not found", nil) + check("GET", "/foo", "404: 404 page not found", nil) + check("GET", "/bar/", "404: 404 page not found", nil) + check("GET", "/bar", "404: 404 page not found", nil) + check("GET", "/foo/bar/", "404: 404 page not found", nil) + check("GET", "/foo/bar", "200: that's me", nil) + check("PUT", "/foo/bar", "201: here I am", nil) + check("DELETE", "/foo/bar", "202: goodbye", nil) + check("GET", "/foo%2Fbar", "404: 404 page not found", nil) // check Choice(), different behavior for MethodHead in Handlers() m = pr.Choice( @@ -123,12 +141,12 @@ func TestRouting(t *testing.T) { pr.Element("one", pr.Handlers(pr.ByMethod{ http.MethodGet: h(http.StatusOK, "one $path"), })), - pr.Element("two", pr.Element("two", pr.Handlers(pr.ByMethod{ + pr.Elements("two/two", pr.Handlers(pr.ByMethod{ http.MethodGet: h(http.StatusOK, "two $path"), - }))), - pr.Element("three", pr.Element("three", pr.Element("three", pr.Handlers(pr.ByMethod{ + })), + pr.Elements("three/three/three", pr.Handlers(pr.ByMethod{ http.MethodGet: h(http.StatusOK, "three $path"), - })))), + })), )) check("GET", "/foo/bar/one", "200: one foo/bar", nil) @@ -147,9 +165,9 @@ func TestRouting(t *testing.T) { pr.Element("one", pr.Handlers(pr.ByMethod{ http.MethodGet: h(http.StatusOK, "one $path"), })), - pr.Element("two", pr.Element("two", pr.Handlers(pr.ByMethod{ + pr.Elements("two/two", pr.Handlers(pr.ByMethod{ http.MethodGet: h(http.StatusOK, "two $path"), - }))), + })), )) check("GET", "/foo/bar/baz/one", "200: one foo/bar/baz", nil) @@ -159,9 +177,9 @@ func TestRouting(t *testing.T) { 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{ + m = pr.Elements("nice/objects", pr.Variable("id", pr.Here(pr.Handlers(pr.ByMethod{ http.MethodPut: h(http.StatusCreated, "created object $id"), - }))))) + })))) check("PUT", "/nice/objects", "404: 404 page not found", nil) check("PUT", "/nice/objects/", "404: 404 page not found", nil) -- cgit v1.3.1