reflectutil

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: Unlicense Imports: 3 Imported by: 0

Documentation

Overview

Package reflectutil contains various utilities that are useful when working with reflection

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoPointerNewFromType added in v0.5.1

func AutoPointerNewFromType(typ reflect.Type) interface{}

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

func AutoPointerNewFromValue(val reflect.Value) interface{}

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 Clone added in v0.8.2

func Clone(obj interface{}) interface{}

func CloneValue added in v0.8.2

func CloneValue(val reflect.Value) reflect.Value

func Copy added in v0.10.0

func Copy(from interface{}, to interface{}) interface{}

func CopyValueToModel added in v0.10.0

func CopyValueToModel(modelType reflect.Type, modelValue reflect.Value, newModel interface{})

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

func EnsureSinglePointerType(typ reflect.Type) reflect.Type

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

func EnsureSinglePointerValue(val reflect.Value) reflect.Value

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

func NewFromType(modelType reflect.Type) interface{}

NewFromType creates a new instance of the given type and returns a pointer to the new instance

func NewPtrValueFromType added in v0.5.1

func NewPtrValueFromType(modelType reflect.Type) reflect.Value

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

func NewSliceOfType(typ reflect.Type) interface{}

NewSliceOfType creates a new slice of capacity 1 of type `typ`

func NewSliceOfTypeWithSize added in v0.6.0

func NewSliceOfTypeWithSize(typ reflect.Type, len int, cap int) interface{}

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

func NewSliceWithSize(model interface{}, len int, cap int) interface{}

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

func PurifyToType(model interface{}) reflect.Type

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

func PurifyType(typ reflect.Type) reflect.Type

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 SetFieldByName(obj interface{}, name string, fieldObj interface{}) bool

func SetSlice

func SetSlice(valueSlice []interface{}, fieldValue reflect.Value)

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

func UnwrapType(typ reflect.Type) (unwrapped reflect.Type)

UnwrapType dereferences all pointers of `typ` and returns the actual dereferenced type

func UnwrapValue added in v0.3.1

func UnwrapValue(val reflect.Value) (unwrapped reflect.Value)

UnwrapValue dereferences all pointers of `val` and returns the actual dereferenced value

func Wrap added in v0.4.0

func Wrap(obj interface{}) interface{}

Wrap wraps `obj` into a new pointer. This is equivalent to prefixing a non-generic (non-interface{}) variable with an ampersand. Use this when you have an interface{} that needs to be a pointer

func WrapValue added in v0.4.0

func WrapValue(val reflect.Value) reflect.Value

WrapValue wraps `val` into a new pointer. This is equivalent to prefixing a non-generic (non-interface{}) variable with an ampersand

Types

This section is empty.

Jump to

Keyboard shortcuts

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