Skip to content

Commit d9192a2

Browse files
committed
Ctx initialisation
1 parent 1b44fb7 commit d9192a2

File tree

7 files changed

+46
-43
lines changed

7 files changed

+46
-43
lines changed

ctx.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const (
1818
maxBodySize = 1048576
1919
)
2020

21-
// ctx is the context of the request.
21+
// Ctx is the context of the request.
2222
// It contains the request body, the path parameters, the query parameters, and the HTTP request.
2323
// Please do not use a pointer type as parameter.
24-
type ctx[B any] interface {
24+
type Ctx[B any] interface {
2525
// Body returns the body of the request.
2626
// If (*B) implements [InTransformer], it will be transformed after deserialization.
2727
// It caches the result, so it can be called multiple times.
@@ -129,7 +129,7 @@ type ContextNoBody struct {
129129
}
130130

131131
var (
132-
_ ctx[any] = ContextNoBody{} // Check that ContextNoBody implements Ctx.
132+
_ Ctx[any] = ContextNoBody{} // Check that ContextNoBody implements Ctx.
133133
_ context.Context = ContextNoBody{} // Check that ContextNoBody implements context.Context.
134134
)
135135

@@ -160,10 +160,10 @@ type readOptions struct {
160160
}
161161

162162
var (
163-
_ ctx[any] = &ContextWithBody[any]{} // Check that ContextWithBody[any] implements Ctx.
164-
_ ctx[string] = &ContextWithBody[string]{} // Check that ContextWithBody[string] implements Ctx.
165-
_ ctx[any] = &ContextNoBody{} // Check that ContextNoBody implements Ctx.
166-
_ ctx[any] = ContextNoBody{} // Check that ContextNoBody implements Ctx.
163+
_ Ctx[any] = &ContextWithBody[any]{} // Check that ContextWithBody[any] implements Ctx.
164+
_ Ctx[string] = &ContextWithBody[string]{} // Check that ContextWithBody[string] implements Ctx.
165+
_ Ctx[any] = &ContextNoBody{} // Check that ContextNoBody implements Ctx.
166+
_ Ctx[any] = ContextNoBody{} // Check that ContextNoBody implements Ctx.
167167
)
168168

169169
func (c ContextNoBody) Redirect(code int, url string) (any, error) {

extra/fuegogin/adaptor.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ func Get[T, B any](
1111
s *fuego.OpenAPI,
1212
e *gin.Engine,
1313
path string,
14-
handler func(c *ContextWithBody[T]) (B, error),
14+
handler func(c *ContextWithBody[B]) (T, error),
1515
options ...func(*fuego.BaseRoute),
16-
) *fuego.Route[B, T] {
16+
) *fuego.Route[T, B] {
1717
return Handle(s, e, "GET", path, handler, options...)
1818
}
1919

2020
func Post[T, B any](
2121
s *fuego.OpenAPI,
2222
e *gin.Engine,
2323
path string,
24-
handler func(c *ContextWithBody[T]) (B, error),
24+
handler func(c *ContextWithBody[B]) (T, error),
2525
options ...func(*fuego.BaseRoute),
26-
) *fuego.Route[B, T] {
26+
) *fuego.Route[T, B] {
2727
return Handle(s, e, "POST", path, handler, options...)
2828
}
2929

@@ -32,10 +32,10 @@ func Handle[T, B any](
3232
e *gin.Engine,
3333
method,
3434
path string,
35-
handler func(c *ContextWithBody[T]) (B, error),
35+
handler func(c *ContextWithBody[B]) (T, error),
3636
options ...func(*fuego.BaseRoute),
37-
) *fuego.Route[B, T] {
38-
route := &fuego.Route[B, T]{
37+
) *fuego.Route[T, B] {
38+
route := &fuego.Route[T, B]{
3939
BaseRoute: fuego.BaseRoute{
4040
Method: method,
4141
Path: path,
@@ -50,12 +50,14 @@ func Handle[T, B any](
5050
o(&route.BaseRoute)
5151
}
5252

53+
route.BaseRoute.GenerateDefaultDescription()
54+
5355
e.Handle(method, path, func(c *gin.Context) {
54-
context := &ContextWithBody[T]{
55-
ContextNoBody: ContextNoBody{
56-
ginCtx: c,
57-
},
56+
context := &ContextWithBody[B]{
57+
58+
ginCtx: c,
5859
}
60+
5961
ans, err := handler(context)
6062
if err != nil {
6163
c.Error(err)

extra/fuegogin/context.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package fuegogin
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
76
"net/url"
87

@@ -12,17 +11,13 @@ import (
1211
)
1312

1413
type ContextWithBody[B any] struct {
15-
ContextNoBody
16-
}
17-
18-
type ContextNoBody struct {
19-
fuego.ContextNoBody
2014
ginCtx *gin.Context
2115
}
2216

17+
type ContextNoBody = ContextWithBody[any]
18+
2319
// Body implements fuego.Ctx.
2420
func (c *ContextWithBody[B]) Body() (B, error) {
25-
fmt.Println("c.ginCtx", c.ginCtx)
2621
var body B
2722
err := c.ginCtx.Bind(&body)
2823
return body, err

extra/fuegogin/lib/lib.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func SetupGin() (*gin.Engine, *fuego.OpenAPI) {
2828
e.GET("/gin", ginController)
2929

3030
// Register to Gin router with Fuego wrapper for same OpenAPI spec
31-
fuegogin.Get(openapi, e, "/fuego", fuegoController)
32-
fuegogin.Get(openapi, e, "/fuego-with-options", fuegoController,
31+
fuegogin.Get(openapi, e, "/fuego", fuegoControllerGet)
32+
fuegogin.Post(openapi, e, "/fuego-with-options", fuegoControllerPost,
3333
option.Description("Some description"),
3434
option.OperationID("SomeOperationID"),
3535
option.AddError(409, "Name Already Exists"),
@@ -50,7 +50,13 @@ func ginController(c *gin.Context) {
5050
c.String(200, "pong")
5151
}
5252

53-
func fuegoController(c *fuegogin.ContextWithBody[HelloRequest]) (HelloResponse, error) {
53+
func fuegoControllerGet(c *fuegogin.ContextNoBody) (HelloResponse, error) {
54+
return HelloResponse{
55+
Message: "Hello",
56+
}, nil
57+
}
58+
59+
func fuegoControllerPost(c *fuegogin.ContextWithBody[HelloRequest]) (HelloResponse, error) {
5460
body, err := c.Body()
5561
if err != nil {
5662
return HelloResponse{}, err
@@ -61,7 +67,7 @@ func fuegoController(c *fuegogin.ContextWithBody[HelloRequest]) (HelloResponse,
6167
fmt.Println("name", name)
6268

6369
return HelloResponse{
64-
Message: "Hello " + body.Name,
70+
Message: "Hello " + body.Name + name,
6571
}, nil
6672
}
6773

mux.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,27 @@ func Group(s *Server, path string, routeOptions ...func(*BaseRoute)) *Server {
4545
}
4646

4747
// Capture all methods (GET, POST, PUT, PATCH, DELETE) and register a controller.
48-
func All[ReturnType, Body any, Contexted ctx[Body]](s *Server, path string, controller func(Contexted) (ReturnType, error), options ...func(*BaseRoute)) *Route[ReturnType, Body] {
48+
func All[ReturnType, Body any, Contexted Ctx[Body]](s *Server, path string, controller func(Contexted) (ReturnType, error), options ...func(*BaseRoute)) *Route[ReturnType, Body] {
4949
return registerFuegoController(s, "", path, controller, options...)
5050
}
5151

52-
func Get[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
52+
func Get[T, B any, Contexted Ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
5353
return registerFuegoController(s, http.MethodGet, path, controller, options...)
5454
}
5555

56-
func Post[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
56+
func Post[T, B any, Contexted Ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
5757
return registerFuegoController(s, http.MethodPost, path, controller, options...)
5858
}
5959

60-
func Delete[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
60+
func Delete[T, B any, Contexted Ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
6161
return registerFuegoController(s, http.MethodDelete, path, controller, options...)
6262
}
6363

64-
func Put[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
64+
func Put[T, B any, Contexted Ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
6565
return registerFuegoController(s, http.MethodPut, path, controller, options...)
6666
}
6767

68-
func Patch[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
68+
func Patch[T, B any, Contexted Ctx[B]](s *Server, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
6969
return registerFuegoController(s, http.MethodPatch, path, controller, options...)
7070
}
7171

serve.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *Server) url() string {
6161
// initializes any Context type with the base ContextNoBody context.
6262
//
6363
// var ctx ContextWithBody[any] // does not work because it will create a ContextWithBody[any] with a nil value
64-
func initContext[Contextable ctx[Body], Body any](baseContext ContextNoBody) (Contextable, error) {
64+
func InitContext[Contextable Ctx[Body], Body any](baseContext ContextNoBody) (Contextable, error) {
6565
var c Contextable
6666

6767
err := validateParams(baseContext)
@@ -86,7 +86,7 @@ func initContext[Contextable ctx[Body], Body any](baseContext ContextNoBody) (Co
8686
// HTTPHandler converts a Fuego controller into a http.HandlerFunc.
8787
// Uses Server for configuration.
8888
// Uses Route for route configuration. Optional.
89-
func HTTPHandler[ReturnType, Body any, Contextable ctx[Body]](s *Server, controller func(c Contextable) (ReturnType, error), route *BaseRoute) http.HandlerFunc {
89+
func HTTPHandler[ReturnType, Body any, Contextable Ctx[Body]](s *Server, controller func(c Contextable) (ReturnType, error), route *BaseRoute) http.HandlerFunc {
9090
// Just a check, not used at request time
9191
baseContext := *new(Contextable)
9292
if reflect.TypeOf(baseContext) == nil {
@@ -109,7 +109,7 @@ func HTTPHandler[ReturnType, Body any, Contextable ctx[Body]](s *Server, control
109109
templates = template.Must(s.template.Clone())
110110
}
111111

112-
ctx, err := initContext[Contextable](ContextNoBody{
112+
ctx, err := InitContext[Contextable](ContextNoBody{
113113
Req: r,
114114
Res: w,
115115
readOptions: readOptions{

serve_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func TestIni(t *testing.T) {
351351
t.Run("can initialize ContextNoBody", func(t *testing.T) {
352352
req := httptest.NewRequest("GET", "/ctx/error-in-rendering", nil)
353353
w := httptest.NewRecorder()
354-
ctx, err := initContext[ContextNoBody](ContextNoBody{
354+
ctx, err := InitContext[ContextNoBody](ContextNoBody{
355355
Req: req,
356356
Res: w,
357357
})
@@ -365,7 +365,7 @@ func TestIni(t *testing.T) {
365365
t.Run("can initialize ContextNoBody", func(t *testing.T) {
366366
req := httptest.NewRequest("GET", "/ctx/error-in-rendering", nil)
367367
w := httptest.NewRecorder()
368-
ctx, err := initContext[*ContextNoBody](ContextNoBody{
368+
ctx, err := InitContext[*ContextNoBody](ContextNoBody{
369369
Req: req,
370370
Res: w,
371371
})
@@ -379,7 +379,7 @@ func TestIni(t *testing.T) {
379379
t.Run("can initialize ContextWithBody[string]", func(t *testing.T) {
380380
req := httptest.NewRequest("GET", "/ctx/error-in-rendering", nil)
381381
w := httptest.NewRecorder()
382-
ctx, err := initContext[*ContextWithBody[string]](ContextNoBody{
382+
ctx, err := InitContext[*ContextWithBody[string]](ContextNoBody{
383383
Req: req,
384384
Res: w,
385385
})
@@ -393,7 +393,7 @@ func TestIni(t *testing.T) {
393393
t.Run("can initialize ContextWithBody[struct]", func(t *testing.T) {
394394
req := httptest.NewRequest("GET", "/ctx/error-in-rendering", nil)
395395
w := httptest.NewRecorder()
396-
ctx, err := initContext[*ContextWithBody[ans]](ContextNoBody{
396+
ctx, err := InitContext[*ContextWithBody[ans]](ContextNoBody{
397397
Req: req,
398398
Res: w,
399399
})
@@ -409,7 +409,7 @@ func TestIni(t *testing.T) {
409409
w := httptest.NewRecorder()
410410

411411
require.Panics(t, func() {
412-
initContext[ctx[any]](ContextNoBody{
412+
InitContext[Ctx[any]](ContextNoBody{
413413
Req: req,
414414
Res: w,
415415
})

0 commit comments

Comments
 (0)