reflecti

package
v0.0.0-...-1f4ee57 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NoError           = "NoError"
	JsonError         = "JsonError"
	MethodNotFound    = "MethodNotFound"
	ParameterNotMatch = "ParameterNotMatch"
)

Variables

View Source
var (
	PtrOffset = func() uintptr {
		return unsafe.Offsetof(e.Word)
	}()
	KindOffset = func() uintptr {
		return unsafe.Offsetof(e.Typ.Kind)
	}()
	ElemOffset = func() uintptr {
		return unsafe.Offsetof(new(PtrType).Elem)
	}()
	SliceDataOffset = func() uintptr {
		return unsafe.Offsetof(new(reflect.SliceHeader).Data)
	}()
)
View Source
var Converters = map[reflect.Kind]Converter{
	reflect.Bool:    convertBool,
	reflect.Float32: convertFloat32,
	reflect.Float64: convertFloat64,
	reflect.Int:     convertInt,
	reflect.Int8:    convertInt8,
	reflect.Int16:   convertInt16,
	reflect.Int32:   convertInt32,
	reflect.Int64:   convertInt64,
	reflect.String:  convertString,
	reflect.Uint:    convertUint,
	reflect.Uint8:   convertUint8,
	reflect.Uint16:  convertUint16,
	reflect.Uint32:  convertUint32,
	reflect.Uint64:  convertUint64,
}

Default converters for basic types.

Functions

func ContainType

func ContainType()

func CopyProperties

func CopyProperties(source interface{}, target interface{}) error

func CreateFuncForCodePtr

func CreateFuncForCodePtr(outFuncPtr interface{}, entry uintptr)

CreateFuncForCodePtr is given a code pointer and creates a function value that uses that pointer. The outFun argument should be a pointer to a function of the proper type (e.g. the address of a local variable), and will be set to the result function value.

func DereferenceInterfaceValue

func DereferenceInterfaceValue(v reflect.Value) reflect.Value

DereferenceInterfaceValue returns the value of the underlying type that implements the interface v.

func DereferencePtrValue

func DereferencePtrValue(v reflect.Value) reflect.Value

DereferencePtrValue returns the underlying non-pointer type value.

func DereferenceType

func DereferenceType(t reflect.Type) reflect.Type

func DereferenceValue

func DereferenceValue(v reflect.Value) reflect.Value

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

func FindFuncWithName

func FindFuncWithName(name string) (uintptr, error)

FindFuncWithName searches through the moduledata table created by the linker and returns the function's code pointer. If the function was not found, it returns an error. Since the data structures here are not exported, we copy them below (and they need to stay in sync or else things will fail catastrophically).

func GetCustomizeTag

func GetCustomizeTag(customize, key string) string

func GetDereferenceType

func GetDereferenceType(typ reflect.Type) reflect.Type

func GetFieldValue

func GetFieldValue(src interface{}, dst interface{}) bool

获取子类型的值 参数父类型,子类型的指针

func GetFunc

func GetFunc(outFuncPtr interface{}, name string) error

func Hash

func Hash(v interface{}) uint32

func OriginalType

func OriginalType(typ reflect.Type) reflect.Type

func RuntimeTypeID

func RuntimeTypeID(t reflect.Type) uintptr

func Set

func Set(o interface{}, field string, v interface{})

func SetField

func SetField(src interface{}, sub interface{}) bool

func SetFieldValue

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

func SetStructByJSON

func SetStructByJSON(obj interface{}, mapData map[string]interface{}) error

SetStructByJSON 由json对象生成 struct

func TypeInfo

func TypeInfo(v interface{})

Types

type Bitvector

type Bitvector struct {
	// contains filtered or unexported fields
}

type ChanType

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

ChanType represents a channel type.

type Converter

type Converter func(string) reflect.Value

type Eface

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

type EmptyInterface

type EmptyInterface struct {
	Typ  *Rtype
	Word unsafe.Pointer
}

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 Functab

type Functab struct {
	// contains filtered or unexported fields
}

type Iface

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

type Imethod

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

imethod represents a method on an interface type

type InterfaceType

type InterfaceType struct {
	Rtype
	PkgPath Name      // import path
	Methods []Imethod // sorted by hash
}

interfaceType represents an interface type.

type Itab

type Itab struct {
	Inter  *InterfaceType
	Type   *Type
	Link   *Itab
	Hash   uint32 // copy of _type.hash. Used for type switches.
	Bad    bool   // type does not implement interface
	Inhash bool   // has this Itab been added to hash?
	Unused [2]byte
	Fun    [1]uintptr // variable sized
}

type MapType

type MapType struct {
	Rtype
	Key    *Rtype // map key type
	Elem   *Rtype // map element (value) type
	Bucket *Rtype // 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.

type Moduledata

type Moduledata struct {
	// contains filtered or unexported fields
}
var Firstmoduledata Moduledata

type Name

type Name struct {
	Bytes *byte
}

type NameOff

type NameOff int32 // offset to a name

type NonEmptyInterface

type NonEmptyInterface struct {
	// see ../runtime/iface.go:/Itab
	Itab *struct {
		Ityp *Rtype // static interface type
		Typ  *Rtype // dynamic concrete type
		Hash uint32 // copy of typ.hash

		Fun [100000]unsafe.Pointer // method table
		// contains filtered or unexported fields
	}
	Word unsafe.Pointer
}

NonEmptyInterface is the header for an interface value with methods.

type PtrType

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

PtrType represents a pointer type.

type ReflectInvoker

type ReflectInvoker struct {
	Methods map[string]reflect.Value
}

func NewReflectinvoker

func NewReflectinvoker() *ReflectInvoker

func (*ReflectInvoker) InvokeByInterfaceArgs

func (r *ReflectInvoker) InvokeByInterfaceArgs(funcName string, Params []interface{}) []reflect.Value

func (*ReflectInvoker) InvokeByJson

func (r *ReflectInvoker) InvokeByJson(byteData []byte) []byte

func (*ReflectInvoker) InvokeByReflectArgs

func (r *ReflectInvoker) InvokeByReflectArgs(funcName string, par []reflect.Value) []reflect.Value

func (*ReflectInvoker) RegisterMethod

func (r *ReflectInvoker) RegisterMethod(v interface{})

type Request

type Request struct {
	FuncName string        `json:"funcName"`
	Params   []interface{} `json:"params"`
}

type Response

type Response struct {
	FuncName string        `json:"funcName"`
	Result   []interface{} `json:"result"`
	ErrorMsg string        `json:"errorMsg"`
}

func InvokeByValues

func InvokeByValues(method reflect.Value, params []reflect.Value) *Response

type Rtype

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

	Align      uint8 // alignment of variable with this type
	FieldAlign uint8 // alignment of struct field with this type
	Kind       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    *byte   // garbage collection data
	Str       NameOff // string form
	PtrToThis TypeOff // type for pointer to this type, may be zero
	// contains filtered or unexported fields
}

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

func RtypeOff

func RtypeOff(section unsafe.Pointer, off int32) *Rtype

type SliceType

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

SliceType represents a Slice type.

type StructField

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

Struct field

type StructTag

type StructTag string

func (StructTag) Lookup

func (tag StructTag) Lookup(key string) (value string, ok bool)

type StructType

type StructType struct {
	Rtype
	PkgPath Name
	Fields  []StructField // sorted by offset
}

StructType represents a struct type.

type Tag

type Tag struct {
	// Key is the tag key, such as json, xml, etc..
	// i.e: `json:"foo,omitempty". Here key is: "json"
	Key string

	// Name is a part of the value
	// i.e: `json:"foo,omitempty". Here name is: "foo"
	Name string

	// Options is a part of the value. It contains a Slice of tag options i.e:
	// `json:"foo,omitempty". Here options is: ["omitempty"]
	Options []string
}

Tag defines a single struct's string literal tag

func (*Tag) GoString

func (t *Tag) GoString() string

GoString implements the fmt.GoStringer interface

func (*Tag) HasOption

func (t *Tag) HasOption(opt string) bool

HasOption returns true if the given option is available in options

func (*Tag) String

func (t *Tag) String() string

String reassembles the tag into a valid tag field representation

func (*Tag) Value

func (t *Tag) Value() string

Value returns the raw value of the tag, i.e. if the tag is `json:"foo,omitempty", the Value is "foo,omitempty"

type Tags

type Tags struct {
	// contains filtered or unexported fields
}

Tags represent a set of tags from a single struct field

func Parse

func Parse(tag string) (*Tags, error)

Parse parses a single struct field tag and returns the set of tags.

func (*Tags) AddOptions

func (t *Tags) AddOptions(key string, options ...string)

AddOptions adds the given option for the given key. If the option already exists it doesn't add it again.

func (*Tags) Delete

func (t *Tags) Delete(keys ...string)

Delete deletes the tag for the given keys

func (*Tags) DeleteOptions

func (t *Tags) DeleteOptions(key string, options ...string)

DeleteOptions deletes the given options for the given key

func (*Tags) Get

func (t *Tags) Get(key string) (*Tag, error)

Get returns the tag associated with the given key. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the tag exists or not (which the return value is nil).

func (*Tags) Keys

func (t *Tags) Keys() []string

Tags returns a Slice of tags. The order is the original tag order unless it was changed.

func (*Tags) Len

func (t *Tags) Len() int

func (*Tags) Less

func (t *Tags) Less(i int, j int) bool

func (*Tags) Set

func (t *Tags) Set(tag *Tag) error

Set sets the given tag. If the tag key already exists it'll override it

func (*Tags) String

func (t *Tags) String() string

String reassembles the tags into a valid literal tag field representation

func (*Tags) Swap

func (t *Tags) Swap(i int, j int)

func (*Tags) Tags

func (t *Tags) Tags() []*Tag

Tags returns a Slice of tags. The order is the original tag order unless it was changed.

type Tflag

type Tflag uint8

type Type

type Type struct {
	Size       uintptr
	Ptrdata    uintptr // size of memory prefix holding all pointers
	Hash       uint32
	Tflag      Tflag
	Align      uint8
	FieldAlign uint8
	Kind       uint8
	// 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
	Str       NameOff
	PtrToThis TypeOff
}

type TypeOff

type TypeOff int32 // offset to an *Rtype

type Value

type Value struct {
	Typ *Rtype
	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

Directories

Path Synopsis
Package tagexpr is an interesting go struct tag expression syntax for field validation, etc.
Package tagexpr is an interesting go struct tag expression syntax for field validation, etc.

Jump to

Keyboard shortcuts

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