improve documentation

This commit is contained in:
Hayden 2023-03-05 20:17:53 -09:00
parent c2f432cb4d
commit e1f9c2997b
No known key found for this signature in database
GPG key ID: 17CF79474E257545
5 changed files with 83 additions and 2 deletions

View file

@ -10,7 +10,18 @@ import (
// It decodes the request body into a value of type T and passes it to the function f.
// The function f is expected to return a value of type Y and an error.
//
// Note: Action differs from Query in that it decodes the request body.
// Example:
//
// type Body struct {
// Foo string `json:"foo"`
// }
//
// fn := func(ctx context.Context, b Body) (any, error) {
// // do something with b
// return nil, nil
// }
//
// r.Post("/foo", adapters.Action(fn, http.StatusCreated))
func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
v, err := decode[T](r)
@ -28,6 +39,19 @@ func Action[T any, Y any](f AdapterFunc[T, Y], ok int) server.HandlerFunc {
}
// ActionID functions the same as Action, but it also decodes a UUID from the URL path.
//
// Example:
//
// type Body struct {
// Foo string `json:"foo"`
// }
//
// fn := func(ctx context.Context, ID uuid.UUID, b Body) (any, error) {
// // do something with ID and b
// return nil, nil
// }
//
// r.Post("/foo/{id}", adapters.ActionID(fn, http.StatusCreated))
func ActionID[T any, Y any](param string, f IDFunc[T, Y], ok int) server.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
ID, err := routeUUID(r, param)