encoding

package
v2.549.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package encoding provides value encoding/decoding helpers and DI wiring used by go-service.

This package defines a small Encoder interface (encode to an io.Writer, decode from an io.Reader) and a registry (Map) used to select an encoder by kind at runtime.

Registry

Map is a kind-to-Encoder lookup. It is commonly used by configuration loading and transport layers to choose a decoder/encoder based on either:

  • a file extension (for example "yaml", "yml", "toml", "json"), or
  • a content kind / media subtype (for example "proto", "plain", "octet-stream").

Callers typically obtain a *Map via DI and then use Map.Get to select an encoder, often falling back to a default when the requested kind is not registered.

Wiring

Module wires the default encoder implementations and provides a *Map pre-populated with common kinds used throughout go-service, including:

  • JSON, HJSON, YAML, TOML, MessagePack
  • protobuf binary/text/JSON variants
  • gob
  • "plain"/bytes passthrough for io.ReaderFrom/io.WriterTo payloads

Start with Encoder, Map, NewMap, and Module.

Index

Constants

This section is empty.

Variables

Module wires the default encoder implementations and the encoder registry into go.uber.org/fx/go.uber.org/dig.

It provides protobuf encoders (*proto.Binary, *proto.Text, *proto.JSON), structured config encoders (*json.Encoder, *hjson.Encoder, *toml.Encoder, *yaml.Encoder, *msgpack.Encoder), and passthrough/specialized encoders (*gob.Encoder and *bytes.Encoder).

Finally, it constructs a *Map via NewMap, pre-populated with common kind aliases (for example "yaml"/"yml", protobuf kind synonyms, and "plain"/"octet-stream" passthrough kinds).

Functions

This section is empty.

Types

type Encoder

type Encoder interface {
	// Encode writes a serialized representation of v to w.
	Encode(w io.Writer, v any) error

	// Decode reads from r and decodes into v.
	Decode(r io.Reader, v any) error
}

Encoder encodes values to a writer and decodes values from a reader.

Encoder is intentionally minimal so multiple concrete encodings (JSON/YAML/TOML/protobuf/gob, etc.) can be used interchangeably.

Encode contract

Encode must serialize v to w. Implementations may require that v satisfies additional interfaces or is of a particular shape (for example a protobuf encoder may require v to implement google.golang.org/protobuf/proto.Message).

Decode contract

Decode must read from r and populate v. In most cases v is expected to be a pointer to the target value so the decoder can mutate it (e.g. *MyStruct). Implementations may return an error if v is not a supported type (for example github.com/alexfalkowski/go-service/v2/encoding/errors.ErrInvalidType).

Structured single-value decoders should reject additional encoded values after the first payload, either by consuming the whole input or by returning github.com/alexfalkowski/go-service/v2/encoding/errors.ErrTrailingData. Stream or passthrough encoders may delegate full-consumption semantics to the concrete value they decode into.

Some implementations buffer the remaining contents of r before decoding. When r contains untrusted input, callers must bound it before calling Decode. Standard go-service HTTP and cache wiring applies those limits before values reach encoders.

Implementations should return any underlying I/O errors and any parse/unmarshal errors produced by their respective codecs.

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map provides lookup and registration of encoders by kind.

This type is a thin convenience around a string-keyed map and is commonly used with configuration to select an encoder at runtime.

Map is not concurrency-safe. If you mutate it via Register, do so during initialization.

func NewMap

func NewMap(params MapParams) *Map

NewMap constructs a Map pre-populated with default encoders.

The returned registry includes common kinds used throughout go-service, including:

  • Structured config formats: "json", "hjson", "yaml", "yml", "toml", "msgpack"

  • Protobuf formats:

  • binary: "proto", "protobuf", "pb", "protobin", "pbbin"

  • text: "prototext", "prototxt", "pbtxt"

  • JSON: "protojson", "pbjson"

  • gob: "gob"

  • bytes/plain passthrough: "plain", "octet-stream", "markdown"

Callers can add additional kinds or override existing kinds via Map.Register.

func (*Map) Get

func (f *Map) Get(kind string) Encoder

Get returns the encoder registered for kind.

If no encoder is registered for kind, Get returns nil. Callers typically treat nil as "unknown kind" and fall back to a default encoder elsewhere.

func (*Map) Keys

func (f *Map) Keys() []string

Keys returns the list of registered encoder kinds.

The returned slice is not guaranteed to be sorted.

func (*Map) Register

func (f *Map) Register(kind string, enc Encoder)

Register associates kind with enc, overwriting any existing encoder.

If kind already exists, the previous encoder is replaced.

type MapParams

type MapParams struct {
	di.In

	// JSON is the encoder implementation registered under kind "json".
	JSON *json.Encoder

	// HumanJSON is the encoder implementation registered under kind "hjson".
	HumanJSON *hjson.Encoder

	// YAML is the encoder implementation registered under kinds "yaml" and "yml".
	YAML *yaml.Encoder

	// TOML is the encoder implementation registered under kind "toml".
	TOML *toml.Encoder

	// MessagePack is the encoder implementation registered under kind "msgpack".
	MessagePack *msgpack.Encoder `optional:"true"`

	// ProtoBinary is the encoder implementation registered under common binary kinds
	// (e.g. "proto", "protobuf", "pb", etc.).
	ProtoBinary *proto.Binary

	// ProtoText is the encoder implementation registered under common text kinds
	// (e.g. "prototext", "prototxt", "pbtxt").
	ProtoText *proto.Text

	// ProtoJSON is the encoder implementation registered under common JSON kinds
	// (e.g. "protojson", "pbjson").
	ProtoJSON *proto.JSON

	// GOB is the encoder implementation registered under kind "gob".
	GOB *gob.Encoder

	// Bytes is the passthrough encoder for [io.ReaderFrom]/[io.WriterTo] payloads, registered under kinds
	// like "plain", "octet-stream", and "markdown".
	Bytes *bytes.Encoder
}

MapParams defines the dependencies used to construct an encoding Map.

It is intended for dependency injection (go.uber.org/fx/go.uber.org/dig). The default wiring is provided by Module.

Directories

Path Synopsis
Package base64 provides encoding helpers and adapters used by go-service.
Package base64 provides encoding helpers and adapters used by go-service.
Package bytes provides byte-oriented encoding helpers and adapters used by go-service.
Package bytes provides byte-oriented encoding helpers and adapters used by go-service.
Package errors provides encoding error values and helpers used by go-service.
Package errors provides encoding error values and helpers used by go-service.
Package gob provides Gob encoding helpers and adapters used by go-service.
Package gob provides Gob encoding helpers and adapters used by go-service.
Package hjson provides HJSON encoding helpers and adapters used by go-service.
Package hjson provides HJSON encoding helpers and adapters used by go-service.
Package json provides the go-service JSON import path.
Package json provides the go-service JSON import path.
Package msgpack provides MessagePack encoding helpers and adapters used by go-service.
Package msgpack provides MessagePack encoding helpers and adapters used by go-service.
Package proto provides Protocol Buffers (protobuf) encoding helpers and adapters used by go-service.
Package proto provides Protocol Buffers (protobuf) encoding helpers and adapters used by go-service.
Package toml provides TOML encoding helpers and adapters used by go-service.
Package toml provides TOML encoding helpers and adapters used by go-service.
Package yaml provides YAML encoding helpers and adapters used by go-service.
Package yaml provides YAML encoding helpers and adapters used by go-service.

Jump to

Keyboard shortcuts

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