serializer

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2015 License: MIT Imports: 2 Imported by: 1

README

Go serializer Build Status GoDoc

This package helps you to serialize your struct into map easily. It provides a Serializer type which contains chainable function to add, remove or modify fields. The result is returned as a map[string]interface{}. It is then up to you to encode the result in JSON, XML or whatever you like.

Here is an example.

import "github.com/tuvistavie/serializer"

type User struct {
    ID        int
    FirstName string
    LastName  string
    Email     string
    HideEmail bool
    CreatedAt time.Time
    UpdatedAt time.Time
}

user := &User{"ID": 1, "FirstName": "Foo", "LastName": "Bar", "Email": "foo@example.org", "HideEmail": true}

serializer.New(user).
           Pick("ID", "FirstName", "LastName", "Email").
           OmitIf(func(u interface{}) bool {
               return u.(User).HideEmail
           }, "Email").
           Add("CurrentTime", time.Now()).
           AddFunc("FullName", func(u interface{}) interface{} {
               return u.(User).FirstName + " " + u.(User).LastName
           }).
           Result()
// -> {"ID": 1, "FirstName": "Foo", "LastName": "Bar", "CurrentTime": time.Time{...}, "FullName": "Foo Bar"}

The full documentation is available at https://godoc.org/github.com/tuvistavie/serializer.

Building your own serializer

With Serializer as a base, you can easily build your serializer.

type UserSerializer struct {
  *serializer.Serializer
}

func NewUserSerializer(user User) *UserSerializer {
  u := &UserSerializer{serializer.New(user)}
  u.Pick("ID", "CreatedAt", "UpdatedAt", "DeletedAt")
  return u
}

func (u *UserSerializer) WithPrivateInfo() *UserSerializer {
  u.Pick("Email")
  return u
}

userMap := NewUserSerializer(user).WithPrivateInfo.Result()

Note that the u.Pick, and all other methods do modify the serializer, they do not return a new serializer each time. This is why it works even when ignoring u.Pick return value.

License

This is released under the MIT license. See the LICENSE file for more information.

Documentation

Overview

Package serializer contains

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Serializer

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

func New

func New(entity interface{}) *Serializer

Creates a new Serializer

func (*Serializer) Add

func (s *Serializer) Add(field string, value interface{}) *Serializer

Add a custom field to the result

func (*Serializer) AddFunc

func (s *Serializer) AddFunc(field string, f converter) *Serializer

Add a computed custom field to the result

func (*Serializer) AddFuncIf

func (s *Serializer) AddFuncIf(p predicate, field string, f converter) *Serializer

Add a computed custom field to the result if the predicate returns true

func (*Serializer) AddIf

func (s *Serializer) AddIf(p predicate, field string, value interface{}) *Serializer

Add a custom field to the result if the predicate returns true

func (*Serializer) Convert

func (s *Serializer) Convert(field string, f converter) *Serializer

Convert the field using the given converter

func (*Serializer) ConvertIf

func (s *Serializer) ConvertIf(p predicate, field string, f converter) *Serializer

Convert the field using the given converter if the predicate returns true

func (*Serializer) Omit

func (s *Serializer) Omit(fields ...string) *Serializer

Omit the given fields from the result

func (*Serializer) OmitIf

func (s *Serializer) OmitIf(p predicate, fields ...string) *Serializer

Omit the given fields from the result if the predicate returns true

func (*Serializer) Pick

func (s *Serializer) Pick(fields ...string) *Serializer

Add the given fields to the result

func (*Serializer) PickAll

func (s *Serializer) PickAll() *Serializer

Add all the exported fields to the result

func (*Serializer) PickIf

func (s *Serializer) PickIf(p predicate, fields ...string) *Serializer

Add the given fields to the result if the predicate returns true

func (*Serializer) Result

func (s *Serializer) Result() map[string]interface{}

Returns the result of the serialization as a map[string]interface{}

Jump to

Keyboard shortcuts

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