binding

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 11 Imported by: 0

README

binding

GoDoc Widget

binding is a go modules which assist in binding request data to struct. The source code is taken from Echo and modified to support std lib and chi.

Few things you should know
  • URL Params binding using struct tags is only supported for chi. (not std lib)
  • Echo bind both query-form and body-form in form tag. But in this version it only bind body when using form struct tag. Use query tag if you want to bind query params.
  • Echo doesn't bind query for method other than GET, HEAD and DELETE but as we not don't bind query-form in form tag, query will be binded for all methods.

Docs

Parsing request data is a crucial part of a web application. In this package this is done with a process called binding. This is done with information passed by the client in the following parts of an HTTP request:

  • URL Path parameter
  • URL Query parameter
  • Header
  • Request body

This package provides different ways to perform binding, each described in the sections below.

Struct Tag Binding

With struct binding you define a Go struct with tags specifying the data source and corresponding key. In your request handler you simply call binding.Bind(req, i interface{}) with a pointer to your struct. The tags tell the binder everything it needs to know to load data from the request.

In this example a struct type User tells the binder to bind the query string parameter id to its string field ID:

type User struct {
  ID string `query:"id"`
}

// in the handler for /users?id=<userID>
var user User
err := binding.Bind(req, &user); if err != nil {
    w.WriteHeader(http.StatusBadRequest)
    w.Write([]byte("bad request"))
}
Data Sources

This package supports the following tags specifying data sources:

  • query - query parameter
  • param - path parameter (also called route)
  • header - header parameter
  • json - request body. Uses builtin Go json package for unmarshalling.
  • xml - request body. Uses builtin Go xml package for unmarshalling.
  • form - form data. Values are request body only. Uses Go standard library form parsing.
Data Types

When decoding the request body, the following data types are supported as specified by the Content-Type header:

  • application/json
  • application/xml
  • application/x-www-form-urlencoded

When binding path parameter, query parameter, header, or form data, tags must be explicitly set on each struct field. However, JSON and XML binding is done on the struct field name if the tag is omitted. This is according to the behaviour of Go's json package.

Multiple Sources

It is possible to specify multiple sources on the same field. In this case request data is bound in this order:

  1. Path parameters
  2. Query parameters
  3. Request body
type User struct {
  ID string `param:"id" query:"id" form:"id" json:"id" xml:"id"`
}

Note that binding at each stage will overwrite data bound in a previous stage. This means if your JSON request contains the query param name=query and body {"name": "body"} then the result will be User{Name: "body"}.

Direct Source

It is also possible to bind data directly from a specific source:

Request body:

err := binding.BindBody(req, &payload)

Query parameters:

err := binding.BindQueryParams(req, &payload)

Path parameters:

err := binding.BindPathParams(req, &payload)

Header parameters:

err := binding.BindHeaders(req, &payload)

Note that headers is not one of the included sources with binding.Bind. The only way to bind header data is by calling BindHeaders directly.

Security

To keep your application secure, avoid passing bound structs directly to other methods if these structs contain fields that should not be bindable. It is advisable to have a separate struct for binding and map it explicitly to your business struct.

Consider what will happen if your bound struct has an Exported field IsAdmin bool and the request body contains {IsAdmin: true, Name: "hacker"}.

Example

In this example we define a User struct type with field tags to bind from json, form, or query request data:

type User struct {
  Name  string `json:"name" form:"name" query:"name"`
  Email string `json:"email" form:"email" query:"email"`
}

type UserDTO struct {
  Name    string
  Email   string
  IsAdmin bool
}

And a handler at the POST /users route binds request data to the struct:

mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
  u := new(User)
  if err := binding.Bind(u); err != nil {
    // 400- bad request
  }

  // Load into separate struct for security
  user := UserDTO{
    Name: u.Name,
    Email: u.Email,
    IsAdmin: false // avoids exposing field that should not be bound
  }

  executeSomeBusinessLogic(user)

 // ...
})
JSON Data
curl -X POST http://localhost:1323/users \
  -H 'Content-Type: application/json' \
  -d '{"name":"Joe","email":"joe@labstack"}'
Form Data
curl -X POST http://localhost:1323/users \
  -d 'name=Joe' \
  -d 'email=joe@labstack.com'
Query Parameters
curl -X GET 'http://localhost:1323/users?name=Joe&email=joe@labstack.com'

Fluent Binding

This package provides an interface to bind explicit data types from a specified source. It uses method chaining, also known as a Fluent Interface.

The following methods provide a handful of methods for binding to Go data type. These binders offer a fluent syntax and can be chained to configure & execute binding, and handle errors.

  • binding.QueryParamsBinder(req) - binds query parameters (source URL)
  • binding.PathParamsBinder(req) - binds path parameters (source URL)
  • binding.FormFieldBinder(req) - binds form fields body. See also Request.PostFormValue.
Error Handling

A binder is usually completed by calling BindError() or BindErrors(). If any errors have occurred, BindError() returns the first error encountered, whileBindErrors() returns all bind errors. Any errors stored in the binder are also reset.

With FailFast(true) the binder can be configured to stop binding on the first error, or with FailFast(false) execute the entire binder call chain. Fail fast is enabled by default and should be disabled when using BindErrors().

Example
// url =  "/api/search?active=true&id=1&id=2&id=3&length=25"
var opts struct {
  IDs []int64
  Active bool
}
length := int64(50) // default length is 50

// creates query params binder that stops binding at first error
err := binding.QueryParamsBinder(req).
  Int64("length", &length).
  Int64s("ids", &opts.IDs).
  Bool("active", &opts.Active).
  BindError() // returns first binding error
Supported Data Types
Data Type Notes
bool
float32
float64
int
int8
int16
int32
int64
uint
uint8/byte Does not support bytes(). Use BindUnmarshaler/CustomFunc to convert value from base64 etc to []byte{}.
uint16
uint32
uint64
string
time
duration
BindUnmarshaler() binds to a type implementing BindUnmarshaler interface
TextUnmarshaler() binds to a type implementing encoding.TextUnmarshaler interface
JsonUnmarshaler() binds to a type implementing json.Unmarshaler interface
UnixTime() converts Unix time (integer) to time.Time
UnixTimeMilli() converts Unix time with millisecond precision (integer) to time.Time
UnixTimeNano() converts Unix time with nanosecond precision (integer) to time.Time
CustomFunc() callback function for your custom conversion logic

This package supported type has the following methods:

  • <Type>("param", &destination) - if parameter value exists then binds it to given destination of that type i.e Int64(...).
  • Must<Type>("param", &destination) - parameter value is required to exist, binds it to given destination of that type i.e MustInt64(...).
  • <Type>s("param", &destination) - (for slices) if parameter values exists then binds it to given destination of that type i.e Int64s(...).
  • Must<Type>s("param", &destination) - (for slices) parameter value is required to exist, binds it to given destination of that type i.e MustInt64s(...).

For certain slice types BindWithDelimiter("param", &dest, ",") supports splitting parameter values before type conversion is done. For example binding an integer slice from the URL /api/search?id=1,2,3&id=1 will result in []int64{1,2,3,1}.

License

Copyright (c) 2024-present Anurag Kumar

Licensed under MIT License

Documentation

Index

Constants

View Source
const (

	// MIMEApplicationJSON JavaScript Object Notation (JSON) https://www.rfc-editor.org/rfc/rfc8259
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + charsetUTF8
	MIMETextXML                          = "text/xml"
	MIMETextXMLCharsetUTF8               = MIMETextXML + "; " + charsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + charsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + charsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEOctetStream                      = "application/octet-stream"
)

MIME types

View Source
const (
	HeaderAccept         = "Accept"
	HeaderAcceptEncoding = "Accept-Encoding"
	// HeaderAllow is the name of the "Allow" header field used to list the set of methods
	// advertised as supported by the target resource. Returning an Allow header is mandatory
	// for status 405 (method not found) and useful for the OPTIONS method in responses.
	// See RFC 7231: https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1
	HeaderAllow               = "Allow"
	HeaderAuthorization       = "Authorization"
	HeaderContentDisposition  = "Content-Disposition"
	HeaderContentEncoding     = "Content-Encoding"
	HeaderContentLength       = "Content-Length"
	HeaderContentType         = "Content-Type"
	HeaderCookie              = "Cookie"
	HeaderSetCookie           = "Set-Cookie"
	HeaderIfModifiedSince     = "If-Modified-Since"
	HeaderLastModified        = "Last-Modified"
	HeaderLocation            = "Location"
	HeaderRetryAfter          = "Retry-After"
	HeaderUpgrade             = "Upgrade"
	HeaderVary                = "Vary"
	HeaderWWWAuthenticate     = "WWW-Authenticate"
	HeaderXForwardedFor       = "X-Forwarded-For"
	HeaderXForwardedProto     = "X-Forwarded-Proto"
	HeaderXForwardedProtocol  = "X-Forwarded-Protocol"
	HeaderXForwardedSsl       = "X-Forwarded-Ssl"
	HeaderXUrlScheme          = "X-Url-Scheme"
	HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
	HeaderXRealIP             = "X-Real-Ip"
	HeaderXRequestID          = "X-Request-Id"
	HeaderXCorrelationID      = "X-Correlation-Id"
	HeaderXRequestedWith      = "X-Requested-With"
	HeaderServer              = "Server"
	HeaderOrigin              = "Origin"
	HeaderCacheControl        = "Cache-Control"
	HeaderConnection          = "Connection"

	// Access control
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"

	// Security
	HeaderStrictTransportSecurity         = "Strict-Transport-Security"
	HeaderXContentTypeOptions             = "X-Content-Type-Options"
	HeaderXXSSProtection                  = "X-XSS-Protection"
	HeaderXFrameOptions                   = "X-Frame-Options"
	HeaderContentSecurityPolicy           = "Content-Security-Policy"
	HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
	HeaderXCSRFToken                      = "X-CSRF-Token"
	HeaderReferrerPolicy                  = "Referrer-Policy"
)

Headers

Variables

View Source
var ErrUnsupportedMediaType = errors.New("unsupported media type")

Functions

func Bind

func Bind(r *http.Request, i interface{}) (err error)

Bind implements the `Binder#Bind` function. Binding is done in following order: 1) path params; 2) query params; 3) request body. Each step COULD override previous step binded values. For single source binding use their own methods BindBody, BindQueryParams, BindPathParams.

func BindBody

func BindBody(r *http.Request, i interface{}) (err error)

BindBody binds request body contents to bindable object NB: then binding forms take note that this implementation uses standard library form parsing which parses form data from BOTH URL and BODY if content type is not MIMEMultipartForm See non-MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseForm See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm

func BindHeaders

func BindHeaders(r *http.Request, i interface{}) error

BindHeaders binds HTTP headers to a bindable object

func BindPathParams

func BindPathParams(r *http.Request, i interface{}) error

BindPathParams binds path params to bindable object; only chi is supported

func BindQueryParams

func BindQueryParams(r *http.Request, i interface{}) error

BindQueryParams binds query params to bindable object

func NewBindingError

func NewBindingError(sourceParam string, values []string, message string, internalError error) error

NewBindingError creates new instance of binding error

Types

type BindUnmarshaler

type BindUnmarshaler interface {
	// UnmarshalParam decodes and assigns a value from an form or query param.
	UnmarshalParam(param string) error
}

BindUnmarshaler is the interface used to wrap the UnmarshalParam method. Types that don't implement this, but do implement encoding.TextUnmarshaler will use that interface instead.

type Binder

type Binder interface {
	Bind(i interface{}, r *http.Request) error
}

Binder is the interface that wraps the Bind method.

type BindingError

type BindingError struct {
	// Field is the field name where value binding failed
	Field string `json:"field"`
	// Values of parameter that failed to bind.
	Values        []string `json:"-"`
	Message       string
	InternalError error
}

BindingError represents an error that occurred while binding request data.

func (*BindingError) Error

func (be *BindingError) Error() string

Error returns error message

type ValueBinder

type ValueBinder struct {

	// ValueFunc is used to get single parameter (first) value from request
	ValueFunc func(sourceParam string) string
	// ValuesFunc is used to get all values for parameter from request. i.e. `/api/search?ids=1&ids=2`
	ValuesFunc func(sourceParam string) []string
	// ErrorFunc is used to create errors. Allows you to use your own error type, that for example marshals to your specific json response
	ErrorFunc func(sourceParam string, values []string, message string, internalError error) error
	// contains filtered or unexported fields
}

ValueBinder provides utility methods for binding query or path parameter to various Go built-in types

func ChiPathParamsBinder

func ChiPathParamsBinder(r *http.Request) *ValueBinder

PathParamsBinder creates path parameter value binder

func FormFieldBinder

func FormFieldBinder(r *http.Request) *ValueBinder

FormFieldBinder creates form field value binder For all requests, FormFieldBinder parses the raw query from the URL and uses query params as form fields

For POST, PUT, and PATCH requests, it also reads the request body, parses it as a form and uses query params as form fields. Request body parameters take precedence over URL query string values in r.Form.

NB: when binding forms take note that this implementation uses standard library form parsing which parses form data from BOTH URL and BODY if content type is not MIMEMultipartForm See https://golang.org/pkg/net/http/#Request.ParseForm

func PathParamsBinder

func PathParamsBinder(r *http.Request) *ValueBinder

PathParamsBinder creates path parameter value binder

func QueryParamsBinder

func QueryParamsBinder(r *http.Request) *ValueBinder

QueryParamsBinder creates query parameter value binder

func (*ValueBinder) BindError

func (b *ValueBinder) BindError() error

BindError returns first seen bind error and resets/empties binder errors for further calls

func (*ValueBinder) BindErrors

func (b *ValueBinder) BindErrors() []error

BindErrors returns all bind errors and resets/empties binder errors for further calls

func (*ValueBinder) BindUnmarshaler

func (b *ValueBinder) BindUnmarshaler(sourceParam string, dest BindUnmarshaler) *ValueBinder

BindUnmarshaler binds parameter to destination implementing BindUnmarshaler interface

func (*ValueBinder) BindWithDelimiter

func (b *ValueBinder) BindWithDelimiter(sourceParam string, dest interface{}, delimiter string) *ValueBinder

BindWithDelimiter binds parameter to destination by suitable conversion function. Delimiter is used before conversion to split parameter value to separate values

func (*ValueBinder) Bool

func (b *ValueBinder) Bool(sourceParam string, dest *bool) *ValueBinder

Bool binds parameter to bool variable

func (*ValueBinder) Bools

func (b *ValueBinder) Bools(sourceParam string, dest *[]bool) *ValueBinder

Bools binds parameter values to slice of bool variables

func (*ValueBinder) Byte

func (b *ValueBinder) Byte(sourceParam string, dest *byte) *ValueBinder

Byte binds parameter to byte variable

func (*ValueBinder) CustomFunc

func (b *ValueBinder) CustomFunc(sourceParam string, customFunc func(values []string) []error) *ValueBinder

CustomFunc binds parameter values with Func. Func is called only when parameter values exist.

func (*ValueBinder) Duration

func (b *ValueBinder) Duration(sourceParam string, dest *time.Duration) *ValueBinder

Duration binds parameter to time.Duration variable

func (*ValueBinder) Durations

func (b *ValueBinder) Durations(sourceParam string, dest *[]time.Duration) *ValueBinder

Durations binds parameter values to slice of time.Duration variables

func (*ValueBinder) FailFast

func (b *ValueBinder) FailFast(value bool) *ValueBinder

FailFast set internal flag to indicate if binding methods will return early (without binding) when previous bind failed NB: call this method before any other binding methods as it modifies binding methods behaviour

func (*ValueBinder) Float32

func (b *ValueBinder) Float32(sourceParam string, dest *float32) *ValueBinder

Float32 binds parameter to float32 variable

func (*ValueBinder) Float32s

func (b *ValueBinder) Float32s(sourceParam string, dest *[]float32) *ValueBinder

Float32s binds parameter values to slice of float32 variables

func (*ValueBinder) Float64

func (b *ValueBinder) Float64(sourceParam string, dest *float64) *ValueBinder

Float64 binds parameter to float64 variable

func (*ValueBinder) Float64s

func (b *ValueBinder) Float64s(sourceParam string, dest *[]float64) *ValueBinder

Float64s binds parameter values to slice of float64 variables

func (*ValueBinder) Int

func (b *ValueBinder) Int(sourceParam string, dest *int) *ValueBinder

Int binds parameter to int variable

func (*ValueBinder) Int16

func (b *ValueBinder) Int16(sourceParam string, dest *int16) *ValueBinder

Int16 binds parameter to int16 variable

func (*ValueBinder) Int16s

func (b *ValueBinder) Int16s(sourceParam string, dest *[]int16) *ValueBinder

Int16s binds parameter to slice of int16

func (*ValueBinder) Int32

func (b *ValueBinder) Int32(sourceParam string, dest *int32) *ValueBinder

Int32 binds parameter to int32 variable

func (*ValueBinder) Int32s

func (b *ValueBinder) Int32s(sourceParam string, dest *[]int32) *ValueBinder

Int32s binds parameter to slice of int32

func (*ValueBinder) Int64

func (b *ValueBinder) Int64(sourceParam string, dest *int64) *ValueBinder

Int64 binds parameter to int64 variable

func (*ValueBinder) Int64s

func (b *ValueBinder) Int64s(sourceParam string, dest *[]int64) *ValueBinder

Int64s binds parameter to slice of int64

func (*ValueBinder) Int8

func (b *ValueBinder) Int8(sourceParam string, dest *int8) *ValueBinder

Int8 binds parameter to int8 variable

func (*ValueBinder) Int8s

func (b *ValueBinder) Int8s(sourceParam string, dest *[]int8) *ValueBinder

Int8s binds parameter to slice of int8

func (*ValueBinder) Ints

func (b *ValueBinder) Ints(sourceParam string, dest *[]int) *ValueBinder

Ints binds parameter to slice of int

func (*ValueBinder) JSONUnmarshaler

func (b *ValueBinder) JSONUnmarshaler(sourceParam string, dest json.Unmarshaler) *ValueBinder

JSONUnmarshaler binds parameter to destination implementing json.Unmarshaler interface

func (*ValueBinder) MustBindUnmarshaler

func (b *ValueBinder) MustBindUnmarshaler(sourceParam string, dest BindUnmarshaler) *ValueBinder

MustBindUnmarshaler requires parameter value to exist to bind to destination implementing BindUnmarshaler interface. Returns error when value does not exist

func (*ValueBinder) MustBindWithDelimiter

func (b *ValueBinder) MustBindWithDelimiter(sourceParam string, dest interface{}, delimiter string) *ValueBinder

MustBindWithDelimiter requires parameter value to exist to bind destination by suitable conversion function. Delimiter is used before conversion to split parameter value to separate values

func (*ValueBinder) MustBool

func (b *ValueBinder) MustBool(sourceParam string, dest *bool) *ValueBinder

MustBool requires parameter value to exist to bind to bool variable. Returns error when value does not exist

func (*ValueBinder) MustBools

func (b *ValueBinder) MustBools(sourceParam string, dest *[]bool) *ValueBinder

MustBools requires parameter values to exist to bind to slice of bool variables. Returns error when values does not exist

func (*ValueBinder) MustByte

func (b *ValueBinder) MustByte(sourceParam string, dest *byte) *ValueBinder

MustByte requires parameter value to exist to bind to byte variable. Returns error when value does not exist

func (*ValueBinder) MustCustomFunc

func (b *ValueBinder) MustCustomFunc(sourceParam string, customFunc func(values []string) []error) *ValueBinder

MustCustomFunc requires parameter values to exist to bind with Func. Returns error when value does not exist.

func (*ValueBinder) MustDuration

func (b *ValueBinder) MustDuration(sourceParam string, dest *time.Duration) *ValueBinder

MustDuration requires parameter value to exist to bind to time.Duration variable. Returns error when value does not exist

func (*ValueBinder) MustDurations

func (b *ValueBinder) MustDurations(sourceParam string, dest *[]time.Duration) *ValueBinder

MustDurations requires parameter values to exist to bind to slice of time.Duration variables. Returns error when values does not exist

func (*ValueBinder) MustFloat32

func (b *ValueBinder) MustFloat32(sourceParam string, dest *float32) *ValueBinder

MustFloat32 requires parameter value to exist to bind to float32 variable. Returns error when value does not exist

func (*ValueBinder) MustFloat32s

func (b *ValueBinder) MustFloat32s(sourceParam string, dest *[]float32) *ValueBinder

MustFloat32s requires parameter values to exist to bind to slice of float32 variables. Returns error when values does not exist

func (*ValueBinder) MustFloat64

func (b *ValueBinder) MustFloat64(sourceParam string, dest *float64) *ValueBinder

MustFloat64 requires parameter value to exist to bind to float64 variable. Returns error when value does not exist

func (*ValueBinder) MustFloat64s

func (b *ValueBinder) MustFloat64s(sourceParam string, dest *[]float64) *ValueBinder

MustFloat64s requires parameter values to exist to bind to slice of float64 variables. Returns error when values does not exist

func (*ValueBinder) MustInt

func (b *ValueBinder) MustInt(sourceParam string, dest *int) *ValueBinder

MustInt requires parameter value to exist to bind to int variable. Returns error when value does not exist

func (*ValueBinder) MustInt16

func (b *ValueBinder) MustInt16(sourceParam string, dest *int16) *ValueBinder

MustInt16 requires parameter value to exist to bind to int16 variable. Returns error when value does not exist

func (*ValueBinder) MustInt16s

func (b *ValueBinder) MustInt16s(sourceParam string, dest *[]int16) *ValueBinder

MustInt16s requires parameter value to exist to bind to int16 slice variable. Returns error when value does not exist

func (*ValueBinder) MustInt32

func (b *ValueBinder) MustInt32(sourceParam string, dest *int32) *ValueBinder

MustInt32 requires parameter value to exist to bind to int32 variable. Returns error when value does not exist

func (*ValueBinder) MustInt32s

func (b *ValueBinder) MustInt32s(sourceParam string, dest *[]int32) *ValueBinder

MustInt32s requires parameter value to exist to bind to int32 slice variable. Returns error when value does not exist

func (*ValueBinder) MustInt64

func (b *ValueBinder) MustInt64(sourceParam string, dest *int64) *ValueBinder

MustInt64 requires parameter value to exist to bind to int64 variable. Returns error when value does not exist

func (*ValueBinder) MustInt64s

func (b *ValueBinder) MustInt64s(sourceParam string, dest *[]int64) *ValueBinder

MustInt64s requires parameter value to exist to bind to int64 slice variable. Returns error when value does not exist

func (*ValueBinder) MustInt8

func (b *ValueBinder) MustInt8(sourceParam string, dest *int8) *ValueBinder

MustInt8 requires parameter value to exist to bind to int8 variable. Returns error when value does not exist

func (*ValueBinder) MustInt8s

func (b *ValueBinder) MustInt8s(sourceParam string, dest *[]int8) *ValueBinder

MustInt8s requires parameter value to exist to bind to int8 slice variable. Returns error when value does not exist

func (*ValueBinder) MustInts

func (b *ValueBinder) MustInts(sourceParam string, dest *[]int) *ValueBinder

MustInts requires parameter value to exist to bind to int slice variable. Returns error when value does not exist

func (*ValueBinder) MustJSONUnmarshaler

func (b *ValueBinder) MustJSONUnmarshaler(sourceParam string, dest json.Unmarshaler) *ValueBinder

MustJSONUnmarshaler requires parameter value to exist to bind to destination implementing json.Unmarshaler interface. Returns error when value does not exist

func (*ValueBinder) MustString

func (b *ValueBinder) MustString(sourceParam string, dest *string) *ValueBinder

MustString requires parameter value to exist to bind to string variable. Returns error when value does not exist

func (*ValueBinder) MustStrings

func (b *ValueBinder) MustStrings(sourceParam string, dest *[]string) *ValueBinder

MustStrings requires parameter values to exist to bind to slice of string variables. Returns error when value does not exist

func (*ValueBinder) MustTextUnmarshaler

func (b *ValueBinder) MustTextUnmarshaler(sourceParam string, dest encoding.TextUnmarshaler) *ValueBinder

MustTextUnmarshaler requires parameter value to exist to bind to destination implementing encoding.TextUnmarshaler interface. Returns error when value does not exist

func (*ValueBinder) MustTime

func (b *ValueBinder) MustTime(sourceParam string, dest *time.Time, layout string) *ValueBinder

MustTime requires parameter value to exist to bind to time.Time variable. Returns error when value does not exist

func (*ValueBinder) MustTimes

func (b *ValueBinder) MustTimes(sourceParam string, dest *[]time.Time, layout string) *ValueBinder

MustTimes requires parameter values to exist to bind to slice of time.Time variables. Returns error when values does not exist

func (*ValueBinder) MustUint

func (b *ValueBinder) MustUint(sourceParam string, dest *uint) *ValueBinder

MustUint requires parameter value to exist to bind to uint variable. Returns error when value does not exist

func (*ValueBinder) MustUint16

func (b *ValueBinder) MustUint16(sourceParam string, dest *uint16) *ValueBinder

MustUint16 requires parameter value to exist to bind to uint16 variable. Returns error when value does not exist

func (*ValueBinder) MustUint16s

func (b *ValueBinder) MustUint16s(sourceParam string, dest *[]uint16) *ValueBinder

MustUint16s requires parameter value to exist to bind to uint16 slice variable. Returns error when value does not exist

func (*ValueBinder) MustUint32

func (b *ValueBinder) MustUint32(sourceParam string, dest *uint32) *ValueBinder

MustUint32 requires parameter value to exist to bind to uint32 variable. Returns error when value does not exist

func (*ValueBinder) MustUint32s

func (b *ValueBinder) MustUint32s(sourceParam string, dest *[]uint32) *ValueBinder

MustUint32s requires parameter value to exist to bind to uint32 slice variable. Returns error when value does not exist

func (*ValueBinder) MustUint64

func (b *ValueBinder) MustUint64(sourceParam string, dest *uint64) *ValueBinder

MustUint64 requires parameter value to exist to bind to uint64 variable. Returns error when value does not exist

func (*ValueBinder) MustUint64s

func (b *ValueBinder) MustUint64s(sourceParam string, dest *[]uint64) *ValueBinder

MustUint64s requires parameter value to exist to bind to uint64 slice variable. Returns error when value does not exist

func (*ValueBinder) MustUint8

func (b *ValueBinder) MustUint8(sourceParam string, dest *uint8) *ValueBinder

MustUint8 requires parameter value to exist to bind to uint8 variable. Returns error when value does not exist

func (*ValueBinder) MustUint8s

func (b *ValueBinder) MustUint8s(sourceParam string, dest *[]uint8) *ValueBinder

MustUint8s requires parameter value to exist to bind to uint8 slice variable. Returns error when value does not exist

func (*ValueBinder) MustUints

func (b *ValueBinder) MustUints(sourceParam string, dest *[]uint) *ValueBinder

MustUints requires parameter value to exist to bind to uint slice variable. Returns error when value does not exist

func (*ValueBinder) MustUnixTime

func (b *ValueBinder) MustUnixTime(sourceParam string, dest *time.Time) *ValueBinder

MustUnixTime requires parameter value to exist to bind to time.Duration variable (in local time corresponding to the given Unix time). Returns error when value does not exist.

Example: 1609180603 bind to 2020-12-28T18:36:43.000000000+00:00

Note:

  • time.Time{} (param is empty) and time.Unix(0,0) (param = "0") are not equal

func (*ValueBinder) MustUnixTimeMilli

func (b *ValueBinder) MustUnixTimeMilli(sourceParam string, dest *time.Time) *ValueBinder

MustUnixTimeMilli requires parameter value to exist to bind to time.Duration variable (in local time corresponding to the given Unix time in millisecond precision). Returns error when value does not exist.

Example: 1647184410140 bind to 2022-03-13T15:13:30.140000000+00:00

Note:

  • time.Time{} (param is empty) and time.Unix(0,0) (param = "0") are not equal

func (*ValueBinder) MustUnixTimeNano

func (b *ValueBinder) MustUnixTimeNano(sourceParam string, dest *time.Time) *ValueBinder

MustUnixTimeNano requires parameter value to exist to bind to time.Duration variable (in local Time corresponding to the given Unix time value in nano second precision). Returns error when value does not exist.

Example: 1609180603123456789 binds to 2020-12-28T18:36:43.123456789+00:00 Example: 1000000000 binds to 1970-01-01T00:00:01.000000000+00:00 Example: 999999999 binds to 1970-01-01T00:00:00.999999999+00:00

Note:

  • time.Time{} (param is empty) and time.Unix(0,0) (param = "0") are not equal
  • Javascript's Number type only has about 53 bits of precision (Number.MAX_SAFE_INTEGER = 9007199254740991). Compare it to 1609180603123456789 in example.

func (*ValueBinder) String

func (b *ValueBinder) String(sourceParam string, dest *string) *ValueBinder

String binds parameter to string variable

func (*ValueBinder) Strings

func (b *ValueBinder) Strings(sourceParam string, dest *[]string) *ValueBinder

Strings binds parameter values to slice of string

func (*ValueBinder) TextUnmarshaler

func (b *ValueBinder) TextUnmarshaler(sourceParam string, dest encoding.TextUnmarshaler) *ValueBinder

TextUnmarshaler binds parameter to destination implementing encoding.TextUnmarshaler interface

func (*ValueBinder) Time

func (b *ValueBinder) Time(sourceParam string, dest *time.Time, layout string) *ValueBinder

Time binds parameter to time.Time variable

func (*ValueBinder) Times

func (b *ValueBinder) Times(sourceParam string, dest *[]time.Time, layout string) *ValueBinder

Times binds parameter values to slice of time.Time variables

func (*ValueBinder) Uint

func (b *ValueBinder) Uint(sourceParam string, dest *uint) *ValueBinder

Uint binds parameter to uint variable

func (*ValueBinder) Uint16

func (b *ValueBinder) Uint16(sourceParam string, dest *uint16) *ValueBinder

Uint16 binds parameter to uint16 variable

func (*ValueBinder) Uint16s

func (b *ValueBinder) Uint16s(sourceParam string, dest *[]uint16) *ValueBinder

Uint16s binds parameter to slice of uint16

func (*ValueBinder) Uint32

func (b *ValueBinder) Uint32(sourceParam string, dest *uint32) *ValueBinder

Uint32 binds parameter to uint32 variable

func (*ValueBinder) Uint32s

func (b *ValueBinder) Uint32s(sourceParam string, dest *[]uint32) *ValueBinder

Uint32s binds parameter to slice of uint32

func (*ValueBinder) Uint64

func (b *ValueBinder) Uint64(sourceParam string, dest *uint64) *ValueBinder

Uint64 binds parameter to uint64 variable

func (*ValueBinder) Uint64s

func (b *ValueBinder) Uint64s(sourceParam string, dest *[]uint64) *ValueBinder

Uint64s binds parameter to slice of uint64

func (*ValueBinder) Uint8

func (b *ValueBinder) Uint8(sourceParam string, dest *uint8) *ValueBinder

Uint8 binds parameter to uint8 variable

func (*ValueBinder) Uint8s

func (b *ValueBinder) Uint8s(sourceParam string, dest *[]uint8) *ValueBinder

Uint8s binds parameter to slice of uint8

func (*ValueBinder) Uints

func (b *ValueBinder) Uints(sourceParam string, dest *[]uint) *ValueBinder

Uints binds parameter to slice of uint

func (*ValueBinder) UnixTime

func (b *ValueBinder) UnixTime(sourceParam string, dest *time.Time) *ValueBinder

UnixTime binds parameter to time.Time variable (in local Time corresponding to the given Unix time).

Example: 1609180603 bind to 2020-12-28T18:36:43.000000000+00:00

Note:

  • time.Time{} (param is empty) and time.Unix(0,0) (param = "0") are not equal

func (*ValueBinder) UnixTimeMilli

func (b *ValueBinder) UnixTimeMilli(sourceParam string, dest *time.Time) *ValueBinder

UnixTimeMilli binds parameter to time.Time variable (in local time corresponding to the given Unix time in millisecond precision).

Example: 1647184410140 bind to 2022-03-13T15:13:30.140000000+00:00

Note:

  • time.Time{} (param is empty) and time.Unix(0,0) (param = "0") are not equal

func (*ValueBinder) UnixTimeNano

func (b *ValueBinder) UnixTimeNano(sourceParam string, dest *time.Time) *ValueBinder

UnixTimeNano binds parameter to time.Time variable (in local time corresponding to the given Unix time in nanosecond precision).

Example: 1609180603123456789 binds to 2020-12-28T18:36:43.123456789+00:00 Example: 1000000000 binds to 1970-01-01T00:00:01.000000000+00:00 Example: 999999999 binds to 1970-01-01T00:00:00.999999999+00:00

Note:

  • time.Time{} (param is empty) and time.Unix(0,0) (param = "0") are not equal
  • Javascript's Number type only has about 53 bits of precision (Number.MAX_SAFE_INTEGER = 9007199254740991). Compare it to 1609180603123456789 in example.

Jump to

Keyboard shortcuts

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