Documentation ¶
Overview ¶
Package structs helps with reflection and is based on the archived github.com/fatih/structs package. Reflection is used a tremendous amount in the confire package, so it makes sense to have helpers for reflection to simplify things.
Index ¶
- func IsStruct(spec interface{}) bool
- type Field
- func (f *Field) CanAddr() bool
- func (f *Field) CanSet() bool
- func (f *Field) Elem() *Field
- func (f *Field) Fields() []*Field
- func (f *Field) Init() error
- func (f *Field) InterfaceFrom(fn func(interface{}, *bool))
- func (f *Field) IsEmbedded() bool
- func (f *Field) IsExported() bool
- func (f *Field) IsNil() bool
- func (f *Field) IsZero() bool
- func (f *Field) Kind() reflect.Kind
- func (f *Field) Name() string
- func (f *Field) Pointer() interface{}
- func (f *Field) Reflect() reflect.Value
- func (f *Field) Set(val interface{}) error
- func (f *Field) Tag(key string) string
- func (f *Field) Type() reflect.Type
- func (f *Field) TypeKind() reflect.Kind
- func (f *Field) Value() interface{}
- func (f *Field) Zero() error
- type Struct
- type Zero
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a single struct field and encapsulates high-level functionality for managing and working with the field using the reflect package.
func (*Field) CanAddr ¶
CanAddr reports whether the value's pointer can be obtained with Pointer(). Such values are called addressable. A value is addressable if it is an element of a slice, an element of an addressable array, a field of an addressable struct, or the result of dereferencing a pointer. If CanAddr returns false, calling Addr will panic.
func (*Field) Elem ¶
Elem returns a Field with the value that the interface f contains or that the pointer f points to. It panics if f's Kind is not Interface or Pointer. It returns the zero Value if f is nil.
func (*Field) Fields ¶
Fields returns a slice of Fields, usually used to get the fields from a nested struct.
func (*Field) InterfaceFrom ¶
InterfaceFrom is a complex type assertion that allows you to pass in a function that performs a type assertion to the underlying field value. If the underlying value can interface and implements the specified assertion, then the value and assertion bool are set on the arguments, allowing you to "extract" an interface type from the value.
That was a bit complicated, so here is an example. Say you wanted to use the field as a encoding.TextUnmarshaler (e.g. you want to call the UnmarshalText() method of the value in the field). You would pass in a function as follows:
field.InterfaceFrom(func(v interface{}, ok *bool) { t, *ok := v.(encoding.TextUnmarshaler )})
The variable t is now a TextUnmarshaler, and you can call t.UnmarshalText() so long as the ok is true or t is not nil.
func (*Field) IsEmbedded ¶
IsEmbedded returns true if the given field is an anonymous field.
func (*Field) IsExported ¶
IsExported returns true if the given field is exported.
func (*Field) IsNil ¶
IsNil reports if the field value is nil. The value must be a chan, func, interface, map, pointer, or slice value, otherwise this method panics.
func (*Field) IsZero ¶
IsZero returns true if the given field has a zero-value. Panics if the field is not exported
func (*Field) Pointer ¶
func (f *Field) Pointer() interface{}
Pointer returns a pointer interface representing the address of f. It panics if CanAddr() returns false. This is typically used to used to obtain a pointer to a struct field or slice element in order to call a method that requires a pointer receiver.
func (*Field) Set ¶
Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.
func (*Field) Tag ¶
Tag returns the value associated with the key in the tag string. If there is no such key in the tag, an empty string is returned.
func (*Field) TypeKind ¶
Kind returns the type of the field's kind, e.g. Array, Chan, Map, Pointer, or Slice.
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct encapsulates a struct type to provide reflection around the struct.
func New ¶
New returns a wrapped struct ready for reflection. It returns an error if the spec is not a struct or a pointer to a struct.
func (*Struct) Field ¶
Field returns a field by the specified name, returning an error if not found.
func (*Struct) HasZero ¶
HasZero returns true if any field in a struct is zero-valued for their type. Primitive types such as string, bool, int have zero-values "", false, 0, etc. Collection types are generally zero-valued if they are empty or nil. Nested structs are zero-valued if they are nil (for pointers) or if they are also zero-valued, e.g. all the fields on the struct are the zero-value for their type. If the field implements the IsZero() method (e.g. such as time.Time) then it is used.
func (*Struct) IsZero ¶
IsZero returns true if all fields on the struct are a zero-value for their type. Primitive types such as string, bool, int have zero-values "", false, 0, etc. Collection types are generally zero-valued if they are empty or nil. Nested structs are zero-valued if they are nil (for pointers) or if they are also zero-valued, e.g. all the fields on the struct are the zero-value for their type. If the field implements the IsZero() method (e.g. such as time.Time) then it is used.