map2struct

package module
v0.0.0-...-0778f56 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2016 License: Apache-2.0 Imports: 8 Imported by: 2

README

go-map2struct

Go Report Card Build Status GoDoc Coverage Status

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFactory

func RegisterFactory(factory Factory)

RegisterFactory register factories.

func Unmarshal

func Unmarshal(dest, src interface{}) error

Unmarshal unmarshal map[string]interface{} to a struct instance

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.

Jump to

Keyboard shortcuts

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