Documentation
¶
Overview ¶
Package structomap contains
Index ¶
- Constants
- func SetDefaultCase(caseType KeyCase)
- type Base
- func (b *Base) Add(key string, value interface{}) Serializer
- func (b *Base) AddFunc(key string, f ValueConverter) Serializer
- func (b *Base) AddFuncIf(p Predicate, key string, f ValueConverter) Serializer
- func (b *Base) AddIf(p Predicate, key string, value interface{}) Serializer
- func (b *Base) ConvertKeys(keyConverter KeyConverter) Serializer
- func (b *Base) MustTransformArray(entities interface{}) []map[string]interface{}
- func (b *Base) Omit(keys ...string) Serializer
- func (b *Base) OmitIf(p Predicate, keys ...string) Serializer
- func (b *Base) Pick(keys ...string) Serializer
- func (b *Base) PickAll() Serializer
- func (b *Base) PickFunc(converter ValueConverter, keys ...string) Serializer
- func (b *Base) PickFuncIf(p Predicate, converter ValueConverter, keys ...string) Serializer
- func (b *Base) PickIf(p Predicate, keys ...string) Serializer
- func (b *Base) Transform(entity interface{}) map[string]interface{}
- func (b *Base) TransformArray(entities interface{}) ([]map[string]interface{}, error)
- func (b *Base) UseCamelCase() Serializer
- func (b *Base) UsePascalCase() Serializer
- func (b *Base) UseSnakeCase() Serializer
- type KeyCase
- type KeyConverter
- type Predicate
- type Serializer
- type ValueConverter
Examples ¶
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func SetDefaultCase ¶ added in v0.6.2
func SetDefaultCase(caseType KeyCase)
SetDefaultCase set the default key case (snake_case, camelCase or PascalCase) for all new serializers
Types ¶
type Base ¶ added in v0.6.2
type Base struct {
// contains filtered or unexported fields
}
Base is a basic implementation of Serializer
func (*Base) Add ¶ added in v0.6.2
func (b *Base) Add(key string, value interface{}) Serializer
Add adds a custom field to the result
func (*Base) AddFunc ¶ added in v0.6.2
func (b *Base) AddFunc(key string, f ValueConverter) Serializer
AddFunc adds a computed custom field to the result
func (*Base) AddFuncIf ¶ added in v0.6.2
func (b *Base) AddFuncIf(p Predicate, key string, f ValueConverter) Serializer
AddFuncIf adds a computed custom field to the result if the Predicate returns true
func (*Base) AddIf ¶ added in v0.6.2
func (b *Base) AddIf(p Predicate, key string, value interface{}) Serializer
AddIf adds a custom field to the result if the Predicate returns true
func (*Base) ConvertKeys ¶ added in v0.6.2
func (b *Base) ConvertKeys(keyConverter KeyConverter) Serializer
ConvertKeys converts all the keys using the given converter
func (*Base) MustTransformArray ¶ added in v0.6.2
MustTransformArray transforms the entities into a []map[string]interface{} array ready to be serialized. Panics if entities is not a slice or an array
func (*Base) Omit ¶ added in v0.6.2
func (b *Base) Omit(keys ...string) Serializer
Omit omits the given fields from the result
func (*Base) OmitIf ¶ added in v0.6.2
func (b *Base) OmitIf(p Predicate, keys ...string) Serializer
OmitIf omits the given fields from the result if the Predicate returns true
func (*Base) Pick ¶ added in v0.6.2
func (b *Base) Pick(keys ...string) Serializer
Pick adds the given fields to the result
func (*Base) PickAll ¶ added in v0.6.2
func (b *Base) PickAll() Serializer
PickAll adds all the exported fields to the result
func (*Base) PickFunc ¶ added in v0.6.2
func (b *Base) PickFunc(converter ValueConverter, keys ...string) Serializer
PickFunc adds the given fields to the result after applying the converter
func (*Base) PickFuncIf ¶ added in v0.6.2
func (b *Base) PickFuncIf(p Predicate, converter ValueConverter, keys ...string) Serializer
PickFuncIf adds the given fields to the result after applying the converter if the predicate returns true
func (*Base) PickIf ¶ added in v0.6.2
func (b *Base) PickIf(p Predicate, keys ...string) Serializer
PickIf adds the given fields to the result if the Predicate returns true
func (*Base) Transform ¶ added in v0.6.2
Transform transforms the entity into a map[string]interface{} ready to be serialized
func (*Base) TransformArray ¶ added in v0.6.2
TransformArray transforms the entities into a []map[string]interface{} array ready to be serialized. Entities must be a slice or an array
func (*Base) UseCamelCase ¶ added in v0.6.2
func (b *Base) UseCamelCase() Serializer
UseCamelCase uses camelCase keys for the serializer
func (*Base) UsePascalCase ¶ added in v0.6.2
func (b *Base) UsePascalCase() Serializer
UsePascalCase uses PascalCase keys for the serializer
func (*Base) UseSnakeCase ¶ added in v0.6.2
func (b *Base) UseSnakeCase() Serializer
UseSnakeCase uses snake_case keys for the serializer
type KeyCase ¶ added in v0.6.2
type KeyCase int
KeyCase represenets the word case of the output keys
type KeyConverter ¶ added in v0.6.2
KeyConverter is an alias for func(string) string used to transform keys
type Predicate ¶ added in v0.6.2
type Predicate func(interface{}) bool
Predicate is an alias for func(interface{}) bool used to test for inclusion
type Serializer ¶
type Serializer interface { Transform(entity interface{}) map[string]interface{} TransformArray(entities interface{}) ([]map[string]interface{}, error) MustTransformArray(entities interface{}) []map[string]interface{} ConvertKeys(keyConverter KeyConverter) Serializer UseSnakeCase() Serializer UseCamelCase() Serializer UsePascalCase() Serializer PickAll() Serializer Pick(keys ...string) Serializer PickIf(predicate Predicate, keys ...string) Serializer PickFunc(converter ValueConverter, keys ...string) Serializer PickFuncIf(predicate Predicate, converter ValueConverter, keys ...string) Serializer Omit(keys ...string) Serializer OmitIf(predicate Predicate, keys ...string) Serializer Add(key string, value interface{}) Serializer AddIf(predicate Predicate, key string, value interface{}) Serializer AddFunc(key string, converter ValueConverter) Serializer AddFuncIf(predicate Predicate, key string, converter ValueConverter) Serializer }
Serializer is the base interface containing all serialization methods
Example ¶
userMap := exampleSerializer.Transform(user) str, _ := json.MarshalIndent(userMap, "", " ") fmt.Println(string(str))
Output: { "created_at": "2015-05-13T15:30:00Z", "current_time": "2015-05-15T17:41:00Z", "first_name": "Foo", "full_name": "Foo Bar", "id": 1, "last_name": "Bar", "updated_at": "2015-05-13T15:30:00Z" }
type ValueConverter ¶ added in v0.6.2
type ValueConverter func(interface{}) interface{}
ValueConverter is an alias for func(interface{}) interface{} used to transform values