reser

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2021 License: MIT Imports: 4 Imported by: 0

README

reser

Serialization util for golang.

It allows for things like:

  • Abstracting serialization using interfaces
  • Polymorphic serializtion with tag
  • Polymorphic serializtion without tag

It's very simplistic and does not implement any serialization format on it's own. Instead it builds tools on top of existing serialization formats like JSON and their existing implementations, like one in golang stdlib.

It also simplifies doing lots of amazing things like integrating ZSTD dictionary compression directly into serializer/deserializer function.

Examples

See docs and examples files (WIP)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultTagSerializer

type DefaultTagSerializer struct {
	Serializer       Serializer
	Deserializer     Deserializer
	TagTypeResgistry *TagTypeResgistry
}

DefaultTagSerializer is default implementation of TagSerializer and TagDeserializer.

func (*DefaultTagSerializer) Deserialize

func (ds *DefaultTagSerializer) Deserialize(data []byte, tt TypeTag) (res interface{}, err error)

func (*DefaultTagSerializer) GetTypeTag

func (ds *DefaultTagSerializer) GetTypeTag(data interface{}) (tt TypeTag, err error)

func (*DefaultTagSerializer) Serialize

func (ds *DefaultTagSerializer) Serialize(event interface{}) (res []byte, err error)

type Deserializer

type Deserializer interface {
	Deserialize(data []byte, dst interface{}) (err error)
}

Deserializer, which fits most interfaces exposed by golang already. For instance, json.Unmarshal with SerializerFunc.

type DeserializerFunc

type DeserializerFunc func(data []byte, dst interface{}) (err error)

func (DeserializerFunc) Deserialize

func (f DeserializerFunc) Deserialize(data []byte, dst interface{}) (err error)

type ETPolySerializer

type ETPolySerializer struct {
	Serializer       Serializer
	Deserializer     Deserializer
	TagTypeResgistry *TagTypeResgistry
}

ETPolySerializer uses external field called `type`. Actual data is stored in `data` json field. This approach is known as adjacent tagging in most(?) serialization frameworks(for instance serde_json). It works with serializers like JSON one build in golang stdlib.

Note: in order for this to work deserializer must accept unknown fields in data, otherwise it won't work. Note#2: It's implementation is somwhat magic and it's expected to work only against JSON serializer in go stdlib. No unit testing is performed against any other serialization implementation.

func (*ETPolySerializer) PolyDeserialize

func (ets *ETPolySerializer) PolyDeserialize(data []byte) (res interface{}, err error)

func (*ETPolySerializer) PolySerialize

func (ets *ETPolySerializer) PolySerialize(data interface{}) (res []byte, err error)

type PolyDeserializer

type PolyDeserializer interface {
	PolyDeserialize(data []byte) (res interface{}, err error)
}

PolyDeserializer handles polymprphic deserialization without any external hints about type of data deserialized provided by caller.

Note: This deserializer always returns struct values rather than pointer values.

type PolySerializer

type PolySerializer interface {
	PolySerialize(data interface{}) (res []byte, err error)
}

PolySerializer handles polymorphic serialization without any external hints about type of data serialized.

type PrefixPolySerializer

type PrefixPolySerializer struct {
	TypeTagSerializer   PolySerializer
	TypeTagDeserializer PolyDeserializer

	DataSerializer   TagSerializer
	DataDeserializer TagDeserializer
}

PrefixPolySerializer serializes and stores tag as prefix in result byte slice in order to mix tag along with data stored with it.

It adds minimal overhead to serialized data. When tag is shorter than 127 bytes then only one additional byte(apart from tag) is added to serialized data.

func (*PrefixPolySerializer) PolyDeserialize

func (pps *PrefixPolySerializer) PolyDeserialize(data []byte) (res interface{}, err error)

func (*PrefixPolySerializer) PolySerialize

func (pps *PrefixPolySerializer) PolySerialize(data interface{}) (res []byte, err error)

type STPolySerializer

type STPolySerializer struct {
	Type         reflect.Type
	Serializer   Serializer
	Deserializer Deserializer
}

STPolySerializer is poly serializer and deserializer, which is able to serialize only single type.

func (*STPolySerializer) PolyDeserialize

func (ser *STPolySerializer) PolyDeserialize(data []byte) (res interface{}, err error)

func (*STPolySerializer) PolySerialize

func (ser *STPolySerializer) PolySerialize(data interface{}) (res []byte, err error)

type Serializer

type Serializer interface {
	Serialize(data interface{}) (res []byte, err error)
}

Serializer, which fits most interfaces exposed by golang already. For instance, json.Marshal with SerializerFunc.

type SerializerFunc

type SerializerFunc func(data interface{}) (res []byte, err error)

func (SerializerFunc) Serialize

func (f SerializerFunc) Serialize(data interface{}) (res []byte, err error)

type TagDeserializer

type TagDeserializer interface {
	Deserialize(data []byte, tag TypeTag) (res interface{}, err error)
}

TagDeserializer uses tag and data in order to deserialize actual type.

Note: This deserializer always returns struct pointers rather than struct values.

type TagNotFoundError

type TagNotFoundError struct {
	Type reflect.Type
}

func (*TagNotFoundError) Error

func (e *TagNotFoundError) Error() string

type TagRegisterError

type TagRegisterError struct {
	Type    reflect.Type
	TypeTag TypeTag
}

func (*TagRegisterError) Error

func (e *TagRegisterError) Error() string

type TagSerializer

type TagSerializer interface {
	GetTypeTag(data interface{}) (tt TypeTag, err error)
	Serialize(data interface{}) (res []byte, err error)
}

TagSerializer uses external information(so called tag) to make polymorphic marshalling work.

type TagTypeResgistry

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

TagTypeResgistry maps type to type tag and vice versa.

func NewTagTypeResgistry

func NewTagTypeResgistry(tagType reflect.Type) *TagTypeResgistry

NewTagTypeResgistry creates new tag type registry accepting tags with specified type.

func (*TagTypeResgistry) GetTag

func (ttr *TagTypeResgistry) GetTag(ty reflect.Type) (tt TypeTag, err error)

func (*TagTypeResgistry) GetType

func (ttr *TagTypeResgistry) GetType(tt TypeTag) (ty reflect.Type, err error)

func (*TagTypeResgistry) GetTypeTagType

func (ttr *TagTypeResgistry) GetTypeTagType() reflect.Type

GetTypeTagType returns type of type tags used in this registry.

func (*TagTypeResgistry) RegisterType

func (ttr *TagTypeResgistry) RegisterType(ty reflect.Type, et TypeTag) (err error)

Registers specified type, so that it can be deserialized using specified type tag.

type TypeNotFoundError

type TypeNotFoundError struct {
	TypeTag TypeTag
}

func (*TypeNotFoundError) Error

func (e *TypeNotFoundError) Error() string

type TypeTag

type TypeTag interface{}

type TypeTagTypeError

type TypeTagTypeError struct {
	ExpectedType reflect.Type
	Tag          TypeTag
}

func (*TypeTagTypeError) Error

func (e *TypeTagTypeError) Error() string

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

func (*UnsupportedTypeError) Error

func (ute *UnsupportedTypeError) Error() string

type UnsupportedValueError

type UnsupportedValueError struct {
	Val interface{}
}

func (*UnsupportedValueError) Error

func (uve *UnsupportedValueError) Error() string

Jump to

Keyboard shortcuts

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