reflection

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ObjectUnknown = iota
	ObjectSimpletype
	ObjectStruct
	ObjectSlice
	ObjectMap

	ObjectCustom = 50000
)

Variables

View Source
var (
	EmptyString       string
	BoolDefault       bool
	ByteDefault       byte
	Complex64Default  complex64
	Complex128Default complex128
	Float32Default    float32
	Float64Default    float64
	Int64Default      int64
	Uint64Default     uint64
	Int32Default      int32
	Uint32Default     uint32
	Int16Default      int16
	Uint16Default     uint16
	Int8Default       int8
	Uint8Default      uint8
	IntDefault        int
	UintDefault       uint
	TimeDefault       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

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

	Settable
	Newable
}

func (*MapInfo) AddValue

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

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, ov interface{})

func (*MapInfo) SetValue

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

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 interface{})
	// AddValue 添加元素值
	AddValue(v reflect.Value)
	// 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)
	// 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 added in v0.2.7

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

func (*Settable) CanSet added in v0.2.7

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

func (*Settable) GetValue added in v0.2.7

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

func (*Settable) ResetValue added in v0.2.7

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

func (*Settable) SetValue added in v0.2.7

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

type SimpleTypeInfo

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

	Settable
	Newable
}

func (*SimpleTypeInfo) AddValue

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

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, ov interface{})

func (*SimpleTypeInfo) SetValue

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

type SliceInfo

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

	Settable
	Newable
}

func (*SliceInfo) AddValue

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

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 interface{})

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

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, ov interface{})

Jump to

Keyboard shortcuts

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