encoding

package
v2.303.5 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 10 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→Encoder lookup. It is commonly used by configuration loading and transport layers to choose a decoder/encoder based on either:

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

Callers typically obtain a `*Map` via DI and then use `Get(kind)` 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, YAML, TOML
  • 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 Fx/Dig.

Provided constructors:

  • Protobuf encoders:

  • *proto.Binary (via proto.NewBinary)

  • *proto.Text (via proto.NewText)

  • *proto.JSON (via proto.NewJSON)

  • Structured config encoders:

  • *json.Encoder (via json.NewEncoder)

  • *toml.Encoder (via toml.NewEncoder)

  • *yaml.Encoder (via yaml.NewEncoder)

  • Other encoders:

  • *gob.Encoder (via gob.NewEncoder)

  • *bytes.Encoder (via bytes.NewEncoder) for io.ReaderFrom/io.WriterTo passthrough

Finally, it constructs an `*encoding.Map` via `NewMap`, pre-populated with common kind aliases (e.g. "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 encoding/errors.ErrInvalidType).

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", "yaml", "yml", "toml"

  • 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 JSON encoder implementation registered under kind "json".
	JSON *json.Encoder

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

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

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

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

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

	// GOB is the gob 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 (Fx/Dig). The default wiring is provided by `encoding.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 helpers and adapters used by go-service.
Package errors provides encoding helpers and adapters 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 json provides JSON encoding helpers and adapters used by go-service.
Package json provides JSON 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