binder

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package binder is used to bind a value to the http request.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindFunc added in v0.31.0

type BindFunc func(dst interface{}, req *http.Request) error

BindFunc is a function type implementing the interface Binder.

func (BindFunc) Bind added in v0.31.0

func (f BindFunc) Bind(dst interface{}, req *http.Request) error

Bind implements the interface Binder.

type Binder

type Binder interface {
	// Bind parses the data from http.Request to dst.
	//
	// Notice: dst must be a non-nil pointer.
	Bind(dst interface{}, req *http.Request) error
}

Binder is used to bind the data to the http request.

var (
	DefaultMuxBinder = NewMuxBinder()

	DefaultQueryBinder Binder = BindFunc(func(dst interface{}, r *http.Request) error {
		return binder.BindStructToURLValues(dst, "query", r.URL.Query())
	})

	DefaultHeaderBinder Binder = BindFunc(func(dst interface{}, r *http.Request) error {
		return binder.BindStructToHTTPHeader(dst, "header", r.Header)
	})

	DefaultValidateFunc = func(v interface{}, r *http.Request) error {
		return defaults.ValidateStruct(v)
	}

	BodyBinder   Binder = WrapBinder(DefaultMuxBinder, DefaultValidateFunc)
	QueryBinder  Binder = WrapBinder(DefaultQueryBinder, DefaultValidateFunc)
	HeaderBinder Binder = WrapBinder(DefaultHeaderBinder, DefaultValidateFunc)
)

Predefine some binder to bind the body, query and header of the request.

func FormBinder

func FormBinder(maxMemory int64) Binder

FormBinder returns a binder to bind the data to the request body as Form, which supports the struct tag "form".

func JSONBinder

func JSONBinder() Binder

JSONBinder returns a binder to bind the data to the request body as JSON.

func WrapBinder added in v0.18.0

func WrapBinder(binder Binder, nextHandler func(interface{}, *http.Request) error) Binder

WrapBinder wraps the binder and returns a new one that goes to handle the result after binding the request.

func XMLBinder

func XMLBinder() Binder

XMLBinder returns a binder to bind the data to the request body as XML.

type MuxBinder

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

MuxBinder is a multiplexer for kinds of Binders based on the request header "Content-Type".

func NewMuxBinder

func NewMuxBinder() *MuxBinder

NewMuxBinder returns a new MuxBinder.

func (*MuxBinder) Add

func (mb *MuxBinder) Add(contentType string, binder Binder)

Add adds a binder to bind the content for the header "Content-Type".

func (*MuxBinder) Bind

func (mb *MuxBinder) Bind(dst interface{}, req *http.Request) error

Bind implements the interface Binder, which looks up the registered binder by the request header "Content-Type" and calls it to bind the value dst to req.

func (*MuxBinder) Del

func (mb *MuxBinder) Del(contentType string)

Del removes the corresponding binder by the header "Content-Type".

func (*MuxBinder) Get

func (mb *MuxBinder) Get(contentType string) Binder

Get returns the corresponding binder by the header "Content-Type".

Return nil if not found.

Jump to

Keyboard shortcuts

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