Documentation ¶
Overview ¶
Package reflection provides varied structures and functions built on the go `reflect` package, which provide a high level of meta functions useful in various circumstances.
Index ¶
- Variables
- func CanSetFor(target reflect.Type, val reflect.Value) (canSet bool, mustConvert bool)
- func CanSetForType(target, val reflect.Type) (canSet bool, mustConvert bool)
- func Convert(target reflect.Type, val reflect.Value) (reflect.Value, error)
- func FuncType(elem interface{}) (reflect.Type, error)
- func FuncValue(elem interface{}) (reflect.Value, error)
- func GetFuncArgumentsType(elem interface{}) ([]reflect.Type, error)
- func HasArgumentSize(elem interface{}, len int) bool
- func InterfaceFromValues(vals []reflect.Value) []interface{}
- func IsFuncType(elem interface{}) bool
- func IsStruct(elem interface{}) bool
- func MakeArgumentsValues(args []reflect.Type) []reflect.Value
- func MakeNew(elem interface{}) (interface{}, error)
- func MakeValueFor(t reflect.Type) reflect.Value
- func MatchElement(me interface{}, other interface{}, allowFunctions bool) bool
- func MatchFuncArgumentTypeWithValues(elem interface{}, vals []reflect.Value) int
- func MatchFunction(me interface{}, other interface{}) bool
- func MergeMap(tag string, elem interface{}, values map[string]interface{}, allowAll bool) error
- func StrictCanSetForType(target, val reflect.Type) (canSet bool, mustConvert bool)
- func StructAndEmbeddedTypeNames(elem interface{}) (FieldType, []FieldType, error)
- func StructAndEmbeddedTypes(elem interface{}) (reflect.Type, []reflect.Type, error)
- func ToMap(tag string, elem interface{}, allowNaturalNames bool) (map[string]interface{}, error)
- type Field
- type FieldType
- type Fields
- type InverseMapAdapter
- type MapAdapter
- type Mapper
- type StructMapper
- func (sm *StructMapper) AddAdapter(ty reflect.Type, adapter MapAdapter)
- func (sm *StructMapper) AddInverseAdapter(ty reflect.Type, adapter InverseMapAdapter)
- func (sm *StructMapper) HasAdapter(ty reflect.Type) bool
- func (sm *StructMapper) HasInverseAdapter(ty reflect.Type) bool
- func (sm *StructMapper) MapFrom(tag string, target interface{}) (map[string]interface{}, error)
- func (sm *StructMapper) MapTo(tag string, target interface{}, data map[string]interface{}) error
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoFieldWithTagFound = errors.New("field with tag name not found in struct")
)
errors ...
var ErrNotFunction = errors.New("Not A Function Type")
ErrNotFunction is returned when the type is not a reflect.Func.
var ErrNotStruct = errors.New("Not a struct type")
ErrNotStruct is returned when the reflect type is not a struct.
Functions ¶
func CanSetFor ¶
CanSetFor checks if the giving val can be set in the place of the target type. It returns true bool, where the first returns if the value can be used and if it must be converted into the type first.
func CanSetForType ¶
CanSetForType checks if a val reflect.Type can be used for the target type. It returns true bool, where the first returns if the value can be used and if it must be converted into the type first.
func FuncType ¶
FuncType return the Function reflect.Type of the interface provided else returns a non-nil error.
func FuncValue ¶
FuncValue return the Function reflect.Value of the interface provided else returns a non-nil error.
func GetFuncArgumentsType ¶
GetFuncArgumentsType returns the arguments type of function which should be a function type,else returns a non-nil error.
func HasArgumentSize ¶
HasArgumentSize return true/false to indicate if the function type has the size of arguments. It will return false if the interface is not a function type.
func InterfaceFromValues ¶
InterfaceFromValues returns a list of interfaces representing the concrete values within the lists of reflect.Value types.
func IsFuncType ¶
func IsFuncType(elem interface{}) bool
IsFuncType returns true/false if the interface provided is a func type.
func IsStruct ¶
func IsStruct(elem interface{}) bool
IsStruct returns true/false if the elem provided is a type of struct.
func MakeArgumentsValues ¶
MakeArgumentsValues takes a list of reflect.Types and returns a new version of those types, ensuring to dereference if it receives a pointer reflect.Type.
func MakeNew ¶
func MakeNew(elem interface{}) (interface{}, error)
MakeNew returns a new version of the giving type, returning a nonpointer type. If the type is not a struct then an error is returned.
func MakeValueFor ¶
MakeValueFor makes a new reflect.Value for the reflect.Type.
func MatchElement ¶
MatchElement attempts to validate that both element are equal in type and value.
func MatchFuncArgumentTypeWithValues ¶
MatchFuncArgumentTypeWithValues validates specific values matches the elems function arguments.
func MatchFunction ¶
func MatchFunction(me interface{}, other interface{}) bool
MatchFunction attempts to validate if giving types are functions and exactly match in arguments and returns.
func MergeMap ¶
MergeMap merges the key names of the provided map into the appropriate field place where the element has the provided tag.
func StrictCanSetForType ¶
StrictCanSetForType checks if a val reflect.Type can be used for the target type. It returns true/false if value matches the expected type and another true/false if the value can be converted to the expected type. Difference between this version and the other CanSet is that, it returns only true/false for the Assignability of the types and not based on the Assignability and convertibility.
func StructAndEmbeddedTypeNames ¶
StructAndEmbeddedTypeNames returns the name and field names of the provided elem which must be a struct.
func StructAndEmbeddedTypes ¶
StructAndEmbeddedTypes returns the type of the giving element and a slice of all composed types.
Types ¶
type Field ¶
type Field struct { Index int Name string Tag string Type reflect.Type Value reflect.Value // NameLC is the name in lower case. NameLC string }
Field defines a specific tag field with its details from a giving struct.
type FieldType ¶
FieldType defines a struct which holds details the name and package which a giving field belongs to.
type InverseMapAdapter ¶
InverseMapAdapter defines a function type which takes a Field and concrete value returning appropriate go value or an error. It does the inverse of a MapAdapter.
func TimeInverseMapper ¶
func TimeInverseMapper(layout string) InverseMapAdapter
TimeInverseMapper returns a InverseMapAdapter for time.Time values which turns incoming string values of time into Time.Time object.
type MapAdapter ¶
MapAdapter defines a function type which takes a Field returning a appropriate representation value or an error.
func TimeMapper ¶
func TimeMapper(layout string) MapAdapter
TimeMapper returns a MapAdapter which always formats time into provided layout and returns the string version of the giving time.
type Mapper ¶
type Mapper interface { MapTo(string, interface{}, map[string]interface{}) error MapFrom(string, interface{}) (map[string]interface{}, error) }
Mapper defines an interface which exposes methods to map a struct from giving tags to a map and vise-versa.
type StructMapper ¶
type StructMapper struct {
// contains filtered or unexported fields
}
StructMapper implements a struct mapping utility which allows mapping struct fields to a map and vise-versa. It uses custom adapters which if available for a giving type will handle the necessary conversion else use the default value's of those fields in the map. This means, no nil struct pointer instance should be passed for either conversion or mapping back. WARNING: StructMapper is not goroutine safe.
func NewStructMapper ¶
func NewStructMapper() *StructMapper
NewStructMapper returns a new instance of StructMapper.
func (*StructMapper) AddAdapter ¶
func (sm *StructMapper) AddAdapter(ty reflect.Type, adapter MapAdapter)
AddAdapter adds giving adapter to be responsible for giving type. It replaces any previous adapter with new adapter for type. WARNING: Ensure to use StructMapper.HasAdapter to validate if adapter exists for type.
func (*StructMapper) AddInverseAdapter ¶
func (sm *StructMapper) AddInverseAdapter(ty reflect.Type, adapter InverseMapAdapter)
AddInverseAdapter adds giving inverse adapter to be responsible for generating go type for giving reflect type. It replaces any previous inverse adapter with new inverse adapter for type. WARNING: Ensure to use StructMapper.HasAdapter to validate if adapter exists for type.
func (*StructMapper) HasAdapter ¶
func (sm *StructMapper) HasAdapter(ty reflect.Type) bool
HasAdapter returns true/false if giving type has adapter registered.
func (*StructMapper) HasInverseAdapter ¶
func (sm *StructMapper) HasInverseAdapter(ty reflect.Type) bool
HasInverseAdapter returns true/false if giving type has inverse adapter registered.
func (*StructMapper) MapFrom ¶
func (sm *StructMapper) MapFrom(tag string, target interface{}) (map[string]interface{}, error)
MapFrom returns a map which contains all values of provided struct returned as a map using giving tag name. Ensure provided type is non-nil.
func (*StructMapper) MapTo ¶
func (sm *StructMapper) MapTo(tag string, target interface{}, data map[string]interface{}) error
MapTo takes giving struct(target) and map of values which it attempts to map back into struct field types using tag. It returns error if operation fails. Ensure provided type is a pointer of giving struct type and is non-nil.