safe_reflect

package
v1.2.31 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Invalid reflect.Kind = iota
	Bool
	Int
	I8
	I16
	I32
	I64
	Uint
	U8
	U16
	U32
	U64
	UPtr
	F32
	F64
	C64
	C128
	Arr
	Chan
	Func
	Any
	Map
	Ptr
	Slice
	String
	Struct
	UnsafePtr
)

alias of reflect.Kind

View Source
const (
	EFace = Any
	IFace = Any
)

Variables

This section is empty.

Functions

func ArrayMake added in v1.2.0

func ArrayMake(length int, elem any) any

func CanDirectCompare added in v1.2.8

func CanDirectCompare(typ reflect.Type) bool

func CopyFieldValue

func CopyFieldValue(src any, dst any)

CopyFieldValue copy field value in src to dst which fields with same name and same type regardless src type same with dst type no deep copy, only one level iface +case 1 src can be Struct, Struct Pointer, Struct Pointer to Pointer, Struct Pointer to..., ... dst must be Struct Pointer, Struct Pointer to Pointer, Struct Pointer to..., ...

func FuncMake added in v1.2.0

func FuncMake(in, out []any, variadic bool, metaFunc MetaFunc) any

func GetTags added in v1.2.13

func GetTags(v any, app, key string) []string

GetTags +example:

type T1 struct {
	Name string `gorm:"column:name"`
	Desc string `gorm:"column:desc"`
}

+param: v=T1{}, app="gorm", key="column" +result: {"name", "desc"}

func HasField

func HasField(v any, fieldName string, fieldVal any) (ok bool)

HasField check type: 1. struct that do not allow embedded field 2. array elem is struct 3. slice elem is struct 4. map[string]any 5. latter: []map[string]any

func HasFields

func HasFields(v any, fields map[string]any) bool

func Impl added in v1.2.11

func Impl(v any, iFace any) bool

Impl iFace must an iface or called not an empty interface

type _typeIFace interface {
	Method0(in...)out...
	Method1(in...)out...
	...
}

+param iFace var iFace = (*_typeIFace)(nil)

func IndirectImpl added in v1.2.14

func IndirectImpl(v any, iFace any) (inst any, inst2 any)

IndirectImpl +param iFace same with Impl +param v specialized for T, *T, **T, *...*T -> T Arr, *Arr, *...*Arr -> Arr.Elem Slice, *Slice, *...*Slice -> Slice.Elem

func IndirectRVal added in v1.2.29

func IndirectRVal(v reflect.Value) reflect.Value

func InterfaceOf added in v1.1.9

func InterfaceOf(v reflect.Value) any

func InterfacesOf added in v1.1.9

func InterfacesOf(v []reflect.Value) []any

func IsArray

func IsArray(v reflect.Value) bool

func IsBool

func IsBool(v reflect.Value) bool

func IsChan

func IsChan(v reflect.Value) bool

func IsFloat

func IsFloat(v reflect.Value) bool

func IsFunc

func IsFunc(v reflect.Value) bool

func IsInt

func IsInt(v reflect.Value) bool

func IsMap

func IsMap(v reflect.Value) bool

func IsSlice

func IsSlice(v reflect.Value) bool

func IsString

func IsString(v reflect.Value) bool

func IsStruct

func IsStruct(v reflect.Value) bool

func IsUint

func IsUint(v reflect.Value) bool

func MapMake added in v1.2.0

func MapMake(key, elem any, capacity int) any

func NewOf added in v1.2.14

func NewOf(typ reflect.Type) any

NewOf new value for type:typ no pointer

func NewOf2 added in v1.2.14

func NewOf2(typ reflect.Type) any

NewOf2 new value for type:typ pointer sometimes useful

func Set added in v1.2.27

func Set(src, dst any)

func SetField

func SetField(v any, fieldName string, fieldVal any)

func SetFields

func SetFields(v any, fields map[string]any)

func SliceMake added in v1.2.0

func SliceMake(elem any, length, capacity int) any

func StructMake added in v1.2.0

func StructMake(fields []FieldDesc) any

func StructType added in v1.2.25

func StructType(v any) (typ reflect.Type, ok bool)

StructType Struct and Ptr Struct Slice Struct, Arr Struct, Ptr Slice Ptr Struct, Ptr Arr Ptr Struct Map [K] Struct, Ptr Map [K] Ptr Struct Any cast

Types

type FieldDesc added in v1.2.0

type FieldDesc struct {
	Name  string
	Value any
	Tag   reflect.StructTag
}

func (FieldDesc) StructField added in v1.2.0

func (f FieldDesc) StructField() reflect.StructField

type FieldTagKV added in v1.2.15

type FieldTagKV map[string]TagKV

FieldTagKV fieldName -> TagKV

type MetaFunc added in v1.2.0

type MetaFunc func([]reflect.Value) []reflect.Value

type TagKV added in v1.2.14

type TagKV map[string]string

TagKV as parseTagKV description +example: `app:"k1:v1;k2:v2;k3:v3;key;val"` +result: k1 -> v1 k2 -> v2 k3 -> v3 key -> key val -> val

type TagMap

type TagMap map[string]string

TagMap in an app, fieldName -> fieldTag +example: Field0 string `app:"k1:v1;k2:v2;k3:v3;key;val"` +result: Field0 -> k1:v1;k2:v2;k3:v3;key;val

func (TagMap) GetValList added in v1.2.11

func (m TagMap) GetValList(key string) []string

GetValList +example: Field0 string `app:"k1:v1;k2:v2;k3:v3;key;val"` Field1 string `app:"k1:v4;key-val"` +param: key = k1 +result: []string{v1, v4}

func (TagMap) ParseGetVal added in v1.2.0

func (m TagMap) ParseGetVal(field, key string) string

ParseGetVal get val in an app's, field's, key's val +example `gorm:"column:user_name;type:varchar(128);comment:user's name, account's name"` +example `app:"unique:1;union_unique:0;field:name;"` `app:"unique:0;union_unique:1;field:name_part1;follow:-"` `app:"unique:0;union_unique:1;field:name_part1;follow:+"`

type TagVV added in v1.2.14

type TagVV map[string]string

func StructGetTag2 added in v1.2.25

func StructGetTag2(v any, app0, key0, app1, key1 string) (tag0, tag1 TagVV, ok bool)

type TypVal

type TypVal struct {
	// final Value and final Type
	Typ reflect.Type
	Val reflect.Value
	// contains filtered or unexported fields
}

func IndirectOf

func IndirectOf(v any) TypVal

func Of

func Of(v any) TypVal

func Wrap added in v1.2.15

func Wrap(val reflect.Value) TypVal

func Wrap2 added in v1.2.15

func Wrap2(typ reflect.Type, val reflect.Value) TypVal

func (TypVal) ArrayCanSet added in v1.2.15

func (tv TypVal) ArrayCanSet() bool

func (TypVal) ArraySet

func (tv TypVal) ArraySet(index int, elem any)

ArraySet set array index to elem -> arr[index] = elem

func (TypVal) ArraySetStructFields

func (tv TypVal) ArraySetStructFields(fields map[string]any)

ArraySetStructFields set array struct fields fieldName to fieldVal

func (TypVal) CanDirectAssign

func (tv TypVal) CanDirectAssign() bool

CanDirectAssign only use Type not use flag, a bit of incoming rule

func (TypVal) CanDirectCompare added in v1.2.1

func (tv TypVal) CanDirectCompare() bool

func (TypVal) FieldLen

func (tv TypVal) FieldLen() int

func (TypVal) FuncCall added in v1.2.0

func (tv TypVal) FuncCall(in []any) ([]any, bool)

FuncCall use param (in []any), to call func, result is ([]any, called)

func (TypVal) FuncCanSet added in v1.2.15

func (tv TypVal) FuncCanSet() bool

func (TypVal) FuncSet

func (tv TypVal) FuncSet(fn any)

FuncSet set func self to fn

func (TypVal) HasMethod added in v1.2.7

func (tv TypVal) HasMethod(methodName string, in, out []any) bool

HasMethod check if Typ has method by methodName and in: methodParamInType, out: methodResultOut

func (TypVal) MapAssign

func (tv TypVal) MapAssign(key, elem any)

MapAssign key exist assign

func (TypVal) MapAssign2

func (tv TypVal) MapAssign2(key, elem any)

MapAssign2 dry assign

func (TypVal) MapAssignMake added in v1.1.9

func (tv TypVal) MapAssignMake(key, elem any)

MapAssignMake if nil make map

func (TypVal) MapDelete

func (tv TypVal) MapDelete(key any)

MapDelete dry delete

func (TypVal) MapDeleteElemIsZeroKeys added in v1.2.8

func (tv TypVal) MapDeleteElemIsZeroKeys()

Only support any-int, any-uint, any-string

func (TypVal) MapKeyOk

func (tv TypVal) MapKeyOk(key any) bool

MapKeyOk key exist

func (TypVal) SliceAppend added in v1.2.1

func (tv TypVal) SliceAppend(elem any)

SliceAppend stand for: sl = append(sl, elem)

func (TypVal) SliceAppendSlice added in v1.2.1

func (tv TypVal) SliceAppendSlice(slAdd any)

SliceAppendSlice stand for: sl = append(sl, slAdd...)

func (TypVal) SliceAppends added in v1.2.1

func (tv TypVal) SliceAppends(elem ...any)

SliceAppends stand for: sl = append(sl, elem...)

func (TypVal) SliceOrArrayStructGetTagVal added in v1.2.11

func (tv TypVal) SliceOrArrayStructGetTagVal(app, key string) []string

SliceOrArrayStructGetTagVal get slice-struct or array-struct fields app key tag value

func (TypVal) SliceOrArrayStructHasFields added in v1.2.6

func (tv TypVal) SliceOrArrayStructHasFields(fields map[string]any) map[string]struct{}

SliceOrArrayStructHasFields like StructHasFields only check type not value

func (TypVal) SliceSet

func (tv TypVal) SliceSet(index int, elem any)

SliceSet set slice index to elem -> slice[index] = elem

func (TypVal) SliceSetMake added in v1.1.9

func (tv TypVal) SliceSetMake(index int, elem any)

SliceSetMake set slice index to elem -> slice[index] = elem allow slice is nil, if slice is nil make a slice allow index out of bound capacity, auto resize length and resize capacity

func (TypVal) SliceSetOoc added in v1.1.9

func (tv TypVal) SliceSetOoc(index int, elem any)

SliceSetOoc set slice index to elem -> slice[index] = elem that allow length and capacity out of bound

func (TypVal) SliceSetOol added in v1.1.9

func (tv TypVal) SliceSetOol(index int, elem any)

SliceSetOol set slice index to elem -> slice[index] = elem that allow length out of bound, but capacity inbound

func (TypVal) SliceSetStructFields

func (tv TypVal) SliceSetStructFields(fields map[string]any)

SliceSetStructFields set slice struct fields fieldName to fieldVal

func (TypVal) SliceStruct2SliceMap added in v1.2.3

func (tv TypVal) SliceStruct2SliceMap(fields map[string]struct{}) []map[string]any

SliceStruct2SliceMap Struct to map[fieldName]fieldValue

func (TypVal) SliceStructFieldValues added in v1.2.2

func (tv TypVal) SliceStructFieldValues(fieldName string) map[any]struct{}

SliceStructFieldValues []Struct get field(fieldName) values

func (TypVal) SliceStructFieldsValues added in v1.2.1

func (tv TypVal) SliceStructFieldsValues(fields map[string]struct{}) map[string]map[any]struct{}

SliceStructFieldsValues []Struct get fields(fieldNames) values

func (TypVal) Struct2Map added in v1.2.3

func (tv TypVal) Struct2Map(fields map[string]struct{}) map[string]any

func (TypVal) Struct2MapAll added in v1.2.29

func (tv TypVal) Struct2MapAll() map[string]any

func (TypVal) Struct2TagKeyMap added in v1.2.7

func (tv TypVal) Struct2TagKeyMap(app, key string) map[string]any

Struct2TagKeyMap +example `app:"unique:1;union_unique:0;field:name;"` `app:"unique:0;union_unique:1;field:name_part1;follow:-"` `app:"unique:0;union_unique:1;field:name_part1;follow:+"`

func (TypVal) StructCanSet added in v1.2.15

func (tv TypVal) StructCanSet() bool

func (TypVal) StructFieldValue added in v1.2.2

func (tv TypVal) StructFieldValue(fieldName string) any

func (TypVal) StructFieldValueAll added in v1.2.29

func (tv TypVal) StructFieldValueAll() map[string]reflect.Value

func (TypVal) StructFieldsIndex added in v1.2.1

func (tv TypVal) StructFieldsIndex() map[string][]int

StructFieldsIndex +result: fieldName -> fieldIndex

func (TypVal) StructFieldsType added in v1.2.1

func (tv TypVal) StructFieldsType() map[string]reflect.Type

StructFieldsType +result: fieldName -> fieldType

func (TypVal) StructFieldsValues added in v1.2.1

func (tv TypVal) StructFieldsValues(fieldIndex map[string][]int) map[string]map[any]struct{}

func (TypVal) StructHasFields added in v1.2.6

func (tv TypVal) StructHasFields(fields map[string]any) map[string]struct{}

StructHasFields match all fields by FieldName and FieldType fields FieldValue must direct

func (TypVal) StructMakeSlice added in v1.2.5

func (tv TypVal) StructMakeSlice(length, capacity int) any

StructMakeSlice Type Struct -> return []Struct

func (TypVal) StructParseTag added in v1.2.0

func (tv TypVal) StructParseTag(app string) (tagMap TagMap)

StructParseTag +example `app1:"tag_val1" app2:"tag_val2" app3:"tag_val3"`

func (TypVal) StructParseTag2 added in v1.2.14

func (tv TypVal) StructParseTag2(app0, key0, app1, key1 string) (vv0, vv1 TagVV)

func (TypVal) StructParseTagKV added in v1.2.15

func (tv TypVal) StructParseTagKV(app string) (fieldTagKv FieldTagKV)

StructParseTagKV +param: an app +result: FieldTagKV

func (TypVal) StructParseTagKV2 added in v1.2.29

func (tv TypVal) StructParseTagKV2(app string) (fieldTagKv FieldTagKV)

StructParseTagKV2 +param: an app +result: FieldTagKV

func (TypVal) StructSet

func (tv TypVal) StructSet(srcV any)

func (TypVal) StructSetFields

func (tv TypVal) StructSetFields(fields map[string]any)

StructSetFields +param: fieldName -> fieldValue

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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