Documentation ¶
Overview ¶
Package binder provides request parameter binding for the web framework margo.
Basic example:
// Gin binding model. // For detailed information on model definition, // refer to https://github.com/gin-gonic/gin#model-binding-and-validation. type ExampleBodyParams struct { Message string `json:"message" binding:"required,max=500"` } func main() { app := margo.NewApplication() endpoint := binder.POST("/messages", func(c *gin.Context) margo.Response { // parsed body params can be retrieved in handler // using BodyParams method bodyParams := binder.QueryParams(c).(*ExampleBodyParams) // do something with body parameters, // for example send them back to the user via json return margo.JSON200(bodyParams) }).SetBodyParamsModel(ExampleBodyParams{}) // set the expected body params model app.Endpoint(endpoint) app.Run(":8080") }
Index ¶
- func BodyParams(context *gin.Context) interface{}
- func NewErrorResponse(err error) margo.Response
- func QueryParams(context *gin.Context) interface{}
- type Binder
- type BindingEndpoint
- func DELETE(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
- func GET(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
- func NewBindingEndpoint(method string, path string, handlers ...margo.HandlerFunc) *BindingEndpoint
- func PATCH(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
- func POST(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
- func PUT(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BodyParams ¶
BodyParams returns a pointer to the model instance bound to context by a BindingEndpoint. Returns nil if no body parameter binding was done.
func NewErrorResponse ¶
NewErrorResponse returns a margo.Response based on a binding error.
func QueryParams ¶
QueryParams returns a pointer to the model instance bound to context by a BindingEndpoint. Returns nil if no query parameter binding was done.
Types ¶
type BindingEndpoint ¶
A BindingEndpoint is a margo.Endpoint providing support for query and body parameter binding.
func DELETE ¶
func DELETE(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
DELETE returns a new DELETE BindingEndpoint for a path and at least one HandlerFunc.
func GET ¶
func GET(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
GET returns a new GET BindingEndpoint for a path and at least one HandlerFunc.
func NewBindingEndpoint ¶
func NewBindingEndpoint(method string, path string, handlers ...margo.HandlerFunc) *BindingEndpoint
NewBindingEndpoint returns a new BindingEndpoint for a given HTTP method and URL path, with at least one HandlerFunc to be executed when the Endpoint is called.
Panics if no HandlerFunc is provided.
func PATCH ¶
func PATCH(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
PATCH returns a new PATCH BindingEndpoint for a path and at least one HandlerFunc.
func POST ¶
func POST(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
POST returns a new POST BindingEndpoint for a path and at least one HandlerFunc.
func PUT ¶
func PUT(path string, handlers ...margo.HandlerFunc) *BindingEndpoint
PUT returns a new PUT BindingEndpoint for a path and at least one HandlerFunc.
func (*BindingEndpoint) Handlers ¶
func (e *BindingEndpoint) Handlers() margo.HandlerChain
func (*BindingEndpoint) SetBodyParamsModel ¶
func (e *BindingEndpoint) SetBodyParamsModel(model interface{}) *BindingEndpoint
SetBodyParamsModel sets the type to bind request body parameters into. If the model type implements Binder, the binding.Binding returned by Binding() is used when binding. For more information on model definition, refer to https://github.com/gin-gonic/gin#model-binding-and-validation.
The parsed query parameters can be retrieved from the Context in a HandlerFunc using binder.BodyParams(context).
If model is nil, query parameters are not parsed and validated. Panics if model is not a struct instance.
Returns self to allow for method chaining.
func (*BindingEndpoint) SetQueryParamsModel ¶
func (e *BindingEndpoint) SetQueryParamsModel(model interface{}) *BindingEndpoint
SetQueryParamsModel sets the type to bind request query parameters into. If the model type implements Binder, the binding.Binding returned by Binding() is used when binding. For more information on model definition, refer to https://github.com/gin-gonic/gin#model-binding-and-validation.
The parsed query parameters can be retrieved from the Context in a HandlerFunc using binder.QueryParams(context).
If model is nil, query parameters are not parsed and validated. Panics if model is not a struct instance.
Returns self to allow for method chaining.