README
¶
go-map2struct
go-map2struct convert map[string]interface{} to struct.
Example
type Foo struct {
IntA int
IntB uint32
IntC int64
IntHex uint32
IntOct uint16
FloatA float32
BoolA bool
BoolB bool
DurationA time.Duration
TimeA time.Time
EmbedA Bar
}
type Bar struct {
I int
}
src := map[string]interface{}{
"IntA": 1,
"IntB": 2,
"IntC": "3",
"IntHex": "0x0fffffff",
"IntOct": "0755",
"FloatA": 1.0,
"BoolA": false,
"BoolB": "true",
"DurationA": "1h",
"TimeA": now.Format("2006-01-02:15:04:05-0700"),
"EmbedA": map[string]interface{}{
"I": "99",
},
}
var dest Foo
UnmarshalMap(dest, src)
Documentation
¶
Index ¶
- func RegisterFactory(factory Factory)
- func Unmarshal(dest, src interface{}) error
- type Factory
- type GeneralInterfaceFactory
- func (factory *GeneralInterfaceFactory) Create(data map[string]interface{}) (interface{}, error)
- func (factory *GeneralInterfaceFactory) GetInstanceType() reflect.Type
- func (factory *GeneralInterfaceFactory) RegisterInstance(name string, instance interface{})
- func (factory *GeneralInterfaceFactory) RegisterType(name string, instanceType reflect.Type)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Factory ¶
type Factory interface { // GetInstanceType returns the type of the instance create by this factory. // The instance type is used to index the factory. GetInstanceType() reflect.Type // Create returns the new instance. Create(map[string]interface{}) (interface{}, error) }
Factory represents a instance factory used in unmarshaling.
type GeneralInterfaceFactory ¶
type GeneralInterfaceFactory struct {
// contains filtered or unexported fields
}
GeneralInterfaceFactory provides a general factory for interface. For creating instance, the input map need a key to specified the type implelement the interface. The factory new a instance with the type associated the type key, and unmarshal the map to the instance struct.
func NewGeneralInterfaceFactory ¶
func NewGeneralInterfaceFactory(interfaceType reflect.Type, typeKey string, initializer func(interface{}) error) *GeneralInterfaceFactory
NewGeneralInterfaceFactory creates a GeneralInterfaceFactory instance.
func (*GeneralInterfaceFactory) Create ¶
func (factory *GeneralInterfaceFactory) Create(data map[string]interface{}) (interface{}, error)
Create creates a new instance implement the interface.
func (*GeneralInterfaceFactory) GetInstanceType ¶
func (factory *GeneralInterfaceFactory) GetInstanceType() reflect.Type
GetInstanceType returns the interface type.
func (*GeneralInterfaceFactory) RegisterInstance ¶
func (factory *GeneralInterfaceFactory) RegisterInstance(name string, instance interface{})
RegisterInstance register new instance and its associated key.
func (*GeneralInterfaceFactory) RegisterType ¶
func (factory *GeneralInterfaceFactory) RegisterType(name string, instanceType reflect.Type)
RegisterType register new type and its associated key.