Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a generator to generate the CTX interface #3024

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
// userContextKey define the key name for storing context.Context in *fasthttp.RequestCtx
const userContextKey contextKey = 0 // __local_user_context__

// DefaultCtx is the default implementation of the Ctx interface
// generation tool `go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c`
// https://github.com/vburenin/ifacemaker/blob/975a95966976eeb2d4365a7fb236e274c54da64c/ifacemaker.go#L14-L30
//
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface Ctx --pkg fiber --output ctx_interface_gen.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
type DefaultCtx struct {
app *App // Reference to *App
route *Route // Reference to *Route
Expand Down Expand Up @@ -1711,16 +1716,80 @@
return c.bind
}

// Convert a string value to a specified type, handling errors and optional default values.
func Convert[T any](value string, convertor func(string) (T, error), defaultValue ...T) (T, error) {
converted, err := convertor(value)
if err != nil {
if len(defaultValue) > 0 {
return defaultValue[0], nil
}
// Reset is a method to reset context fields by given request when to use server handlers.
func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) {
// Reset route and handler index
c.indexRoute = -1
c.indexHandler = 0
// Reset matched flag
c.matched = false
// Set paths
c.pathOriginal = c.app.getString(fctx.URI().PathOriginal())
// Set method
c.method = c.app.getString(fctx.Request.Header.Method())
c.methodINT = c.app.methodInt(c.method)
// Attach *fasthttp.RequestCtx to ctx
c.fasthttp = fctx
// reset base uri
c.baseURI = ""
// Prettify path
c.configDependentPaths()
}

return converted, fmt.Errorf("failed to convert: %w", err)
// Release is a method to reset context fields when to use ReleaseCtx()
func (c *DefaultCtx) release() {
c.route = nil
c.fasthttp = nil
c.bind = nil
c.redirectionMessages = c.redirectionMessages[:0]
c.viewBindMap = sync.Map{}
if c.redirect != nil {
ReleaseRedirect(c.redirect)
c.redirect = nil
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
}
}
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved

// Methods to use with next stack.
func (c *DefaultCtx) getMethodINT() int {
return c.methodINT
}

func (c *DefaultCtx) getIndexRoute() int {
return c.indexRoute
}

func (c *DefaultCtx) getTreePath() string {
return c.treePath
}

func (c *DefaultCtx) getDetectionPath() string {
return c.detectionPath
}

func (c *DefaultCtx) getPathOriginal() string {
return c.pathOriginal

Check warning on line 1770 in ctx.go

View check run for this annotation

Codecov / codecov/patch

ctx.go#L1769-L1770

Added lines #L1769 - L1770 were not covered by tests
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *DefaultCtx) getValues() *[maxParams]string {
return &c.values
}

func (c *DefaultCtx) getMatched() bool {
return c.matched
}

func (c *DefaultCtx) setIndexHandler(handler int) {
c.indexHandler = handler
}

func (c *DefaultCtx) setIndexRoute(route int) {
c.indexRoute = route
}

func (c *DefaultCtx) setMatched(matched bool) {
c.matched = matched
}

return converted, nil
func (c *DefaultCtx) setRoute(route *Route) {
c.route = route
}
Loading
Loading