binding

package
Version: v0.16.30 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MaxFileMemory can be used to set the maximum size, in bytes, for files to be
	// stored in memory during uploaded for multipart requests.
	// See https://golang.org/pkg/net/http/#Request.ParseMultipartForm for more
	// information on how this impacts file uploads.
	MaxFileMemory int64 = 5 * 1024 * 1024

	// BaseRequestBinder is an instance of the requestBinder, it comes with preconfigured
	// content type binders for HTML, JSON, XML and Files, as well as custom types decoders
	// for time.Time and nulls.Time
	BaseRequestBinder = NewRequestBinder(
		HTMLContentTypeBinder{
			decoder: formDecoder,
		},
		JSONContentTypeBinder{},
		XMLRequestTypeBinder{},
		FileRequestTypeBinder{
			decoder: formDecoder,
		},
	)
)

Functions

func Exec

func Exec(req *http.Request, value interface{}) error

Exec will bind the interface to the request.Body. The type of binding is dependent on the "Content-Type" for the request. If the type is "application/json" it will use "json.NewDecoder". If the type is "application/xml" it will use "xml.NewDecoder". The default binder is "https://github.com/monoculum/formam".

func Register

func Register(contentType string, fn Binder)

Register maps a request Content-Type (application/json) to a Binder.

func RegisterCustomDecoder added in v0.16.23

func RegisterCustomDecoder(fn CustomTypeDecoder, types []interface{}, fields []interface{})

RegisterCustomDecoder allows to define custom decoders for certain types In the request.

func RegisterTimeFormats

func RegisterTimeFormats(layouts ...string)

RegisterTimeFormats allows to add custom time layouts that the binder will be able to use for decoding.

Types

type Bindable added in v0.16.23

type Bindable interface {
	Bind(*http.Request) error
}

Bindable when implemented, on a type will override any Binders that have been configured when using buffalo#Context.Bind

type Binder

type Binder func(*http.Request, interface{}) error

Binder takes a request and binds it to an interface. If there is a problem it should return an error.

type ContenTypeBinder added in v0.16.23

type ContenTypeBinder interface {
	BinderFunc() Binder
	ContentTypes() []string
}

ContenTypeBinder are those capable of handling a request type like JSON or XML

type CustomTypeDecoder

type CustomTypeDecoder func([]string) (interface{}, error)

CustomTypeDecoder converts a custom type from the request into its exact type.

type File added in v0.10.3

type File struct {
	multipart.File
	*multipart.FileHeader
}

File holds information regarding an uploaded file

func (File) String added in v0.10.3

func (f File) String() string

func (File) Valid added in v0.10.3

func (f File) Valid() bool

Valid if there is an actual uploaded file

type FileRequestTypeBinder added in v0.16.23

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

FileRequestTypeBinder is in charge of binding File request types.

func (FileRequestTypeBinder) BinderFunc added in v0.16.23

func (ht FileRequestTypeBinder) BinderFunc() Binder

BinderFunc that will take care of the HTML File binding

func (FileRequestTypeBinder) ContentTypes added in v0.16.23

func (ht FileRequestTypeBinder) ContentTypes() []string

ContentTypes returns the list of content types for FileRequestTypeBinder

type HTMLContentTypeBinder added in v0.16.23

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

HTMLContentTypeBinder is in charge of binding HTML request types.

func (HTMLContentTypeBinder) BinderFunc added in v0.16.23

func (ht HTMLContentTypeBinder) BinderFunc() Binder

BinderFunc that will take care of the HTML binding

func (HTMLContentTypeBinder) ContentTypes added in v0.16.23

func (ht HTMLContentTypeBinder) ContentTypes() []string

ContentTypes that will be used to identify HTML requests

type JSONContentTypeBinder added in v0.16.23

type JSONContentTypeBinder struct{}

JSONContentTypeBinder is in charge of binding JSON request types.

func (JSONContentTypeBinder) BinderFunc added in v0.16.23

func (js JSONContentTypeBinder) BinderFunc() Binder

BinderFunc returns the Binder for this JSONRequestTypeBinder

func (JSONContentTypeBinder) ContentTypes added in v0.16.23

func (js JSONContentTypeBinder) ContentTypes() []string

ContentTypes that will be wired to this the JSON Binder

type RequestBinder added in v0.16.23

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

RequestBinder is in charge of binding multiple requests types to struct.

func NewRequestBinder added in v0.16.23

func NewRequestBinder(requestBinders ...ContenTypeBinder) *RequestBinder

NewRequestBinder creates our request binder with support for XML, JSON, HTTP and File request types.

func (*RequestBinder) Exec added in v0.16.23

func (rb *RequestBinder) Exec(req *http.Request, value interface{}) error

Exec binds a request with a passed value, depending on the content type It will look for the correct RequestTypeBinder and use it.

func (*RequestBinder) Register added in v0.16.23

func (rb *RequestBinder) Register(contentType string, fn Binder)

Register maps a request Content-Type (application/json) to a Binder.

type XMLRequestTypeBinder added in v0.16.23

type XMLRequestTypeBinder struct{}

XMLRequestTypeBinder is in charge of binding XML request types.

func (XMLRequestTypeBinder) BinderFunc added in v0.16.23

func (xm XMLRequestTypeBinder) BinderFunc() Binder

BinderFunc returns the Binder for this RequestTypeBinder

func (XMLRequestTypeBinder) ContentTypes added in v0.16.23

func (xm XMLRequestTypeBinder) ContentTypes() []string

ContentTypes that will be wired to this the XML Binder

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL