Skip to content

Commit

Permalink
Hide Serialize and SerializeError from the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 22, 2024
1 parent 6b19564 commit 2329fb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
22 changes: 6 additions & 16 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ type ContextWithBody[B any] interface {
// SetStatus sets the status code of the response.
// Alias to http.ResponseWriter.WriteHeader.
SetStatus(code int)
// SetDefaultStatusCode sets the status code of the response defined in the options.
// Is automatically done by Fuego but you might need it.
SetDefaultStatusCode()

// Redirect redirects to the given url with the given status code.
// Example:
Expand All @@ -104,13 +101,6 @@ type ContextWithBody[B any] interface {
// return c.Redirect(301, "/recipes-list")
// })
Redirect(code int, url string) (any, error)

// Serialize serializes the given data to the response.
// Is automatically done by Fuego but you might need it.
Serialize(data any) error
// SerializeError serializes the given error to the response.
// Is automatically done by Fuego but you might need it.
SerializeError(err error)
}

// NewNetHTTPContext returns a new context. It is used internally by Fuego. You probably want to use Ctx[B] instead.
Expand Down Expand Up @@ -144,9 +134,9 @@ type netHttpContext[Body any] struct {
fs fs.FS
templates *template.Template

readOptions readOptions
serializer Sender
serializeError ErrorSender
readOptions readOptions
serializer Sender
errorSerializer ErrorSender
}

var (
Expand Down Expand Up @@ -280,14 +270,14 @@ func (c netHttpContext[B]) Serialize(data any) error {

// SerializeError serializes the given error to the response. It uses the Content-Type header to determine the serialization format.
func (c netHttpContext[B]) SerializeError(err error) {
if c.serializeError == nil {
if c.errorSerializer == nil {
SendError(c.Res, c.Req, err)
return
}
c.serializeError(c.Res, c.Req, err)
c.errorSerializer(c.Res, c.Req, err)
}

// SetDefaultStatusCode sets the default status code of the response.
// setDefaultStatusCode sets the default status code of the response.
func (c netHttpContext[B]) SetDefaultStatusCode() {
if c.DefaultStatusCode != 0 {
c.SetStatus(c.DefaultStatusCode)
Expand Down
19 changes: 16 additions & 3 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,29 @@ func HTTPHandler[ReturnType, Body any](s *Server, controller func(c ContextWithB
MaxBodySize: s.maxBodySize,
})
ctx.serializer = s.Serialize
ctx.serializeError = s.SerializeError
ctx.errorSerializer = s.SerializeError
ctx.fs = s.fs
ctx.templates = templates

flow(s.Engine, ctx, controller)
Flow(s.Engine, ctx, controller)
}
}

// Contains the logic for the flow of a Fuego controller.
// Extends ContextWithBody with methods not exposed in the Controllers.
type ContextFlowable[B any] interface {
ContextWithBody[B]

// SetDefaultStatusCode sets the status code of the response defined in the options.
SetDefaultStatusCode()
// Serialize serializes the given data to the response.
Serialize(data any) error
// SerializeError serializes the given error to the response.
SerializeError(err error)
}

// Generic handler for Fuego controllers.
func flow[B, T any](s *Engine, ctx ContextWithBody[B], controller func(c ContextWithBody[B]) (T, error)) {
func Flow[B, T any](s *Engine, ctx ContextFlowable[B], controller func(c ContextWithBody[B]) (T, error)) {
ctx.SetHeader("X-Powered-By", "Fuego")
ctx.SetHeader("Trailer", "Server-Timing")

Expand Down

0 comments on commit 2329fb2

Please sign in to comment.