encoding

package
v2.0.0-alpha.63 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilRegistry      = errors.New("encoding: registry is nil")
	ErrNilCodec         = errors.New("encoding: codec is nil")
	ErrEmptyContentType = errors.New("encoding: content type is empty")
	ErrCodecNotFound    = errors.New("encoding: codec not found")
	ErrRegistryNotFound = errors.New("encoding: registry not found in context")
)
View Source
var (
	// ErrOutputInUse identifies a competing consumption attempt.
	ErrOutputInUse = result.ErrInUse
	// ErrOutputClosed identifies consumption after stopping, or interrupted by Close.
	ErrOutputClosed = result.ErrClosed
)

Functions

func DecodeBytes

func DecodeBytes(ctx context.Context, dec Decoder, data []byte) (runtime.Value, error)

DecodeBytes decodes a complete byte document through the reader-based API.

func EncodeBytes

func EncodeBytes(ctx context.Context, enc Encoder, value runtime.Value) ([]byte, error)

EncodeBytes collects an encoder's writes into caller-owned bytes, including any prefix written before an error. Publish the bytes only on success when whole-document output must be atomic.

func WithReadOnlyRegistry

func WithReadOnlyRegistry(ctx context.Context, registry *Registry) context.Context

WithReadOnlyRegistry adds a finalized registry to the context. Lookup helpers share the registered codecs, and RegistryFrom returns a shallow snapshot of the registrations. The caller must keep the original registrations fixed.

func WithRegistry

func WithRegistry(ctx context.Context, registry *Registry) context.Context

WithRegistry adds a caller-owned registry to the context.

Types

type Codec

type Codec interface {
	ContentType() string
	Encoder
	Decoder
}

Codec combines encoder and decoder capabilities.

func CodecFrom

func CodecFrom(ctx context.Context, contentType string) (Codec, error)

CodecFrom resolves codec by content type from registry in context.

type CodecRegistrar

type CodecRegistrar interface {
	Register(codec Codec) error
}

CodecRegistrar registers codecs by content type.

type CodecResolver

type CodecResolver interface {
	Codec(contentType string) (Codec, error)
	Encoder(contentType string) (Encoder, error)
	Decoder(contentType string) (Decoder, error)
}

CodecResolver looks up registered codecs and their encoding and decoding capabilities.

type Consumer

type Consumer = result.Consumer

Consumer receives synchronous, borrowed, read-only encoded chunks.

type Content

type Content = result.Content

Content is detached encoded data owned by its recipient.

type Decoder

type Decoder interface {
	Decode(ctx context.Context, src io.Reader) (runtime.Value, error)
	DecodeWith() DecoderConfigurer
}

Decoder reads one complete document into a materialized runtime value. It borrows src only until Decode returns and must not close or retain it, or read asynchronously. It may buffer and read ahead. Failure may consume part or all of src. The caller owns the reader's lifecycle. Context is propagated, but cannot interrupt arbitrary blocking reader implementations. Nil contexts and readers must return an error wrapping runtime.ErrInvalidArgument. Decoders reject extra roots, malformed input, and truncated containers.

func DecoderFrom

func DecoderFrom(ctx context.Context, contentType string) (Decoder, error)

DecoderFrom resolves decoder by content type from registry in context.

type DecoderConfigurer

type DecoderConfigurer interface {
	PreHook(PreDecoderHook) DecoderConfigurer
	PostHook(PostDecoderHook) DecoderConfigurer
	Decoder() Decoder
}

type Encoder

type Encoder interface {
	Encode(ctx context.Context, dst io.Writer, value runtime.Value) error
	EncodeWith() EncoderConfigurer
}

Encoder writes a runtime value to a borrowed writer using the caller's context. It must not close or retain the writer, or use it after returning. Write buffers may be reused after Write returns, as required by io.Writer. Success completes all codec-owned writes and flushing; the caller owns flushing, finalizing, and closing its own wrappers. Failure may leave a prefix in dst. Use EncodeBytes and publish only on success for atomic output. Codecs may buffer internally and cannot interrupt arbitrary blocking I/O. Nil contexts and writers must return an error wrapping runtime.ErrInvalidArgument. Preserve non-nil I/O errors; incomplete writes without an error return io.ErrShortWrite and stop without retrying.

func EncoderFrom

func EncoderFrom(ctx context.Context, contentType string) (Encoder, error)

EncoderFrom resolves encoder by content type from registry in context.

type EncoderConfigurer

type EncoderConfigurer interface {
	PreHook(PreEncoderHook) EncoderConfigurer
	PostHook(PostEncoderHook) EncoderConfigurer
	Encoder() Encoder
}

type Metadata

type Metadata = result.Metadata

Metadata is the immutable descriptor of the complete encoded representation.

type Output

type Output = result.Output

Output is a caller-owned, one-shot encoded output handle. Consume or Collect observes its terminal outcome; Close abandons it and reports cleanup errors.

type PostDecoderHook

type PostDecoderHook func(context.Context, runtime.Value, error) error

PostDecoderHook runs once after decoding, with the decoded value on success or runtime.None on failure. Hooks run in order and stop on the first error.

type PostEncoderHook

type PostEncoderHook func(context.Context, runtime.Value, error) error

PostEncoderHook runs after each visited value, including on encoding failure. Ancestor hooks receive descendant failures. Hooks stop on the first error.

type PreDecoderHook

type PreDecoderHook func(context.Context) error

PreDecoderHook runs once before decoding, without consuming the input. Returning an error skips decoding and post-hooks.

type PreEncoderHook

type PreEncoderHook func(context.Context, runtime.Value) error

PreEncoderHook runs before each visited value, in registration order. Returning an error skips that value's encoding and post-hooks.

type Registry

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

Registry stores codecs by content type.

func NewEmptyRegistry

func NewEmptyRegistry() *Registry

NewEmptyRegistry creates a registry without predefined codecs.

func NewRegistry

func NewRegistry(codecs ...Codec) *Registry

NewRegistry creates a registry seeded with the provided codecs.

func RegistryFrom

func RegistryFrom(ctx context.Context) (*Registry, error)

RegistryFrom gets the registry from the context. A registry bound with WithRegistry is returned directly. A registry bound with WithReadOnlyRegistry is cloned, so edits to the returned registrations are local to the clone. Codec instances are shared in both cases.

func (*Registry) Clone

func (r *Registry) Clone() *Registry

func (*Registry) Codec

func (r *Registry) Codec(contentType string) (Codec, error)

Codec returns a codec for the content type.

func (*Registry) Decoder

func (r *Registry) Decoder(contentType string) (Decoder, error)

Decoder returns a decoder for the content type.

func (*Registry) Encoder

func (r *Registry) Encoder(contentType string) (Encoder, error)

Encoder returns an encoder for the content type.

func (*Registry) Register

func (r *Registry) Register(codec Codec) error

Register stores a full codec for the content type.

Directories

Path Synopsis
internal
codecutil
Package codecutil owns the built-in codecs' bounded I/O, cancellation checkpoints, borrowed adoption callbacks, and acquired iterator cleanup.
Package codecutil owns the built-in codecs' bounded I/O, cancellation checkpoints, borrowed adoption callbacks, and acquired iterator cleanup.

Jump to

Keyboard shortcuts

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