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 ¶
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 ¶
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 ¶
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.
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 JSONDecoder ¶
type JSONDecoder struct{}
JSONDecoder claims payloads that are a JSON object or array.
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) 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.
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.