reflection

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

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

Go to latest
Published: Jan 16, 2018 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	ErrNoFieldWithTagFound = errors.New("field with tag name not found in struct")
)

errors ...

View Source
var ErrNotFunction = errors.New("Not A Function Type")

ErrNotFunction is returned when the type is not a reflect.Func.

View Source
var ErrNotStruct = errors.New("Not a struct type")

ErrNotStruct is returned when the reflect type is not a struct.

Functions

func CanSetFor

func CanSetFor(target reflect.Type, val reflect.Value) (canSet bool, mustConvert bool)

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

func CanSetForType(target, val reflect.Type) (canSet bool, mustConvert bool)

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 Convert

func Convert(target reflect.Type, val reflect.Value) (reflect.Value, error)

Convert takes a val and converts it into the target type provided if possible.

func FuncType

func FuncType(elem interface{}) (reflect.Type, error)

FuncType return the Function reflect.Type of the interface provided else returns a non-nil error.

func FuncValue

func FuncValue(elem interface{}) (reflect.Value, error)

FuncValue return the Function reflect.Value of the interface provided else returns a non-nil error.

func GetFuncArgumentsType

func GetFuncArgumentsType(elem interface{}) ([]reflect.Type, error)

GetFuncArgumentsType returns the arguments type of function which should be a function type,else returns a non-nil error.

func HasArgumentSize

func HasArgumentSize(elem interface{}, len int) bool

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

func InterfaceFromValues(vals []reflect.Value) []interface{}

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

func MakeArgumentsValues(args []reflect.Type) []reflect.Value

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

func MakeValueFor(t reflect.Type) reflect.Value

MakeValueFor makes a new reflect.Value for the reflect.Type.

func MatchElement

func MatchElement(me interface{}, other interface{}, allowFunctions bool) bool

MatchElement attempts to validate that both element are equal in type and value.

func MatchFuncArgumentTypeWithValues

func MatchFuncArgumentTypeWithValues(elem interface{}, vals []reflect.Value) int

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

func MergeMap(tag string, elem interface{}, values map[string]interface{}, allowAll bool) error

MergeMap merges the key names of the provided map into the appropriate field place where the element has the provided tag.

func StrictCanSetForType

func StrictCanSetForType(target, val reflect.Type) (canSet bool, mustConvert bool)

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

func StructAndEmbeddedTypeNames(elem interface{}) (FieldType, []FieldType, error)

StructAndEmbeddedTypeNames returns the name and field names of the provided elem which must be a struct.

func StructAndEmbeddedTypes

func StructAndEmbeddedTypes(elem interface{}) (reflect.Type, []reflect.Type, error)

StructAndEmbeddedTypes returns the type of the giving element and a slice of all composed types.

func ToMap

func ToMap(tag string, elem interface{}, allowNaturalNames bool) (map[string]interface{}, error)

ToMap returns a map of the giving values from a struct using a provided tag to capture the needed values, it extracts those tags values out into a map. It returns an error if the element is not a struct.

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

type FieldType struct {
	TypeName string `json:"field_type"`
	Pkg      string `json:"pkg"`
}

FieldType defines a struct which holds details the name and package which a giving field belongs to.

type Fields

type Fields []Field

Fields defines a lists of Field instances.

func GetTagFields

func GetTagFields(elem interface{}, tag string, allowNaturalNames bool) (Fields, error)

GetTagFields retrieves all fields of the giving elements with the giving tag type.

type InverseMapAdapter

type InverseMapAdapter func(Field, interface{}) (interface{}, error)

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

type MapAdapter func(Field) (interface{}, error)

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.

Jump to

Keyboard shortcuts

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