reflect

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT, Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

View Source
const (
	F_direct    = 1 << 5
	F_kind_mask = (1 << 5) - 1
)

Type.KindFlags const

Variables

View Source
var (
	PtrOffset = func() uintptr {
		return unsafe.Offsetof(e.Value)
	}()
	KindOffset = func() uintptr { return unsafe.Offsetof(e.Type.KindFlags) }()
	ElemOffset = func() uintptr {
		return unsafe.Offsetof(new(PtrType).Elem)
	}()
	SliceDataOffset = func() uintptr {
		return unsafe.Offsetof(new(reflect.SliceHeader).Data)
	}()
)

Functions

func CanCast

func CanCast(t1, t2 reflect.Type, strict bool) bool

func CopyFieldValueByType

func CopyFieldValueByType(src any, sub any) bool

CopyFieldValueByType 根据类型复制字段 参数均为指针,sub类型为src field的类型

func CopyStruct

func CopyStruct(src any, dest any) error

Copy 复制结构体,浅表复制

func DerefType

func DerefType(typ reflect.Type) reflect.Type

获取引用类型的原始类型

func DerefValue

func DerefValue(v reflect.Value) reflect.Value

DereferenceValue dereference and unpack interface, get the underlying non-pointer and non-interface value.

func FuncAddr

func FuncAddr(f interface{}) unsafe.Pointer

func GetFuncPC

func GetFuncPC(fn interface{}) uintptr

func GetSubField

func GetSubField[T any](v any) *T

func Hash

func Hash(v any) uint32

func InitPtr

func InitPtr(v reflect.Value) reflect.Value

func InitStruct

func InitStruct(v reflect.Value)

func RuntimeTypeID

func RuntimeTypeID(t reflect.Type) uintptr

func SetField

func SetField(structValue reflect.Value, name string, value any) error

func SetField2

func SetField2(o any, field string, v any)

func SetSubField

func SetSubField(src any, sub any) bool

SetSubField 设置字段值 参数均为指针,dst类型为src field的类型

func SetSubFieldValue

func SetSubFieldValue(srcValue reflect.Value, subValue reflect.Value) bool

SetSubFieldValue 设置字段值 subValue为srcValue field的类型

func TypeInfo

func TypeInfo(v interface{})

Types

type ArrayType

type ArrayType struct {
	Type
	Elem  *Type // array element type
	Slice *Type // Slice type
	Len   uintptr
}

ArrayType represents a fixed array type.

type ChanType

type ChanType struct {
	Type
	Elem *Type   // channel element type
	Dir  uintptr // channel direction (ChanDir)
}

ChanType represents a channel type.

type Eface

type Eface struct {
	Type  *Type
	Value unsafe.Pointer
}

func UnpackEface

func UnpackEface(v interface{}) Eface

func (Eface) Pack

func (self Eface) Pack() (v any)

type Flag

type Flag uintptr
const (
	FlagKindWidth        = 5 // there are 27 kinds
	FlagKindMask    Flag = 1<<FlagKindWidth - 1
	FlagStickyRO    Flag = 1 << 5
	FlagEmbedRO     Flag = 1 << 6
	FlagIndir       Flag = 1 << 7
	FlagAddr        Flag = 1 << 8
	FlagMethod      Flag = 1 << 9
	FlagMethodShift      = 10
	FlagRO          Flag = FlagStickyRO | FlagEmbedRO
)

type Func

type Func struct {
	Entry   uintptr // start pc
	Nameoff int32   // function name

	Args        int32  // in/out args size
	Deferreturn uint32 // offset of start of a deferreturn call instruction from entry, if any.

	Pcsp     uint32
	Pcfile   uint32
	Pcln     uint32
	Npcdata  uint32
	CuOffset uint32 // runtime.cutab offset of this function's CU
	FuncID   FuncID // set for certain special runtime functions

	Nfuncdata uint8 // must be last
	// contains filtered or unexported fields
}

type FuncID

type FuncID uint8

type FuncType

type FuncType struct {
	Type
	InCount  uint16
	OutCount uint16 // top bit is set if last input parameter is ...
}

FuncType represents a function type.

A *Rtype for each in and out parameter is stored in an array that directly follows the funcType (and possibly its uncommonType). So a function type with one method, one input, and one output is:

struct {
	funcType
	uncommonType
	[2]*Rtype    // [0] is in, [1] is out
}

type IMethod

type IMethod struct {
	Name NameOff // name of method
	Typ  TypeOff // .(*FuncType) underneath
}

IMethod represents a method on an interface type

type Iface

type Iface struct {
	Itab  *Itab
	Value unsafe.Pointer
}

func AssertI2I added in v0.0.42

func AssertI2I(t *Type, i Iface) (r Iface)

func UnpackIface

func UnpackIface(v interface{}) Iface

type InterfaceType

type InterfaceType struct {
	Type
	PkgPath *byte     // import path
	Methods []IMethod // sorted by hash
}

interfaceType represents an interface type.

func IfaceType

func IfaceType(t *Type) *InterfaceType

type Itab

type Itab struct {
	Inter  *InterfaceType
	Type   *Type
	Hash   uint32 // copy of _type.hash. Used for type switches.
	Unused [4]byte
	Fun    [1]uintptr // variable sized
}

func GetItab added in v0.3.2

func GetItab(inter *InterfaceType, typ *Type, canfail bool) *Itab

type KindHandler

type KindHandler [reflect.UnsafePointer + 1][]ValueRecursionHandler

func (*KindHandler) AddHandler

func (k *KindHandler) AddHandler(kind reflect.Kind, v ValueRecursionHandler)

func (*KindHandler) FillFloat

func (k *KindHandler) FillFloat(v ValueRecursionHandler)

func (*KindHandler) FillInt

func (k *KindHandler) FillInt(v ValueRecursionHandler)

func (*KindHandler) FillUint

func (k *KindHandler) FillUint(v ValueRecursionHandler)

func (*KindHandler) Handle

func (k *KindHandler) Handle(value reflect.Value)

type Map

type Map struct {
	Count      int
	Flags      uint8
	B          uint8
	Overflow   uint16
	Hash0      uint32
	Buckets    unsafe.Pointer
	OldBuckets unsafe.Pointer
	Evacuate   uintptr
	Extra      unsafe.Pointer
}

type MapIterator

type MapIterator struct {
	K           unsafe.Pointer
	V           unsafe.Pointer
	T           *MapType
	H           *Map
	Buckets     unsafe.Pointer
	Bptr        *unsafe.Pointer
	Overflow    *[]unsafe.Pointer
	OldOverflow *[]unsafe.Pointer
	StartBucket uintptr
	Offset      uint8
	Wrapped     bool
	B           uint8
	I           uint8
	Bucket      uintptr
	CheckBucket uintptr
}

type MapType

type MapType struct {
	Type
	Key    *Type // map key type
	Elem   *Type // map element (value) type
	Bucket *Type // internal bucket structure
	// function for hashing keys (ptr to key, seed) -> hash
	Hasher     func(unsafe.Pointer, uintptr) uintptr
	KeySize    uint8  // size of key slot
	ValueSize  uint8  // size of value slot
	BucketSize uint16 // size of bucket
	Flags      uint32
}

MapType represents a map type.

func ToMapType

func ToMapType(t *Type) *MapType

func (*MapType) IndirectElem

func (self *MapType) IndirectElem() bool

type NameOff

type NameOff int32 // offset to a name

type PtrType

type PtrType struct {
	Type
	Elem *Type // pointer element (pointed at) type
}

PtrType represents a pointer type.

type Slice

type Slice struct {
	Ptr unsafe.Pointer
	Len int
	Cap int
}

type SliceType

type SliceType struct {
	Type
	Elem *Type // Slice element type
}

SliceType represents a Slice type.

type String

type String struct {
	Ptr unsafe.Pointer
	Len int
}

type StructField

type StructField struct {
	Name        *byte   // name is always non-empty
	Typ         *Type   // type of field
	OffsetEmbed uintptr // byte offset of field<<1 | isEmbedded
}

Struct field

type StructType

type StructType struct {
	Type
	PkgPath *byte
	Fields  []StructField // sorted by offset
}

StructType represents a struct type.

type TextOff

type TextOff int32 // offset from top of text section

type Type

type Type struct {
	Size       uintptr
	Ptrdata    uintptr // number of bytes in the type that can contain pointers
	Hash       uint32  // hash of type; avoids computation in hash tables
	Flags      uint8   // extra type information flags
	Align      uint8   // alignment of variable with this type
	FieldAlign uint8   // alignment of struct field with this type
	KindFlags  uint8   // enumeration for C
	// function for comparing objects of this type
	// (ptr to object A, ptr to object B) -> ==?
	Equal func(unsafe.Pointer, unsafe.Pointer) bool
	// gcdata stores the GC type data for the garbage collector.
	// If the KindGCProg bit is set in kind, gcdata is a GC program.
	// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
	Gcdata    *byte   // garbage collection data
	Str       NameOff // string form
	PtrToThis TypeOff // type for pointer to this type, may be zero
}

Type must be kept in sync with ../runtime/type.go:/^type._type.

func PtrElem

func PtrElem(t *Type) *Type

func TypeByOff

func TypeByOff(section unsafe.Pointer, off int32) *Type

func UnpackType

func UnpackType(t reflect.Type) *Type

func (*Type) Indirect

func (self *Type) Indirect() bool

func (*Type) IsNamed

func (self *Type) IsNamed() bool

func (*Type) Kind

func (self *Type) Kind() reflect.Kind

func (*Type) Pack

func (self *Type) Pack() (t reflect.Type)

func (*Type) String

func (self *Type) String() string

type TypeOff

type TypeOff int32 // offset to an *Rtype

type Value

type Value struct {
	Typ *Type
	Ptr unsafe.Pointer
	Flag
}

func ConvertValue

func ConvertValue(v reflect.Value) Value

func ValueOf

func ValueOf(v interface{}) Value

func (Value) Kind

func (v Value) Kind() reflect.Kind

Kind gets the reflect.Kind fastly.

func (Value) Pointer

func (v Value) Pointer() uintptr

Pointer gets the pointer of i. NOTE:

*T and T, gets diffrent pointer

type ValueHandler

type ValueHandler func(reflect.Value)

type ValueRecursionHandler

type ValueRecursionHandler func(reflect.Value, ValueHandler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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