reflection

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign(value any, out any) bool

Assign simply performs a reflective replacement of the value, making sure to try to properly handle pointers.

func BindingName

func BindingName(field reflect.StructField) string

BindingName just returns the name of the field/attribute on the struct unless it has a `json` tag defined. If so, it will use the remapped name for this field instead.

type Foo struct {
    A string
    B string `json:"hello"
}

The binding name for the first attribute is "A", but the binding name for the other is "hello".

func FindField

func FindField(structType reflect.Type, name string) (reflect.StructField, bool)

FindField looks up the struct field attribute for the given field on the given struct.

func FlattenPointerType

func FlattenPointerType(t reflect.Type) reflect.Type

FlattenPointerType looks at the reflective type and if it's a pointer it will flatten it to the type it is a pointer for (e.g. "*string"->"string"). If it's already a non-pointer then we will leave this type as-is.

func IndirectTypeKind

func IndirectTypeKind(valueType reflect.Type) reflect.Kind

IndirectTypeKind returns the underlying reflection Kind. This will unravel pointers until we get to a primordial value type.

IndirectTypeKind(reflect.Type(someInt))             -> reflect.Int
IndirectTypeKind(reflect.Type(someIntPointer))      -> reflect.Int
IndirectTypeKind(reflect.Type(someString))          -> reflect.String
IndirectTypeKind(reflect.Type(someStringPointer))   -> reflect.String
IndirectTypeKind(reflect.Type(someDuration))        -> reflect.Int64
IndirectTypeKind(reflect.Type(someDurationPointer)) -> reflect.Int64

func IsNil

func IsNil(value reflect.Value) bool

IsNil returns true if the given value's type is both nil-able and nil.

func IsStructOrPointerTo

func IsStructOrPointerTo(valueType reflect.Type) bool

IsStructOrPointerTo returns true if the given type is any sort of struct or a pointer to some sort of struct type.

func ToBindingValue

func ToBindingValue(value any, bindingPath string, out any) bool

ToBindingValue is used for fetching one-off values from struct instances given their binding string. For example, you can take an instance of a User{} struct and the binding path "Group.ID" to fetch the user's group id.

Example:

user := User{Name: "Bob", Group: Group{ID:"12345", Name:"Admins"}}
name := ToBindingValue(user, "Name") // will be "Bob"
groupID := ToBindingValue(user, "Group.ID") // will be "12345"
groupName := ToBindingValue(user, "Group.Name") // will be "Admins"

Types

This section is empty.

Jump to

Keyboard shortcuts

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