reflection

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OBJECT_UNKNOWN = iota
	OBJECT_SIMPLETYPE
	OBJECT_STRUCT
	OBJECT_SLICE
	OBJECT_MAP

	OBJECT_COSTOM = 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,
}

Functions

func CanSet

func CanSet(i interface{}) bool

func GetBeanClassName

func GetBeanClassName(model interface{}) string

func GetTypeClassName

func GetTypeClassName(rt reflect.Type) string

func IsNil

func IsNil(i interface{}) bool

func IsSimpleObject

func IsSimpleObject(bean interface{}) bool

func IsSimpleType

func IsSimpleType(t reflect.Type) bool

是否是数据库使用的简单类型,注意不能是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 ParseParams

func ParseParams(params ...interface{}) map[string]interface{}

func ParseSliceParamString

func ParseSliceParamString(src string) []string

func ReflectValue

func ReflectValue(bean interface{}) reflect.Value

func SafeSetValue

func SafeSetValue(f reflect.Value, v interface{}) bool

func SetModelNameType

func SetModelNameType(mtype reflect.Type)

func SetValue

func SetValue(f reflect.Value, v interface{}) bool

func ToSlice

func ToSlice(arr interface{}) []interface{}

Types

type MapInfo

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

	Setable
	Newable
}

func (*MapInfo) AddValue

func (o *MapInfo) AddValue(v reflect.Value)

func (*MapInfo) CanAddValue

func (o *MapInfo) CanAddValue() bool

func (*MapInfo) CanSet

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

func (*MapInfo) CanSetField

func (o *MapInfo) CanSetField() bool

func (*MapInfo) GetClassName

func (o *MapInfo) GetClassName() string

func (*MapInfo) Kind

func (o *MapInfo) Kind() int

func (*MapInfo) New

func (o *MapInfo) New() Object

func (*MapInfo) NewElem

func (o *MapInfo) NewElem() Object

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

func (*MapInfo) SetField

func (o *MapInfo) SetField(name string, ov interface{})

func (*MapInfo) SetValue

func (o *MapInfo) SetValue(v reflect.Value)

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

type Newable

type Newable struct {
	Type reflect.Type
}

func (*Newable) NewValue

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

type Object

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

	//获得值
	NewValue() reflect.Value

	//是否能够设置
	CanSet(v reflect.Value) bool
	//设置值
	SetValue(v reflect.Value)
	//获得值
	GetValue() reflect.Value
	//变换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 Setable

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

func (*Setable) CanSet

func (o *Setable) CanSet(v reflect.Value) bool

func (*Setable) GetValue

func (o *Setable) GetValue() reflect.Value

func (*Setable) ResetValue

func (o *Setable) ResetValue(v reflect.Value)

func (*Setable) SetValue

func (o *Setable) SetValue(v reflect.Value)

type SimpleTypeInfo

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

	Setable
	Newable
}

func (*SimpleTypeInfo) AddValue

func (o *SimpleTypeInfo) AddValue(v reflect.Value)

func (*SimpleTypeInfo) CanAddValue

func (o *SimpleTypeInfo) CanAddValue() bool

func (*SimpleTypeInfo) CanSet

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

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

func (*SimpleTypeInfo) CanSetField

func (o *SimpleTypeInfo) CanSetField() bool

func (*SimpleTypeInfo) GetClassName

func (o *SimpleTypeInfo) GetClassName() string

func (*SimpleTypeInfo) Kind

func (o *SimpleTypeInfo) Kind() int

func (*SimpleTypeInfo) New

func (o *SimpleTypeInfo) New() Object

func (*SimpleTypeInfo) NewElem

func (o *SimpleTypeInfo) NewElem() Object

func (*SimpleTypeInfo) SetField

func (o *SimpleTypeInfo) SetField(name string, ov interface{})

func (*SimpleTypeInfo) SetValue

func (o *SimpleTypeInfo) SetValue(v reflect.Value)

type SliceInfo

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

	Setable
	Newable
}

func (*SliceInfo) AddValue

func (o *SliceInfo) AddValue(v reflect.Value)

func (*SliceInfo) CanAddValue

func (o *SliceInfo) CanAddValue() bool

func (*SliceInfo) CanSetField

func (o *SliceInfo) CanSetField() bool

func (*SliceInfo) GetClassName

func (o *SliceInfo) GetClassName() string

func (*SliceInfo) Kind

func (o *SliceInfo) Kind() int

func (*SliceInfo) New

func (o *SliceInfo) New() Object

func (*SliceInfo) NewElem

func (o *SliceInfo) NewElem() Object

func (*SliceInfo) SetField

func (o *SliceInfo) SetField(name string, v interface{})

type StructInfo

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

	Setable

	Newable
}

func GetReflectStructInfo

func GetReflectStructInfo(rt reflect.Type, rv reflect.Value) (*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、如果结构体中含有xfield的tag,则: a)、如果tag为‘-’,则不进行columne与field的映射; b)、如果tag不为‘-’使用tag name作为column名称与field映射。 4、如果结构体中不含有xfield的tag,则使用field name作为column名称与field映射 5、如果字段的tag为‘-’,则不进行columne与field的映射;

func (*StructInfo) AddValue

func (o *StructInfo) AddValue(v reflect.Value)

func (*StructInfo) CanAddValue

func (o *StructInfo) CanAddValue() bool

func (*StructInfo) CanSetField

func (o *StructInfo) CanSetField() bool

func (*StructInfo) FillMapValue

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

func (*StructInfo) GetClassName

func (o *StructInfo) GetClassName() string

func (*StructInfo) Kind

func (o *StructInfo) Kind() int

func (*StructInfo) MapValue

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

func (*StructInfo) New

func (o *StructInfo) New() Object

func (*StructInfo) NewElem

func (o *StructInfo) NewElem() Object

func (*StructInfo) SetField

func (o *StructInfo) SetField(name string, ov interface{})

Jump to

Keyboard shortcuts

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