Documentation ¶
Overview ¶
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 ArrayGet(a any, idxs ...int) (any, error)
- func ArrayLen(a any) (int, error)
- func ArrayOf[T any](args ...T) []T
- func ArraySet(a any, i int, v any) (any, error)
- func Convert(v any, t reflect.Type) (cv any, err error)
- func DerefType(t reflect.Type) reflect.Type
- func DerefValue(v reflect.Value) reflect.Value
- func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value
- func FieldByIndexesReadOnly(v reflect.Value, indexes []int) reflect.Value
- func IsArrayType(v any) bool
- func IsComplexType(v any) bool
- func IsFloatType(v any) bool
- func IsIntType(v any) bool
- func IsMapType(v any) bool
- func IsPtrType(v any) bool
- func IsSliceType(v any) bool
- func MapGet(dict any, keys ...any) (any, error)
- func MapSet(dict any, key, val any) (any, error)
- func SetProperty(o any, k string, v any) (err error)
- type FieldInfo
- type Mapper
- func (m *Mapper) FieldByName(v reflect.Value, name string) reflect.Value
- func (m *Mapper) FieldMap(v reflect.Value) map[string]reflect.Value
- func (m *Mapper) FieldsByName(v reflect.Value, names []string) []reflect.Value
- func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int
- func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error
- func (m *Mapper) TypeMap(t reflect.Type) *StructMap
- type StructMap
Constants ¶
This section is empty.
Variables ¶
var ( TypeString = reflect.TypeOf("") TypeBool = reflect.TypeOf(false) TypeByte = reflect.TypeOf(byte(0)) TypeRune = reflect.TypeOf(rune(0)) TypeInt = reflect.TypeOf(int(0)) TypeInt32 = reflect.TypeOf(int32(0)) TypeInt64 = reflect.TypeOf(int64(0)) TypeFloat32 = reflect.TypeOf(float32(0)) TypeFloat64 = reflect.TypeOf(float64(0)) TypeDuration = reflect.TypeOf(time.Duration(0)) TypeTime = reflect.TypeOf(time.Time{}) )
Functions ¶
func ArrayGet ¶ added in v1.0.10
ArrayGet getting value from array or slice by index usage:
a := [][]string{ { "0,0", "0,1" }, { "1,0", "1,1" }, }
{{ArrayGet a 0 1 }} // return "0,1"
func ArrayOf ¶ added in v1.0.10
func ArrayOf[T any](args ...T) []T
ArrayOf returns a []T{args[0], args[1], ...}
func DerefValue ¶ added in v1.0.26
DerefValue is Indirect for reflect.Value
func FieldByIndexes ¶ added in v1.0.15
FieldByIndexes returns a value for the field given by the struct traversal for the given value.
func FieldByIndexesReadOnly ¶ added in v1.0.15
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.
func IsArrayType ¶ added in v1.0.10
IsArrayType return true if v is a array
func IsSliceType ¶ added in v1.0.18
IsSliceType return true if v is a array
func MapGet ¶ added in v1.0.10
MapGet getting value from map by keys usage:
m := map[string]any{ "a": 1, "1": map[string]float64{ "c": 4, }, }
{{MapGet m "a" }} // return 1 {{MapGet m 1 "c" }} // return 4
Types ¶
type FieldInfo ¶ added in v1.0.15
type FieldInfo struct { Index []int Path string Field reflect.StructField Zero reflect.Value Name string Options map[string]string Embedded bool Children []*FieldInfo Parent *FieldInfo }
A FieldInfo is metadata for a struct field.
type Mapper ¶ added in v1.0.15
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 ¶ added in v1.0.15
NewMapper returns a new mapper using the tagName as its struct field tag. If tagName is the empty string, it is ignored.
func NewMapperFunc ¶ added in v1.0.15
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 ¶ added in v1.0.15
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 ¶ added in v1.0.15
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 ¶ added in v1.0.15
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) FieldsByName ¶ added in v1.0.15
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 ¶ added in v1.0.15
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 ¶ added in v1.0.15
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.
type StructMap ¶ added in v1.0.15
type StructMap struct { Tree *FieldInfo Index []*FieldInfo Paths map[string]*FieldInfo Names map[string]*FieldInfo }
A StructMap is an index of field metadata for a struct.
func (StructMap) GetByPath ¶ added in v1.0.15
GetByPath returns a *FieldInfo for a given string path.
func (StructMap) GetByTraversal ¶ added in v1.0.15
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.