Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/base/reflect_util

reflect_util

go
import "gitlab.soludian.com/soludian/fountain/libs/base/reflect_util"

package reflect_util Provide extends reflect util functions.

Index

Constants

base kinds

go
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()

go
var IsEmptyValue = IsEmptyReal

IsZero reflect value check, alias of the IsEmpty()

go
var IsZero = IsEmpty

func BaseTypeVal

go
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 	    => string

returns int64,string,float or error

func ConvSlice

go
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, floatX

func EachMap

go
func EachMap(mp reflect.Value, fn func(key, val reflect.Value))

EachMap process any map data

func EachStrAnyMap

go
func EachStrAnyMap(mp reflect.Value, fn func(key string, val any))

EachStrAnyMap process any map data as string key and any value

func Elem

go
func Elem(v reflect.Value) reflect.Value

Elem returns the value that the interface v contains or that the pointer v points to. otherwise, will return self

func FlatMap

go
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

go
func FullObjectName(i any) string

FullObjectName ...

func FunctionName

go
func FunctionName(i any) string

FunctionName ...

func GetDiff

go
func GetDiff(old, new any, excludePatterns ...string) (any, any)

GetDiff bắt đầu quá trình so sánh đệ quy.

func GetDiffGeneric

go
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

go
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

go
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

go
func HasChild(v reflect.Value) bool

HasChild type check. eg: array, slice, map, struct

func Indirect

go
func Indirect(v reflect.Value) reflect.Value

Indirect like reflect.Indirect(), but can also indirect reflect.Interface. otherwise, will return self

func IsAnyInt

go
func IsAnyInt(k reflect.Kind) bool

IsAnyInt check is intX or uintX type

func IsArrayOrSlice

go
func IsArrayOrSlice(k reflect.Kind) bool

IsArrayOrSlice check. eg: array, slice

func IsDefaultValue

go
func IsDefaultValue(val any) bool

Kiểm tra xem any có mang giá trị default của kiểu dữ liệu tương ứng hay không

func IsEmpty

go
func IsEmpty(v reflect.Value) bool

IsEmpty reflect value check

func IsEmptyReal

go
func IsEmptyReal(v reflect.Value) bool

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

go
func IsEqual(src, dst any) bool

IsEqual determines if two objects are considered equal.

TIP: cannot compare function type

func IsFunc

go
func IsFunc(val any) bool

IsFunc value

func IsIntx

go
func IsIntx(k reflect.Kind) bool

IsIntx check is intX type

func IsNil

go
func IsNil(v reflect.Value) bool

IsNil reflect value

func IsPointer

go
func IsPointer(val any) bool

IsPointer value check

func IsSimpleKind

go
func IsSimpleKind(k reflect.Kind) bool

IsSimpleKind kind in: string, bool, intX, uintX, floatX

func IsUintX

go
func IsUintX(k reflect.Kind) bool

IsUintX check is uintX type

func Len

go
func Len(v reflect.Value) int

Len get reflect value length

func MakeSliceByElem

go
func MakeSliceByElem(elTyp reflect.Type, len, cap int) reflect.Value

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 SetRValue

go
func SetRValue(rv, val reflect.Value)

SetRValue to a `reflect.Value`. will direct set value without convert type.

func SetUnexportedValue

go
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

go
func SetValue(rv reflect.Value, val any) error

SetValue to a `reflect.Value`. will auto convert type if needed.

func SliceElemKind

go
func SliceElemKind(typ reflect.Type) reflect.Kind

SliceElemKind get sub-elem kind of the array, slice, variadic-var.

Usage:

SliceElemKind(reflect.TypeOf([]string{"abc"})) // reflect.String

func SliceSubKind

go
func SliceSubKind(typ reflect.Type) reflect.Kind

SliceSubKind get sub-elem kind of the array, slice, variadic-var. alias SliceElemKind()

func String

go
func String(rv reflect.Value) string

String convert

func ToString

go
func ToString(rv reflect.Value) (str string, err error)

ToString convert

func TypeElem

go
func TypeElem(t reflect.Type) reflect.Type

TypeElem returns the array, slice, chan, map type's element type. otherwise, will return self.

func TypeReal

go
func TypeReal(t reflect.Type) reflect.Type

TypeReal returns a ptr type's real type. otherwise, will return self.

func UnexportedValue

go
func UnexportedValue(rv reflect.Value) any

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 ValToString

go
func ValToString(rv reflect.Value, defaultAsErr bool) (str string, err error)

ValToString convert handle

func ValueByKind

go
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, floatX

func ValueByType

go
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
go
type BKind = reflect.Kind

func ToBKind

go
func ToBKind(kind reflect.Kind) BKind

ToBKind convert reflect.Kind to base kind

func ToBaseKind

go
func ToBaseKind(kind reflect.Kind) BKind

ToBaseKind convert reflect.Kind to base kind

type FlatFunc

FlatFunc custom collect handle func

go
type FlatFunc func(path string, val reflect.Value)

type Type

Type struct

go
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

go
func TypeOf(v any) Type

TypeOf value

type Value

Value struct

go
type Value struct {
    reflect.Value
    // contains filtered or unexported fields
}

func ValueOf

go
func ValueOf(v any) Value

ValueOf the give value

func Wrap

go
func Wrap(rv reflect.Value) Value

Wrap the give value

func (Value) BKind

go
func (v Value) BKind() BKind

BKind value

func (Value) BaseKind

go
func (v Value) BaseKind() BKind

BaseKind value

func (Value) Elem

go
func (v Value) Elem() Value

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.

func (Value) HasChild

go
func (v Value) HasChild() bool

HasChild check. eg: array, slice, map, struct

func (Value) Indirect

go
func (v Value) Indirect() Value

Indirect value. alias of the reflect.Indirect()

func (Value) Int

go
func (v Value) Int() int64

Int value. if is uintX will convert to int64

func (Value) Type

go
func (v Value) Type() Type

Type of value.

func (Value) Uint

go
func (v Value) Uint() uint64

Uint value. if is intX will convert to uint64

Generated by gomarkdoc