codec

package
v3.10.96 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: Apache-2.0 Imports: 7 Imported by: 22

Documentation

Overview

Package codec is an interface for encoding messages

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidMessage returned when invalid messge passed to codec
	ErrInvalidMessage = errors.New("invalid message")
	// ErrUnknownContentType returned when content-type is unknown
	ErrUnknownContentType = errors.New("unknown content-type")
)
View Source
var (
	// DefaultCodec is the global default codec
	DefaultCodec = NewCodec()
	// DefaultTagName specifies struct tag name to control codec Marshal/Unmarshal
	DefaultTagName = "codec"
)

Functions

func NewContext

func NewContext(ctx context.Context, c Codec) context.Context

NewContext put codec in context

Types

type Codec

type Codec interface {
	Marshal(v interface{}, opts ...Option) ([]byte, error)
	Unmarshal(b []byte, v interface{}, opts ...Option) error
	String() string
}

Codec encodes/decodes various types of messages.

func FromContext

func FromContext(ctx context.Context) (Codec, bool)

FromContext returns codec from context

func NewCodec

func NewCodec(opts ...Option) Codec

NewCodec returns new noop codec

type CodecV2 added in v3.10.87

type CodecV2 interface {
	Marshal(buf []byte, v interface{}, opts ...Option) ([]byte, error)
	Unmarshal(buf []byte, v interface{}, opts ...Option) error
	String() string
}

type Frame

type Frame struct {
	Data []byte
}

Frame gives us the ability to define raw data to send over the pipes

func NewFrame added in v3.8.11

func NewFrame(data []byte) *Frame

NewFrame returns new frame with data

func (*Frame) Marshal

func (m *Frame) Marshal() ([]byte, error)

Marshal returns frame data

func (*Frame) MarshalJSON

func (m *Frame) MarshalJSON() ([]byte, error)

MarshalJSON returns frame data

func (*Frame) ProtoMessage

func (m *Frame) ProtoMessage()

ProtoMessage noop func

func (*Frame) Reset

func (m *Frame) Reset()

Reset resets frame

func (*Frame) String

func (m *Frame) String() string

String returns frame as string

func (*Frame) Unmarshal

func (m *Frame) Unmarshal(data []byte) error

Unmarshal set frame data

func (*Frame) UnmarshalJSON

func (m *Frame) UnmarshalJSON(data []byte) error

UnmarshalJSON set frame data

type Option

type Option func(*Options)

Option func

func Flatten added in v3.10.88

func Flatten(b bool) Option

Flatten enables checking for flatten tag name

func Logger

func Logger(l logger.Logger) Option

Logger sets the logger

func Meter

func Meter(m meter.Meter) Option

Meter sets the meter

func SetOption

func SetOption(k, v interface{}) Option

SetOption returns a function to setup a context with given value

func TagName

func TagName(n string) Option

TagName sets the codec tag name in struct

func Tracer

func Tracer(t tracer.Tracer) Option

Tracer to be used for tracing

type Options

type Options struct {
	// Meter used for metrics
	Meter meter.Meter
	// Logger used for logging
	Logger logger.Logger
	// Tracer used for tracing
	Tracer tracer.Tracer
	// Context stores additional codec options
	Context context.Context
	// TagName specifies tag name in struct to control codec
	TagName string
	// Flatten specifies that struct must be analyzed for flatten tag
	Flatten bool
}

Options contains codec options

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions returns new options

type RawMessage added in v3.10.19

type RawMessage []byte

RawMessage is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay decoding or precompute a encoding.

func (*RawMessage) MarshalJSON added in v3.10.19

func (m *RawMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns m as the JSON encoding of m.

func (*RawMessage) UnmarshalJSON added in v3.10.19

func (m *RawMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *m to a copy of data.

Jump to

Keyboard shortcuts

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