format

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package format bridges Codec[T] to concrete serialization formats (JSON, YAML, TOML).

A codec works with an intermediate representation (map[string]any) that is format-agnostic. Format wraps that intermediate layer so the same codec can read and write multiple wire formats without any changes to the codec itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format[T any] struct {
	// contains filtered or unexported fields
}

Format binds a Codec[T] to a specific serialization format. Use JSON, YAML, or TOML to construct one.

func JSON

func JSON[T any](c codex.Codec[T]) Format[T]

JSON returns a Format that reads and writes JSON.

func New

func New[T any](c codex.Codec[T], marshal func(any) ([]byte, error), unmarshal func([]byte) (any, error)) Format[T]

New creates a Format from a codec and custom marshal/unmarshal functions. Use this to integrate formats not covered by the built-in constructors.

func TOML

func TOML[T any](c codex.Codec[T]) Format[T]

TOML returns a Format that reads and writes TOML.

func YAML

func YAML[T any](c codex.Codec[T]) Format[T]

YAML returns a Format that reads and writes YAML.

func (Format[T]) Marshal

func (f Format[T]) Marshal(v T) ([]byte, error)

Marshal encodes v to bytes using the codec and then the format serializer.

func (Format[T]) Schema

func (f Format[T]) Schema() schema.Schema

Schema returns the schema.Schema from the underlying codec.

func (Format[T]) Unmarshal

func (f Format[T]) Unmarshal(data []byte) (T, error)

Unmarshal deserializes data into an intermediate and then decodes it via the codec.

func (Format[T]) Validate

func (f Format[T]) Validate(v T) error

Validate checks v against the codec's constraints without serializing to bytes. It delegates to Codec.Validate — see its documentation for the rationale.

Jump to

Keyboard shortcuts

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