Documentation
¶
Overview ¶
Package reflectutil contains various utilities that are useful when working with reflection
Index ¶
- func AutoPointerNewFromType(typ reflect.Type) interface{}
- func AutoPointerNewFromValue(val reflect.Value) interface{}
- func Clone(obj interface{}) interface{}
- func CloneValue(val reflect.Value) reflect.Value
- func Copy(from interface{}, to interface{}) interface{}
- func CopyValueToModel(modelType reflect.Type, modelValue reflect.Value, newModel interface{})
- func EnsureSinglePointer(obj interface{}) interface{}
- func EnsureSinglePointerType(typ reflect.Type) reflect.Type
- func EnsureSinglePointerValue(val reflect.Value) reflect.Value
- func FuncNameOf(fn interface{}) string
- func GetFieldByName(obj interface{}, name string) interface{}
- func GetModelName(model interface{}) string
- func NameOf(anything interface{}) (str string)
- func NewFromModel(model interface{}) interface{}
- func NewFromType(modelType reflect.Type) interface{}
- func NewPtrValueFromType(modelType reflect.Type) reflect.Value
- func NewSlice(model interface{}) interface{}
- func NewSliceOfType(typ reflect.Type) interface{}
- func NewSliceOfTypeWithSize(typ reflect.Type, len int, cap int) interface{}
- func NewSliceWithSize(model interface{}, len int, cap int) interface{}
- func PurifyToType(model interface{}) reflect.Type
- func PurifyType(typ reflect.Type) reflect.Type
- func SetFieldByName(obj interface{}, name string, fieldObj interface{}) bool
- func SetSlice(valueSlice []interface{}, fieldValue reflect.Value)
- func Unwrap(obj interface{}) interface{}
- func UnwrapType(typ reflect.Type) (unwrapped reflect.Type)
- func UnwrapValue(val reflect.Value) (unwrapped reflect.Value)
- func Wrap(obj interface{}) interface{}
- func WrapValue(val reflect.Value) reflect.Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoPointerNewFromType ¶ added in v0.5.1
AutoPointerNewFromType creates a new instance of `typ` and makes sure that the instance is a pointer and that it points to the new value (and not another pointer)
func AutoPointerNewFromValue ¶ added in v0.5.1
AutoPointerNewFromType creates a new instance of the type of `val` and makes sure that the instance is a pointer and that it points to the new value (and not another pointer)
func CopyValueToModel ¶ added in v0.10.0
func EnsureSinglePointer ¶ added in v0.5.1
func EnsureSinglePointer(obj interface{}) interface{}
EnsureSinglePointer makes sure that `obj` is a pointer and points to a value. If `obj` is not a pointer, it will be wrapped into one. If what `obj` points to is also a pointer, it will be unwrapped until it points to a value.
func EnsureSinglePointerType ¶ added in v0.5.1
EnsureSinglePointerValue makes sure that the kind of `typ` is a pointer and points to a value. If the kind of `typ` is not a pointer, it will be wrapped into one. If the element kind of `typ` is also a pointer, it will be unwrapped until its element's kind is a value.
func EnsureSinglePointerValue ¶ added in v0.5.1
EnsureSinglePointerValue makes sure that the kind of `val` is a pointer and points to a value. If the kind of `val` is not a pointer, it will be wrapped into one. If the element kind of `val` is also a pointer, it will be unwrapped until its element's kind is a value.
func FuncNameOf ¶ added in v0.6.2
func FuncNameOf(fn interface{}) string
FuncNameOf returns the name of the function `fn`
func GetFieldByName ¶ added in v0.10.0
func GetFieldByName(obj interface{}, name string) interface{}
func GetModelName ¶
func GetModelName(model interface{}) string
GetModelName returns the struct name of `model` whereas model can be either an instance of a struct or reflect.Type. This function also does some additional checks and unwrapping to account for pointers, slices, etc.
func NameOf ¶ added in v0.6.2
func NameOf(anything interface{}) (str string)
NameOf returns the name of whatever is passed as `anything`
func NewFromModel ¶ added in v0.5.1
func NewFromModel(model interface{}) interface{}
NewFromModel creates a new instance of the same type as `model` and returns a pointer to the new instance
func NewFromType ¶
NewFromType creates a new instance of the given type and returns a pointer to the new instance
func NewPtrValueFromType ¶ added in v0.5.1
NewPtrValueFromType creates a new instance of the given type and returns a reflect.Value of a pointer to the new instance
func NewSlice ¶ added in v0.5.1
func NewSlice(model interface{}) interface{}
NewSlice creates a new slice of capacity 1 that has the same type as `model`
func NewSliceOfType ¶ added in v0.6.0
NewSliceOfType creates a new slice of capacity 1 of type `typ`
func NewSliceOfTypeWithSize ¶ added in v0.6.0
NewSliceOfTypeWithSize creates a new slice of length `len` and capacity `cap` of type `typ`. `cap` should be equal or greater than `len`.
func NewSliceWithSize ¶ added in v0.6.0
NewSliceWithSize creates a new slice of length `len` and capacity `cap` that has the same type as `model`. `cap` should be equal or greater than `len`.
func PurifyToType ¶ added in v0.6.0
PurifyType dereferences all pointers, slices, maps, channels and arrays, effectively returning the pure type at the end of the chain. This means that the type `**[]*string` will be purified to `string` only.
func PurifyType ¶ added in v0.6.0
PurifyType dereferences all pointers, slices, maps, channels and arrays, effectively returning the pure type at the end of the chain. This means that the type `**[]*string` will be purified to `string` only.
func SetFieldByName ¶ added in v0.10.0
func SetSlice ¶
SetSlice decodes all elements of `valueSlice` and puts them into a new slice which is then assigned to `fieldValue`. The expected `valueSlice` slice type is map[string]<anything>
func Unwrap ¶ added in v0.4.0
func Unwrap(obj interface{}) interface{}
Unwrap dereferences all pointers of `obj` and returns the actual dereferenced value
func UnwrapType ¶ added in v0.3.1
UnwrapType dereferences all pointers of `typ` and returns the actual dereferenced type
func UnwrapValue ¶ added in v0.3.1
UnwrapValue dereferences all pointers of `val` and returns the actual dereferenced value
Types ¶
This section is empty.