reflectx

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2021 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrParamNotStruct 参数不是结构体
	ErrParamNotStruct = fmt.Errorf("Please input struct param")
)

Functions

func CopyStructField

func CopyStructField(refType reflect.Type, refValue reflect.Value) (reflect.Type, reflect.Value)

CopyStructField 复制结构体字段

func InitParam

func InitParam(param interface{}, specType reflect.Type, specValue reflect.Value, copy bool) (interface{}, error)

InitParam 初始化-使用反射初始化param里的指定类型

func IsStructPointer

func IsStructPointer(v interface{}) bool

IsStructPointer 是否结构体指针

func RegisterSpecType added in v0.5.0

func RegisterSpecType(typ reflect.Type, handler func(typ reflect.Type) reflect.Value)

RegisterSpecType 注册特别类型

func SetStructFieldValue added in v0.6.0

func SetStructFieldValue(arg reflect.Value, fieldName string, value interface{}) (fieldType reflect.StructField)

SetStructFieldValue 设置结构体字段的值,arg必须是结构体指针,value值类型必须与结构体对应fieldName类型一致 没有没有找到指定字段,则返回的StructField为空

func SetStructRandom added in v0.5.0

func SetStructRandom(v interface{})

SetStructRandom 设置结构体随机值

func ToInterface added in v0.5.0

func ToInterface(in []reflect.Value) (out []interface{})

ToInterface 如果in数组里存在无法取Interface的Value,则会置为nil

Types

type Address

type Address struct {
	Basic           // 基础信息
	Position string // 位置
}

Address 地址

type Basic

type Basic struct {
	Name string `json:"name"` // 名字
}

Basic 基础信息

type Field

type Field struct {
	reflect.StructField        // 内嵌反射结构体字段类型
	Comment             string // 注释
	Struct              Struct // 字段的类型是其它结构体
}

Field 字段

type IStruct added in v0.5.0

type IStruct interface{ GetFields() []Field }

type IStructMock added in v0.5.0

type IStructMock interface{ GetFields() []Field }

type InnerModel

type InnerModel struct {
	CornerStone string `json:"stone,omitempty"` // 基石
}

InnerModel 内部模型

type Model

type Model struct {
	InnerModel
	// 级别类型:1 P1;2 P2;3 P3
	Level       int      `json:"level"` // 级别
	LevelPtr    *int     // 级别
	Position    float64  // 位置
	PositionPtr *float64 // 位置
	Head        string   // 头
	HeadPtr     *string  // 头
	IsTurbo     bool     // 是否涡轮
	IsTurboPtr  *bool    // 是否涡轮

}

Model 模型

模型注释

模型描述. 模型描述2.

type Struct

type Struct struct {
	Name        string       // 名字
	Comment     string       // 注释
	Description string       // 描述
	Type        reflect.Type // 反射类型
	Fields      []Field      // 结构体字段
}

Struct 结构体

func MakeStruct

func MakeStruct() Struct

MakeStruct 新建结构体

func ResolveStruct

func ResolveStruct(value interface{}) (Struct, error)

ResolveStruct 解析结构体

func (Struct) GetFields

func (s Struct) GetFields() []Field

GetFields 返回结构体的所有field,包括匿名字段的field

type StructMock added in v0.5.0

type StructMock struct {
	GetFieldsFunc func() []Field
}

func (*StructMock) GetFields added in v0.5.0

func (mockRecv *StructMock) GetFields() []Field

type StructMockMock added in v0.5.0

type StructMockMock struct {
	GetFieldsFunc func() []Field
}

func (*StructMockMock) GetFields added in v0.5.0

func (mockRecv *StructMockMock) GetFields() []Field

type User

type User struct {
	*Model
	Basic      // 基础信息
	Age   int  // 年纪
	Sex   *int // 性别

	Phone        *string   // 电话号码
	Salary       float64   // 薪水
	SalaryPtr    *float64  // 薪水
	IsManager    bool      // 是否管理层
	IsManagerPtr *bool     // 是否管理层
	AddressList  []Address // 地址列表
	CreatedAt    time.Time // 创建时间

	Test       Model   `json:"test"`      // 测试
	TestSlice  []Model `json:"testSlice"` // 测试数组
	TestStruct struct {
		Model
		InnerTest      Model   `json:"innerTest"`      // 内部测试
		InnerTestSlice []Model `json:"innerTestSlice"` // 内部测试数组
	}

	// 随机赋值时,若有at tag,则使用tag里指定的值
	Range    int    `json:"range" at:"range=one(1,5,8,13)"`     // 范围,值是[1, 5)或[8, 13),支持多个范围
	Regexp   string `json:"regexp" at:"regexp=one('^[a-z]+$')"` // 正则表达式,值必须是满足它的字符串
	EnumOne  *int   `json:"enumOne" at:"enum=one(1,2,3)"`       // 枚举,值必须是1,2,3里的一个,不传用nil表示
	EnumMany []int  `json:"enumMany" at:"enum=many(1,2,3)"`     // 枚举,值可以是1,2,3里的多种组合,不传用nil表示
	DBOne    string `json:"dbOne" at:"db=one(org,id)"`          // 值是来自数据库的数据,org表示表名,id表示表字段
	DBMany   string `json:"dbMany" at:"db=many(org,id)"`        // 值是来自数据库的数据组合,org表示表名,id表示表字段
	// contains filtered or unexported fields
}

User 用户 这是一个表示用户的结构体

Jump to

Keyboard shortcuts

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