Import path:
gitlab.soludian.com/soludian/fountain/libs/base/reflect_util
reflect_util
import "gitlab.soludian.com/soludian/fountain/libs/base/reflect_util"package reflect_util Provide extends reflect util functions.
Index
- Constants
- Variables
- func BaseTypeVal(v reflect.Value) (value any, err error)
- func ConvSlice(oldSlRv reflect.Value, newElemTyp 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 FullObjectName(i any) string
- func FunctionName(i any) string
- func GetDiff(old, new any, excludePatterns ...string) (any, any)
- func GetDiffGeneric[T any](old, new T, excludePatterns ...string) (T, T)
- func GetDiffString(old, new any, excludePatterns ...string) (string, string)
- func GetJSONDiffString(oldJSON, newJSON string, excludePatterns ...string) (string, string)
- 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 IsDefaultValue(val any) 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 IsIntx(k reflect.Kind) bool
- func IsNil(v reflect.Value) bool
- func IsPointer(val any) bool
- func IsSimpleKind(k reflect.Kind) bool
- func IsUintX(k reflect.Kind) bool
- func Len(v reflect.Value) int
- func MakeSliceByElem(elTyp reflect.Type, len, cap int) reflect.Value
- 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 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 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 FlatFunc
- type Type
- type Value
Constants
const (
// Int for all intX types
KInt = reflect.Int
// Uint for all uintX types
KUint = reflect.Uint
// Float for all floatX types
KFloat = reflect.Float32
// Array for array,slice types
KArray = reflect.Array
// Complex for all complexX types
KComplex = reflect.Complex64
)Variables
IsEmptyValue reflect value check, alias of the IsEmptyReal()
var IsEmptyValue = IsEmptyRealIsZero reflect value check, alias of the IsEmpty()
var IsZero = IsEmptyfunc BaseTypeVal
func BaseTypeVal(v reflect.Value) (value any, err error)BaseTypeVal convert custom type or intX,uintX,floatX to generic base type.
intX/unitX => int64
floatX => float64
string => stringreturns int64,string,float or error
func ConvSlice
func ConvSlice(oldSlRv reflect.Value, newElemTyp reflect.Type) (rv reflect.Value, err error)ConvSlice make new type slice from old slice, will auto convert element type.
TIPs:
Only support kind: string, bool, intX, uintX, floatXfunc EachMap
func EachMap(mp reflect.Value, fn func(key, val reflect.Value))EachMap process any map data
func EachStrAnyMap
func EachStrAnyMap(mp reflect.Value, fn func(key string, val any))EachStrAnyMap process any map data as string key and any value
func Elem
func Elem(v reflect.Value) reflect.ValueElem returns the value that the interface v contains or that the pointer v points to. otherwise, will return self
func FlatMap
func FlatMap(rv reflect.Value, fn FlatFunc)FlatMap process tree map to flat key-value map.
Examples:
{"top": {"sub": "value", "sub2": "value2"} }
->
{"top.sub": "value", "top.sub2": "value2" }func FullObjectName
func FullObjectName(i any) stringFullObjectName ...
func FunctionName
func FunctionName(i any) stringFunctionName ...
func GetDiff
func GetDiff(old, new any, excludePatterns ...string) (any, any)GetDiff bắt đầu quá trình so sánh đệ quy.
func GetDiffGeneric
func GetDiffGeneric[T any](old, new T, excludePatterns ...string) (T, T)GetDiffGeneric là phiên bản generic, type-safe của hàm so sánh. Nó nhận hai đối tượng cùng kiểu T và trả về hai đối tượng diff cũng có kiểu T.
func GetDiffString
func GetDiffString(old, new any, excludePatterns ...string) (string, string)GetDiffString là hàm public, bắt đầu quá trình so sánh và trả về chuỗi.
func GetJSONDiffString
func GetJSONDiffString(oldJSON, newJSON string, excludePatterns ...string) (string, string)GetDiffString là hàm public, bắt đầu quá trình so sánh và trả về chuỗi.
func HasChild
func HasChild(v reflect.Value) boolHasChild type check. eg: array, slice, map, struct
func Indirect
func Indirect(v reflect.Value) reflect.ValueIndirect like reflect.Indirect(), but can also indirect reflect.Interface. otherwise, will return self
func IsAnyInt
func IsAnyInt(k reflect.Kind) boolIsAnyInt check is intX or uintX type
func IsArrayOrSlice
func IsArrayOrSlice(k reflect.Kind) boolIsArrayOrSlice check. eg: array, slice
func IsDefaultValue
func IsDefaultValue(val any) boolKiểm tra xem any có mang giá trị default của kiểu dữ liệu tương ứng hay không
func IsEmpty
func IsEmpty(v reflect.Value) boolIsEmpty reflect value check
func IsEmptyReal
func IsEmptyReal(v reflect.Value) boolIsEmptyReal 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
func IsEqual(src, dst any) boolIsEqual determines if two objects are considered equal.
TIP: cannot compare function type
func IsFunc
func IsFunc(val any) boolIsFunc value
func IsIntx
func IsIntx(k reflect.Kind) boolIsIntx check is intX type
func IsNil
func IsNil(v reflect.Value) boolIsNil reflect value
func IsPointer
func IsPointer(val any) boolIsPointer value check
func IsSimpleKind
func IsSimpleKind(k reflect.Kind) boolIsSimpleKind kind in: string, bool, intX, uintX, floatX
func IsUintX
func IsUintX(k reflect.Kind) boolIsUintX check is uintX type
func Len
func Len(v reflect.Value) intLen get reflect value length
func MakeSliceByElem
func MakeSliceByElem(elTyp reflect.Type, len, cap int) reflect.ValueMakeSliceByElem 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 elementsfunc SetRValue
func SetRValue(rv, val reflect.Value)SetRValue to a `reflect.Value`. will direct set value without convert type.
func SetUnexportedValue
func SetUnexportedValue(rv reflect.Value, value any)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 SetValue
func SetValue(rv reflect.Value, val any) errorSetValue to a `reflect.Value`. will auto convert type if needed.
func SliceElemKind
func SliceElemKind(typ reflect.Type) reflect.KindSliceElemKind get sub-elem kind of the array, slice, variadic-var.
Usage:
SliceElemKind(reflect.TypeOf([]string{"abc"})) // reflect.Stringfunc SliceSubKind
func SliceSubKind(typ reflect.Type) reflect.KindSliceSubKind get sub-elem kind of the array, slice, variadic-var. alias SliceElemKind()
func String
func String(rv reflect.Value) stringString convert
func ToString
func ToString(rv reflect.Value) (str string, err error)ToString convert
func TypeElem
func TypeElem(t reflect.Type) reflect.TypeTypeElem returns the array, slice, chan, map type's element type. otherwise, will return self.
func TypeReal
func TypeReal(t reflect.Type) reflect.TypeTypeReal returns a ptr type's real type. otherwise, will return self.
func UnexportedValue
func UnexportedValue(rv reflect.Value) anyUnexportedValue 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 ValToString
func ValToString(rv reflect.Value, defaultAsErr bool) (str string, err error)ValToString convert handle
func ValueByKind
func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error)ValueByKind create reflect.Value by give reflect.Kind
TIPs:
Only support kind: string, bool, intX, uintX, floatXfunc ValueByType
func ValueByType(val any, typ reflect.Type) (rv reflect.Value, err error)ValueByType create reflect.Value by give reflect.Type
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
type BKind = reflect.Kindfunc ToBKind
func ToBKind(kind reflect.Kind) BKindToBKind convert reflect.Kind to base kind
func ToBaseKind
func ToBaseKind(kind reflect.Kind) BKindToBaseKind convert reflect.Kind to base kind
type FlatFunc
FlatFunc custom collect handle func
type FlatFunc func(path string, val reflect.Value)type Type
Type struct
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
}func TypeOf
func TypeOf(v any) TypeTypeOf value
type Value
Value struct
type Value struct {
reflect.Value
// contains filtered or unexported fields
}func ValueOf
func ValueOf(v any) ValueValueOf the give value
func Wrap
func Wrap(rv reflect.Value) ValueWrap the give value
func (Value) BKind
func (v Value) BKind() BKindBKind value
func (Value) BaseKind
func (v Value) BaseKind() BKindBaseKind value
func (Value) Elem
func (v Value) Elem() ValueElem 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.
func (Value) HasChild
func (v Value) HasChild() boolHasChild check. eg: array, slice, map, struct
func (Value) Indirect
func (v Value) Indirect() ValueIndirect value. alias of the reflect.Indirect()
func (Value) Int
func (v Value) Int() int64Int value. if is uintX will convert to int64
func (Value) Type
func (v Value) Type() TypeType of value.
func (Value) Uint
func (v Value) Uint() uint64Uint value. if is intX will convert to uint64
Generated by gomarkdoc