Skip to content

Commit

Permalink
add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Dec 26, 2024
1 parent 99eab04 commit e20fba3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/web/router_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (g *RouterPathGroup) ServeHTTP(resp http.ResponseWriter, req *http.Request)
g.r.chiRouter.NotFoundHandler().ServeHTTP(resp, req)
}

// MatchPath matches the request method, and uses regexp to match the path.
// The pattern uses "<...>" to define path parameters, for example: "/<name>" (different from chi router)
func (g *RouterPathGroup) MatchPath(methods, pattern string, h ...any) {
g.processors = append(g.processors, newRouterPathMatcher(methods, pattern, h...))
}
Expand Down
4 changes: 4 additions & 0 deletions modules/web/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func TestPathProcessor(t *testing.T) {
assert.True(t, p.matchPath(chiCtx, uri), "use pattern %s to process uri %s", pattern, uri)
assert.Equal(t, expectedPathParams, chiURLParamsToMap(chiCtx), "use pattern %s to process uri %s", pattern, uri)
}

// the "<...>" is intentionally designed to distinguish from chi's path parameters, because:
// 1. their behaviors are totally different, we do not to mislead developers
// 2. we can write regexp in "<name:\w{3,4}>" easily and parse it easily
testProcess("/<p1>/<p2>", "/a/b", map[string]string{"p1": "a", "p2": "b"})
testProcess("/<p1:*>", "", map[string]string{"p1": ""}) // this is a special case, because chi router could use empty path
testProcess("/<p1:*>", "/", map[string]string{"p1": ""})
Expand Down

0 comments on commit e20fba3

Please sign in to comment.