binding

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package binding provide some common binder for binding http.Request data to strcut

Index

Constants

This section is empty.

Variables

View Source
var (
	Form   = FormBinder{FormTagName}
	Header = HeaderBinder{HeaderTagName}
	Query  = QueryBinder{QueryTagName}

	JSON = JSONBinder{}
	XML  = XMLBinder{}

	// Binders mapping
	Binders = map[string]Binder{
		"xml":    XML,
		"json":   JSON,
		"query":  Query,
		"form":   Form,
		"header": Header,
	}
)

build-in data binder

View Source
var DefaultMaxMemory int64 = 32 << 20 // 32 MB

DefaultMaxMemory for parse data form

View Source
var FormTagName = "form"

FormTagName for decode form data

View Source
var HeaderTagName = "header"

HeaderTagName for binding data

View Source
var QueryTagName = "query"

TagName for decode url.Values(form,query) data

Functions

func Auto

func Auto(r *http.Request, obj any) (err error)

Auto bind request data to a ptr value

body, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
	c.Logger().Errorf("could not read request body: %v", err)
}
c.Set("request_body", body)
// fix: can not read request body multiple times
c.Request().Body = ioutil.NopCloser(bytes.NewReader(body))

func Bind

func Bind(r *http.Request, obj any) error

Bind auto bind request data to an struct ptr

func DecodeUrlValues

func DecodeUrlValues(values map[string][]string, ptr any, tagName string) error

DecodeUrlValues data to struct

func DisableValidator

func DisableValidator()

DisableValidator for data binding

func MustBind

func MustBind(r *http.Request, obj any)

MustBind auto bind request data to an struct ptr

func Register

func Register(name string, b Binder)

Register new binder with name

func Remove

func Remove(names ...string)

Remove exists binder(s)

func ResetValidator

func ResetValidator()

ResetValidator for the package

func Validate

func Validate(obj any) error

Validate bounded data

Types

type Binder

type Binder interface {
	Name() string
	Bind(r *http.Request, obj any) error
}

Binder interface

func GetBinder

func GetBinder(name string) Binder

GetBinder get an binder by name

type BinderFunc

type BinderFunc func(r *http.Request, obj any) error

BinderFunc bind func, implement the Binder() interface

func (BinderFunc) Bind

func (fn BinderFunc) Bind(r *http.Request, obj any) error

Bind implements the Binder interface

func (BinderFunc) Name

func (fn BinderFunc) Name() string

Name implements the Binder interface

type DataValidator

type DataValidator interface {
	Validate(i any) error
}

DataValidator interface

var Validator DataValidator = &stdValidator{}

Validator for validate bounded data

type FormBinder

type FormBinder struct {
	TagName string
}

FormBinder binding Form/url.Values data to struct

func (FormBinder) Bind

func (b FormBinder) Bind(r *http.Request, ptr any) error

Bind Form data from http.Request

func (FormBinder) BindValues

func (b FormBinder) BindValues(values url.Values, ptr any) error

BindValues data from url.Values

func (FormBinder) Name

func (FormBinder) Name() string

Name get name

type HeaderBinder

type HeaderBinder struct {
	TagName string
}

HeaderBinder binding URL query data to struct

func (HeaderBinder) Bind

func (b HeaderBinder) Bind(r *http.Request, ptr any) error

Bind Header data binding

func (HeaderBinder) BindValues

func (b HeaderBinder) BindValues(headers map[string][]string, ptr any) error

BindValues data from headers

func (HeaderBinder) Name

func (HeaderBinder) Name() string

Name get name

type JSONBinder

type JSONBinder struct{}

JSONBinder binding JSON data to struct

func (JSONBinder) Bind

func (JSONBinder) Bind(r *http.Request, ptr any) error

Bind JSON data from http.Request

func (JSONBinder) BindBytes

func (JSONBinder) BindBytes(bts []byte, ptr any) error

BindBytes raw JSON data to struct

func (JSONBinder) Name

func (JSONBinder) Name() string

Name get name

type QueryBinder

type QueryBinder struct {
	TagName string
}

QueryBinder binding URL query data to struct

func (QueryBinder) Bind

func (b QueryBinder) Bind(r *http.Request, ptr any) error

Bind Query data binder

func (QueryBinder) BindValues

func (b QueryBinder) BindValues(values url.Values, ptr any) error

BindValues data from url.Values

func (QueryBinder) Name

func (QueryBinder) Name() string

Name get name

type XMLBinder

type XMLBinder struct{}

XMLBinder Xml data binder struct binding-tag default is "xml". eg: `xml:"field"`

func (XMLBinder) Bind

func (XMLBinder) Bind(r *http.Request, obj any) error

Bind XML data binder

func (XMLBinder) BindBytes

func (XMLBinder) BindBytes(bts []byte, ptr any) error

BindBytes raw JSON data to struct

func (XMLBinder) Name

func (XMLBinder) Name() string

Name get name

Jump to

Keyboard shortcuts

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