reflection

package module
v0.0.0-...-03da83a Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: MIT Imports: 4 Imported by: 4

README

go-reflection

Utilities extending the Go reflect package

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeOfError          = reflect.TypeOf((*error)(nil)).Elem()
	TypeOfEmptyInterface = reflect.TypeOf((*interface{})(nil)).Elem()
)

Functions

func DerefType

func DerefType(t reflect.Type) reflect.Type

func DerefValue

func DerefValue(val any) reflect.Value

DerefValue dereferences val until a non pointer type or nil is found

func DerefValueAndType

func DerefValueAndType(val any) (reflect.Value, reflect.Type)

func EnumFlatExportedStructFields

func EnumFlatExportedStructFields(val any, callback func(reflect.StructField, reflect.Value))

EnumFlatExportedStructFields returns reflect.StructField and reflect.Value of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. The argument val can be a struct, a pointer to a struct, or a reflect.Value.

func FlatExportedStructFieldValueNameMap

func FlatExportedStructFieldValueNameMap(val any, nameTag string) map[string]StructFieldValueName

FlatExportedStructFieldValueNameMap returns a slice of StructFieldValueName of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. The argument val can be a struct, a pointer to a struct, or a reflect.Value.

func FlatExportedStructFieldsIter

func FlatExportedStructFieldsIter(s any) iter.Seq2[reflect.StructField, reflect.Value]

FlatExportedStructFieldsIter returns an iterator over flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. The argument s can be a struct, a pointer to a struct, or a reflect.Value.

func FlatStructFieldCount

func FlatStructFieldCount(t reflect.Type) int

FlatStructFieldCount returns the number of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct.

func FlatStructFieldNames

func FlatStructFieldNames(t reflect.Type) (names []string)

FlatStructFieldNames returns the names of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct.

func FlatStructFieldTags

func FlatStructFieldTags(t reflect.Type, tagKey string) (tagValues []string)

FlatStructFieldTags returns the tag values for a tagKey of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. An empty string is returned for fields that don't have a matching tag.

func FlatStructFieldTagsOrNames

func FlatStructFieldTagsOrNames(t reflect.Type, tagKey string) (tagsOrNames []string)

FlatStructFieldTagsOrNames returns the tag values for tagKey or the names of the field if no tag with tagKey is defined at a struct field. Fields are flattened, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct.

func FlatStructFieldValues

func FlatStructFieldValues(v reflect.Value) (values []reflect.Value)

FlatStructFieldValues returns the values of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct.

func IsNil

func IsNil(v reflect.Value) bool

IsNil returns if val is of a type that can be nil and if it is nil. Unlike reflect.Value.IsNil() it is safe to call this function for any value and type. The zero value of reflect.Value will yield true because it can be the result of reflect.ValueOf(nil)

func IsZero

func IsZero(v any) bool

IsZero returns if underlying value of v is the zero (default) value of its type, or if v itself is nil.

func ValueOf

func ValueOf(val any) reflect.Value

ValueOf differs from reflect.ValueOf in that it returns the argument val casted to reflect.Value if val is alread a reflect.Value. Else the standard result of reflect.ValueOf(val) will be returned.

func ValuesToInterfaces

func ValuesToInterfaces(values ...reflect.Value) []any

ValuesToInterfaces returns a slice of interface{} by calling reflect.Value.Interfac() for all values.

func ZeroValueExportedStructFieldNames

func ZeroValueExportedStructFieldNames(st any, namePrefix, nameTag string, namesToValidate ...string) (zeroNames []string)

ZeroValueExportedStructFieldNames returns the names of exported zero (default) value struct fields. If a struct field has a tag with the key nameTag, then the tag value will be used as field name, else the Go struct field name will be used. All returned names are prefixed with namePrefix. Anonymous sub structs will be flattened, named sub structs are checked recursively with their name used as prefix delimited with a point. Zero array and slice fields will be added with thair name and index formated as "%s[%d]".

Types

type FieldError

type FieldError struct {
	FieldName  string
	FieldError error
}

func ValidateStructFields

func ValidateStructFields(validateFunc func(any) error, st any, namePrefix, nameTag string, namesToValidate ...string) (fieldErrors []FieldError)

func (FieldError) Error

func (f FieldError) Error() string

type NamedStructField

type NamedStructField struct {
	Field reflect.StructField
	Name  string
}

func FlatExportedNamedStructFields

func FlatExportedNamedStructFields(t reflect.Type, nameTag string) []NamedStructField

FlatExportedNamedStructFields returns a slice of NamedStructField of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. The argument t can be a struct, a pointer to a struct, or a reflect.Value.

type StructFieldValue

type StructFieldValue struct {
	Field reflect.StructField
	Value reflect.Value
}

func FlatExportedStructFields

func FlatExportedStructFields(val any) []StructFieldValue

FlatExportedStructFields returns a slice of StructFieldValue of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. The argument val can be a struct, a pointer to a struct, or a reflect.Value.

type StructFieldValueName

type StructFieldValueName struct {
	Field reflect.StructField
	Value reflect.Value
	Name  string
}

func FlatExportedStructFieldValueNames

func FlatExportedStructFieldValueNames(val any, nameTag string) []StructFieldValueName

FlatExportedStructFieldValueNames returns a slice of StructFieldValueName of flattened struct fields, meaning that the fields of anonoymous embedded fields are flattened to the top level of the struct. The argument val can be a struct, a pointer to a struct, or a reflect.Value.

Jump to

Keyboard shortcuts

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