safe_reflect

package
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: Apache-2.0 Imports: 3 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

Variables

This section is empty.

Functions

func ArrayMake added in v1.2.0

func ArrayMake(length int, elem any) any

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 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 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 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

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 MetaFunc added in v1.2.0

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

type TagMap

type TagMap map[string]string

TagMap in an app -> map[field]tag_val

func (TagMap) ParseGetVal added in v1.2.0

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

ParseGetVal +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 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 (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)

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

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

func (TypVal) SliceAppendSlice added in v1.2.1

func (tv TypVal) SliceAppendSlice(sl any)

func (TypVal) SliceAppends added in v1.2.1

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

func (TypVal) SliceOrArrayStructHasFields added in v1.2.6

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

like TypVal.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

func (TypVal) SliceStructFieldValues added in v1.2.2

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

func (TypVal) SliceStructFieldsValues added in v1.2.1

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

func (TypVal) Struct2Map added in v1.2.3

func (tv TypVal) Struct2Map(fields map[string]struct{}) 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) StructFieldValue added in v1.2.2

func (tv TypVal) StructFieldValue(fieldName string) any

func (TypVal) StructFieldsIndex added in v1.2.1

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

func (TypVal) StructFieldsType added in v1.2.1

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

func (TypVal) StructFieldsValues added in v1.2.1

func (tv TypVal) StructFieldsValues(fields 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

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) StructSet

func (tv TypVal) StructSet(srcV any)

func (TypVal) StructSetFields

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

Jump to

Keyboard shortcuts

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