codec

package
v0.0.1-alpha.8 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

View Source
const JSONApplicationType = "application/json"
View Source
const ProtocolBuffersApplicationType = "application/x-google-protobuf"

ProtocolBuffersApplicationType the ProtocolBuffers application type.

Reference: https://github.com/google/protorpc/blob/95849c9a3e414b9ba2d3da91fd850156348fe558/protorpc/protobuf.py#L52

Variables

View Source
var ErrCodecNotFound = errors.New("streams: codec not found")

ErrCodecNotFound the specified codec is not registered in streams' codec package.

View Source
var (
	ErrInvalidFormat = errors.New("streams.codec: received invalid format")
)

Functions

func Unmarshal

func Unmarshal[T any](codecType string, src []byte, dst T) error

Unmarshal decodes a message using the codec specified in codecType (e.g. JSONApplicationType).

Types

type Codec

type Codec interface {
	// Encode encodes the given input into an array of bytes using an underlying serialization type.
	Encode(v any) ([]byte, error)
	// Decode decodes the given input of an array of bytes into a Go type using an underlying serialization type.
	//
	// Go type MUST be a pointer reference so serializer can append data into it.
	Decode(src []byte, dst any) error
	// ApplicationType returns the RFC application type of the underlying serializer implementation
	// (e.g. application/json).
	ApplicationType() string
}

A Codec is a device or computer program that encodes or decodes a data stream or signal.

type JSON

type JSON struct{}

func (JSON) ApplicationType

func (j JSON) ApplicationType() string

func (JSON) Decode

func (j JSON) Decode(src []byte, dst any) error

func (JSON) Encode

func (j JSON) Encode(v any) ([]byte, error)

type Mock

type Mock struct {
	EncodeBytes           []byte
	EncodeError           error
	DecodeError           error
	ApplicationTypeString string
}

A Mock is a dummy type of Codec ready to be used for testing purposes.

func (Mock) ApplicationType

func (n Mock) ApplicationType() string

func (Mock) Decode

func (n Mock) Decode(_ []byte, _ any) error

func (Mock) Encode

func (n Mock) Encode(_ any) ([]byte, error)

type ProtocolBuffers

type ProtocolBuffers struct{}

The ProtocolBuffers codec is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

func (ProtocolBuffers) ApplicationType

func (b ProtocolBuffers) ApplicationType() string

func (ProtocolBuffers) Decode

func (b ProtocolBuffers) Decode(src []byte, dst any) error

func (ProtocolBuffers) Encode

func (b ProtocolBuffers) Encode(v any) ([]byte, error)

Jump to

Keyboard shortcuts

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