serialize

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package serialize for transforming between json serialized []byte and go structs

Index

Constants

View Source
const TypeBool = true
View Source
const TypeInt = 1
View Source
const TypeString = ``

Variables

View Source
var (

	// ErrUnableToConvertNilToStruct - nil cannot be converted to struct
	ErrUnableToConvertNilToStruct = errors.New(`unable to convert nil to [struct,array,slice,ptr]`)
	// ErrUnableToConvertValueToStruct - value  cannot be converted to struct
	ErrUnableToConvertValueToStruct = errors.New(`unable to convert value to struct`)
)
View Source
var (
	// AnyType defines that serializer supports any supportable variable type
	AnyType SuppportedType = `any`
	// StructType defines that serializer supports struct type
	StructType SuppportedType = `struct`
	ProtoType  SuppportedType = `proto`
	ScalarType SuppportedType = `scalar`

	DefaultTarget TargetType = `default`
	PreferJSON    TargetType = `json`

	DefaultSerializer = &GenericSerializer{
		Supports:      []SuppportedType{AnyType},
		Target:        DefaultTarget,
		UseProtoNames: true,
	}

	PreferJSONSerializer = &GenericSerializer{
		Supports:      []SuppportedType{AnyType},
		Target:        PreferJSON,
		UseProtoNames: true,
	}
	KeySerializer = &StringSerializer{}

	ErrOnlyStringSupported = errors.New(`only string supported`)
)
View Source
var (
	ErrToBytesConverterIsNil = errors.New(`to bytes converter is nil`)
)

Functions

func ArgsToBytes

func ArgsToBytes(iargs []interface{}, toBytesConverter ToBytesConverter) (aa [][]byte, err error)

ArgsToBytes converts func arguments to bytes

func BinaryProtoMarshal

func BinaryProtoMarshal(entry proto.Message) ([]byte, error)

func BinaryProtoUnmarshal

func BinaryProtoUnmarshal(bb []byte, messageType proto.Message) (message proto.Message, err error)

BinaryProtoUnmarshal r unmarshalls []byte as proto.Message to pointer, and returns value pointed to

func FromBytesToStruct

func FromBytesToStruct(bb []byte, target interface{}) (result interface{}, err error)

FromBytesToStruct converts []byte to struct,array,slice depending on target type

func JSONProtoMarshal

func JSONProtoMarshal(entry proto.Message, mo *protojson.MarshalOptions) ([]byte, error)

func JSONProtoUnmarshal

func JSONProtoUnmarshal(json []byte, messageType proto.Message) (message proto.Message, err error)

func JSONUnmarshalPtr

func JSONUnmarshalPtr(bb []byte, to interface{}) (result interface{}, err error)

JSONUnmarshalPtr unmarshalls []byte as json to pointer, and returns value pointed to

Types

type BinaryProtoSerializer

type BinaryProtoSerializer struct {
}

func (*BinaryProtoSerializer) FromBytes

func (ps *BinaryProtoSerializer) FromBytes(serialized []byte, target proto.Message) (proto.Message, error)

func (*BinaryProtoSerializer) ToBytes

func (ps *BinaryProtoSerializer) ToBytes(entry proto.Message) ([]byte, error)

type BinarySerializer

type BinarySerializer struct {
}

type FromByter

type FromByter interface {
	FromBytes([]byte) (interface{}, error)
}

FromByter interface supports FromBytes func for converting from slice of bytes to some defined target type

type FromBytesConverter

type FromBytesConverter interface {
	FromBytesTo(from []byte, target interface{}) (interface{}, error)
}

FromBytesConverter interface supports FromBytesConverter func for converting from slice of bytes to target type

type GenericSerializer

type GenericSerializer struct {
	Supports      []SuppportedType
	Target        TargetType
	UseProtoNames bool // to use proto field name instead of lowerCamelCase name in JSON field names
}

func (*GenericSerializer) FromBytesTo

func (g *GenericSerializer) FromBytesTo(serialized []byte, target interface{}) (interface{}, error)

func (*GenericSerializer) ToBytesFrom

func (g *GenericSerializer) ToBytesFrom(entry interface{}) ([]byte, error)

type JSONProtoSerializer

type JSONProtoSerializer struct {
	UseProtoNames bool // to use proto field name instead of lowerCamelCase name in JSON field names
}

func (*JSONProtoSerializer) FromBytes

func (js *JSONProtoSerializer) FromBytes(serialized []byte, target proto.Message) (proto.Message, error)

func (*JSONProtoSerializer) ToBytes

func (js *JSONProtoSerializer) ToBytes(entry proto.Message) ([]byte, error)

type JSONSerializer

type JSONSerializer struct {
}

type ProtoSerializer

type ProtoSerializer interface {
	ToBytes(proto.Message) ([]byte, error)
	FromBytes(serialized []byte, target proto.Message) (proto.Message, error)
}

type Serializable

type Serializable interface {
	ToBytes(ToBytesConverter) ([]byte, error)
}

type Serializer

type Serializer interface {
	ToBytesConverter
	FromBytesConverter
}

Serializer generic interface for serializing

type StringSerializer

type StringSerializer struct {
}

func (*StringSerializer) FromBytesTo

func (g *StringSerializer) FromBytesTo(serialized []byte, target interface{}) (interface{}, error)

func (*StringSerializer) ToBytesFrom

func (g *StringSerializer) ToBytesFrom(entry interface{}) ([]byte, error)

type SuppportedType

type SuppportedType string

type TargetType

type TargetType string

type ToByter

type ToByter interface {
	ToBytes() ([]byte, error)
}

ToByter interface supports ToBytes func for converting to slice of bytes from source type

type ToBytesConverter

type ToBytesConverter interface {
	ToBytesFrom(from interface{}) ([]byte, error)
}

ToBytesConverter supports ToBytesConverter func converting from some interface to bytes

Jump to

Keyboard shortcuts

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