mappy

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2018 License: MIT Imports: 3 Imported by: 0

README

mappy

Build Status Documentation Go Report Card

The purpose of this tiny library is to easily parse string->string maps to typed structures in Go. I've faced a real problem where I needed to define a list of keys that may appear in the map, then have each key associated with struct's field and they had to be manually transformed. mappy tackles the problem using struct tags, where each field is represented with the key that is to be found in the map.

Usage

In order to transform a struct instance into a map, use Marshal(..) function:

type Person struct {
	FirstName string `map:"first_name"`
	LastName  string `map:"last_name"`
}

p := Person{FirstName: "Tim", LastName: "Duncan"}

pMap, _ := Marshal(p)

fmt.Println(pMap["first_name"])
fmt.Println(pMap["last_name"])

// Output:
// Tim
// Duncan

In order to transform a map into a struct, use Unmarshal(..) function:

type Person struct {
	FirstName string `map:"first_name"`
	LastName  string `map:"last_name"`
}

pMap := map[string]string{
	"first_name": "Shaquille",
	"last_name":  "O'Neal",
}

var p Person
Unmarshal(pMap, &p)

fmt.Println(p.FirstName)
fmt.Println(p.LastName)

// Output:
// Shaquille
// O'Neal

Performance

The idea behind mappy is to enhance readability of the code, not to improve its performance. If you value the speed of execution above anything else, this will not be a good choice for you. Manual mapping is much faster for both marshaling:

BenchmarkMarshal-8               3000000               575 ns/op
BenchmarkStructToMap-8           5000000               240 ns/op

and unmarshaling:

BenchmarkUnarshal-8              5000000               368 ns/op
BenchmarkMapToStruct-8         100000000              13.3 ns/op

If, however, you find such solution good enough and useful, enjoy!

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMapMarshal = stderr.New("failed to marshal struct into map")

ErrMapMarshal is returned when it is not possible to marshal struct into a map.

View Source
var ErrMapUnmarshal = stderr.New("failed to unmarshal map into struct")

ErrMapMarshal is returned when it is not possible to unmarshal map into a struct.

Functions

func Marshal

func Marshal(data interface{}) (m map[string]string, err error)

Marshal transforms a custom struct into string->string map.

Example
type Person struct {
	FirstName string `map:"first_name"`
	LastName  string `map:"last_name"`
}

p := Person{FirstName: "Tim", LastName: "Duncan"}

pMap, _ := Marshal(p)

fmt.Println(pMap["first_name"])
fmt.Println(pMap["last_name"])
Output:

Tim
Duncan

func Unmarshal

func Unmarshal(m map[string]string, data interface{}) (err error)

Unmarshal transforms a string->string map into a custom struct.

Example
type Person struct {
	FirstName string `map:"first_name"`
	LastName  string `map:"last_name"`
}

pMap := map[string]string{
	"first_name": "Shaquille",
	"last_name":  "O'Neal",
}

var p Person
Unmarshal(pMap, &p)

fmt.Println(p.FirstName)
fmt.Println(p.LastName)
Output:

Shaquille
O'Neal

Types

This section is empty.

Jump to

Keyboard shortcuts

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