cerealbox

package module
v0.0.0-...-2cdbda6 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2021 License: MIT Imports: 8 Imported by: 0

README

cerealbox

Custom JSON Serialisation for Go

Documentation

For now.. See ceralbox_tomap_test for examples of usage.

Why?

I needed more flexiablity on how I serialised my Structs into JSON for our Rest API Coming from Django Rest Framework I missed the power of the Serializers so I have attempted to add the same idea in Go.

I found https://github.com/danhper/structomap but it didn't do all I wanted. It missed parsing Mapt to struct and I also figured here is a good place to add in vaidation at the same time.

Example

Create your struct

type Example struct {
	Name        string
	Age         int
	DateOfBirth time.Time
	Hide        bool
}

Add a Serialize method to implement ISerializable

func (this Example) Serialize(builder ISerializer) ISerializer {
	return builder.DoString("name", "Name", true, 0, 255).
		DoInt("age", "Age", true, 0, 100).
		DoTime("date_of_birth", "DateOfBirth", true, nil, nil).
		DoBool("hidden", "Hide", true)
}

This is builder function that handles converting you structure into Json. Note there are validation rules here but they are not used when converting to a Map

Usage

    example := Example{
		Name:        "Jack Benny",
		Age:         21,
		DateOfBirth: time.Now(),
		Hide:        false}

	map := ToMap(&example)
    //TODO use your favourite JSON library to generate the JSON

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromMap

func FromMap(item interface{}, jsonmap map[string]interface{}) (interface{}, validation.ValidationErrors)

func FromMapWithFunc

func FromMapWithFunc(item interface{}, jsonmap map[string]interface{}, serializerFunc SerializerFunc) (interface{}, validation.ValidationErrors)

func ToMap

func ToMap(item interface{}) map[string]interface{}

func ToMapWithFunc

func ToMapWithFunc(item interface{}, serializerFunc SerializerFunc) map[string]interface{}

func ToSlice

func ToSlice(items interface{}) []map[string]interface{}

func ToSliceWithFunc

func ToSliceWithFunc(items interface{}, serializerFunc SerializerFunc) []map[string]interface{}

Types

type ISerializable

type ISerializable interface {
	Serialize(builder ISerializer) ISerializer
}

type IValidator

type IValidator interface {
	Validate(interface{}) []error
}

type SerializerFromMap

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

func (SerializerFromMap) DoBool

func (this SerializerFromMap) DoBool(keyName string, fieldName string, required bool) ISerializer

func (SerializerFromMap) DoFloat32

func (this SerializerFromMap) DoFloat32(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerFromMap) DoFloat64

func (this SerializerFromMap) DoFloat64(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerFromMap) DoInt

func (this SerializerFromMap) DoInt(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerFromMap) DoSlice

func (this SerializerFromMap) DoSlice(keyName string, fieldName string) ISerializer

func (SerializerFromMap) DoString

func (this SerializerFromMap) DoString(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerFromMap) DoTime

func (this SerializerFromMap) DoTime(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerFromMap) DoUint

func (this SerializerFromMap) DoUint(keyName string, fieldName string, required bool, validator IValidator) ISerializer

type SerializerFunc

type SerializerFunc func(builder ISerializer) ISerializer

type SerializerToMap

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

func (SerializerToMap) DoBool

func (this SerializerToMap) DoBool(keyName string, fieldName string, required bool) ISerializer

func (SerializerToMap) DoFloat32

func (this SerializerToMap) DoFloat32(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerToMap) DoFloat64

func (this SerializerToMap) DoFloat64(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerToMap) DoInt

func (this SerializerToMap) DoInt(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerToMap) DoSlice

func (this SerializerToMap) DoSlice(keyName string, fieldName string) ISerializer

func (SerializerToMap) DoString

func (this SerializerToMap) DoString(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerToMap) DoTime

func (this SerializerToMap) DoTime(keyName string, fieldName string, required bool, validator IValidator) ISerializer

func (SerializerToMap) DoUint

func (this SerializerToMap) DoUint(keyName string, fieldName string, required bool, validator IValidator) ISerializer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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