reflect

package
v0.0.0-...-02bf512 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package reflect provides reflection helpers

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DerefType

func DerefType(typ goreflect.Type) goreflect.Type

DerefType returns the element type of zero or more pointers to a type.

func DerefTypeMaxOnePtr

func DerefTypeMaxOnePtr(typ goreflect.Type) goreflect.Type

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

func DerefValue(val goreflect.Value) goreflect.Value

DerefValue returns the element of zero or more pointers to a value. If any pointer is nil, an invalid Value is returned.

func DerefValueMaxOnePtr

func DerefValueMaxOnePtr(val goreflect.Value) goreflect.Value

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

func IsBigPtr(typ goreflect.Type) bool

IsBigPtr returns true if the given type is a *big.Int, *big.Float, or *big.Rat, and false otherwise

func IsNil

func IsNil(val goreflect.Value) bool

IsNil returns true if the value is invalid or a nillable type whose value is nil

func IsNillable

func IsNillable[T KindElem[T]](ke T) bool

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 IsNumeric

func IsNumeric(typ goreflect.Type) bool

IsNumeric returns true if the given type satisfies constraint.Numeric

func IsPrimitive

func IsPrimitive[T KindElem[T]](val T) bool

func MustTypeAssert

func MustTypeAssert(val goreflect.Value, typ goreflect.Type, msg ...string)

MustTypeAssert is a must version of TypeAssert

func NumPointers

func NumPointers(val goreflect.Type) (res int)

NumPointers returns the number of pointers a type represents

func ResolveValueType

func ResolveValueType(val goreflect.Value) goreflect.Value

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

func SetPointerValue(dst, val goreflect.Value)

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

func TypeAssert(val goreflect.Value, typ goreflect.Type, msg ...string) error

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

func TypeOf(val goreflect.Value) string

TypeOf returns the string type of the wrapped value. If val is invalid, the String() is returned, else Type().String() is returned.

func TypeToBaseType

func TypeToBaseType(typ goreflect.Type) goreflect.Type

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

func ValueMaxOnePtrType(val goreflect.Value) goreflect.Type

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

func ValueToBaseType(val goreflect.Value) goreflect.Value

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.

Types

type KindElem

type KindElem[T any] interface {
	Kind() goreflect.Kind
	Elem() T
}

KindElem describes the Kind and Elem methods common to both Value and Type objects

Jump to

Keyboard shortcuts

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