goreflect

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: GPL-3.0 Imports: 4 Imported by: 9

Documentation

Index

Constants

View Source
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

View Source
var (
	ErrNotAFunction                  = errors.New("not a function")
	ErrNilFunctionValue              = errors.New("nil function value")
	ErrFailedToMapToStructNotAStruct = errors.New("failed to map to struct: destination is not a struct")
	ErrNilMap                        = errors.New("nil map")
)

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

func GetDereferencedType(instance any) reflect.Type

GetDereferencedType returns the dereferenced type reflection

Parameters:

- instance: the instance to reflect

Returns:

- reflect.Type: the dereferenced type reflection

func GetDereferencedValue

func GetDereferencedValue(instance any) reflect.Value

GetDereferencedValue returns the dereferenced value reflection

Parameters:

- instance: the instance to reflect

Returns:

- reflect.Value: the dereferenced value reflection

func GetType

func GetType(instance any) reflect.Type

GetType returns the type reflection

Parameters:

- instance: the instance to reflect

Returns:

- reflect.Type: the type reflection

func GetTypeName

func GetTypeName(typeReflection reflect.Type) string

GetTypeName returns the type name

Parameters:

- typeReflection: the type reflection

Returns:

- string: the type name

func GetTypeOf added in v0.2.0

func GetTypeOf(bodyType any) reflect.Type

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

func GetValue(instance any) reflect.Value

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

func NewInstance(bodyType any) any

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

func NewInstanceFromType(t reflect.Type) any

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

func SafeCallFunction(fn any, params ...any) (
	[]any,
	error,
)

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

func UniqueTypeReference(i any) string

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

func UnsafeCallFunction(fnValue *reflect.Value, paramsValues ...reflect.Value) (
	[]any,
	error,
)

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

func ValueToReflectArray(value any, destValue reflect.Value, skip bool) error

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

func ValueToReflectMap(value any, destValue reflect.Value, skip bool) error

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

func ValueToReflectPtr(value any, destValue reflect.Value, skip bool) error

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

func ValueToReflectSlice(value any, destValue reflect.Value, skip bool) error

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

func ValueToReflectStruct(m map[string]any, destValue reflect.Value, skip bool) error

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

func ValueToReflectType(value any, destValue reflect.Value, skip bool) error

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

Jump to

Keyboard shortcuts

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