binder

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

nolint:wrapcheck

Index

Constants

View Source
const (
	PathTag  = "path"
	QueryTag = "query"
	FormTag  = "form"

	SkipParamFieldName = "-"

	MIMEApplicationXML  = "application/xml"
	MIMETextXML         = "text/xml"
	MIMEApplicationForm = "application/x-www-form-urlencoded"
	MIMEMultipartForm   = "multipart/form-data"
	MIMEApplicationJSON = "application/json"
)

Variables

View Source
var (
	ErrDestNilPtr       = errors.New("dest must be a non-nil pointer")
	ErrDestNotStructPtr = errors.New("dest must point to a struct")
)

Functions

func BindData added in v1.8.2

func BindData(valuesMap map[string][]string, dest any, tag string) error

*

  • BindData binds data from a map of string slices into the fields of a struct pointed to by dest.
  • The struct fields are matched based on struct tags or fallback field names converted to lowerCamelCase. *
  • Parameters:
  • - valuesMap: a map of string keys to slices of strings representing input values, typically from form or query parameters.
  • - dest: a pointer to a struct instance where the data should be bound.
  • - tag: the struct tag key used to lookup field names in valuesMap (e.g., "form", "query"). *
  • Returns:
  • - error: an error if any binding or type conversion fails. *
  • Behavior:
  • - Uses reflection to walk through the struct fields.
  • - Recursively binds embedded (anonymous) structs and pointer-to-struct fields.
  • - Supports slice types and initializes them element-wise.
  • - Skips fields with tag `-` or unsettable fields.
  • - Uses helper functions to parse and assign input values. *
  • Example: *
  • var data MyStruct
  • err := BindData(r.URL.Query(), &data, "query")
  • if err != nil {
  • // handle error
  • }

func BindPath added in v1.8.2

func BindPath(r *http.Request, dest any) error

*

  • BindPath extracts path parameters from the request and binds them to the provided destination struct.
  • It supports nested and embedded struct fields using the `path` struct tag. *
  • Parameters:
  • - r: the incoming HTTP request
  • - dest: a pointer to the destination struct to bind path parameters into *
  • Returns:
  • - error: if any binding or conversion error occurs

Types

type BindUnmarshaler

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

type RequestBinder

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

func NewRequestBinder

func NewRequestBinder(validator Validator) *RequestBinder

func (*RequestBinder) Bind

func (b *RequestBinder) Bind(
	ctx context.Context,
	contentType string,
	r *http.Request,
	destType reflect.Type,
) (reflect.Value, error)

*

  • Bind takes the incoming HTTP request, extracts the necessary data based on the request method,
  • and binds it to the provided destination type. It handles query parameters, path parameters,
  • and request body binding. It also performs validation on the bound data using the validator. *
  • Parameters:
  • - ctx: the context of the request
  • - contentType: the content type of the request body
  • - r: the incoming HTTP request
  • - destType: the reflect.Type of the destination object *
  • Returns:
  • - reflect.Value: the reflect.Value of the bound data
  • - error: an error if any binding or validation fails

func (*RequestBinder) BindBody added in v1.8.2

func (b *RequestBinder) BindBody(
	contentType string,
	r *http.Request,
	dest reflect.Value,
) error

*

  • BindBody binds the request body based on the content type.
  • It supports binding for form data, JSON, and XML content types. *
  • Parameters:
  • - ctype: the content type of the request
  • - r: the http.Request object
  • - dest: the reflect.Value destination to bind the request body to *
  • Returns an error if the binding process encounters any issues.

type Validator

type Validator interface {
	Validate(value any) (bool, map[string]string)
}

Jump to

Keyboard shortcuts

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