encoding

package
v2.736.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 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", "toml", "json"), or
  • a content kind / media subtype (for example "protobuf", "bytes").

Map registers each encoder under exactly one canonical kind; callers that need to accept alternate spellings (such as HTTP media subtype aliases "pb" or "octet-stream") translate them to the canonical kind before calling Map.Get rather than relying on this registry to know every alias.

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

NewMap constructs a *Map that registers default encoders under common kinds used throughout go-service, including:

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

Module provides the default *Map for Fx applications.

Start with Encoder, Map, NewMap, and Module.

Index

Constants

This section is empty.

Variables

Module provides the default encoder registry as a *Map.

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() *Map

NewMap constructs a Map with the default encoders.

The returned registry includes these kinds, each registered under exactly one canonical name (no aliases, matching github.com/alexfalkowski/go-service/v2/encoding/stream.Map's design): "json", "hjson", "yaml", "toml", "msgpack", "protobuf", "prototext", "protojson", "gob", "bytes".

Callers that need to accept alternate spellings of these kinds (for example HTTP media subtypes such as "pb" or "octet-stream") are expected to translate them to the canonical kind above before calling Map.Get; see github.com/alexfalkowski/go-service/v2/net/http/content/unary's unaryKind.

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, or if kind was registered with a nil encoder, Get returns nil. Callers typically treat nil as "unknown or unavailable 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.

Keys includes kinds registered with nil encoders. 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.

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 stream provides streaming (multi-value) encode/decode interfaces used by go-service.
Package stream provides streaming (multi-value) encode/decode interfaces used by go-service.
gob
Package gob provides a streaming gob github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
Package gob provides a streaming gob github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
json
Package json provides a streaming JSON github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
Package json provides a streaming JSON github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
msgpack
Package msgpack provides a streaming MessagePack github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
Package msgpack provides a streaming MessagePack github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
yaml
Package yaml provides a streaming YAML github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair used by go-service.
Package yaml provides a streaming YAML github.com/alexfalkowski/go-service/v2/encoding/stream.Encoder/ github.com/alexfalkowski/go-service/v2/encoding/stream.Decoder pair 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