Documentation
¶
Index ¶
- Variables
- func Map(src, dst any) error
- func MapContext(ctx *Context, src, dst any) error
- func MapRefl(src, dst reflect.Value) error
- func MapReflContext(ctx *Context, src, dst reflect.Value) error
- type Context
- func (c *Context) WithByteOrder(byteOrder binary.ByteOrder) *Context
- func (c *Context) WithCustom(custom any) *Context
- func (c *Context) WithDisabledCache(disableCache bool) *Context
- func (c *Context) WithFieldMapper(fieldMapper func(string) string) *Context
- func (c *Context) WithStrictTypes(strictTypes bool) *Context
- func (c *Context) WithTag(tag string) *Context
- type Hooks
- type InvalidMappingErr
- type MapFrom
- type MapFunc
- type MapFuncProvider
- type MapTo
- type Mapper
Constants ¶
This section is empty.
Variables ¶
var Default = New()
Default is the default Mapper used by the Map and MapRefl functions. It also provides additional mapping rules for time.Time, big.Int, big.Float and big.Rat. It can be modified to change the default behavior, but if the mapper is used by other packages, it is recommended to create a copy of the default mapper and modify the copy.
var InvalidDstErr = errors.New("mapper: invalid destination value")
InvalidDstErr is returned when reflect.IsValid returns false for the destination value. It may happen when the destination value was not passed as a pointer.
var InvalidSrcErr = errors.New("mapper: invalid source value")
InvalidSrcErr is returned when reflect.IsValid returns false for the source value.
var MappingInterfaceHooks = Hooks{ MapFuncHook: func(m *Mapper, src, dst reflect.Type) MapFunc { if isSimpleType(src) && isSimpleType(dst) { return nil } if implMapTo(src) { return mapToInterface } if implMapFrom(dst) { return mapFromInterface } return nil }, SourceValueHook: func(v reflect.Value) reflect.Value { for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr { if _, ok := v.Interface().(MapTo); ok { return v } v = v.Elem() } return reflect.Value{} }, DestinationValueHook: func(v reflect.Value) reflect.Value { for v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr { if v.Kind() == reflect.Ptr && v.IsNil() { if !v.CanSet() { return reflect.Value{} } v.Set(reflect.New(v.Type().Elem())) } if _, ok := v.Interface().(MapFrom); ok { return v } v = v.Elem() } return reflect.Value{} }, }
MappingInterfaceHooks is a set of hooks that checks if the source or destination type implements the MapTo or MapFrom interface. If so, it will use one of those interfaces to map the value. If both interfaces are implemented, MapTo will be used.
Functions ¶
func Map ¶
Map maps the source value to the destination value.
It is shorthand for Default.mapRefl(src, dst).
func MapContext ¶
MapContext maps the source value to the destination value.
It is shorthand for Default.MapContext(ctx, src, dst).
Types ¶
type Context ¶
type Context struct { // StrictTypes enables strict type checking. If enabled, the source and // destination types must be exactly the same for the mapping to be // successful. However, mapping between different data structures, such as // `struct` ⇔ `struct`, `struct` ⇔ `map` and `map` ⇔ `map` is always // allowed. If the destination type is an empty interface, the source value // will be assigned to it regardless of the strict type check setting. StrictTypes bool // Tag is the name of the struct tag that is used by the mapper to // determine the name of the field to map to. Tag string // ByteOrder is the byte order used to map numbers to and from byte slices. ByteOrder binary.ByteOrder // DisableCache disables the cache of the type mappers. DisableCache bool // FieldMapper is a function that maps a struct field name to another name, // it is used only when the tag is not present. FieldMapper func(string) string // Custom is a custom value that can be used to pass additional information // to the mapping functions. Custom any }
Context is a context that is passed to the mapping functions. It can be used to pass additional information to the mapping functions or to change the behavior of the mapper without modifying the global state or creating a copy of the mapper.
func (*Context) WithByteOrder ¶
WithByteOrder returns a copy of the context with the ByteOrder field set to the given value.
func (*Context) WithCustom ¶
WithCustom returns a copy of the context with the Custom field set to the given value.
func (*Context) WithDisabledCache ¶ added in v0.3.0
WithDisabledCache returns a copy of the context with the DisableCache field set to the given value.
func (*Context) WithFieldMapper ¶
WithFieldMapper returns a copy of the context with the FieldMapper field set to the given value.
func (*Context) WithStrictTypes ¶
WithStrictTypes returns a copy of the context with the StrictTypes field set to the given value.
type Hooks ¶
type Hooks struct { // MapFuncHook allows to bypass the default mapping rules and use a custom // mapping function. If the hook returns nil, then the default mapping // rules are used. // // Returned MapFunc is cached. MapFuncHook MapFuncProvider // SourceValueHook returns a value that should be used as the source // value. It is called before the source value is used in the mapping. // // If the hook returns an invalid value, then the default function is used. // // By default, mapper unpacks pointers and dereferences interfaces. This // hook can be used to change this behavior. SourceValueHook func(reflect.Value) reflect.Value // DestinationValueHook returns a value that should be used as the destination // value. It is called before the destination value is used in the mapping. // // If the hook returns an invalid value, then the default function is used. // // By default, mapper unpacks pointers and dereferences interfaces. This // hook can be used to change this behavior. DestinationValueHook func(reflect.Value) reflect.Value }
Hooks are functions that are called during the mapping process. They can modify the behavior of the mapper.
type InvalidMappingErr ¶
func NewInvalidMappingError ¶
func NewInvalidMappingError(from, to reflect.Type, reason string) *InvalidMappingErr
func NewStrictMappingError ¶
func NewStrictMappingError(from, to reflect.Type) *InvalidMappingErr
func (*InvalidMappingErr) Error ¶
func (e *InvalidMappingErr) Error() string
type MapFrom ¶
type MapFrom interface { // MapFrom sets the receiver value from the source value. MapFrom(m *Mapper, src reflect.Value) error }
MapFrom interface is implemented by types that can set their value from another type.
type MapFunc ¶
MapFunc is a function that maps a src value to a dst value. It returns an error if the mapping is not possible. The src and dst values are never pointers.
type MapFuncProvider ¶
MapFuncProvider is a function that returns a MapFunc for given src and dst types. If mapping is not supported, it returns nil.
type MapTo ¶
type MapTo interface { // MapTo maps the receiver value to the destination value. MapTo(m *Mapper, dst reflect.Value) error }
MapTo interface is implemented by types that can map themselves to another type.
type Mapper ¶
type Mapper struct { // Context is the default context used by the mapper. Context *Context // Mappers is a map of custom mapper providers. The key is the type that // the mapper can map to and from. The value is a function that returns // a MapFunc that maps the source type to the destination type. Provider // can return nil if the mapping is not possible. // // If both source and destination types have defined providers, then // the provider for source value is used first, and if it returns nil, // then the provider for destination value is used. Mappers map[reflect.Type]MapFuncProvider // Hooks are functions that are called during the mapping process. They // can modify the behavior of the mapper. See Hooks for more information. Hooks Hooks // contains filtered or unexported fields }
Mapper hold the mapper configuration.
func (*Mapper) MapContext ¶
MapContext maps the source value to the destination value.