handler

package
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause, BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertPathParamValue

func ConvertPathParamValue[Param any](value any) (Param, bool)

ConvertPathParamValue converts a raw path parameter value, as the route macro evaluator stored it, into the type the handler declared for it.

The macro and the handler signature are chosen independently by the end-developer, and they routinely disagree on the Go type:

  • the "uuid" macro stores a uuid.UUID, while a handler declares a string id (the default id type, which MVC maps to the uuid macro),
  • an untyped "{id}" stores a string, while a handler declares uuid.UUID or int,
  • the ":int" macro stores an int, while a handler declares int64.

Without this conversion those handlers reject every request with 400, which made the whole CRUD surface unusable with a uuid.UUID id.

It reports whether the conversion succeeded. It never panics: the value is client-controlled, so an unexpected shape has to be an error, not a crash.

func Create

func Create[Context ContextType, Request RequestType, Response ResponseType](fn Func[Context, Request, Response], options ...Option) router.Controller

Create registers a POST request handler. The handler is a function that accepts a context and a request. The handler function must return a response and an optional error. The response is written to the client with a status code of 201. The error is written to the client with a call of the HandleError function using the errors subpackage.

func CreateBy

func CreateBy[Context ContextType, Param ParamType, Request RequestType, Response ResponseType](fn ParameterizedFunc[Context, Param, Request, Response], options ...Option) router.Controller

CreateBy registers a POST request handler with a path parameter. The path parameter is resolved by the last field with the `param` tag. The path parameter is used to create the route path.

func Delete

func Delete[Context ContextType, RequestOrParam RequestType | ParamType](fn Func[Context, RequestOrParam, bool], options ...Option) router.Controller

func DeleteBy

func DeleteBy[Context ContextType, Param ParamType, Request RequestType](fn ParameterizedFunc[Context, Param, Request, bool], options ...Option) router.Controller

func ExecuteRequestHandlers

func ExecuteRequestHandlers[Request RequestType](ctx *context.Context, handlers ...DirectRequestDecoder) error

ExecuteRequestHandlers calls the DecodeRequest method of each handler in the order they are passed.

func Get

func Get[Context ContextType, Param ParamType, Response ResponseType](fn Func[Context, Param, Response], options ...Option) router.Controller

Get registers a GET request handler. The handler is a function that accepts a context and returns a response.

func GetBy

func GetBy[Context ContextType, Param ParamType, Request RequestType, Response ResponseType](fn ParameterizedFunc[Context, Param, Request, Response], options ...Option) router.Controller

GetBy registers a GET request handler with a path parameter. The path parameter is resolved by the last field with the `param` tag. The path parameter is used to create the route path. The handler is a function that accepts a context, a parameter, and returns a response.

func Handler

func Handler[Context ContextType, Request RequestType, Response ResponseType](fn Func[Context, Request, Response], options ...Option) context.Handler

Handler returns a new Iris Handler which is ready to consume a request (by url and body) and send a success or fail response.

The `fn` is the function which accepts a standard context.Context and a request model and returns a response model and an error. The `opts` are the handler options which can be used to configure the handler.

If the error is not nil, it calls HandleError to send an appropriate error response to the client. If the response value is a string or int or int64 or uint64 or uuid.UUID it sends the IDPayload response as a JSON payload with the handler's status code, pass `WithStatusCode(201)` when a 201 (Created) is expected. If the response value is a boolean and true, it sets the status code to 204 (No Content). If the response value is a boolean and false, it sets the status code to 304 (Not Modified): the call succeeded but nothing changed. Otherwise it sets the status code to the handler's status code and sends the response as a JSON payload.

Usage:

app.Post("/", Handler(handler, WithStatusCode(iris.StatusMovedPermanently))

func handler(ctx context.Context, req Request) (Response, error) {
	// ...
}

func HandlerBy

func HandlerBy[Context ContextType, Param ParamType, Request RequestType, Response ResponseType](fn ParameterizedFunc[Context, Param, Request, Response], options ...Option) context.Handler

HandlerBy returns a new Iris Handler which is ready to consume two request objects and send a success or fail response. Usually the first is an object which contains the path parameters and the second one is the request body model itself. It works like `Handler` but it accepts an extra request model (Params) which can be used to bind the url path parameters.

Usage:

app.Post("/{company_id:string}/lists/{list_id:string}/objects", handler.HandlerBy(handleCreateByParams))

type Params struct {
	CompanyID string `param:"company_id"`
	ListID    string `param:"list_id"`
}

func handleCreateByParams(ctx context.Context, params Params, request Request) (string, error) {
	fmt.Printf("creating by params: %+v and request: %+v\n", params, request)
	return "generated-id", nil
}

func HandlerByColumns

func HandlerByColumns[Context ContextType, Param ParamType, Request RequestType, Response ResponseType](fn func(Context, Param, Request, ...string) (*Response, bool, error), options ...Option) context.Handler

HandlerByColumns returns a new Iris Handler which is ready to consume two request objects (one Parameter and one the Request body) and send a success or fail response. It's like the `HandlerBy` but accepts a variadic list of strings which are the columns that the client requested to be updated. The "fn" returns the response to render with the handler's status code, or nil to let the boolean decide: true renders a 204 No Content, false a 304 Not Modified, both with an empty body. A non-nil error renders through the rest/errors pipeline.

func Index

func Index[Context ContextType, Response ResponseType](fn ActionFunc[Context, Response], options ...Option) router.Controller

Index registers a handler for the root path. The handler is a function that accepts a context and returns a response. The handler function must return a response and an optional error.

func IndexHandler

func IndexHandler[Context ContextType, Response ResponseType](fn ActionFunc[Context, Response], options ...Option) context.Handler

IndexHandler returns a new Iris Handler which just returns a response.

func Job

func Job[Context ContextType, Request RequestType](fn JobFunc[Context, Request], options ...Option) router.Controller

Job registers a handler for the root path. The handler is a function that accepts a context and a request. The handler function may return an error.

func JobHandler

func JobHandler[Context ContextType, Request RequestType](fn JobFunc[Context, Request], options ...Option) context.Handler

JobHandler returns a new Iris Handler which is ready to consume a request (by url and body) and do something but doesn't return a json response to the client.

func List

func List[Context ContextType, Filter FilterType, Response ResponseType, Total CountType](fn ListFunc[Context, Filter, Response, Total], options ...Option) router.Controller

List registers a list handler on POST and QUERY /list.

func ListBy

func ListBy[Context ContextType, Param ParamType, Filter FilterType, Response ResponseType, Total CountType](fn ParameterizedListFunc[Context, Param, Filter, Response, Total], options ...Option) router.Controller

func ListByHandler

func ListByHandler[Context ContextType, Param ParamType, Filter FilterType, Response ResponseType, Total CountType](fn ParameterizedListFunc[Context, Param, Filter, Response, Total], options ...Option) context.Handler

ListByHandler same as `ListHandler` but it accepts an extra parameter which can be used to bind the extra parameters such us dynamic path or url parameters.

func ListHandler

func ListHandler[Context ContextType, Filter FilterType, Response ResponseType, Total CountType](fn ListFunc[Context, Filter, Response, Total], options ...Option) context.Handler

ListHandler returns a handler which handles a generic response and error from a service paginated call and sends a JSON response to the client. If the error is not nil, it calls errors.HandleError to send an appropriate error response to the client. It reads the PageOptions from the URL Query and any filter options of generic T from the request body. It sends a *context.Page[Response] response as a JSON payload with the current (200 OK by default) status code.

The Filter type is the type of the filter options that the client can send to the server. The Response type is the type of the response that the server will send to the client. The TotalCount type is the type of the total count of the items.

Usage:

app.Post("/list", handler.ListHandler(handleList))

func handleList(ctx context.Context, filter Filter, limit int, offset int) ([]Response, int, error) {
	fmt.Printf("listing with limit: %d offset: %d and filter: %+v\n", limit, offset, filter)
	return []Response{{Firstname: "Makis", Lastname: "Maropoulos"}}, 1, nil
}

func ListMethod

func ListMethod[Context ContextType, Filter FilterType, Response ResponseType, Total CountType](method string, fn ListFunc[Context, Filter, Response, Total], options ...Option) router.Controller

ListMethod registers a handler for the specified HTTP method(s), space separated, on "/list". The handler is a function that accepts a context, a filter, a response, and a total count.

func ListMethodBy

func ListMethodBy[Context ContextType, Param ParamType, Filter FilterType, Response ResponseType, Total CountType](method string, fn ParameterizedListFunc[Context, Param, Filter, Response, Total], options ...Option) router.Controller

ListMethodBy registers a handler for the specified HTTP method and path just like ListMethod but it accepts an extra parameter. The handler is a function that accepts a context, a parameter, a filter, a response, and a total count.

func Method

func Method[Context ContextType, Request RequestType | ParamType, Response ResponseType](method string, fn Func[Context, Request, Response], options ...Option) router.Controller

Method registers a handler for the specified HTTP method and path. The path parameter is resolved by the last field with the `param` tag. The path parameter is used to create the route path. The handler is a function that accepts a context, a request, and a response. The handler function must return a response and an optional error. The response is written to the client with a status code of 200. The error is written to the client with a call of the HandleError function using the errors subpackage. The handler function can return a custom success status code by using the `WithStatusCode` option.

func MethodBy

func MethodBy[Context ContextType, Param ParamType, Request RequestType, Response ResponseType](method string, fn ParameterizedFunc[Context, Param, Request, Response], options ...Option) router.Controller

MethodBy registers a handler for the specified HTTP method and path just like Method but it accepts an extra parameter. The handler is a function that accepts a context, a parameter, a request, and a response. The handler function must return a response and an optional error.

func ResponseHandler

func ResponseHandler[Response ResponseType](response Response, options ...Option) context.Handler

ResponseHandler returns a context.Handler that sends a response to the client.

func SetParam

func SetParam[Param any](ctx *context.Context, request Param)

SetParam sets the request for the given param type.

func SetParamDecoder

func SetParamDecoder[Param ParamType](ctx *context.Context, paramDecoder RequestDecoder[Param]) error

SetParamDecoder sets the request decoder for the given param type.

func SetRequest

func SetRequest[Request any](ctx *context.Context, request Request)

SetRequest sets the request for the given request type.

func SetRequestDecoder

func SetRequestDecoder[Request any](ctx *context.Context, requestDecoder RequestDecoder[Request]) error

SetRequestDecoder sets the request decoder for the given request type.

func Update

func Update[Context ContextType, RequestOrParam RequestType | ParamType](fn Func[Context, RequestOrParam, bool], options ...Option) router.Controller

func UpdateBy

func UpdateBy[Context ContextType, Param ParamType, Request RequestType](fn ParameterizedFunc[Context, Param, Request, bool], options ...Option) router.Controller

func UpdatePartial

func UpdatePartial[Context ContextType, ID IDType, Request RequestType, Response ResponseType](fn func(Context, ID, Request, ...string) (*Response, bool, error), options ...Option) router.Controller

Types

type ActionFunc

type ActionFunc[Context ContextType, Response ResponseType] func(ctx Context) (Response, error)

ActionFunc is a function which takes a context and returns a generic type Response and an error. It is used to call a service function without a request payload.

type ContextType

type ContextType interface {
	stdContext.Context
}

ContextType is a type which can be used as a context.Context or iris.Context.

CONSTRAINT: instantiate this with EXACTLY one of two types:

*iris.Context        (github.com/kataras/iris/v14/context.Context)
context.Context      (the standard library interface)

The Go constraint cannot express that, so it permits more than the implementation supports. `getCtx` builds the value with an unsafe reinterpretation rather than a type assertion, which is only sound for those two shapes:

  • A DIFFERENT interface, e.g.

    type TenantContext interface { context.Context TenantID() string }

    compiles and is accepted here, but its itab has a different method set, so calling TenantID dispatches to the wrong slot.

  • A concrete struct implementing context.Context is read as if it were a 16-byte interface header.

Neither is diagnosed at build time. Use one of the two supported types.

type CountType

type CountType = context.CountType

CountType is a type that can be used as a total count of the items in a list.

type DirectRequestDecoder

type DirectRequestDecoder interface {
	DecodeRequest(*context.Context) error
}

type FilterType

type FilterType any

FilterType is a type which can be used as a filter options for a list. It should be a struct.

type Func

type Func[Context ContextType, Request RequestType, Response ResponseType] func(ctx Context, req Request) (Response, error)

Func is a function which takes a context and a generic type Request and returns a generic type Response and an error. It is used to bind a request payload to a generic type Request and call a service function with it.

type IDType

type IDType = context.IDType

IDType is a type which can be used as an unique identifier for a resource. It can be a string or int or int64 or uint64 or the standard library's uuid.UUID.

type JobFunc

type JobFunc[Context ContextType, Request RequestType] func(ctx Context, req Request) error

JobFunc is a function which takes a context and a generic type Request and returns an error. It is used to bind a request payload to a generic type Request and call a service function with it. No response is sent to the client if error is nil.

type ListFunc

type ListFunc[Context ContextType, Filter FilterType, Response ResponseType, Total CountType] func(ctx Context, filter Filter, limit int, offset int) ([]Response, Total, error)

ListFunc is a function which takes a context, a generic type Filter, limit and offset values and returns a slice []Response, total count of the items and an error.

It's used on the List function.

type Option

type Option = func(*Options)
var (
	// NoOption is a special option which can be used to skip the default options.
	NoOption Option = func(opts *Options) {
		opts.skipDefaults = true
		opts.appendRequestOptions(noOption)
	}
)

func WithMiddleware

func WithMiddleware(handlers ...context.Handler) Option

WithMiddleware returns an option which registers the given handlers as middleware.

func WithOptions

func WithOptions(prepared *Options) Option

WithOptions returns an option which applies a prepared Options value, built through `NewOptions` and its chainable methods, onto the target options. It appends the prepared middleware and request options and carries the skip-defaults sentinel (see `NoOption`) over.

func WithPagination

func WithPagination(paginationOptions ...context.PaginationOption) Option

WithPagination returns an option which sets the sort policy of a list handler, the options `Context.BindPagination` takes (see context.AllowSort and context.DenySort). A sort the policy does not allow answers 400 before the handler's function runs.

func WithParamDecoder

func WithParamDecoder[Param any](paramDecoder RequestDecoder[Param]) Option

WithParamDecoder returns an option which sets the request decoder for the given param type.

func WithParamDecoderFunc

func WithParamDecoderFunc[Param ParamType](fn RequestDecoderFunc[Param]) Option

WithParamDecoderFunc returns an option which sets the request decoder for the given param type.

func WithPathDecoder

func WithPathDecoder[Param any](paramName string) Option

WithPathDecoder returns an option which sets the path parameter decoder for the given param type. If the Param type is a struct, it will decode the path parameters into the struct fields. If the Param type is a single parameter, of string or int or int64 or uint64 or uuid.UUID, it will decode the path parameter into the parameter.

func WithPathParameter

func WithPathParameter[Param any](paramName string) Option

WithPathParameter returns an option which sets the path parameter decoder for the given param type.

func WithRequestDecoder

func WithRequestDecoder[Request any](requestDecoder RequestDecoder[Request]) Option

WithRequestDecoder returns an option which sets the request decoder for the given request type.

func WithRequestDecoderFunc

func WithRequestDecoderFunc[Request RequestType](fn RequestDecoderFunc[Request]) Option

WithRequestDecoderFunc returns an option which sets the request decoder for the given request type.

func WithRequestOptions

func WithRequestOptions(options ...RequestOption) Option

func WithStatusCode

func WithStatusCode(statusCode int) Option

WithStatusCode returns an option which sets the status code of the response.

type Options

type Options struct {
	// contains filtered or unexported fields
}

func NewOptions

func NewOptions() *Options

NewOptions returns a new, empty Options value, ready to be filled through the chainable With* methods. Pass the prepared result to a handler through the `WithOptions` option:

opts := handler.NewOptions().WithStatusCode(201).WithPathParameter[string]("id")
handler.Handler(fn, handler.WithOptions(opts))

func (*Options) Pagination

func (opts *Options) Pagination() []context.PaginationOption

Pagination returns the sort policy options registered through `WithPagination`. The list handlers pass them to `Context.BindPagination`.

func (*Options) SetDefaultRequestOptions

func (opts *Options) SetDefaultRequestOptions(options ...RequestOption)

SetDefaultRequestOptions sets the default request options for the handler. It does nothing when NoOption was applied.

func (*Options) WithMiddleware

func (opts *Options) WithMiddleware(handlers ...context.Handler) *Options

WithMiddleware is the chainable form of the package-level `WithMiddleware` option.

func (*Options) WithPagination

func (opts *Options) WithPagination(paginationOptions ...context.PaginationOption) *Options

WithPagination is the chainable form of the package-level `WithPagination` option.

func (*Options) WithParamDecoder

func (opts *Options) WithParamDecoder[Param any](paramDecoder RequestDecoder[Param]) *Options

WithParamDecoder is the chainable form of the package-level `WithParamDecoder` option.

func (*Options) WithParamDecoderFunc

func (opts *Options) WithParamDecoderFunc[Param ParamType](fn RequestDecoderFunc[Param]) *Options

WithParamDecoderFunc is the chainable form of the package-level `WithParamDecoderFunc` option.

func (*Options) WithPathDecoder

func (opts *Options) WithPathDecoder[Param any](paramName string) *Options

WithPathDecoder is the chainable form of the package-level `WithPathDecoder` option.

func (*Options) WithPathParameter

func (opts *Options) WithPathParameter[Param any](paramName string) *Options

WithPathParameter is the chainable form of the package-level `WithPathParameter` option.

func (*Options) WithRequestDecoder

func (opts *Options) WithRequestDecoder[Request any](requestDecoder RequestDecoder[Request]) *Options

WithRequestDecoder is the chainable form of the package-level `WithRequestDecoder` option.

func (*Options) WithRequestDecoderFunc

func (opts *Options) WithRequestDecoderFunc[Request RequestType](fn RequestDecoderFunc[Request]) *Options

WithRequestDecoderFunc is the chainable form of the package-level `WithRequestDecoderFunc` option.

func (*Options) WithStatusCode

func (opts *Options) WithStatusCode(statusCode int) *Options

WithStatusCode is the chainable form of the package-level `WithStatusCode` option.

type ParamType

type ParamType interface {
	any | IDType // actually it's just an any type but we use IDType to show the usage.
}

ParamType is a type which can be used as an extra parameter for a service function, e.g. path parameters and/or headers and/or url query parameters.

type ParameterizedFunc

type ParameterizedFunc[Context ContextType, Param ParamType, Request RequestType, Response ResponseType] func(ctx Context, param Param, req Request) (Response, error)

ParameterizedFunc is a function which takes a context, an ID and a generic type Request and returns a boolean and an error. It is used to bind a request payload to a generic type Request and call a service function with it. It is used for Update operations.

type ParameterizedListFunc

type ParameterizedListFunc[Context ContextType, Param ParamType, Filter FilterType, Response ResponseType, Total CountType] func(ctx Context, param Param, filter Filter, limit int, offset int) ([]Response, Total, error)

ParameterizedListFunc is a function which takes a context, a parameter, a generic type Filter, limit and offset values and returns a slice []Response, total count of the items and an error. It is used for ListHandlerBy operations.

type PathParamDecoder

type PathParamDecoder[Param ParamType] struct {
	// contains filtered or unexported fields
}

PathParamDecoder is a RequestDecoder which reads a path parameter from the context and returns it as a generic type Request.

func NewPathParamDecoder

func NewPathParamDecoder[Param ParamType](paramName string) *PathParamDecoder[Param]

NewPathParamDecoder returns a RequestDecoder which reads a path parameter from the context and returns it as a generic type Request. If paramName is empty then it reads the last path parameter from the context. It is used for XHandler functions.

func (*PathParamDecoder[Param]) DecodeRequest

func (p *PathParamDecoder[Param]) DecodeRequest(ctx *context.Context, req *Param) error

DecodeRequest reads a path parameter from the context and returns it as a generic type Request. It implements the RequestDecoder interface.

func (*PathParamDecoder[Param]) String

func (p *PathParamDecoder[Param]) String() string

String returns the name of the path parameter.

type Renderer

type Renderer = context.Renderer

Renderer is the interface that wraps the Render method. It is used to render a response to the client. If a Response value implements it, this will be called instead of the default JSON encoding.

type RequestDecoder

type RequestDecoder[RequestOrParam any] interface {
	DecodeRequest(ctx *context.Context, req *RequestOrParam) error
}

RequestDecoder is an interface which contains a function which takes a context and binds a generic type Request. It is used to call a service function with a generic type Request. It is used for functions which do not bind a request payload. It is used for XHandler functions. Developers can design their own RequestDecoder and use them with the XHandler functions.

See the NewPathParamDecoder, NewQueryDecoder and NewValueDecoder package-level helpers too.

func DefaultParamDecoder

func DefaultParamDecoder[Param any]() RequestDecoder[Param]

DefaultParamDecoder returns a RequestDecoder which reads a Single path parameter (string, int, int64, uint64 or uuid.UUID type) or URL path parameters and/or (param field tag) Context values (context field tag) Headers (header field tag) URL query and/or (url field tag) from the context and returns it as a generic type Request.

func DefaultRequestDecoder

func DefaultRequestDecoder[Request any]() RequestDecoder[Request]

DefaultRequestDecoder returns a RequestDecoder which reads a Single path parameter (string, int, int64, uint64 or uuid.UUID type) or URL path parameters and/or (param field tag) Context values (context field tag) Headers (header field tag) URL query and/or (url field tag) Form data and/or (form field tag) JSON payload (json field tag) from the context and returns it as a generic type Request.

func NewFormDecoder

func NewFormDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewFormDecoder returns a RequestDecoder which reads the form data from the context and returns it as a generic type Request.

func NewHeaderDecoder

func NewHeaderDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewHeaderDecoder returns a RequestDecoder which reads headers values from the context and returns it as a generic type Request.

func NewJSONDecoder

func NewJSONDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewJSONDecoder returns a RequestDecoder which reads a JSON payload from the context and returns it as a generic type Request.

func NewPathParamsDecoder

func NewPathParamsDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewPathParamsDecoder returns a RequestDecoder which reads one or more path parameters from the context and returns it as a generic type Request.

func NewQueryDecoder

func NewQueryDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewQueryDecoder returns a RequestDecoder which reads a URL query from the context and returns it as a generic type Request.

func NewURLDecoder

func NewURLDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewURLDecoder returns a RequestDecoder which reads URL values from the context and returns it as a generic type Request.

func NewUserDecoder

func NewUserDecoder() RequestDecoder[context.User]

NewUserDecoder returns a RequestDecoder which reads the user from the context and returns it as a generic type Request.

func NewValueDecoder

func NewValueDecoder[RequestOrParam any](value RequestOrParam) RequestDecoder[RequestOrParam]

NewValueDecoder returns a RequestDecoder which returns a generic type Request. It is used for XHandler functions.

func NewZeroValueDecoder

func NewZeroValueDecoder[RequestOrParam any]() RequestDecoder[RequestOrParam]

NewZeroValueDecoder returns a RequestDecoder which returns a zero value of a generic type Request.

func Transformer

func Transformer[From any, To RequestType](fn func(From) (To, error)) RequestDecoder[To]

Transformer returns a RequestDecoder which reads a generic type From from the context, transforms it to a generic type To and returns it.

type RequestDecoderFunc

type RequestDecoderFunc[RequestOrParam any] func(ctx *context.Context, req *RequestOrParam) error

RequestDecoderFunc is a function which takes a context and returns a generic type Request. Implements the RequestDecoder interface.

func (RequestDecoderFunc[Request]) DecodeRequest

func (fn RequestDecoderFunc[Request]) DecodeRequest(ctx *context.Context, req *Request) error

DecodeRequest calls the RequestDecoderFunc.

type RequestOption

type RequestOption = func(*context.Context) error

RequestOption is a function which can be used to modify the request context. It can be used to set the request, path parameters, status code, etc. It can also be used to skip the default options. The function should return an error if the option fails to apply. If the error is not nil, the request will be stopped and the error will be handled.

type RequestType

type RequestType any

RequestType is a type which can be used as a request payload. It should be a struct.

type ResponseType

type ResponseType any

ResponseType is a type which can be used as a response payload. It should be a struct.

type RouteParam

type RouteParam[T any] struct {
	Name string
	Type string
}

RouteParam is a helper struct that contains the name and type of a path parameter.

func ResolveRouteParam

func ResolveRouteParam[T ParamType](relPath string) (*RouteParam[T], bool)

ResolveRouteParam resolves the route parameter of T for the given relative path, reporting whether one was found.

The only good idea I thought to resolve the parameter single or multi by struct, is to have a rule that the last field with param tag is the current one that should be used here, its name and its type. If it's just a single IDType, then the name is generated.

func (*RouteParam[T]) Path

func (id *RouteParam[T]) Path() string

Path returns the route path of the path parameter as a string.

func (*RouteParam[T]) Value

func (id *RouteParam[T]) Value(ctx *context.Context) T

Value returns the request value of the path parameter from the request.

type ViewBuilder

type ViewBuilder struct {
	// contains filtered or unexported fields
}

ViewBuilder is the view builder. It's used to build a view. It's used by the `View` package-level function.

func View

func View() *ViewBuilder

View returns a new View Builder. It can be used as a Response value.

func (*ViewBuilder) Data

func (v *ViewBuilder) Data(data any) *ViewBuilder

Data sets the model data for the view.

func (*ViewBuilder) Layout

func (v *ViewBuilder) Layout(layout string) *ViewBuilder

Layout sets the layout for the view.

func (*ViewBuilder) Name

func (v *ViewBuilder) Name(name string) *ViewBuilder

Name sets the name for the view.

func (*ViewBuilder) Render

func (v *ViewBuilder) Render(ctx *context.Context) error

Render renders the view. It writes the response headers and body. It completes the `Renderer` interface.

func (*ViewBuilder) StatusCode

func (v *ViewBuilder) StatusCode(code int) *ViewBuilder

StatusCode sets the status code for the response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL