Documentation ¶
Overview ¶
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages. The main Mapper type allows for Go-compatible named attribute access, including accessing embedded struct attributes and the ability to use functions and struct tags to customize field names.
Index ¶
- Variables
- func Alloc(t reflect.Type) (v reflect.Value)
- func AllocDefault(typ reflect.Type) reflect.Value
- func AllocIndirect(v reflect.Value) reflect.Value
- func CamelCaseToUnderscore(str string) string
- func CopyStruct(dst, src interface{})
- func DefaultTagFunc(fieldName, tag string) (string, []string)
- func Deref(t reflect.Type) reflect.Type
- func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value
- func FieldByIndexesReadOnly(v reflect.Value, indexes []int) reflect.Value
- func FieldNameToLower(fieldName, tag string) (string, []string)
- func FieldNameToUnderscore(fieldName, tag string) (string, []string)
- func FormToStruct(form map[string][]string, ptr interface{}) error
- func Indirect(v interface{}) (reflect.Type, reflect.Value)
- func IsStruct(v interface{}) bool
- func MustBe(v kinder, expected reflect.Kind)
- func SetDefault(ptr interface{})
- func SetValue(field, v reflect.Value) error
- func StdTagFunc(fieldName, tag string) (string, []string)
- func StrToValue(str string, field reflect.Value) (err error)
- func StructToForm(obj interface{}, form map[string][]string)
- func UnderscoreToCamelCase(s string) string
- func ValueToStr(field reflect.Value) (string, error)
- type DefaultMapper
- type FieldInfo
- type FormMapper
- type Mapper
- func (m *Mapper) FieldByPath(v reflect.Value, path string) (rv reflect.Value)
- func (m *Mapper) FieldMap(v reflect.Value) map[string]reflect.Value
- func (m *Mapper) FieldsByPath(v reflect.Value, paths []string) []reflect.Value
- func (m *Mapper) NameMap(name string) (sm StructMap, exist bool)
- func (m *Mapper) TraversalsByPath(t reflect.Type, paths []string) [][]int
- func (m *Mapper) TypeMap(t reflect.Type) StructMap
- type Reflector
- type StructMap
- type TagFunc
- type Value
- func (v Value) Bool() bool
- func (v Value) Default(defaultVal interface{}) Value
- func (v Value) Float() float64
- func (v Value) Int() int
- func (v Value) Int64() int64
- func (v Value) Interface() interface{}
- func (v Value) IsNil() bool
- func (v Value) MustBool() bool
- func (v Value) MustFloat() float64
- func (v Value) MustInt() int
- func (v Value) MustInt64() int64
- func (v Value) MustUint() uint
- func (v Value) MustUint64() uint64
- func (v Value) Plus(step interface{}) Value
- func (v Value) String() string
- func (v Value) ToBool() (bool, error)
- func (v Value) ToFloat() (float64, error)
- func (v Value) ToInt() (int, error)
- func (v Value) ToInt64() (int64, error)
- func (v Value) ToUint() (uint, error)
- func (v Value) ToUint64() (uint64, error)
- func (v Value) Uint() uint
- func (v Value) Uint64() uint64
Constants ¶
This section is empty.
Variables ¶
var ( IgnoreThisField = "-" //the field is skipped if empty. OmitEmpty = "omitempty" // Field is not processed further by this package. OmitNested = "omitnested" // The FieldStruct's fields will be flattened into the parent level. Flatten = "flatten" // No tag, use the original name of field StdMapper = NewMapper("", nil) )
var (
ErrNil = errors.New("nil value")
)
var (
StructNameKey = "_struct_name"
)
Functions ¶
func Alloc ¶
Alloc allocate a reflect.Value which has the reflect.Type specified by t. The return value can always be set either t is a point or not.
func AllocDefault ¶
Allocate a struct with default value, faster than SetDefault.
func AllocIndirect ¶
IndirectAlloc returns the value that v points to. It is used before v is going to set value. If v is a nil pointer, IndirectAlloc returns a allocated Value.
func CamelCaseToUnderscore ¶
CamelCaseToUnderscore converts from camel case form to underscore separated form. Ex.: MyFunc => my_func
func CopyStruct ¶
func CopyStruct(dst, src interface{})
CopyTo copys fields of src to dst, these fields have the same name.
func DefaultTagFunc ¶
DefaultTagFunc parses tag parts spliting by "," for the given field. Unlike StdTagFunc, the first part is not the field name. It simply returns the input fieldName.
func FieldByIndexes ¶
FieldByIndexes returns a value for the field given by the struct traversal for the given value.
func FieldByIndexesReadOnly ¶
FieldByIndexesReadOnly 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. It returns a invalid value in case of v is a nil point.
func FieldNameToLower ¶
same as StdTagFunc, but mapped fieldName to their lower case.
func FieldNameToUnderscore ¶
Same as StdTagFunc, but mapped fieldName to underscore. Ex.: MyFunc => my_func
func FormToStruct ¶
convert map to struct
func IsStruct ¶
func IsStruct(v interface{}) bool
IsStruct returns whether v or point of v is a struct.
func MustBe ¶
mustBe checks a value against a kind, panicing with a reflect.ValueError if the kind isn't that which is required.
func SetDefault ¶
func SetDefault(ptr interface{})
Set fileds of struct to default value provided by tag if they are not zero.
type Test struct { Field string `default:"some string"` Slice []int `default:"1,2,3,4,5"` Map map[string]int`default:"x=1,y=2,z=3"` }
Note that the zero value of slice is nil, not []int{}.
func SetValue ¶
set dst to field, auto convert types. interface{} --> base type(int, uint, float32....) float, int, uint... ->base type []interface{}, []int, []float... -> []int map[int]float... ---> map[float]float
func StdTagFunc ¶
StdTagFunc parses the target name and tag parts spliting by "," for the given field. e.g. Call with "foo,bar,size=64" returns ("foo", []string{"bar", "szie=64"}) Call with ",bar" returns (fieldName, []string{"bar"})
func StrToValue ¶
Set value of field by string, field must can be set.
func StructToForm ¶
convert struct to form
func UnderscoreToCamelCase ¶
UnderscoreToCamelCase converts from underscore separated form to camel case form. Ex.: my_func => MyFunc
Types ¶
type DefaultMapper ¶
type DefaultMapper struct {
// contains filtered or unexported fields
}
func NewDefaultMapper ¶
func NewDefaultMapper(tagName string, tagFunc TagFunc) *DefaultMapper
func (*DefaultMapper) AllocDefault ¶
func (dm *DefaultMapper) AllocDefault(typ reflect.Type) reflect.Value
Allocate a struct with default value, faster than SetDefault.
func (*DefaultMapper) SetDefault ¶
func (dm *DefaultMapper) SetDefault(ptr interface{})
type FieldInfo ¶
type FieldInfo struct { Index []int Path string IsPtr bool // is a point or not Type reflect.Type // It is not the same as reflect.Zero, If this is a pointer, allocate a new value. // Zero = reflect.Zero(Deref(Type)) Zero reflect.Value Name string Parts []string //parts of tag splite by ",", exclusive of name. Options map[string]string // options parsed from Parts, as "k=v". Embedded bool Children []*FieldInfo Parent *FieldInfo }
A FieldInfo is metadata for a struct field.
type FormMapper ¶
type FormMapper struct {
// contains filtered or unexported fields
}
func NewFormMapper ¶
func NewFormMapper(tagName string, tagFunc TagFunc) FormMapper
default tagName is "form"
func (FormMapper) FormToStruct ¶
func (fm FormMapper) FormToStruct(form map[string][]string, ptr interface{}) error
func (FormMapper) StructToForm ¶
func (fm FormMapper) StructToForm(obj interface{}, form map[string][]string)
Convert struct to form.
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
Mapper is a general purpose mapper of names to struct fields. A Mapper behaves like most marshallers in the standard library, obeying a field tag for name mapping but also providing a basic transform function.
func NewMapper ¶
NewMapper returns a new mapper using the tagName as its struct field tag. If tagName is the empty string, it is ignored. tagFunc is used to get target name and parts of tag from the field. if tagFunc is nil, StdTagFunc will be used.
func (*Mapper) FieldByPath ¶
FieldByName returns a field by its mapped name as a reflect.Value. Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind. Returns zero Value if the name is not found.
func (*Mapper) FieldMap ¶
FieldMap returns the mapper's mapping of field names to reflect values. Panics if v's Kind is not Struct, or v is not Indirectable to a struct kind.
func (*Mapper) FieldsByPath ¶
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) TraversalsByPath ¶
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.
type Reflector ¶
type Reflector interface { Register(v interface{}) Encode(v interface{}) ([]byte, error) Decode(b []byte) (interface{}, error) }
json.Unmarshal or xml.Unmarshal only can decode bytes to map[]interface{}, the type of interface{} contained in map still is map[]intefface{}. Reflector can decode bytes to registed structs.
func NewReflector ¶
opts can set format and tagName default format is json default tagName is "reflector" default tagFunc is StdTagfunc
type StructMap ¶
type StructMap struct { Tree *FieldInfo Fields []*FieldInfo //all the fields of the tree. Paths map[string]*FieldInfo // equal to Fields. Leaves map[string]*FieldInfo // all the leaves of the tree, not including struct type. }
A StructMap is an index of field metadata for a struct.
func (StructMap) GetByTraversal ¶
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.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents a value that is returned from Get().
func (Value) Interface ¶
func (v Value) Interface() interface{}
Interface returns v's current value as an interface{}. It is equivalent to:
var i interface{} = (v's underlying value)
func (Value) MustUint64 ¶
MustUint64 returns the uint64 format number, it panics in case of any error.
func (Value) Plus ¶
Plus adds step to v and returns the result. The return value has the same type as v. It panics if step is not numberical. If v is nil, step will be returned.
func (Value) ToFloat ¶
Float returns returns ErrNil if v is nil. It returns an error if the type of value is not numeric. Note that, if map is decode by json, evey number has float64 type. If map is decode by bson, number only has int, int64 type.
func (Value) ToInt ¶
ToInt returns an int number from v. It returns ErrNil if v is nil. It returns an error if the type of value is not numeric. Note that, if v is decode by json, evey number has float64 type. If v is decode by bson, number only has int, int64 type.
func (Value) ToInt64 ¶
ToInt64 returns an int64 number from v. It returns ErrNil if v is nil. It returns an error if the type of value is not numeric. Note that, if v is decode by json, evey number has float64 type. If v is decode by bson, number only has int, int64 type.
func (Value) ToUint ¶
ToUint returns an int number from v. It returns ErrNil if v is nil. It returns an error if the type of value is not numeric. Note that, if v is decode by json, evey number has float64 type. If v is decode by bson, number only has int, int64 type.