Skip to content

Commit

Permalink
Fuego Context is std lib context
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Mar 27, 2024
1 parent 0275c91 commit 5564264
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 24 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ type ContextNoBody struct {
readOptions readOptions
}

var _ ctx[any] = ContextNoBody{} // Check that ContextNoBody implements Ctx.
var _ context.Context = ContextNoBody{} // Check that ContextNoBody implements context.Context.

func (c ContextNoBody) Body() (any, error) {
slog.Warn("this method should not be called. It probably happened because you passed the context to another controller.")
return body[map[string]any](c)
Expand Down Expand Up @@ -163,6 +166,27 @@ func (c ContextNoBody) Redirect(code int, url string) (any, error) {
return nil, nil
}

// ContextNoBody implements the context interface via [net/http.Request.Context]
func (c ContextNoBody) Deadline() (deadline time.Time, ok bool) {
return c.Req.Context().Deadline()
}

// ContextNoBody implements the context interface via [net/http.Request.Context]
func (c ContextNoBody) Done() <-chan struct{} {
return c.Req.Context().Done()
}

// ContextNoBody implements the context interface via [net/http.Request.Context]
func (c ContextNoBody) Err() error {
return c.Req.Context().Err()
}

// ContextNoBody implements the context interface via [net/http.Request.Context]
func (c ContextNoBody) Value(key any) any {
return c.Req.Context().Value(key)
}

// ContextNoBody implements the context interface via [net/http.Request.Context]
func (c ContextNoBody) Context() context.Context {
return c.Req.Context()
}
Expand Down
6 changes: 3 additions & 3 deletions examples/full-app-gourmet/views/admin.ingredient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func (rs Ressource) adminOneIngredient(c *fuego.ContextWithBody[store.UpdateIngr

updateIngredientArgs.ID = c.PathParam("id")

_, err = rs.IngredientsQueries.UpdateIngredient(c.Context(), updateIngredientArgs)
_, err = rs.IngredientsQueries.UpdateIngredient(c, updateIngredientArgs)
if err != nil {
return nil, err
}

c.Response().Header().Set("HX-Trigger", "entity-updated")
}

ingredient, err := rs.IngredientsQueries.GetIngredient(c.Context(), id)
ingredient, err := rs.IngredientsQueries.GetIngredient(c, id)
if err != nil {
return nil, err
}
Expand All @@ -50,7 +50,7 @@ func (rs Ressource) adminCreateIngredient(c *fuego.ContextWithBody[store.CreateI
return nil, err
}

_, err = rs.IngredientsQueries.CreateIngredient(c.Context(), createIngredientArgs)
_, err = rs.IngredientsQueries.CreateIngredient(c, createIngredientArgs)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5564264

Please sign in to comment.