request

package
v0.2.20 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: MIT Imports: 18 Imported by: 8

Documentation

Overview

Package request implements reflection-based net/http request decoder.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJSONExpected        = errors.New("request with application/json content type expected")
	ErrMissingRequestBody  = errors.New("missing request body")
	ErrMissingRequiredFile = errors.New("missing required file")
)

These errors may be returned on request decoding failure.

Functions

func DecoderMiddleware

func DecoderMiddleware(factory DecoderMaker) func(http.Handler) http.Handler

DecoderMiddleware sets up request decoder in suitable handlers.

func ValidatorMiddleware

func ValidatorMiddleware(factory rest.RequestValidatorFactory) func(http.Handler) http.Handler

ValidatorMiddleware sets up request validator in suitable handlers.

Types

type DecoderFactory

type DecoderFactory struct {
	// ApplyDefaults enables default value assignment for fields missing explicit value in request.
	// Default value is retrieved from `default` field tag.
	ApplyDefaults bool

	// JSONReader allows custom JSON decoder for request body.
	// If not set encoding/json.Decoder is used.
	JSONReader func(rd io.Reader, v interface{}) error
	// contains filtered or unexported fields
}

DecoderFactory decodes http requests.

Please use NewDecoderFactory to create instance.

func NewDecoderFactory

func NewDecoderFactory() *DecoderFactory

NewDecoderFactory creates request decoder factory.

func (*DecoderFactory) MakeDecoder

func (df *DecoderFactory) MakeDecoder(
	method string,
	input interface{},
	customMapping rest.RequestMapping,
) nethttp.RequestDecoder

MakeDecoder creates request.RequestDecoder for a http method and request structure.

Input is checked for `json`, `file` tags only for methods with body semantics (POST, PUT, PATCH) or if input implements openapi3.RequestBodyEnforcer.

CustomMapping can be nil, otherwise it is used instead of field tags to match decoded fields with struct.

func (*DecoderFactory) RegisterFunc

func (df *DecoderFactory) RegisterFunc(fn form.DecodeFunc, types ...interface{})

RegisterFunc adds custom type handling.

func (*DecoderFactory) SetDecoderFunc

func (df *DecoderFactory) SetDecoderFunc(tagName rest.ParamIn, d func(r *http.Request) (url.Values, error))

SetDecoderFunc adds custom decoder function for values of particular field tag name.

type DecoderFunc

type DecoderFunc func(r *http.Request, input interface{}, validator rest.Validator) error

DecoderFunc implements RequestDecoder with a func.

func (DecoderFunc) Decode

func (df DecoderFunc) Decode(r *http.Request, input interface{}, validator rest.Validator) error

Decode implements RequestDecoder.

type DecoderMaker

type DecoderMaker interface {
	MakeDecoder(method string, input interface{}, customMapping rest.RequestMapping) nethttp.RequestDecoder
}

DecoderMaker creates request decoder for particular structured Go input value.

Jump to

Keyboard shortcuts

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