repositorypackage
0.1.0
Repository: https://github.com/jl987-jie/structomap.git
Documentation: pkg.go.dev
# README
Go serializer

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": "[email protected]", "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.