Replies: 4 comments 9 replies
-
Not sure I understand the point of Could be a bit more strict and [Body, Params (short for QueryParams), Headers] but that could get annoying. What could be interesting maybe is with marshal attributes we could |
Beta Was this translation helpful? Give feedback.
-
I'm not so sure now about this way of doing things now. This can be nice but adds complexity to the framework and 2 ways of doing the same thing. Currentfunc (rs ingredientRessource) MountRoutes(s *fuego.Server) {
ingredientsGroup := fuego.Group(s, "/ingredients").
Header("Content-Type", "The content type of the request"). // Group headers
Cookie("session", "The session cookie") // Group cookies
fuego.Get(ingredientsGroup, "/ingredients", rs.getAllIngredients).
QueryParam("name", "Filter by type").
QueryParam("name", "Filter by name", fuego.OpenAPIParam{Example: "carrot", Type: "query"})
fuego.Post(ingredientsGroup, "/ingredients/new", rs.newIngredient)
}
func (rs ingredientRessource) getAllIngredients(c fuego.ContextNoBody) ([]store.Ingredient, error) {
category := c.QueryParam("category") // Access via magic string. Not catch at compile-time, but we can add a warn log in the framework when receiving request if the param is not declared in the OpenAPI config. I like this idea.
name := c.QueryParam("name")
ingredients, err := rs.IngredientRepository.GetIngredients(c.Context(), category, name)
if err != nil {
return nil, err
}
return ingredients, nil
} RFC (doubt that this is good)If I have two route (two controllers) and we want to have a Header param in both, we should do this (see below) because typing cannot be sent through groups. type getAllIngredientsParams struct {
Session string `cookie:"session"` // it is a bit of magic string here too, nothing prevents you to write "sesssion" (with 3 n) or any typo in the tag
}
func (rs ingredientRessource) getAllIngredients(c fuego.ContextWithParams[getAllIngredientsParams]) ([]store.Ingredient, error) {
}
type filterIngredientsParams struct { // not very DRY, it looks like the other struct... Maybe support embedding ? This would mean that we carry a struct in every file...
Session string `cookie:"session"`
Name string `query:"name"`
}
func (rs ingredientRessource) filterIngredients(c fuego.ContextWithParams[filterIngredientsParams]) ([]store.Ingredient, error) {
c.Params().Name // This is typed inside the controller
} We can also support both syntax, but that might cause issues in the future. |
Beta Was this translation helpful? Give feedback.
-
Any thoughts to something along the lines like (this does just concern itself with input params. type ContextWithBody[Params, Body any] struct {
body *Body // Cache the body in request context, because it is not possible to read an http request body multiple times.
ContextNoBody[Params]
}
type ContextNoBody[Params any] struct {
Req *http.Request
Res http.ResponseWriter
params *Params
fs fs.FS
templates *template.Template
readOptions readOptions
} controller signatures would look something like func (rs ingredientRessource) filterIngredients(c fuego.ContextNoBody[Params]) ([]store.Ingredient, error) {
ps, err := c.Params
if err != nil {
...
}
limit := ps.Limit
} We could provide base Params struct in the fuego package as well as letting callers compose their own. All that being said I haven't looked into this very much and their could be a massive technical limitation that i'm overlooking. |
Beta Was this translation helpful? Give feedback.
-
I believe this can be closed? |
Beta Was this translation helpful? Give feedback.
-
RFC fuego-119 ➡️ Refused in favor of #174
Goal
Expose parameters (Cookie, Query Param, Header) to the controller signature so they can be registered on OpenAPI document.
Current situation
The user must declare
route.QueryParam("name")
on the route andc.QueryParam("name")
in the controller.The "magic string" declaration is an issue because nothing prevents the user to declare
route.QueryParam("name")
on one side and try to usec.QueryParam("username")
(username instead of name) on the other.This is no better than comment declaration (except for the typing part for
QueryParamInt
and so), so we should be typing more strongly the parametersProposal
Strongly typed params
Other proposition for Cookies:
Mandatory
*http.Cookie
typing, so the user can specify dynamically the httpOnly, path and other cookie attributes while still declaring the param on the OpenAPI response.The
Context*
types would be like this:fuego.Context[BodyType, ParamsType]
🆕 body+params, useful for PUT and other casesfuego.ContextWithParams[ParamsType]
🆕 no body, just params (headers+query), very useful for GET requestsfuego.ContextWithBody[BodyType]
just body, useful for many POST requestsfuego.ContextNoBody
without params and bodyBeta Was this translation helpful? Give feedback.
All reactions