codec

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codec turns NATS payloads into JSON a person can read, and JSON back into payloads. Decoders form a chain: the first decoder that claims a payload renders it, so a protobuf schema wins over a JSON guess, which wins over text, then a schemaless protobuf dump, then raw bytes.

Index

Constants

View Source
const DefaultTypeHeader = "Proto-Type"

DefaultTypeHeader is the message header a producer can set to name the protobuf type of its payload ("ride.Accept" or "backend:ride.Accept"). It wins over the subject rules. The "Nats-" prefix is reserved by the server, so the header does not use it.

Variables

View Source
var ErrNoType = errors.New("no protobuf type: pass --type or add a subject rule")

ErrNoType reports an encode request whose message type is not known.

Functions

func Encode

func Encode(reg *schema.Registry, ref string, jsonData []byte) ([]byte, protoreflect.MessageDescriptor, error)

Encode turns JSON (protojson syntax: field names in either case, enums by name, Timestamps as RFC 3339) into the wire form of the message ref.

Types

type Chain

type Chain []Decoder

Chain tries each decoder in order.

func DefaultFallbacks

func DefaultFallbacks() Chain

DefaultFallbacks returns the decoders that need no schema, in the order the chain should try them after any protobuf decoder.

func (Chain) Decode

func (c Chain) Decode(msg Message) Result

Decode runs the chain. It always returns a result: when no decoder claims the payload, the bytes are rendered as base64.

type Decoder

type Decoder interface {
	// Name identifies the decoder in notes and logs.
	Name() string
	// Decode returns ok=false when the decoder does not apply. A non-nil
	// error means the decoder applies but failed; the chain records the
	// error as a note and tries the next decoder.
	Decode(msg Message) (res Result, ok bool, err error)
}

Decoder renders a payload, or declines it.

type Format

type Format string

Format names the encoding a payload was decoded from.

const (
	FormatProto     Format = "protobuf"
	FormatJSON      Format = "json"
	FormatProtoWire Format = "protowire"
	FormatText      Format = "text"
	FormatBinary    Format = "binary"
	FormatEmpty     Format = "empty"
)

The formats the built-in decoders produce.

type Header map[string][]string

Header is the header set of a NATS message (nats.Header has this shape).

func (Header) Get

func (h Header) Get(key string) string

Get returns the first value of key, or "".

type JSONDecoder

type JSONDecoder struct{}

JSONDecoder claims payloads that are a JSON object or array.

func (JSONDecoder) Decode

func (JSONDecoder) Decode(msg Message) (Result, bool, error)

Decode implements Decoder.

func (JSONDecoder) Name

func (JSONDecoder) Name() string

Name implements Decoder.

type Message

type Message struct {
	Subject string
	Header  Header
	Data    []byte
}

Message is one payload to decode.

type ProtoDecoder

type ProtoDecoder struct {
	Registry *schema.Registry
	Router   *schema.Router
	// TypeHeader overrides DefaultTypeHeader; "-" turns header lookup off.
	TypeHeader string
	// Force, when set, is the message reference for every payload.
	Force string
	// EmitDefaults renders fields that hold their zero value.
	EmitDefaults bool
}

ProtoDecoder decodes payloads whose type it can resolve: from Force, then from the type header, then from the subject rules.

func (*ProtoDecoder) Decode

func (d *ProtoDecoder) Decode(msg Message) (Result, bool, error)

Decode implements Decoder.

func (*ProtoDecoder) Name

func (*ProtoDecoder) Name() string

Name implements Decoder.

func (*ProtoDecoder) Resolve

func (d *ProtoDecoder) Resolve(msg Message) (ref, origin string)

Resolve returns the message reference for msg and where it came from ("--type", "header", or the matching subject pattern).

type ProtoWireDecoder

type ProtoWireDecoder struct{}

ProtoWireDecoder renders a protobuf payload without its schema: field numbers become keys, and each value is shown in the most likely form. It claims a payload only when the whole payload parses as protobuf.

func (ProtoWireDecoder) Decode

func (ProtoWireDecoder) Decode(msg Message) (Result, bool, error)

Decode implements Decoder.

func (ProtoWireDecoder) Name

func (ProtoWireDecoder) Name() string

Name implements Decoder.

type Result

type Result struct {
	// Format is the encoding the payload was read as.
	Format Format `json:"format"`
	// Type is the message reference ("source:full.Name") for protobuf.
	Type string `json:"type,omitempty"`
	// Body is the payload rendered as JSON. Text is a JSON string; binary
	// is a base64 JSON string.
	Body json.RawMessage `json:"payload"`
	// Notes explain a fallback or a partial decode, for example a schema
	// that did not fit or unknown fields.
	Notes []string `json:"notes,omitempty"`
}

Result is a decoded payload.

type TextDecoder

type TextDecoder struct{}

TextDecoder claims payloads that are printable UTF-8.

func (TextDecoder) Decode

func (TextDecoder) Decode(msg Message) (Result, bool, error)

Decode implements Decoder.

func (TextDecoder) Name

func (TextDecoder) Name() string

Name implements Decoder.

Jump to

Keyboard shortcuts

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