rv

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2016 License: MIT Imports: 11 Imported by: 2

README

rv

HTTP request parsing and validation for Go

Documentation

Overview

Package rv fills and validates values into structs from HTTP requests via reflection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseJSONBody

func ParseJSONBody(body io.Reader) (map[string]interface{}, error)

ParseJSONBody attempts to parse a JSON body from the provided io.Reader

Types

type BasicRequest

type BasicRequest struct {
	Query string
	Path  map[string]string
	Body  string
}

BasicRequest implements the Request interface and can be used for testing or parsing requests from unsupported request types.

func (*BasicRequest) BodyForm

func (r *BasicRequest) BodyForm() (url.Values, error)

BodyForm parses the Body string as a form

func (*BasicRequest) BodyJSON

func (r *BasicRequest) BodyJSON() (map[string]interface{}, error)

BodyJSON parses the Body string as JSON

func (*BasicRequest) PathArgs

func (r *BasicRequest) PathArgs() (map[string]string, error)

PathArgs returns the Path field value

func (*BasicRequest) QueryArgs

func (r *BasicRequest) QueryArgs() (url.Values, error)

QueryArgs parses the Query field

type DefaultHandler

type DefaultHandler struct {
	Default interface{}
}

func (DefaultHandler) Precidence

func (h DefaultHandler) Precidence() int

goes before TypeHandler so the default string will be transformed into the right type

func (DefaultHandler) Run

func (h DefaultHandler) Run(req Request, field *Field)

type Field

type Field struct {
	Value  interface{}
	Errors []error
}

type FieldHandler

type FieldHandler interface {
	Run(Request, *Field)
}

func NewDefaultHandler

func NewDefaultHandler(args []string) (FieldHandler, error)

func NewOptionsHandler

func NewOptionsHandler(args []string) (FieldHandler, error)

func NewRangeHandler

func NewRangeHandler(args []string) (FieldHandler, error)

func NewRequiredHandler

func NewRequiredHandler(args []string) (FieldHandler, error)

func NewSourceFieldHandler

func NewSourceFieldHandler(args []string) (FieldHandler, error)

func NewTypeHandler

func NewTypeHandler(args []string) (FieldHandler, error)

type FieldHandlerCreator

type FieldHandlerCreator func(args []string) (FieldHandler, error)

type FieldHandlers

type FieldHandlers []FieldHandler

FieldHandlers is a sortable list of FieldHandlers

func (FieldHandlers) Len

func (f FieldHandlers) Len() int

Implement sort.Interface

func (FieldHandlers) Less

func (f FieldHandlers) Less(i, j int) bool

func (FieldHandlers) Swap

func (f FieldHandlers) Swap(i, j int)

type ListHandler

type ListHandler struct {
	SubHandlers FieldHandlers
}

func (ListHandler) Run

func (h ListHandler) Run(req Request, field *Field)

type OptionsHandler

type OptionsHandler struct {
	Options map[string]struct{}
}

func (OptionsHandler) Run

func (h OptionsHandler) Run(req Request, field *Field)

type PrecidenceFieldHandler

type PrecidenceFieldHandler interface {
	FieldHandler
	Precidence() int
}

type RangeHandler

type RangeHandler struct {
	Start string
	End   string
}

func (RangeHandler) Run

func (h RangeHandler) Run(req Request, field *Field)

type Request

type Request interface {
	// QueryArgs returns the query parameters from the request URL
	QueryArgs() (url.Values, error)
	// PathArgs returns any parameters from the URL path, if supported by the request
	PathArgs() (map[string]string, error)
	// BodyJSON reads the request body and parses it as JSON
	BodyJSON() (map[string]interface{}, error)
	// BodyForm reads the request body and parses it as a form encoding
	BodyForm() (url.Values, error)
}

Request is the interface to implement to allow rv to read values from a HTTP request.

type RequestHandler

type RequestHandler struct {
	Fields map[string]FieldHandlers
	// contains filtered or unexported fields
}

RequestHandler extracts and validates values from a request based on "rv" tags on the struct fields.

func NewRequestHandler

func NewRequestHandler(requestStruct interface{}) (*RequestHandler, error)

NewRequestHandler builds a RequestHandler which will extract and validate values from a request based on the "rv" tags on the struct fields.

func (*RequestHandler) Bind

func (h *RequestHandler) Bind(req Request, container interface{}) (argErr error, fieldErrors map[string]Field)

Bind searches the container for a field matching the RequestHandler's field type, then fills it by calling RequestHandler.Run with the specified Request and the matching field.

func (*RequestHandler) Run

func (h *RequestHandler) Run(req Request, requestStruct interface{}) (argErr error, fieldErrors map[string]Field)

Run fills the provided struct with data from the request, as specified in the "rv" tags on the struct fields.

type RequiredHandler

type RequiredHandler struct {
	Required bool
}

func (RequiredHandler) Precidence

func (h RequiredHandler) Precidence() int

func (RequiredHandler) Run

func (h RequiredHandler) Run(req Request, field *Field)

type Source

type Source int
const (
	UNDEFINED Source = iota
	PATH
	QUERY
	JSON
	FORM
)

func (Source) String

func (s Source) String() string

type SourceFieldHandler

type SourceFieldHandler struct {
	Source Source
	Field  string
}

SourceFieldHandler takes source and field names from the first argument in the rv struct tag, pulls the value from the request and puts it into the field.

func (SourceFieldHandler) Precidence

func (h SourceFieldHandler) Precidence() int

func (SourceFieldHandler) Run

func (h SourceFieldHandler) Run(r Request, f *Field)

type TypeHandler

type TypeHandler struct {
	Type string
}

func (TypeHandler) Precidence

func (h TypeHandler) Precidence() int

func (TypeHandler) Run

func (h TypeHandler) Run(r Request, f *Field)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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