Documentation
¶
Index ¶
- Constants
- Variables
- func CheckFunction(reflectFn any, reflectParams ...any) (reflectedFn *reflect.Value, reflectedParams []reflect.Value, err error)
- func GetDereferencedType(instance any) reflect.Type
- func GetDereferencedValue(instance any) reflect.Value
- func GetType(instance any) reflect.Type
- func GetTypeName(typeReflection reflect.Type) string
- func GetTypeOf(bodyType any) reflect.Type
- func GetValue(instance any) reflect.Value
- func IsStructFieldExported(field *reflect.StructField) bool
- func NewInstance(bodyType any) any
- func NewInstanceFromType(t reflect.Type) any
- func SafeCallFunction(fn any, params ...any) ([]any, error)
- func UniqueTypeReference(i any) string
- func UnsafeCallFunction(fnValue *reflect.Value, paramsValues ...reflect.Value) ([]any, error)
- func ValueToReflectArray(value any, destValue reflect.Value, skip bool) error
- func ValueToReflectMap(value any, destValue reflect.Value, skip bool) error
- func ValueToReflectPtr(value any, destValue reflect.Value, skip bool) error
- func ValueToReflectSlice(value any, destValue reflect.Value, skip bool) error
- func ValueToReflectStruct(m map[string]any, destValue reflect.Value, skip bool) error
- func ValueToReflectType(value any, destValue reflect.Value, skip bool) error
- type Reflection
- func (r Reflection) GetDereferenceReflectedType() reflect.Type
- func (r Reflection) GetInstance() any
- func (r Reflection) GetReflectedType() reflect.Type
- func (r Reflection) GetReflectedTypeName() string
- func (r Reflection) GetReflectedValue() reflect.Value
- func (r Reflection) HasField(fieldName string) bool
- func (r Reflection) IsStruct() bool
Constants ¶
const ( ErrFunctionParameterCountMismatch = "function parameter count mismatch, expected %d, got %d" ErrFunctionParameterTypeMismatch = "function parameter type mismatch on index %d, expected %s, got %s" ErrExpectedStructDestination = "expected struct destination but got %T" ErrExpectedMapValue = "expected map value but got %T" ErrExpectedMapDestination = "expected map destination but got %T" ErrExpectedMapValueForStruct = "expected map value for struct but got %T" ErrExpectedPointerDestination = "expected pointer destination but got %T" ErrExpectedSliceValue = "expected slice value but got %v" ErrExpectedSliceDestination = "expected slice destination but got %T" ErrExpectedSliceOrArrayValue = "expected slice or array value but got %T" ErrExpectedArrayDestination = "expected array destination but got %T" ErrConversionFailed = "cannot convert %v to %v" ErrUnexpectedInterface = "unexpected interface type: %T, cannot be converted due to unknown underlying type" )
Variables ¶
Functions ¶
func CheckFunction ¶ added in v0.2.2
func CheckFunction(reflectFn any, reflectParams ...any) ( reflectedFn *reflect.Value, reflectedParams []reflect.Value, err error, )
CheckFunction checks if a function is valid
Parameters:
- reflectFn: The function to check - reflectParams: The parameters to pass to the function
Returns:
- A pointer to the reflect.Value of the function - A slice of reflect.Value of the parameters - An error if the function is not valid
func GetDereferencedType ¶
GetDereferencedType returns the dereferenced type reflection
Parameters:
- instance: the instance to reflect
Returns:
- reflect.Type: the dereferenced type reflection
func GetDereferencedValue ¶
GetDereferencedValue returns the dereferenced value reflection
Parameters:
- instance: the instance to reflect
Returns:
- reflect.Value: the dereferenced value reflection
func GetType ¶
GetType returns the type reflection
Parameters:
- instance: the instance to reflect
Returns:
- reflect.Type: the type reflection
func GetTypeName ¶
GetTypeName returns the type name
Parameters:
- typeReflection: the type reflection
Returns:
- string: the type name
func GetTypeOf ¶ added in v0.2.0
GetTypeOf returns the type of the given bodyType
Parameters:
- bodyType: The body type to get the type of
Returns:
- reflect.Type: The type of the given bodyType
func GetValue ¶
GetValue returns the value reflection
Parameters:
- instance: the instance to reflect
Returns:
- reflect.Value: the value reflection
func IsStructFieldExported ¶ added in v0.2.13
func IsStructFieldExported(field *reflect.StructField) bool
IsStructFieldExported checks if a struct field is exported
Parameters:
- field: The reflect.StructField to check, if nil, returns false
Returns:
- bool: True if the field is exported, false otherwise
func NewInstance ¶ added in v0.2.1
NewInstance creates a new instance of the given type
Parameters:
- bodyType: The body type to create a new instance of
Returns:
- any: A pointer to a new instance of the given type
func NewInstanceFromType ¶ added in v0.2.1
NewInstanceFromType creates a new instance of the given type
Parameters:
- t: The reflect.Type to create a new instance of
Returns:
- any: A pointer to a new instance of the given type
func SafeCallFunction ¶ added in v0.2.3
SafeCallFunction calls a function with some typed parameters after checking if the function is valid
Parameters:
- fn: The function to call - params: The parameters to pass to the function
Returns:
- A slice of any with the results of the function call - An error if the function is not valid
func UniqueTypeReference ¶ added in v0.2.8
UniqueTypeReference returns a unique string representation of the type of the given any
Parameters:
- i: The any to get the unique type reference from
Returns:
- string: The unique type reference in the format "package.TypeName"
func UnsafeCallFunction ¶ added in v0.2.3
UnsafeCallFunction calls a function with some typed parameters without checking if the function is valid
Parameters:
- fnValue: The reflect.Value of the function to call - paramsValues: The reflect.Value of the parameters to pass to the function
Returns:
- A slice of any with the results of the function call - An error if the function value is nil
func ValueToReflectArray ¶ added in v0.3.1
ValueToReflectArray maps a value to an array using reflection
Parameters:
- value: The value to map from - destValue: The destination value to map to - skip: Whether to skip fields that cannot be set
Returns:
- error: The error if any
func ValueToReflectMap ¶ added in v0.3.1
ValueToReflectMap maps a value to a map using reflection
Parameters:
- value: The value to map from - destValue: The destination value to map to - skip: Whether to skip fields that cannot be set
Returns:
- error: The error if any
func ValueToReflectPtr ¶ added in v0.3.1
ValueToReflectPtr maps a value to a pointer using reflection
Parameters:
- value: The value to map from
- destValue: The destination value to map to
- skip: Whether to skip fields that cannot be set
Returns:
- error: The error if any
func ValueToReflectSlice ¶ added in v0.3.1
ValueToReflectSlice maps a value to a slice using reflection
Parameters:
- value: The value to map from - destValue: The destination value to map to - skip: Whether to skip fields that cannot be set
Returns:
- error: The error if any
func ValueToReflectStruct ¶ added in v0.3.1
ValueToReflectStruct maps a map to a struct using reflection
Parameters:
- m: The map to map from
- destValue: The destination struct value to map to
- skip: Whether to skip fields that cannot be set
Returns:
- error: The error if any
func ValueToReflectType ¶ added in v0.3.1
ValueToReflectType maps a value to a type using reflection
Parameters:
- value: The value to map from
- destValue: The destination value to map to
- skip: Whether to skip fields that cannot be set
Returns:
- error: The error if any
Types ¶
type Reflection ¶
type Reflection struct {
// contains filtered or unexported fields
}
Reflection struct to hold reflection data
func NewDereferencedReflection ¶
func NewDereferencedReflection(instance any) *Reflection
NewDereferencedReflection creates a new reflection from a dereferenced instance
Parameters:
- instance: the instance to reflect
Returns:
- *Reflection: the reflection instance
func NewReflection ¶
func NewReflection(instance any) *Reflection
NewReflection creates a new reflection from an instance
Parameters:
- instance: the instance to reflect
Returns:
- *Reflection: the reflection instance
func (Reflection) GetDereferenceReflectedType ¶ added in v0.2.9
func (r Reflection) GetDereferenceReflectedType() reflect.Type
GetDereferenceReflectedType returns the dereferenced reflected type
Returns:
- reflect.Type: the dereferenced reflected type
func (Reflection) GetInstance ¶
func (r Reflection) GetInstance() any
GetInstance returns the instance
Returns:
- any: the instance
func (Reflection) GetReflectedType ¶
func (r Reflection) GetReflectedType() reflect.Type
GetReflectedType returns the reflected type
Returns:
- reflect.Type: the reflected type
func (Reflection) GetReflectedTypeName ¶
func (r Reflection) GetReflectedTypeName() string
GetReflectedTypeName returns the reflected type name
Returns:
- string: the reflected type name
func (Reflection) GetReflectedValue ¶
func (r Reflection) GetReflectedValue() reflect.Value
GetReflectedValue returns the reflected value
Returns:
- reflect.Value: the reflected value
func (Reflection) HasField ¶ added in v0.2.9
func (r Reflection) HasField(fieldName string) bool
HasField checks if the reflected type has a field with the given name
Parameters:
- fieldName: the field name to check (works for exported fields only)
Returns:
- bool: true if the reflected type has the field, false otherwise
func (Reflection) IsStruct ¶ added in v0.2.9
func (r Reflection) IsStruct() bool
IsStruct checks if the reflected type is a struct
Returns:
- bool: true if the reflected type is a struct, false otherwise