encoding

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(codec Codec, v any) ([]byte, error)

Marshal converts its input v into []byte using the codec, respecting RestateMarshaler implementors

func Unmarshal

func Unmarshal(codec Codec, data []byte, v any) error

Unmarshal converts its input data and stores the result into v using the codec, respecting RestateUnmarshaler implementors v is expected to be a mutable concrete type, generally a pointer.

Types

type Codec

type Codec interface {
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, v any) error
}

Codec is a mechanism for serialising and deserialising a wide range of types. Care should be taken to ensure that only valid types are passed to a codec, eg proto.Message for ProtoCodec. Codecs *must* marshal deterministically, such that a round trip of []byte -> any -> []byte leaves the bytes unchanged.

type InputPayload

type InputPayload struct {
	Required    bool        `json:"required"`
	ContentType *string     `json:"contentType,omitempty"`
	JsonSchema  interface{} `json:"jsonSchema,omitempty"`
}

InputPayload is provided to Restate upon handler discovery, to teach the ingress how to validate incoming request bodies.

func InputPayloadFor

func InputPayloadFor(codec PayloadCodec, i any) *InputPayload

InputPayloadFor determines the InputPayload for the type stored in i, respecting RestateUnmarshaler implementors

type OutputPayload

type OutputPayload struct {
	ContentType           *string     `json:"contentType,omitempty"`
	SetContentTypeIfEmpty bool        `json:"setContentTypeIfEmpty"`
	JsonSchema            interface{} `json:"jsonSchema,omitempty"`
}

OutputPayload is provided to Restate upon handler discovery, to teach the ingress how to annotate outgoing response bodies.

func OutputPayloadFor

func OutputPayloadFor(codec PayloadCodec, o any) *OutputPayload

OutputPayloadFor determines the OutputPayload for the type stored in o, respecting RestateMarshaler implementors

type PayloadCodec

type PayloadCodec interface {
	InputPayload(i any) *InputPayload
	OutputPayload(o any) *OutputPayload
	Codec
}

PayloadCodec is implemented by a Codec that can also be used in handlers, and so must provide a InputPayload and OutputPayload i and o are zero values of the input/output types, which the codec may use to influence its response.

var (
	// BinaryCodec marshals []byte and unmarshals into *[]byte
	// In handlers, it uses a content type of application/octet-stream
	BinaryCodec PayloadCodec = binaryCodec{}
	// ProtoCodec marshals proto.Message and unmarshals into proto.Message or pointers to types that implement proto.Message
	// In handlers, it uses a content-type of application/proto
	ProtoCodec PayloadCodec = protoCodec{}
	// ProtoJSONCodec marshals proto.Message and unmarshals into proto.Message or pointers to types that implement proto.Message
	// It uses the protojson package to marshal and unmarshal
	// In handlers, it uses a content-type of application/json
	ProtoJSONCodec PayloadCodec = protoJSONCodec{}
	// JSONCodec marshals any json.Marshallable type and unmarshals into any json.Unmarshallable type
	// In handlers, it uses a content-type of application/json
	JSONCodec PayloadCodec = jsonCodec{}
)

type RestateMarshaler

type RestateMarshaler interface {
	RestateMarshal(codec Codec) ([]byte, error)
	OutputPayload(codec Codec) *OutputPayload
}

RestateMarshaler can be implemented by types that want to control their own marshaling Marshaling *must* be deterministic

type RestateUnmarshaler

type RestateUnmarshaler interface {
	RestateUnmarshal(codec Codec, data []byte) error
	InputPayload(codec Codec) *InputPayload
}

RestateUnmarshaler can be implemented by types that want to control their own unmarshaling

type Void

type Void struct{}

Void is a placeholder to signify 'no value' where a type is otherwise needed It implements RestateMarshaler and RestateUnmarshaler to ensure that no marshaling or unmarshaling ever happens on this type.

func (Void) InputPayload

func (v Void) InputPayload(codec Codec) *InputPayload

func (Void) OutputPayload

func (v Void) OutputPayload(codec Codec) *OutputPayload

func (Void) RestateMarshal

func (v Void) RestateMarshal(codec Codec) ([]byte, error)

func (Void) RestateUnmarshal

func (v Void) RestateUnmarshal(codec Codec, data []byte) error

Jump to

Keyboard shortcuts

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