structs

package
v0.0.0-...-3518944 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2017 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoStruct = errors.New("not an struct")
	ErrNoFields = errors.New("struct has no fields")
)

Functions

func Validate

func Validate(obj interface{}, field string, args ...interface{}) error

Validate calls the validation function in obj for the given field, if any. Given a field F, its validation function must be called ValidateF. It might be either a method or a field named like that. If both exist, the method takes precedence. Validation functions must return a single argument of type error. As for the input parameters of the validation function, their types must match the types of the args parameter. However, validation functions are allowed to have a different number of arguments than the amount provided in args. If the validation function takes less arguments any additional arguments are ignored. On the other hand, if the validation function receives more arguments that len(args), they will be zero for the corresponding type (as returned by reflect.Zero).

Validate returns an error in the following three cases:

  • The function exists, but its return type is not error (or has a number of return values != 1).
  • The function exists, but its arguments don't match the types in args.
  • The function exists and when executed returns an error != nil.

func ValidationFunction

func ValidationFunction(obj interface{}, field string, args ...interface{}) (fn reflect.Value, err error)

ValidationFunction returns the validation function in obj to validate the field argument with the given arguments. See the documentation on Validate to learn more about validation functions.

Types

type Configurator

type Configurator interface {
	// Returns wheter a Struct should decompose the given struct field
	// into its fields or just use the struct as is.
	DecomposeField(s *Struct, typ reflect.Type, tag *Tag) bool
}

type Struct

type Struct struct {
	// The Struct type
	Type reflect.Type
	// Lists the mangled names of the fields, in order
	MNames []string
	// List the names of the qualified struct fields (e.g. Foo.Bar) in order
	QNames []string
	// Lists the indexes of the members (for FieldByIndex())
	Indexes [][]int
	// Field types, in order
	Types []reflect.Type
	// Field tags, in order
	Tags []*Tag
	// Maps mangled names to indexes
	MNameMap map[string]int
	// Maps qualified names to indexes
	QNameMap map[string]int
	// Lists the field indexes prefix for pointers in embedded structs
	Pointers [][]int
	// contains filtered or unexported fields
}

func New

func New(t interface{}, tags []string, conf Configurator) (*Struct, error)

func (*Struct) Embeds

func (s *Struct) Embeds(typ reflect.Type) bool

Embeds returns true iff the struct embeds the given type.

func (*Struct) Has

func (s *Struct) Has(field string, typ reflect.Type) bool

Has returns true iff the struct has a field with the given name of the given type. If field is empty, it works like Embeds.

func (*Struct) Map

func (s *Struct) Map(qname string) (string, reflect.Type, error)

Map takes a qualified struct name and returns its mangled name and type

type Tag

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

func MustParseTag

func MustParseTag(tag string) *Tag

MustParseTag works like ParseTag, but panics if there's an error.

func NewStringTagNamed

func NewStringTagNamed(tag string, name string) *Tag

func NewTag

func NewTag(field reflect.StructField, alternatives []string) *Tag

func NewTagNamed

func NewTagNamed(field reflect.StructField, name string) *Tag

func ParseTag

func ParseTag(tag string) (*Tag, error)

ParseTag parses a Gondola style struct tag field from the given tag string.

func (*Tag) Alphanumeric

func (t *Tag) Alphanumeric() bool

func (*Tag) CodecName

func (t *Tag) CodecName() string

func (*Tag) Has

func (t *Tag) Has(opt string) bool

func (*Tag) IntValue

func (t *Tag) IntValue(key string) (int, bool)

func (*Tag) IsEmpty

func (t *Tag) IsEmpty() bool

func (*Tag) Length

func (t *Tag) Length() (int, bool)

func (*Tag) MaxLength

func (t *Tag) MaxLength() (int, bool)

func (*Tag) MinLength

func (t *Tag) MinLength() (int, bool)

func (*Tag) Name

func (t *Tag) Name() string

func (*Tag) Optional

func (t *Tag) Optional() bool

func (*Tag) PipeName

func (t *Tag) PipeName() string

func (*Tag) Required

func (t *Tag) Required() bool

func (*Tag) Value

func (t *Tag) Value(key string) string

Jump to

Keyboard shortcuts

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