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 ¶
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
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 ¶
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 ¶
ToMapper defines a service that is able to convert something to map[string]interface{}
type ToMapperFunc ¶
ToMapperFunc defines a function that implements ToMapper
func (ToMapperFunc) ToMap ¶
func (f ToMapperFunc) ToMap(s interface{}) (map[string]interface{}, error)
ToMap implements ToMapper