Skip to content

Commit eac8b5a

Browse files
committed
Remove useless contextNoBodyImpl type
1 parent c3e9971 commit eac8b5a

File tree

5 files changed

+83
-114
lines changed

5 files changed

+83
-114
lines changed

ctx.go

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ContextNoBody = ContextWithBody[any]
2525
// Please do not use a pointer type as parameter.
2626
type ContextWithBody[B any] interface {
2727
context.Context
28+
2829
// Body returns the body of the request.
2930
// If (*B) implements [InTransformer], it will be transformed after deserialization.
3031
// It caches the result, so it can be called multiple times.
@@ -94,17 +95,9 @@ type ContextWithBody[B any] interface {
9495
Redirect(code int, url string) (any, error)
9596
}
9697

97-
// NewContextWithBody returns a new context. It is used internally by Fuego. You probably want to use Ctx[B] instead.
98-
func NewContextWithBody[B any](w http.ResponseWriter, r *http.Request, options readOptions) ContextWithBody[B] {
99-
c := &contextWithBodyImpl[B]{
100-
contextNoBodyImpl: NewContextNoBody(w, r, options),
101-
}
102-
103-
return c
104-
}
105-
106-
func NewContextNoBody(w http.ResponseWriter, r *http.Request, options readOptions) contextNoBodyImpl {
107-
c := contextNoBodyImpl{
98+
// NewNetHTTPContext returns a new context. It is used internally by Fuego. You probably want to use Ctx[B] instead.
99+
func NewNetHTTPContext[B any](w http.ResponseWriter, r *http.Request, options readOptions) ContextWithBody[B] {
100+
c := &netHttpContext[B]{
108101
Res: w,
109102
Req: r,
110103
readOptions: readOptions{
@@ -113,25 +106,21 @@ func NewContextNoBody(w http.ResponseWriter, r *http.Request, options readOption
113106
},
114107
urlValues: r.URL.Query(),
115108
}
109+
116110
return c
117111
}
118112

113+
var (
114+
_ ContextWithBody[any] = &netHttpContext[any]{} // Check that ContextWithBody implements Ctx.
115+
_ ContextWithBody[string] = &netHttpContext[string]{} // Check that ContextWithBody implements Ctx.
116+
)
117+
119118
// ContextWithBody is the same as fuego.ContextNoBody, but
120119
// has a Body. The Body type parameter represents the expected data type
121120
// from http.Request.Body. Please do not use a pointer as a type parameter.
122-
type contextWithBodyImpl[Body any] struct {
121+
type netHttpContext[Body any] struct {
123122
body *Body // Cache the body in request context, because it is not possible to read an HTTP request body multiple times.
124-
contextNoBodyImpl
125-
}
126-
127-
var (
128-
_ ContextWithBody[any] = &contextWithBodyImpl[any]{} // Check that ContextWithBody implements Ctx.
129-
_ ContextWithBody[string] = &contextWithBodyImpl[string]{} // Check that ContextWithBody implements Ctx.
130-
)
131123

132-
// ContextNoBody is used when the controller does not have a body.
133-
// It is used as a base context for other Context types.
134-
type contextNoBodyImpl struct {
135124
Req *http.Request
136125
Res http.ResponseWriter
137126

@@ -144,27 +133,9 @@ type contextNoBodyImpl struct {
144133
readOptions readOptions
145134
}
146135

147-
var (
148-
_ ContextWithBody[any] = contextNoBodyImpl{} // Check that ContextNoBody implements Ctx.
149-
_ context.Context = contextNoBodyImpl{} // Check that ContextNoBody implements context.Context.
150-
)
151-
152-
func (c contextNoBodyImpl) Body() (any, error) {
153-
slog.Warn("this method should not be called. It probably happened because you passed the context to another controller.")
154-
return body[map[string]any](c)
155-
}
156-
157-
func (c contextNoBodyImpl) MustBody() any {
158-
b, err := c.Body()
159-
if err != nil {
160-
panic(err)
161-
}
162-
return b
163-
}
164-
165136
// SetStatus sets the status code of the response.
166137
// Alias to http.ResponseWriter.WriteHeader.
167-
func (c contextNoBodyImpl) SetStatus(code int) {
138+
func (c netHttpContext[B]) SetStatus(code int) {
168139
c.Res.WriteHeader(code)
169140
}
170141

@@ -175,65 +146,65 @@ type readOptions struct {
175146
LogBody bool
176147
}
177148

178-
func (c contextNoBodyImpl) Redirect(code int, url string) (any, error) {
149+
func (c netHttpContext[B]) Redirect(code int, url string) (any, error) {
179150
http.Redirect(c.Res, c.Req, url, code)
180151

181152
return nil, nil
182153
}
183154

184155
// ContextNoBody implements the context interface via [net/http.Request.Context]
185-
func (c contextNoBodyImpl) Deadline() (deadline time.Time, ok bool) {
156+
func (c netHttpContext[B]) Deadline() (deadline time.Time, ok bool) {
186157
return c.Req.Context().Deadline()
187158
}
188159

189160
// ContextNoBody implements the context interface via [net/http.Request.Context]
190-
func (c contextNoBodyImpl) Done() <-chan struct{} {
161+
func (c netHttpContext[B]) Done() <-chan struct{} {
191162
return c.Req.Context().Done()
192163
}
193164

194165
// ContextNoBody implements the context interface via [net/http.Request.Context]
195-
func (c contextNoBodyImpl) Err() error {
166+
func (c netHttpContext[B]) Err() error {
196167
return c.Req.Context().Err()
197168
}
198169

199170
// ContextNoBody implements the context interface via [net/http.Request.Context]
200-
func (c contextNoBodyImpl) Value(key any) any {
171+
func (c netHttpContext[B]) Value(key any) any {
201172
return c.Req.Context().Value(key)
202173
}
203174

204175
// ContextNoBody implements the context interface via [net/http.Request.Context]
205-
func (c contextNoBodyImpl) Context() context.Context {
176+
func (c netHttpContext[B]) Context() context.Context {
206177
return c.Req.Context()
207178
}
208179

209180
// Get request header
210-
func (c contextNoBodyImpl) Header(key string) string {
181+
func (c netHttpContext[B]) Header(key string) string {
211182
return c.Request().Header.Get(key)
212183
}
213184

214185
// Has request header
215-
func (c contextNoBodyImpl) HasHeader(key string) bool {
186+
func (c netHttpContext[B]) HasHeader(key string) bool {
216187
return c.Header(key) != ""
217188
}
218189

219190
// Sets response header
220-
func (c contextNoBodyImpl) SetHeader(key, value string) {
191+
func (c netHttpContext[B]) SetHeader(key, value string) {
221192
c.Response().Header().Set(key, value)
222193
}
223194

224195
// Get request cookie
225-
func (c contextNoBodyImpl) Cookie(name string) (*http.Cookie, error) {
196+
func (c netHttpContext[B]) Cookie(name string) (*http.Cookie, error) {
226197
return c.Request().Cookie(name)
227198
}
228199

229200
// Has request cookie
230-
func (c contextNoBodyImpl) HasCookie(name string) bool {
201+
func (c netHttpContext[B]) HasCookie(name string) bool {
231202
_, err := c.Cookie(name)
232203
return err == nil
233204
}
234205

235206
// Sets response cookie
236-
func (c contextNoBodyImpl) SetCookie(cookie http.Cookie) {
207+
func (c netHttpContext[B]) SetCookie(cookie http.Cookie) {
237208
http.SetCookie(c.Response(), &cookie)
238209
}
239210

@@ -245,7 +216,7 @@ func (c contextNoBodyImpl) SetCookie(cookie http.Cookie) {
245216
// that the templates will be parsed only once, removing
246217
// the need to parse the templates on each request but also preventing
247218
// to dynamically use new templates.
248-
func (c contextNoBodyImpl) Render(templateToExecute string, data any, layoutsGlobs ...string) (CtxRenderer, error) {
219+
func (c netHttpContext[B]) Render(templateToExecute string, data any, layoutsGlobs ...string) (CtxRenderer, error) {
249220
return &StdRenderer{
250221
templateToExecute: templateToExecute,
251222
templates: c.templates,
@@ -256,7 +227,7 @@ func (c contextNoBodyImpl) Render(templateToExecute string, data any, layoutsGlo
256227
}
257228

258229
// PathParams returns the path parameters of the request.
259-
func (c contextNoBodyImpl) PathParam(name string) string {
230+
func (c netHttpContext[B]) PathParam(name string) string {
260231
return c.Req.PathValue(name)
261232
}
262233

@@ -280,12 +251,12 @@ func (e QueryParamInvalidTypeError) Error() string {
280251
}
281252

282253
// QueryParams returns the query parameters of the request. It is a shortcut for c.Req.URL.Query().
283-
func (c contextNoBodyImpl) QueryParams() url.Values {
254+
func (c netHttpContext[B]) QueryParams() url.Values {
284255
return c.urlValues
285256
}
286257

287258
// QueryParamsArr returns an slice of string from the given query parameter.
288-
func (c contextNoBodyImpl) QueryParamArr(name string) []string {
259+
func (c netHttpContext[B]) QueryParamArr(name string) []string {
289260
_, ok := c.params[name]
290261
if !ok {
291262
slog.Warn("query parameter not expected in OpenAPI spec", "param", name)
@@ -301,7 +272,7 @@ func (c contextNoBodyImpl) QueryParamArr(name string) []string {
301272
// fuego.Get(s, "/test", myController,
302273
// option.Query("name", "Name", param.Default("hey"))
303274
// )
304-
func (c contextNoBodyImpl) QueryParam(name string) string {
275+
func (c netHttpContext[B]) QueryParam(name string) string {
305276
_, ok := c.params[name]
306277
if !ok {
307278
slog.Warn("query parameter not expected in OpenAPI spec", "param", name, "expected_one_of", c.params)
@@ -314,7 +285,7 @@ func (c contextNoBodyImpl) QueryParam(name string) string {
314285
return c.urlValues.Get(name)
315286
}
316287

317-
func (c contextNoBodyImpl) QueryParamIntErr(name string) (int, error) {
288+
func (c netHttpContext[B]) QueryParamIntErr(name string) (int, error) {
318289
param := c.QueryParam(name)
319290
if param == "" {
320291
defaultValue, ok := c.params[name].Default.(int)
@@ -348,7 +319,7 @@ func (c contextNoBodyImpl) QueryParamIntErr(name string) (int, error) {
348319
//
349320
// and the query parameter does not exist, it will return 1.
350321
// If the query parameter does not exist and there is no default value, or if it is not an int, it returns 0.
351-
func (c contextNoBodyImpl) QueryParamInt(name string) int {
322+
func (c netHttpContext[B]) QueryParamInt(name string) int {
352323
param, err := c.QueryParamIntErr(name)
353324
if err != nil {
354325
return 0
@@ -367,7 +338,7 @@ func (c contextNoBodyImpl) QueryParamInt(name string) int {
367338
//
368339
// and the query parameter does not exist in the HTTP request, it will return true.
369340
// Accepted values are defined as [strconv.ParseBool]
370-
func (c contextNoBodyImpl) QueryParamBoolErr(name string) (bool, error) {
341+
func (c netHttpContext[B]) QueryParamBoolErr(name string) (bool, error) {
371342
param := c.QueryParam(name)
372343
if param == "" {
373344
defaultValue, ok := c.params[name].Default.(bool)
@@ -400,7 +371,7 @@ func (c contextNoBodyImpl) QueryParamBoolErr(name string) (bool, error) {
400371
// )
401372
//
402373
// and the query parameter does not exist in the HTTP request, it will return true.
403-
func (c contextNoBodyImpl) QueryParamBool(name string) bool {
374+
func (c netHttpContext[B]) QueryParamBool(name string) bool {
404375
param, err := c.QueryParamBoolErr(name)
405376
if err != nil {
406377
return false
@@ -409,26 +380,26 @@ func (c contextNoBodyImpl) QueryParamBool(name string) bool {
409380
return param
410381
}
411382

412-
func (c contextNoBodyImpl) MainLang() string {
383+
func (c netHttpContext[B]) MainLang() string {
413384
return strings.Split(c.MainLocale(), "-")[0]
414385
}
415386

416-
func (c contextNoBodyImpl) MainLocale() string {
387+
func (c netHttpContext[B]) MainLocale() string {
417388
return strings.Split(c.Req.Header.Get("Accept-Language"), ",")[0]
418389
}
419390

420391
// Request returns the HTTP request.
421-
func (c contextNoBodyImpl) Request() *http.Request {
392+
func (c netHttpContext[B]) Request() *http.Request {
422393
return c.Req
423394
}
424395

425396
// Response returns the HTTP response writer.
426-
func (c contextNoBodyImpl) Response() http.ResponseWriter {
397+
func (c netHttpContext[B]) Response() http.ResponseWriter {
427398
return c.Res
428399
}
429400

430401
// MustBody works like Body, but panics if there is an error.
431-
func (c *contextWithBodyImpl[B]) MustBody() B {
402+
func (c *netHttpContext[B]) MustBody() B {
432403
b, err := c.Body()
433404
if err != nil {
434405
panic(err)
@@ -441,17 +412,17 @@ func (c *contextWithBodyImpl[B]) MustBody() B {
441412
// It caches the result, so it can be called multiple times.
442413
// The reason the body is cached is that it is impossible to read an HTTP request body multiple times, not because of performance.
443414
// For decoding, it uses the Content-Type header. If it is not set, defaults to application/json.
444-
func (c *contextWithBodyImpl[B]) Body() (B, error) {
415+
func (c *netHttpContext[B]) Body() (B, error) {
445416
if c.body != nil {
446417
return *c.body, nil
447418
}
448419

449-
body, err := body[B](c.contextNoBodyImpl)
420+
body, err := body[B](*c)
450421
c.body = &body
451422
return body, err
452423
}
453424

454-
func body[B any](c contextNoBodyImpl) (B, error) {
425+
func body[B any](c netHttpContext[B]) (B, error) {
455426
// Limit the size of the request body.
456427
if c.readOptions.MaxBodySize != 0 {
457428
c.Req.Body = http.MaxBytesReader(nil, c.Req.Body, c.readOptions.MaxBodySize)

0 commit comments

Comments
 (0)