Documentation ¶
Overview ¶
Package reflects Provide extends reflect util functions.
Index ¶
- Constants
- Variables
- func BaseTypeVal(v reflect.Value) (value any, err error)
- func Call(fn reflect.Value, args []reflect.Value, opt *CallOpt) ([]reflect.Value, error)
- func Call2(fn reflect.Value, args []reflect.Value) (reflect.Value, error)
- func CanBeNil(typ reflect.Type) bool
- func ConvSlice(oldSlRv reflect.Value, newElemTyp reflect.Type) (rv reflect.Value, err error)
- func ConvToKind(val any, kind reflect.Kind) (rv reflect.Value, err error)
- func ConvToType(val any, typ reflect.Type) (rv reflect.Value, err error)
- func EachMap(mp reflect.Value, fn func(key, val reflect.Value))
- func EachStrAnyMap(mp reflect.Value, fn func(key string, val any))
- func Elem(v reflect.Value) reflect.Value
- func FlatMap(rv reflect.Value, fn FlatFunc)
- func FlatSlice(sl reflect.Value, depth int) reflect.Value
- func HasChild(v reflect.Value) bool
- func Indirect(v reflect.Value) reflect.Value
- func IsAnyInt(k reflect.Kind) bool
- func IsArrayOrSlice(k reflect.Kind) bool
- func IsEmpty(v reflect.Value) bool
- func IsEmptyReal(v reflect.Value) bool
- func IsEqual(src, dst any) bool
- func IsFunc(val any) bool
- func IsIntLike(k reflect.Kind) bool
- func IsIntx(k reflect.Kind) bool
- func IsNil(v reflect.Value) bool
- func IsSimpleKind(k reflect.Kind) bool
- func IsUintX(k reflect.Kind) bool
- func IsValidPtr(v reflect.Value) bool
- func Len(v reflect.Value) int
- func MakeSliceByElem(elTyp reflect.Type, len, cap int) reflect.Value
- func SafeCall(fun reflect.Value, args []reflect.Value) (ret []reflect.Value, err error)
- func SafeCall2(fun reflect.Value, args []reflect.Value) (val reflect.Value, err error)
- func SetRValue(rv, val reflect.Value)
- func SetUnexportedValue(rv reflect.Value, value any)
- func SetValue(rv reflect.Value, val any) error
- func SliceElemKind(typ reflect.Type) reflect.Kind
- func SliceSubKind(typ reflect.Type) reflect.Kind
- func String(rv reflect.Value) string
- func ToBaseVal(v reflect.Value) (value any, err error)
- func ToString(rv reflect.Value) (str string, err error)
- func TypeElem(t reflect.Type) reflect.Type
- func TypeReal(t reflect.Type) reflect.Type
- func UnexportedValue(rv reflect.Value) any
- func UnwrapAny(v reflect.Value) reflect.Value
- func ValToString(rv reflect.Value, defaultAsErr bool) (str string, err error)
- func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error)
- func ValueByType(val any, typ reflect.Type) (rv reflect.Value, err error)
- type BKind
- type CallOpt
- type FlatFunc
- type FuncX
- func (f *FuncX) Call(args ...any) ([]any, error)
- func (f *FuncX) Call2(args ...any) (any, error)
- func (f *FuncX) CallRV(args []reflect.Value) ([]reflect.Value, error)
- func (f *FuncX) NumIn() int
- func (f *FuncX) NumOut() int
- func (f *FuncX) String() string
- func (f *FuncX) WithEnhanceConv() *FuncX
- func (f *FuncX) WithTypeChecker(checker TypeCheckerFn) *FuncX
- type Type
- type TypeCheckerFn
- type Value
Constants ¶
const ( // Int for all intX types Int = reflect.Int // Uint for all uintX types Uint = reflect.Uint // Float for all floatX types Float = reflect.Float32 // Array for array,slice types Array = reflect.Array // Complex for all complexX types Complex = reflect.Complex64 )
base kinds
Variables ¶
var IsEmptyValue = IsEmptyReal
IsEmptyValue reflect value check, alias of the IsEmptyReal()
var IsZero = IsEmpty
IsZero reflect value check, alias of the IsEmpty()
var OneOrTwoOutChecker = func(typ reflect.Type) error { if !good1or2outFunc(typ) { return errors.New("func allow with 1 result or 2 results where the second is an error") } return nil }
OneOrTwoOutChecker check func type. only allow 1 or 2 return values
Allow func returns:
- 1 return: (value)
- 2 return: (value, error)
Functions ¶
func BaseTypeVal ¶ added in v0.5.10
BaseTypeVal convert custom type or intX,uintX,floatX to generic base type.
func Call ¶ added in v0.6.13
Call returns the result of evaluating the first argument as a function.
- Will check args and try convert input args to func args type.
from text/template/funcs.go#call
func Call2 ¶ added in v0.6.13
Call2 returns the result of evaluating the first argument as a function. The function must return 1 result, or 2 results, the second of which is an error.
- Only support func with 1 or 2 return values: (val) OR (val, err)
- Will check args and try convert input args to func args type.
func CanBeNil ¶ added in v0.6.13
CanBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero.
func ConvSlice ¶ added in v0.6.2
ConvSlice make new type slice from old slice, will auto convert element type.
TIPs:
Only support kind: string, bool, intX, uintX, floatX
func ConvToKind ¶ added in v0.6.13
ConvToKind convert and create reflect.Value by give reflect.Kind
TIPs:
Only support kind: string, bool, intX, uintX, floatX
func ConvToType ¶ added in v0.6.13
ConvToType convert and create reflect.Value by give reflect.Type
func EachStrAnyMap ¶ added in v0.6.8
EachStrAnyMap process any map data as string key and any value
func Elem ¶
Elem returns the value that the interface v contains or that the pointer v points to. otherwise, will return self
func FlatMap ¶ added in v0.5.12
FlatMap process tree map to flat key-value map.
Examples:
{"top": {"sub": "value", "sub2": "value2"} } -> {"top.sub": "value", "top.sub2": "value2" }
func FlatSlice ¶ added in v0.6.13
FlatSlice flatten multi-level slice to given depth-level slice.
Example:
FlatSlice([]any{ []any{3, 4}, []any{5, 6} }, 1) // Output: []any{3, 4, 5, 6}
always return reflect.Value of []any. note: maybe flatSl.Cap != flatSl.Len
func Indirect ¶ added in v0.5.12
Indirect like reflect.Indirect(), but can also indirect reflect.Interface. otherwise, will return self
func IsArrayOrSlice ¶ added in v0.6.2
IsArrayOrSlice check. eg: array, slice
func IsEmptyReal ¶ added in v0.6.11
IsEmptyReal reflect value check.
Note:
Difference the IsEmpty(), if value is ptr or interface, will check real elem.
From src/pkg/encoding/json/encode.go.
func IsEqual ¶ added in v0.5.6
IsEqual determines if two objects are considered equal.
TIP: cannot compare function type
func IsSimpleKind ¶ added in v0.6.9
IsSimpleKind kind in: string, bool, intX, uintX, floatX
func IsValidPtr ¶ added in v0.6.16
IsValidPtr check variable is a valid pointer.
func Len ¶ added in v0.5.6
Len get reflect value length. allow: intX, uintX, floatX, string, map, array, chan, slice.
Note: (u)intX use width. float to string then calc len.
func MakeSliceByElem ¶ added in v0.6.12
MakeSliceByElem create a new slice by the element type.
- elType: the type of the element. - returns: the new slice.
Usage:
sl := MakeSliceByElem(reflect.TypeOf(1), 10, 20) sl.Index(0).SetInt(10) // Or use reflect.AppendSlice() merge two slice // Or use `for` with `reflect.Append()` add elements
func SafeCall ¶ added in v0.6.13
SafeCall runs fun.Call(args), and returns the resulting values, or an error. If the call panics, the panic value is returned as an error.
func SafeCall2 ¶ added in v0.6.13
SafeCall2 runs fun.Call(args), and returns the resulting value and error, if any. If the call panics, the panic value is returned as an error.
NOTE: Only support func with 1 or 2 return values: (val) OR (val, err)
from text/template/funcs.go#safeCall
func SetRValue ¶ added in v0.6.9
SetRValue to a `reflect.Value`. will direct set value without convert type.
func SetUnexportedValue ¶ added in v0.6.9
SetUnexportedValue quickly set unexported field value by reflect
NOTE: this method is unsafe, use it carefully. should ensure rv is addressable by field.CanAddr()
func SliceElemKind ¶ added in v0.6.9
SliceElemKind get sub-elem kind of the array, slice, variadic-var.
Usage:
SliceElemKind(reflect.TypeOf([]string{"abc"})) // reflect.String
func SliceSubKind ¶ added in v0.5.8
SliceSubKind get sub-elem kind of the array, slice, variadic-var. alias SliceElemKind()
func ToBaseVal ¶ added in v0.6.13
ToBaseVal convert custom type or intX,uintX,floatX to generic base type.
intX => int64 unitX => uint64 floatX => float64 string => string
returns int64,string,float or error
func TypeElem ¶ added in v0.6.12
TypeElem returns the array, slice, chan, map type's element type. otherwise, will return self.
func TypeReal ¶ added in v0.6.12
TypeReal returns a ptr type's real type. otherwise, will return self.
func UnexportedValue ¶ added in v0.6.9
UnexportedValue quickly get unexported value by reflect.Value
NOTE: this method is unsafe, use it carefully. should ensure rv is addressable by field.CanAddr()
refer: https://stackoverflow.com/questions/42664837/how-to-access-unexported-struct-fields
func UnwrapAny ¶ added in v0.6.13
UnwrapAny unwrap reflect.Interface value. otherwise, will return self
func ValToString ¶ added in v0.5.12
ValToString convert handle
func ValueByKind ¶ added in v0.5.10
ValueByKind convert and create reflect.Value by give reflect.Kind
Types ¶
type BKind ¶
BKind base data kind type, alias of reflect.Kind
Diff with reflect.Kind:
- Int contains all intX types
- Uint contains all uintX types
- Float contains all floatX types
- Array for array and slice types
- Complex contains all complexX types
func ToBaseKind ¶
ToBaseKind convert reflect.Kind to base kind
type CallOpt ¶ added in v0.6.13
type CallOpt struct { // TypeChecker check func type before call func. eg: check return values TypeChecker TypeCheckerFn // EnhanceConv try to enhance auto convert args to func args type // - support more type: string, int, uint, float, bool EnhanceConv bool }
CallOpt call options
type FuncX ¶ added in v0.6.13
type FuncX struct { CallOpt // Name of func. eg: "MyFunc" Name string // contains filtered or unexported fields }
FuncX wrap a go func. represent a function
func (*FuncX) Call ¶ added in v0.6.13
Call the function with given arguments.
Usage:
func main() { fn := func(a, b int) int { return a + b } fx := NewFunc(fn) ret, err := fx.Call(1, 2) fmt.Println(ret[0], err) // Output: 3 <nil> }
func (*FuncX) Call2 ¶ added in v0.6.13
Call2 returns the result of evaluating the first argument as a function. The function must return 1 result, or 2 results, the second of which is an error.
- Only support func with 1 or 2 return values: (val) OR (val, err)
- Will check args and try convert input args to func args type.
func (*FuncX) CallRV ¶ added in v0.6.13
CallRV call the function with given reflect.Value arguments.
func (*FuncX) WithEnhanceConv ¶ added in v0.6.13
WithEnhanceConv set enhance convert
func (*FuncX) WithTypeChecker ¶ added in v0.6.13
func (f *FuncX) WithTypeChecker(checker TypeCheckerFn) *FuncX
WithTypeChecker set type checker
type Type ¶
type Type interface { reflect.Type // BaseKind value BaseKind() BKind // RealType returns a ptr type's real type. otherwise, will return self. RealType() reflect.Type // SafeElem returns a type's element type. otherwise, will return self. SafeElem() reflect.Type }
Type struct
type TypeCheckerFn ¶ added in v0.6.13
TypeCheckerFn type checker func
type Value ¶
Value struct
func (Value) Elem ¶
Elem returns the value that the interface v contains or that the pointer v points to.
TIP: not like reflect.Value.Elem. otherwise, will return self.