stom

package module
v0.0.0-...-05ccb51 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2016 License: MIT Imports: 3 Imported by: 0

README

SToM: Structure To Map converter (yet another one)

go get "github.com/elgris/stom"

GoDoc Build Status

What is it?

Little handy tool to convert your structures into map[string]interface{}. It works in 2 modes:

General mode. It's when you use exported method ToMap to convert arbitrary struct instance to map.

import "github.com/elgris/stom"
import "fmt"

type SomeGrandparentStruct struct {
    GrandparentID int `db:"grand_parent_id"`
}

type SomeParentStruct struct {
    SomeGrandparentStruct
    ParentID int `db:"parent_id"`
}

// Yes, SToM supports embedded structs.
type SomeAwesomeStruct struct {
    SomeParentStruct
    ID              int             `db:"id" custom_tag:"id"`
    Name            string          `db:"name"`
    Notes           string
}
func main() {
    s := SomeAwesomeStruct{
        ID:    123,
        Name:  "myname",
        Notes: "mynote",
    }
    s.ParentID = 1123
    s.GrandparentID = 11123

    stom.SetTag("db")
    m, _ :=  stom.ToMap(s)
    fmt.Printf("%+v", m)

    /* you will get map:

        "grand_parent_id": 11123,
        "parent_id": 1123,
        "id": 123,
        "name": "myname"

    Field "Notes" is ignored as it has no tag.
    All embedded structs are flattened into flat map.
    */
}

**"Individual" mode, when you create an instance of SToM for one specific type. In this mode all the tags are analyzed and cached before conversion, thus you can speed the whole process up if you need to convert repeatedly. It's very useful when you need to parse a lot of instances of the same struct.

import "github.com/elgris/stom"
import "fmt"

type SomeAwesomeStruct struct {
    ID              int             `db:"id" custom_tag:"id"`
    Name            string          `db:"name"`
    Notes           string
}

func main() {
    s := SomeAwesomeStruct{
        ID:    123,
        Name:  "myname",
        Notes: "mynote",
    }
    converter := stom.MustNewStom(s) // at this point 's' is analyzed and tags 'id' and 'name' are cached for future use

    converter.SetTag("db")

    for i:= 0; i < 100500; i++ {
        m, _ := converter.ToMap(s)
    }
}

Benchmarks

https://github.com/elgris/struct-to-map-conversion-benchmark

License

MIT

Documentation

Overview

Package stom is about converting structs to map[string]interface{} with minimum processing and overhead

Example
package main

import (
	"fmt"

	"github.com/elgris/stom"
)

type SomeGrandparentStruct struct {
	GrandparentID int `db:"grand_parent_id"`
}

type SomeParentStruct struct {
	SomeGrandparentStruct
	ParentID int `db:"parent_id"`
}

// Yes, SToM supports embedded structs.
type SomeAwesomeStruct struct {
	SomeParentStruct
	ID            int         `db:"id" custom_tag:"id"`
	Name          string      `db:"name"`
	AbstractThing interface{} `db:"thing"`
	Notes         string
}

func main() {
	s := SomeAwesomeStruct{
		ID:    123,
		Name:  "myname",
		Notes: "mynote",
	}
	s.ParentID = 1123
	s.GrandparentID = 11123

	converter := stom.MustNewStom(s).
		SetTag("db").
		SetPolicy(stom.PolicyExclude).
		SetDefault("DEFAULT")

	/* you will get map:

	       "grand_parent_id": 11123,
	       "parent_id": 1123,
	       "id": 123,
	       "name": "myname",
	       "thing": "DEFAULT"

	   Field "Notes" is ignored as it has no tag.
	   Field "AbstractThing" is nil, so it replaced with default value.
	   Current policy demands to use default instead of nil values.

	   All embedded structs are flattened into flat map.
	*/
	m, err := converter.ToMap(s)
	fmt.Printf("MAP: %+v\nERROR: %v", m, err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToMap

func ConvertToMap(s interface{}) (map[string]interface{}, error)

ConvertToMap converts given structure into map[string]interface{}

func MustNewStom

func MustNewStom(s interface{}) *stom

MustNewStom creates new instance of a SToM converter for type of given structure. Panics if no structure provided

func SetDefault

func SetDefault(dv interface{})

SetDefault sets package default value to set instead of 'nil' in resulting maps

func SetPolicy

func SetPolicy(p Policy)

SetPolicy sets package setting for policy. Policy defines what to do with 'nil' values in resulting maps. There are 2 policies: - PolicyUseDefault - with this policy default value will be used instead of 'nil' - PolicyExclude - 'nil' values will be discarded

func SetTag

func SetTag(t string)

SetTag sets package setting for tag to look for in incoming structures

Types

type Policy

type Policy uint8

Policy is a type to define policy of dealing with 'nil' values

const (
	// PolicyUseDefault enforces SToM to use defined default value instead
	// of 'nil' value in resulting map[string]interface{}.
	// Default value can be nil also.
	PolicyUseDefault Policy = iota

	// PolicyExclude tells SToM to ignore 'nil' values and to not include them in
	// resulting map
	PolicyExclude
)

type ToMappable

type ToMappable interface {
	ToMap() (map[string]interface{}, error)
}

ToMappable defines an entity that knows how to convert itself to map[string]interface{}. If an entity implements this interface, SToM won't do any magic, it will just call ToMap() method to makes thigs simpler. Such approach allows custom conversion

type ToMapper

type ToMapper interface {
	ToMap(s interface{}) (map[string]interface{}, error)
}

ToMapper defines a service that is able to convert something to map[string]interface{}

type ToMapperFunc

type ToMapperFunc func(s interface{}) (map[string]interface{}, error)

ToMapperFunc defines a function that implements ToMapper

func (ToMapperFunc) ToMap

func (f ToMapperFunc) ToMap(s interface{}) (map[string]interface{}, error)

ToMap implements ToMapper

type Zeroable

type Zeroable interface {
	IsZero() bool
}

Zeroable is an interface that allows to filter values that can explicitly state that they are 'zeroes'. For example, this interface allows to filter zero time.Time,

Jump to

Keyboard shortcuts

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