structomap: github.com/tuvistavie/structomap Index | Examples | Files

package structomap

import "github.com/tuvistavie/structomap"

Package structomap contains

Index

Examples

Package Files

structomap.go

Constants

const (
    // NotSet uses the original case for keys
    NotSet KeyCase = iota

    // CamelCase uses camelCase keys
    CamelCase = iota

    // PascalCase Uses PascalCase keys
    PascalCase = iota

    // SnakeCase uses snake_case keys
    SnakeCase = iota
)

func SetDefaultCase Uses

func SetDefaultCase(caseType KeyCase)

SetDefaultCase set the default key case (snake_case, camelCase or PascalCase) for all new serializers

type Base Uses

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

Base is a basic implementation of Serializer

func New Uses

func New() *Base

New creates a new serializer

func (*Base) Add Uses

func (b *Base) Add(key string, value interface{}) Serializer

Add adds a custom field to the result

func (*Base) AddFunc Uses

func (b *Base) AddFunc(key string, f ValueConverter) Serializer

AddFunc adds a computed custom field to the result

func (*Base) AddFuncIf Uses

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 Uses

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 Uses

func (b *Base) ConvertKeys(keyConverter KeyConverter) Serializer

ConvertKeys converts all the keys using the given converter

func (*Base) MustTransformArray Uses

func (b *Base) MustTransformArray(entities interface{}) []map[string]interface{}

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 Uses

func (b *Base) Omit(keys ...string) Serializer

Omit omits the given fields from the result

func (*Base) OmitIf Uses

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 Uses

func (b *Base) Pick(keys ...string) Serializer

Pick adds the given fields to the result

func (*Base) PickAll Uses

func (b *Base) PickAll() Serializer

PickAll adds all the exported fields to the result

func (*Base) PickFunc Uses

func (b *Base) PickFunc(converter ValueConverter, keys ...string) Serializer

PickFunc adds the given fields to the result after applying the converter

func (*Base) PickFuncIf Uses

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 Uses

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 Uses

func (b *Base) Transform(entity interface{}) map[string]interface{}

Transform transforms the entity into a map[string]interface{} ready to be serialized

func (*Base) TransformArray Uses

func (b *Base) TransformArray(entities interface{}) ([]map[string]interface{}, error)

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 Uses

func (b *Base) UseCamelCase() Serializer

UseCamelCase uses camelCase keys for the serializer

func (*Base) UsePascalCase Uses

func (b *Base) UsePascalCase() Serializer

UsePascalCase uses PascalCase keys for the serializer

func (*Base) UseSnakeCase Uses

func (b *Base) UseSnakeCase() Serializer

UseSnakeCase uses snake_case keys for the serializer

type KeyCase Uses

type KeyCase int

KeyCase represenets the word case of the output keys

type KeyConverter Uses

type KeyConverter func(string) string

KeyConverter is an alias for func(string) string used to transform keys

type Predicate Uses

type Predicate func(interface{}) bool

Predicate is an alias for func(interface{}) bool used to test for inclusion

type Serializer Uses

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

Code:

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 Uses

type ValueConverter func(interface{}) interface{}

ValueConverter is an alias for func(interface{}) interface{} used to transform values

Package structomap imports 4 packages (graph). Updated 2019-05-25. Refresh now. Tools for package owners.