-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for r.PathValue when built for Go 1.22.
Go 1.22 introduced pattern matching into its ServeMux, and as a result the net/http.Request type got a PathValue method, returning the value that matched the given key in the pattern. We can, and should, populate the data behind that mathod when using trout's pattern matching, so handlers can standardize on that built-in function.
- Loading branch information
1 parent
844e004
commit 80a59d6
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build !go1.22 | ||
|
||
package trout | ||
|
||
import "net/http" | ||
|
||
func setBuiltinRequestPathVar(_ *http.Request, _, _ string) { | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build go1.22 | ||
|
||
package trout | ||
|
||
import "net/http" | ||
|
||
func setBuiltinRequestPathVar(r *http.Request, name, value string) { | ||
r.SetPathValue(name, value) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build go1.22 | ||
|
||
package trout_test | ||
|
||
import ( | ||
"net/http" | ||
|
||
"darlinggo.co/trout/v2" | ||
) | ||
|
||
func ExampleRouter_Endpoint_pathValues() { | ||
postsHandler := http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
// we populate the PathValue request property introduced | ||
// in Go 1.22 when you build with Go 1.22. | ||
id := r.PathValue("id") | ||
_, err := w.Write([]byte(id)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
var router trout.Router | ||
router.Endpoint("/posts/{id}").Handler(postsHandler) | ||
|
||
req, _ := http.NewRequest("GET", "http://example.com/posts/foo", nil) | ||
router.ServeHTTP(exampleResponseWriter{}, req) | ||
|
||
// Output: | ||
// foo | ||
} |