Repository navigation
Go router groups: keep the group's prefix on its routes, and read Go 1.22 method patterns - #134
Open
ryoaonetsuki wants to merge 7 commits into
Open
ryoaonetsuki wants to merge 7 commits into
ryoaonetsuki wants to merge 7 commits into
Conversation
…1.22 method patterns ## Problem
…1.22 method patterns ## Problem
…1.22 method patterns ## Problem
…1.22 method patterns ## Problem
…1.22 method patterns ## Problem
|
Nice change. Trying it on a plain |
#1) A ServeMux pattern can name a host ("GET shop.example.com/api/orders", "example.com:8080/"); those endpoints were missed. A host counts only when it looks like a domain name and only on Handle/HandleFunc, so names like "devices/+/status" or a host-like string on another router stay out. A trailing {$} only marks an exact match, so "GET /{$}" is now GET /.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A Go server that groups its endpoints shows them without their prefix, so the map can't connect the website's requests to them:
api := r.Group("/api")in Gin, Echo, Fiber or chi, thenapi.GET("/orders", …)— the map showedGET /orders, notGET /api/ordersr.Route("/api", func(r chi.Router) { r.Get("/orders", …) })— same, the prefix was lostmux.HandleFunc("GET /orders/{id}", …)wasn't read at all (only plain patterns were)Change
extractGonow returns aroutersmap (like Python and C# already do), so the group's prefix is applied when the map is built: nested groups compose (/api+/v1→/api/v1), a group used inline (r.Group("/api").GET(…)) works, and routes chained on one statement (r.GET("/a", …).POST("/b", …)) are both read.Routesubrouter prefixes the routes inside its callback, including nestedRoutes,r.With(auth).Route(…), aRouteon a group variable, and a group made inside the callback."GET /orders/{id}","POST /orders") read their method; a plain pattern staysANY.A route beside a group, or on a router the callback doesn't hand over, keeps no prefix.
Limitations
r.Method("GET", "/x", handler)isn't read yet — chi takes the method as an argument there rather than a call of its own.Mountwith a factory function (r.Mount("/api", ordersRouter())) isn't resolved.Witharguments with nested calls aren't parsed, and aRoutewhose callback is a variable rather than a literal function isn't read.How I tested
tests/go-groups.test.mjs: 10 tests — unit tests for each shape above plus the look-alikes that must stay unprefixed, and an end-to-end test where a chi server's grouped routes land under their prefix and the website's fetches connect to them. All 10 fail on the code before this change and pass with it.npm test— 494 of 498 tests pass. The 2 failures are pre-existing timing-threshold tests (C# and Liquid pattern-matching speed) that also fail on the code before this change (the 4 failures are pre-existing timing failures on this slow machine, unchanged from before this change).routerspattern Python and C# already use.Tested on Linux only.
claude plugin validatewas not available on this machine.