Documentation ¶
Overview ¶
Package reflect provides reflection helpers
SPDX-License-Identifier: Apache-2.0
Index ¶
- func DerefType(typ goreflect.Type) goreflect.Type
- func DerefTypeMaxOnePtr(typ goreflect.Type) goreflect.Type
- func DerefValue(val goreflect.Value) goreflect.Value
- func DerefValueMaxOnePtr(val goreflect.Value) goreflect.Value
- func FieldsByName(typ goreflect.Type) map[string]goreflect.StructField
- func GetFieldByName(typ goreflect.Type, name string) goreflect.StructField
- func IsBigPtr(typ goreflect.Type) bool
- func IsNil(val goreflect.Value) bool
- func IsNillable[T KindElem[T]](ke T) bool
- func IsNumeric(typ goreflect.Type) bool
- func IsPrimitive[T KindElem[T]](val T) bool
- func MustTypeAssert(val goreflect.Value, typ goreflect.Type, msg ...string)
- func NumPointers(val goreflect.Type) (res int)
- func ResolveValueType(val goreflect.Value) goreflect.Value
- func SetPointerValue(dst, val goreflect.Value)
- func TypeAssert(val goreflect.Value, typ goreflect.Type, msg ...string) error
- func TypeOf(val goreflect.Value) string
- func TypeToBaseType(typ goreflect.Type) goreflect.Type
- func ValueMaxOnePtrType(val goreflect.Value) goreflect.Type
- func ValueToBaseType(val goreflect.Value) goreflect.Value
- type KindElem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DerefTypeMaxOnePtr ¶
DerefTypeMaxOnePtr returns zero or one pointers to a type. If the type is more than one pointer, it is derefd to one pointer, otherwse it is returned as is.
func DerefValue ¶
DerefValue returns the element of zero or more pointers to a value. If any pointer is nil, an invalid Value is returned.
func DerefValueMaxOnePtr ¶
DerefValueMaxOnePtr returns zero or one pointers to a value. If the value is more than one pointer, it is derefd to one pointer, otherwise it is returned as is. If any pointer except the last one is nil, an invalid Value is returned.
There are 3 cases of results: - a valid Value for a non-pointer - a valid Value for a nil pointer to a non-pointer - an invalid Value for a nil pointer to a pointer
Examples: DerefValueMaxOnePtr(reflect.ValueOf(0)) is a valid Value
var p *int DerefValueMaxOnePtr(p) is a valid Value
var p *int var p2 = &p DerefValueMaxOnePtr(p2) is a valid Value, since the outer pointer p2 is non-nil (doesn't matter p is nil)
var p2 **int DerefValueMaxOnePtr(p) is an invalid Value, since the outer pointer is nil
func FieldsByName ¶
func FieldsByName(typ goreflect.Type) map[string]goreflect.StructField
FieldsByName collects the fields of a struct into a map. Returns the zero value if the type provided does not represent a struct, or a struct that does not have any fields. If a given struct field is a struct, then another call would have to made on that struct. If a given struct field is a *struct, then it is possible it is a recursive struct (eg Customer{child *Customer}).
func GetFieldByName ¶
func GetFieldByName(typ goreflect.Type, name string) goreflect.StructField
GetFieldByName is a more convenient version of reflect.Type.FieldByName - it only returns a reflect.StructField. If the field does not exist, it panics. Mostly useful in unit testing.
func IsBigPtr ¶
IsBigPtr returns true if the given type is a *big.Int, *big.Float, or *big.Rat, and false otherwise
func IsNillable ¶
IsNillable returns true if ke.Kind() is nillable, which means it is Chan, Func, Interface, Map, Pointer, or Slice.
If ke is nil, it means ke is reflect.TypeOf(a nil value of some interface type). If ke.Kind() is Invalid, it means ke is reflect.ValueOf(a nil value of any type).
If ke is nil or Invalid, the result is true.
func IsPrimitive ¶
func MustTypeAssert ¶
MustTypeAssert is a must version of TypeAssert
func NumPointers ¶
NumPointers returns the number of pointers a type represents
func ResolveValueType ¶
ResolveValueType resolves a value to the real type of value it contains. The only case where the result is different from the argument is when the argument is typed as interface{}. For example, if the interface{} value is actually an int, then the result will be typed as int. This generally only happens in corner cases like iterating the elements of a slice typed as []interface{} - even though the elements may be strings, ints, etc, each element will be typed as interface{}.
func SetPointerValue ¶
SetPointerValue copies the value of val into dst after dereffing all pointers Dst must be at least one pointer that derefs to some type T, and val must be convertible to T, otherwise a panic will occur
func TypeAssert ¶
TypeAssert asserts that the value given has the same type as the type given. If not, the error returned contains a message that is similiar to the one Go provides when the type assertion syntax fails. Unlike Go's type assertion syntax, this function can be called with any kind of value and any type. If desired, an optional message can be provided that is placed at the beginning of the error message, followed by a colon and space.
func TypeOf ¶
TypeOf returns the string type of the wrapped value. If val is invalid, the String() is returned, else Type().String() is returned.
func TypeToBaseType ¶
TypeToBaseType converts a reflect.Type that may be a primitive subtype (eg type foo uint8) to the underlying type (eg uint8). If the given type is not a primitive subtype, nil is returned.
func ValueMaxOnePtrType ¶
ValueMaxOnePtrType returns the underlying type of zero or one pointers to a value. If the value given has multiple pointers, the value is not a valid parameter value, and the result is nil.
Examples: ValueMaxOnePtrType(reflect.ValueOf(0)) == reflect.TypeOf(0)
var p *int ValueMaxOnePtrType(reflect.ValueOf(p)) == reflect.TypeOf(0)
var p2 **int ValueMaxOnePtrType(reflect.ValueOf(p2)) == nil
func ValueToBaseType ¶
ValueToBaseType converts a reflect.Value that may be a primitive subtype (eg type byte uint8) to the underlying type (eg uint8). If the value is a pointer to a primitive subtype, the value is converted to a pointer to the underlying type.