binder

package module
v0.0.0-...-a82edc3 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2018 License: MIT Imports: 9 Imported by: 0

README

binder

GoDoc Go Report Card

Request parameter binding for margo.

Usage 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")
}

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyParams

func BodyParams(context *gin.Context) interface{}

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

func NewErrorResponse(err error) margo.Response

NewErrorResponse returns a margo.Response based on a binding error.

func QueryParams

func QueryParams(context *gin.Context) interface{}

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 Binder

type Binder interface {
	// Binding returns the Binding to use when binding
	// request parameters into an instance of this type.
	// Binding should always return the same value.
	Binding() binding.Binding
}

type BindingEndpoint

type BindingEndpoint struct {
	margo.Endpoint
	// contains filtered or unexported fields
}

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.

Jump to

Keyboard shortcuts

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