reflection

package
v0.0.0-...-df570b3 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2018 License: MIT Imports: 5 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 ErrInvalid = errors.New("invalid types, failed conditions")

ErrInvalid is returned when a type fails certain conditions.

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.

View Source
var ErrPanic = errors.New("panic occured")

ErrPanic is returned when a panic happens.

Functions

func CallFunc

func CallFunc(fn interface{}, args ...interface{}) (res []interface{}, err error)

CallFuncType attempts to call a giving function with provided arguments assuming all of them match the ff: 1. Exected number of arguments 2. Expected type of arguments It returns an error if not possible or returns the response for calling said function or if type is not a function. Note that the function call follows some rules: 1. If the function expects no arguments and some is supplied, they will be ignored.

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 ExternalTypeNames

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

ExternalTypeNames returns the name and field names of the provided elem which must be a struct, excluding all internal types.

func FuncType

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

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

func FuncValue

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

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

func GetFieldByTagAndValue

func GetFieldByTagAndValue(elem interface{}, tag string, value string) (reflect.StructField, error)

GetFieldByTagAndValue returns a giving struct field which has giving tag and value pair.

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 GetFuncReturnsType

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

GetFuncReturnsType returns the returns 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 IsSettable

func IsSettable(ref reflect.Type, elem reflect.Value) bool

IsSettable returns true/false if the element(elem) can be used for for ref type.

func IsSettableType

func IsSettableType(ref reflect.Type, elem reflect.Type) bool

IsSettableType returns true/false if the element(elem) can be used for for ref type.

func IsStrictlyAssignable

func IsStrictlyAssignable(ref reflect.Type, elem reflect.Value) bool

IsStrictlyAssignable returns true/false if the element(elem) can be used for for ref type.

func IsStrictlyAssignableType

func IsStrictlyAssignableType(ref reflect.Type, elem reflect.Type) bool

IsStrictlyAssignableType returns true/false if the element(elem) is of for ref 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 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.

func TypeAndFields

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

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

func ValidateFunc

func ValidateFunc(fn interface{}, argRules, returnRules []TypeValidation) error

ValidateFunc validates giving function arguments and returns types against TypeValidation functions.

func ValidateFuncArea

func ValidateFuncArea(fn interface{}, conditions ...AreaValidation) error

ValidateFuncArea validates giving function arguments and returns types against AreaValidation functions.

Types

type AreaValidation

type AreaValidation func(arguments []reflect.Type, returns []reflect.Type) error

AreaValidation defines a function which validates a a given set against some condition.

type Field

type Field struct {
	Index    int
	Name     string
	Tag      string
	NameLC   string
	TypeName string
	Type     reflect.Type
	Value    reflect.Value
	IsSlice  bool
	IsArray  bool
	IsMap    bool
	IsChan   bool
	IsStruct bool
}

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 GetFields

func GetFields(elem interface{}) (Fields, error)

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

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.

type TypeValidation

type TypeValidation func([]reflect.Type) error

TypeValidation defines a function which validates a a given set against some condition.

Jump to

Keyboard shortcuts

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