reflection

package module
v0.0.0-...-299589b Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 7 Imported by: 5

README

reflection

Documentation

Index

Constants

View Source
const (
	ObjectUnknown = iota
	ObjectSimpletype
	ObjectStruct
	ObjectSlice
	ObjectMap

	ObjectCustom = 50000
)

Variables

View Source
var (
	EMPTY_STRING       string
	BOOL_DEFAULT       bool
	BYTE_DEFAULT       byte
	COMPLEX64_DEFAULT  complex64
	COMPLEX128_DEFAULT complex128
	FLOAT32_DEFAULT    float32
	FLOAT64_DEFAULT    float64
	INT64_DEFAULT      int64
	UINT64_DEFAULT     uint64
	INT32_DEFAULT      int32
	UINT32_DEFAULT     uint32
	INT16_DEFAULT      int16
	UINT16_DEFAULT     uint16
	INT8_DEFAULT       int8
	UINT8_DEFAULT      uint8
	INT_DEFAULT        int
	UINT_DEFAULT       uint
	TIME_DEFAULT       time.Time
)
View Source
var (
	IntKind   = IntType.Kind()
	Int8Kind  = Int8Type.Kind()
	Int16Kind = Int16Type.Kind()
	Int32Kind = Int32Type.Kind()
	Int64Kind = Int64Type.Kind()

	UintKind   = UintType.Kind()
	Uint8Kind  = Uint8Type.Kind()
	Uint16Kind = Uint16Type.Kind()
	Uint32Kind = Uint32Type.Kind()
	Uint64Kind = Uint64Type.Kind()

	Float32Kind = Float32Type.Kind()
	Float64Kind = Float64Type.Kind()

	Complex64Kind  = Complex64Type.Kind()
	Complex128Kind = Complex128Type.Kind()

	StringKind = StringType.Kind()
	BoolKind   = BoolType.Kind()
	ByteKind   = ByteType.Kind()
	BytesKind  = BytesType.Kind()

	TimeKind = TimeType.Kind()
)
View Source
var SqlType2GoType = map[string]reflect.Type{
	"int":                IntType,
	"integer":            IntType,
	"tinyint":            IntType,
	"smallint":           IntType,
	"mediumint":          IntType,
	"bigint":             Int64Type,
	"int unsigned":       UintType,
	"integer unsigned":   UintType,
	"tinyint unsigned":   UintType,
	"smallint unsigned":  UintType,
	"mediumint unsigned": UintType,
	"bigint unsigned":    Uint64Type,
	"bit":                Int8Type,
	"bool":               BoolType,
	"enum":               StringType,
	"set":                StringType,
	"varchar":            StringType,
	"char":               StringType,
	"tinytext":           StringType,
	"mediumtext":         StringType,
	"text":               StringType,
	"longtext":           StringType,
	"blob":               StringType,
	"tinyblob":           StringType,
	"mediumblob":         StringType,
	"longblob":           StringType,
	"date":               TimeType,
	"datetime":           TimeType,
	"timestamp":          TimeType,
	"time":               TimeType,
	"float":              Float64Type,
	"double":             Float64Type,
	"decimal":            Float64Type,
	"binary":             StringType,
	"varbinary":          StringType,
}
View Source
var (
	StructAliasTag = "alias"
)

Functions

func CanSet

func CanSet(i interface{}) bool

func CheckValueNilSafe

func CheckValueNilSafe(vi reflect.Value) bool

func CopyMap

func CopyMap(dest, src reflect.Value) (int, error)

func CopyMapInterface

func CopyMapInterface(dest, src interface{}) (int, error)

func CopySlice

func CopySlice(dest, src reflect.Value) (int, error)

func CopySliceInterface

func CopySliceInterface(dest, src interface{}) (int, error)

func GetBeanClassName

func GetBeanClassName(model interface{}) string

func GetTypeClassName

func GetTypeClassName(rt reflect.Type) string

func IsNil

func IsNil(i interface{}) bool

func IsSimpleType

func IsSimpleType(t reflect.Type) bool

IsSimpleType 是否是数据库使用的简单类型,注意不能是PTR

func MustPtr

func MustPtr(bean interface{}) error

func MustPtrValue

func MustPtrValue(beanValue reflect.Value) error

func New

func New(t reflect.Type) interface{}

func NewValue

func NewValue(t reflect.Type) reflect.Value

func SetFieldValue

func SetFieldValue(dst reflect.Value, fieldName string, value reflect.Value) error

func SetFieldValueByTag

func SetFieldValueByTag(dst reflect.Value, tags string, value reflect.Value, tagName string) error

func SetFieldValueEx

func SetFieldValueEx(v reflect.Value, fieldName string, value reflect.Value, tagName string, modifier func(string) string) error

func SetModelNameType

func SetModelNameType(mtype reflect.Type)

func SetOrCopyMap

func SetOrCopyMap(dest, src reflect.Value, set bool) (int, error)

func SetOrCopySlice

func SetOrCopySlice(dest, src reflect.Value, set bool) (int, error)

func SetStrcutFieldValue

func SetStrcutFieldValue(o interface{}, fieldStr string, value interface{}) error

func SetStrcutFieldValueByTag

func SetStrcutFieldValueByTag(o interface{}, matchStr string, value interface{}, tagName string) error

func SetStrcutFieldValueEx

func SetStrcutFieldValueEx(o interface{}, fieldName string, value interface{}, tagName string, modifier func(string) string) error

func SetValue

func SetValue(dst reflect.Value, value reflect.Value) bool

func SetValueInterface

func SetValueInterface(dst interface{}, v interface{}) error

Types

type MapInfo

type MapInfo struct {
	//包含pkg的名称
	ClassName string
	//元素类型
	ElemType reflect.Type

	Settable
	Newable
}

func (*MapInfo) AddValue

func (mapInfo *MapInfo) AddValue(v reflect.Value) bool

func (*MapInfo) CanAddValue

func (mapInfo *MapInfo) CanAddValue() bool

func (*MapInfo) CanSet

func (mapInfo *MapInfo) CanSet(v reflect.Value) bool

func (*MapInfo) CanSetField

func (mapInfo *MapInfo) CanSetField() bool

func (*MapInfo) GetClassName

func (mapInfo *MapInfo) GetClassName() string

func (*MapInfo) Kind

func (mapInfo *MapInfo) Kind() int

func (*MapInfo) New

func (mapInfo *MapInfo) New() Object

func (*MapInfo) NewElem

func (mapInfo *MapInfo) NewElem() Object

NewElem FIXME: return nil,需要对map元素解析

func (*MapInfo) SetField

func (mapInfo *MapInfo) SetField(name string, vv reflect.Value) bool

func (*MapInfo) SetValue

func (mapInfo *MapInfo) SetValue(v reflect.Value) bool

SetValue TODO: 目前仅支持map[string]interface{},需增加其他类型支持

type Newable

type Newable struct {
	Type reflect.Type
}

func (*Newable) NewValue

func (newable *Newable) NewValue() reflect.Value

type Object

type Object interface {
	Kind() int
	// New 生成克隆空对象
	New() Object
	// NewElem 获得对象的元素
	NewElem() Object
	// SetField 设置字段
	SetField(name string, v reflect.Value) bool
	// AddValue 添加元素值
	AddValue(v reflect.Value) bool
	// GetClassName 获得对象名称
	GetClassName() string
	// CanSetField 是否能设置field
	CanSetField() bool
	// CanAddValue 是否能添加值
	CanAddValue() bool

	// NewValue 获得值
	NewValue() reflect.Value

	// CanSet 是否能够设置
	CanSet(v reflect.Value) bool
	// SetValue 设置值
	SetValue(v reflect.Value) bool
	// GetValue 获得值
	GetValue() reflect.Value
	// ResetValue 变换value对象
	ResetValue(v reflect.Value)
}

func GetObjectInfo

func GetObjectInfo(model interface{}) (Object, error)

func GetReflectMapInfo

func GetReflectMapInfo(rt reflect.Type, rv reflect.Value) (Object, error)

func GetReflectObjectInfo

func GetReflectObjectInfo(rt reflect.Type, rv reflect.Value) (Object, error)

func GetReflectSimpleTypeInfo

func GetReflectSimpleTypeInfo(rt reflect.Type, rv reflect.Value) (Object, error)

func GetReflectSliceInfo

func GetReflectSliceInfo(rt reflect.Type, rv reflect.Value) (Object, error)

type Settable

type Settable struct {
	//值
	Value reflect.Value
}

func (*Settable) CanSet

func (settable *Settable) CanSet(v reflect.Value) bool

func (*Settable) GetValue

func (settable *Settable) GetValue() reflect.Value

func (*Settable) ResetValue

func (settable *Settable) ResetValue(v reflect.Value)

func (*Settable) SetValue

func (settable *Settable) SetValue(v reflect.Value) bool

type SimpleTypeInfo

type SimpleTypeInfo struct {
	//包含pkg的名称
	ClassName string

	Settable
	Newable
}

func (*SimpleTypeInfo) AddValue

func (simpleTypeInfo *SimpleTypeInfo) AddValue(v reflect.Value) bool

func (*SimpleTypeInfo) CanAddValue

func (simpleTypeInfo *SimpleTypeInfo) CanAddValue() bool

func (*SimpleTypeInfo) CanSet

func (simpleTypeInfo *SimpleTypeInfo) CanSet(v reflect.Value) bool

CanSet 直接返回true,需要通过SetValue判断

func (*SimpleTypeInfo) CanSetField

func (simpleTypeInfo *SimpleTypeInfo) CanSetField() bool

func (*SimpleTypeInfo) GetClassName

func (simpleTypeInfo *SimpleTypeInfo) GetClassName() string

func (*SimpleTypeInfo) Kind

func (simpleTypeInfo *SimpleTypeInfo) Kind() int

func (*SimpleTypeInfo) New

func (simpleTypeInfo *SimpleTypeInfo) New() Object

func (*SimpleTypeInfo) NewElem

func (simpleTypeInfo *SimpleTypeInfo) NewElem() Object

func (*SimpleTypeInfo) SetField

func (simpleTypeInfo *SimpleTypeInfo) SetField(name string, vv reflect.Value) bool

func (*SimpleTypeInfo) SetValue

func (simpleTypeInfo *SimpleTypeInfo) SetValue(v reflect.Value) bool

type SliceInfo

type SliceInfo struct {
	//包含pkg的名称
	ClassName string
	Elem      Object

	Settable
	Newable
}

func (*SliceInfo) AddValue

func (sliceInfo *SliceInfo) AddValue(v reflect.Value) bool

func (*SliceInfo) CanAddValue

func (sliceInfo *SliceInfo) CanAddValue() bool

func (*SliceInfo) CanSetField

func (sliceInfo *SliceInfo) CanSetField() bool

func (*SliceInfo) GetClassName

func (sliceInfo *SliceInfo) GetClassName() string

func (*SliceInfo) Kind

func (sliceInfo *SliceInfo) Kind() int

func (*SliceInfo) New

func (sliceInfo *SliceInfo) New() Object

func (*SliceInfo) NewElem

func (sliceInfo *SliceInfo) NewElem() Object

func (*SliceInfo) SetField

func (sliceInfo *SliceInfo) SetField(name string, v reflect.Value) bool

type StructInfo

type StructInfo struct {
	//包含pkg的名称
	ClassName string
	//Model名称(目前用于xml解析是struct的前缀:#{x.username} 中的x)
	Name string
	//表字段和实体字段映射关系
	FieldNameMap map[string]string

	Settable

	Newable
}

func GetReflectStructInfo

func GetReflectStructInfo(rt reflect.Type, rv reflect.Value, aliasTag ...string) (*StructInfo, error)

func GetStructInfo

func GetStructInfo(bean interface{}) (*StructInfo, error)

GetStructInfo 解析结构体,使用: 1、如果结构体中含有gobatis.ModelName类型的字段,则: a)、如果含有tag,则使用tag作为tablename; b)、如果不含有tag,则使用fieldName作为tablename。 2、如果结构体中不含有gobatis.ModelName类型的字段,则使用结构体名称作为tablename 3、如果结构体中含有column的tag,则: a)、如果tag为‘-’,则不进行columne与field的映射; b)、如果tag不为‘-’使用tag name作为column名称与field映射。 4、如果结构体中不含有column的tag,则使用field name作为column名称与field映射 5、如果字段的tag为‘-’,则不进行columne与field的映射;

func (*StructInfo) AddValue

func (structInfo *StructInfo) AddValue(v reflect.Value) bool

func (*StructInfo) CanAddValue

func (structInfo *StructInfo) CanAddValue() bool

func (*StructInfo) CanSetField

func (structInfo *StructInfo) CanSetField() bool

func (*StructInfo) FillMapValue

func (structInfo *StructInfo) FillMapValue(paramMap *map[string]interface{})

func (*StructInfo) GetClassName

func (structInfo *StructInfo) GetClassName() string

func (*StructInfo) Kind

func (structInfo *StructInfo) Kind() int

func (*StructInfo) MapValue

func (structInfo *StructInfo) MapValue() map[string]interface{}

func (*StructInfo) New

func (structInfo *StructInfo) New() Object

func (*StructInfo) NewElem

func (structInfo *StructInfo) NewElem() Object

func (*StructInfo) SetField

func (structInfo *StructInfo) SetField(name string, vv reflect.Value) bool

Jump to

Keyboard shortcuts

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