structmapper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2016 License: MIT Imports: 7 Imported by: 0

README

go-structmapper

GoDoc Build Status codecov

go-structmapper is a Go library which provides functionality that allows mapping arbitrary structs to maps and vice-versa.

Install

With a correctly configured Go toolchain:

go get -u gopkg.in/anexia-it/go-structmapper.v1

Documentation

Index

Examples

Constants

View Source
const DefaultTagName = "mapper"

DefaultTagName defines the default tag name used by Mapper

Variables

View Source
var (
	// ErrTagNameEmpty designates that the passed tag name is empty
	ErrTagNameEmpty = errors.New("Tag name is empty")

	// ErrNotAStruct designates that the passed value is not a struct
	ErrNotAStruct = errors.New("Now a struct")

	// ErrInvalidMap designates that the passed value is not a valid map
	ErrInvalidMap = errors.New("Invalid map")

	// ErrFieldIsInterface designates that a field is an interface
	ErrFieldIsInterface = errors.New("Field is interface")

	// ErrMapIsNil designates that the passed map is nil
	ErrMapIsNil = errors.New("Map is nil")

	// ErrNotAStructPointer designates that the passed value is not a pointer to a struct
	ErrNotAStructPointer = errors.New("Not a struct pointer")
)

Functions

func IsNilOrEmpty

func IsNilOrEmpty(i interface{}, v reflect.Value) bool

IsNilOrEmpty checks if a passed interface is either nil or of the type's zero value.

The zero value depends on the passed type. For example, the zero value of a string is an empty string.

Types

type InvalidTag

type InvalidTag struct {
	// contains filtered or unexported fields
}

InvalidTag is an error that indicates that the tag value was invalid

func IsInvalidTag

func IsInvalidTag(err error) (*InvalidTag, bool)

IsInvalidTag checks if the given error is an InvalidTag error and returns the InvalidTag error along with a boolean that defines if it is indeed an invalid tag error. The returned *InvalidTag may be nil, if the flag is false

func (*InvalidTag) Error

func (it *InvalidTag) Error() string

Error returns the error string and causes InvalidTag to implement the error interface

func (*InvalidTag) Tag

func (it *InvalidTag) Tag() string

Tag returns the tag name

type Mapper

type Mapper struct {
	// contains filtered or unexported fields
}

Mapper provides the mapping logic

Example (Roundtrip)
type ExampleStruct struct {
	A       string
	B       int `mapper:"bee"`
	C       []string
	D       map[string]int
	E       net.IP
	Ignored string `mapper:"-"`
}

// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
	panic(err)
}

source := &ExampleStruct{
	A: "a",
	B: 5,
	C: []string{"c0", "c1"},
	D: map[string]int{
		"d0": 1,
		"d1": 2,
	},
	E:       net.ParseIP("127.0.0.1"),
	Ignored: "should be ignored",
}

// Convert to map
m, err := sm.ToMap(source)
if err != nil {
	panic(err)
}

target := &ExampleStruct{}

// Convert back to struct
if err := sm.ToStruct(m, target); err != nil {
	panic(err)
}

fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", target.A, target.B, strings.Join(target.C, ","),
	target.D["d0"], target.D["d1"], target.E, target.Ignored)
Output:

A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=

func NewMapper

func NewMapper(options ...Option) (*Mapper, error)

NewMapper initializes a new mapper instance. Optionally Mapper options may be passed to this function

func (*Mapper) ToMap

func (mapper *Mapper) ToMap(source interface{}) (map[string]interface{}, error)

ToMap takes a source struct and maps its values onto a map[string]interface{}, which is then returned.

Example
type ExampleStruct struct {
	A       string
	B       int `mapper:"bee"`
	C       []string
	D       map[string]int
	E       net.IP
	Ignored string `mapper:"-"`
}

// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
	panic(err)
}

source := &ExampleStruct{
	A: "a",
	B: 5,
	C: []string{"c0", "c1"},
	D: map[string]int{
		"d0": 1,
		"d1": 2,
	},
	E:       net.ParseIP("127.0.0.1"),
	Ignored: "should be ignored",
}

m, err := sm.ToMap(source)
if err != nil {
	panic(err)
}

c := []string{}

for _, v := range m["C"].([]interface{}) {
	c = append(c, v.(string))
}
d := m["D"].(map[interface{}]interface{})

fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", m["A"], m["bee"], strings.Join(c, ","),
	d["d0"], d["d1"], m["E"], m["Ignored"])
Output:

A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=%!s(<nil>)

func (*Mapper) ToStruct

func (mapper *Mapper) ToStruct(source map[string]interface{}, target interface{}) error

ToStruct takes a source map[string]interface{} and maps its values onto a target struct.

Example
type ExampleStruct struct {
	A       string
	B       int `mapper:"bee"`
	C       []string
	D       map[string]int
	E       net.IP
	Ignored string `mapper:"-"`
}

// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
	panic(err)
}

source := map[string]interface{}{
	"A":   "a",
	"bee": 5,
	"C":   []string{"c0", "c1"},
	"D": map[interface{}]interface{}{
		"d0": 1,
		"d1": 2,
	},
	"E": "127.0.0.1",
}

target := &ExampleStruct{}

if err := sm.ToStruct(source, target); err != nil {
	panic(err)
}

fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", target.A, target.B, strings.Join(target.C, ","),
	target.D["d0"], target.D["d1"], target.E, target.Ignored)
Output:

A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=

type Option

type Option func(*Mapper) error

Option defines the type used by Mapper Option functions

func OptionTagName

func OptionTagName(tagName string) Option

OptionTagName sets the tag name the mapper uses

Jump to

Keyboard shortcuts

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