greflect

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldByIndex

func FieldByIndex(v reflect.Value, indexes []int) reflect.Value

FieldByIndex 为给定值返回结构遍历所给定字段的值。

func FieldByIndexReadOnly

func FieldByIndexReadOnly(v reflect.Value, indexes []int) reflect.Value

FieldByIndexReadOnly returns a value for a particular struct traversal, but is not concerned with allocating nil pointers because the value is going to be used for reading and not setting.

func FindTag

func FindTag(obj interface{}, field, tag string) string

FindTag find struct of tag string.查找struct 的tag信息

Types

type Field

type Field struct {
	Index    []int
	Path     string
	Field    reflect.StructField
	Zero     reflect.Value
	Name     string
	Options  map[string]string
	Embedded bool
	Children []*Field
	Parent   *Field
}

==================================== 以下的代码来自 github.com/jmoiron/sqlx ==================================== Field 是结构体字段的元数据。

type Mapper

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

Mapper 是名称到结构字段的通用映射器。 映射器的行为类似于标准库中的大多数编组器,它遵循用于名称映射的字段标签,但也提供了基本的转换功能。

func NewMapper

func NewMapper(tagName string) *Mapper

NewMapper returns a new mapper using the tagName as its struct field tag. If tagName is the empty string, it is ignored.

func NewMapperFunc

func NewMapperFunc(tagName string, f func(string) string) *Mapper

NewMapperFunc returns a new mapper which optionally obeys a field tag and a struct field name mapper func given by f. Tags will take precedence, but for any other field, the mapped name will be f(field.Name)

func NewMapperTagFunc

func NewMapperTagFunc(tagName string, mapFunc, tagMapFunc func(string) string) *Mapper

NewMapperTagFunc returns a new mapper which contains a mapper for field names AND a mapper for tag values. This is useful for tags like json which can have values like "name,omitempty".

func (*Mapper) FieldByName

func (m *Mapper) FieldByName(v reflect.Value, name string) reflect.Value

FieldByName 获取结构体字段的值

参数<v> 是结构体的反射值,参数<name>是该结构体中字段的名称

func (*Mapper) FieldMap

func (m *Mapper) FieldMap(v reflect.Value) map[string]reflect.Value

FieldMap 返回映射器对字段名称的映射以反映值。如果v的Kind不是Struct或v不可间接访问结构种类,则panic.

func (*Mapper) FieldsByName

func (m *Mapper) FieldsByName(v reflect.Value, names []string) []reflect.Value

FieldsByName returns a slice of values corresponding to the slice of names for the value. Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind. Returns zero Value for each name not found.

func (*Mapper) TraversalsByName

func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int

TraversalsByName returns a slice of int slices which represent the struct traversals for each mapped name. Panics if t is not a struct or Indirectable to a struct. Returns empty int slice for each name not found.

func (*Mapper) TraversalsByNameFunc

func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error

TraversalsByNameFunc traverses the mapped names and calls fn with the index of each name and the struct traversal represented by that name. Panics if t is not a struct or Indirectable to a struct. Returns the first error returned by fn or nil.

func (*Mapper) TypeMap

func (m *Mapper) TypeMap(t reflect.Type) *StructMap

TypeMap 返回结构体所有的字段信息

参数<t>是结构体的反射类型 即:t = reflect.TypeOf(obj)

type StructMap

type StructMap struct {
	Tree  *Field   //存储嵌入字段的信息,即:当前字段是一个结构体!!!
	All   []*Field //存储当前结构体所有的字段信息!!!
	Paths map[string]*Field
	Names map[string]*Field
}

StructMap 是一个结构体中所有字段的元数据. 一个 StructMap 对象只描述 一个结构体中所有的字段信息!!!

func (StructMap) GetByPath

func (f StructMap) GetByPath(path string) *Field

GetByPath returns a *FieldInfo for a given string path.

func (StructMap) GetByTraversal

func (f StructMap) GetByTraversal(index []int) *Field

GetByTraversal returns a *FieldInfo for a given integer path. It is analogous to reflect.FieldByIndex, but using the cached traversal rather than re-executing the reflect machinery each time.

Jump to

Keyboard shortcuts

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