form

package
v0.0.0-...-6b96f8f Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTagName is the tag name used for encoding / decoding if one is not specified.
	DefaultTagName = "form"

	// SkipField is the field name used to skip encode and decode of the field
	SkipField = "-"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {

	// OmitEmpty = true will omit all fields which have the empty value for their type regardless of any tags
	OmitEmpty bool
	// LowerKeys = true will ensure all value keys are lowercase regardless of any tags
	LowerKeys bool
	// TagName is the name of the tag to use if non empty, otherwise DefaultTagName is used.
	TagName string
	// contains filtered or unexported fields
}

Decoder decodes data from a url.Values

func NewDecoder

func NewDecoder(v url.Values) *Decoder

NewDecoder returns a new decoder that writes to v.

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

Decode writes the values decoded from url.Values to a struct or struct pointer

The only supported types for fields are: * int, int8, int16, int32, int64 * uint, uint8, uint16, uint32, uint64 * float32, float64 * string * bool * struct * Ptr to one of the above

Each exported field becomes a key of the Values unless:

  • the field's tag name is "-", or
  • the field is empty and its tag specifies the "omitempty" option or the decoder has OmitEmpty set to true
  • the field is a Ptr which is nil

Each exported field key will be the name of the field unless:

  • the field's tag name is not "-"
  • the field's tag specifies the "lowerkey" option or the decoder has LowerKeys set to true

The supported tag flags are: * omitempty - value is untouched if the form didn't contain a corresponding element. * required - an error is returned if the form didn't contain the corresponding element or if its value was empty string. * json - uses json.Unmarshal to decode the value, only valid for struct ptrs.

The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.

type Empty

type Empty interface {
	IsEmpty() bool
}

Empty is the interface implemented by objects which can determine if the struct is empty

type InvalidFieldError

type InvalidFieldError struct {
	Field string
	Err   error
}

InvalidFieldError indicates the data for a field was invalid.

func NewInvalidFieldError

func NewInvalidFieldError(f string, err error) *InvalidFieldError

NewInvalidFieldError returns a InvalidFieldError for the field f and error err.

func (*InvalidFieldError) Error

func (e *InvalidFieldError) Error() string

type InvalidParameterError

type InvalidParameterError struct {
	Parameter string
	Type      reflect.Type
	Required  string
	// contains filtered or unexported fields
}

InvalidParameterError indicates a parameter was passed but of an invalid type

func NewInvalidFormParameterError

func NewInvalidFormParameterError(p string, t reflect.Type, r string) *InvalidParameterError

NewInvalidFormParameterError creates the associated named error type implicitly with form source and populates it.

func NewInvalidJSONParameterError

func NewInvalidJSONParameterError(p string, t reflect.Type, r string) *InvalidParameterError

NewInvalidJSONParameterError creates the associated named error type implicitly with form source and populates it.

func (*InvalidParameterError) Error

func (e *InvalidParameterError) Error() string

type MissingFieldError

type MissingFieldError struct {
	Field string
	// contains filtered or unexported fields
}

MissingFieldError indicates a field was missing.

func NewMissingFormFieldError

func NewMissingFormFieldError(f string) *MissingFieldError

NewMissingFormFieldError returns a MissingFieldError for the field f with form source.

func NewMissingJSONFieldError

func NewMissingJSONFieldError(f string) *MissingFieldError

NewMissingJSONFieldError returns a MissingFieldError for the field f with json source.

func (*MissingFieldError) Error

func (e *MissingFieldError) Error() string

type TagOptions

type TagOptions string

TagOptions is the string following a comma in a struct field's "form" tag, or the empty string. It does not include the leading comma.

func ParseTag

func ParseTag(tag string) (string, TagOptions)

ParseTag splits a struct field's form tag into its name and comma-separated options.

func (TagOptions) Contains

func (o TagOptions) Contains(optionName string) bool

Contains reports whether a comma-separated list of options contains a particular substr flag. substr must be surrounded by a string boundary or commas.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalForm(string) error
}

Unmarshaler is the interface implemented by objects that can unmarshal themselves into valid object from a form field.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type   reflect.Type
	Source reflect.Type
	Field  string
	// contains filtered or unexported fields
}

UnsupportedTypeError indicates a struct field had a type we couldn't handle.

func NewUnsupportedFormTypeError

func NewUnsupportedFormTypeError(t reflect.Type, s reflect.Type, f string) *UnsupportedTypeError

NewUnsupportedFormTypeError returns a new UnsupportedTypeError for type t, source s and field f.

func NewUnsupportedJSONTypeError

func NewUnsupportedJSONTypeError(t reflect.Type, s reflect.Type, f string) *UnsupportedTypeError

NewUnsupportedJSONTypeError returns a new UnsupportedTypeError for type t, source s and field f.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

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