codec

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 2 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

Overview

Package codec is the ontos canonical binary codec — ontos-codec-v1 (frozen).

Atom(bytes)   = 0x00 || uvarint(len(bytes)) || bytes
Tuple(values) = 0x01 || uvarint(arity)      || encode(each child)

uvarint is unsigned LEB128 in shortest/canonical form over the u64 domain [0, 2^64-1] (spec: docs/spec/ontos-codec.md §3.1). The decoder rejects unknown tags, trailing bytes, non-canonical uvarints, uvarints whose value is >= 2^64 (uvarint_overflow), and inputs exceeding the configured safety limits or this implementation's native materialization width (limit_exceeded, whose DecodeError.LimitKind names the bound; spec §4.2).

This codec is kept beside the model (it imports core, never the reverse). Conformance is pinned by vectors/codec.json (codec id: ontos-codec-v1) and specified normatively in docs/spec/ontos-codec.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(input []byte) (core.Value, error)

Decode decodes one value with default limits, rejecting trailing bytes.

func DecodeWithLimits

func DecodeWithLimits(input []byte, limits Limits) (core.Value, error)

DecodeWithLimits decodes one value with explicit limits, rejecting trailing bytes.

func Encode

func Encode(v core.Value) []byte

Encode returns the canonical ontos-codec-v1 bytes of v.

func EncodeUvarint

func EncodeUvarint(n int) []byte

EncodeUvarint returns the shortest-form LEB128 encoding of n. It is part of the public codec API (the canonical uvarint exposed for callers and pinned by the uvarint conformance vectors); the encoder internals use the private appendUvarint.

Types

type DecodeError

type DecodeError struct {
	Code    string
	Message string
	// LimitKind names the bound that stopped decoding when Code is "limit_exceeded",
	// and is empty for every other code. It is observational: it identifies the check
	// that stopped this decode, and another implementation may name a different
	// bound for the same input (spec §4.2). An empty or unrecognized kind carries no
	// more than the generic "limit_exceeded".
	LimitKind DecodeLimitKind
}

DecodeError carries a cross-language rejection Code (docs/spec/ontos-codec.md §4). The five byte-contract codes (unexpected_eof, unknown_tag, non_canonical_uvarint, uvarint_overflow, trailing_bytes) are normative and pinned by vectors/codec.json. "limit_exceeded" is different: it reports that decoding stopped at an operational bound, so it is not vector-pinned and says nothing about whether the input is a valid encoding. For it, LimitKind names the bound responsible (spec §4.2). The Message is diagnostic only.

Construct a DecodeError with field names. Since LimitKind was added (ontos-internal#352), an unkeyed composite literal such as DecodeError{code, msg} no longer compiles.

func (*DecodeError) Error

func (e *DecodeError) Error() string

type DecodeLimitKind

type DecodeLimitKind string

DecodeLimitKind names an operational decode bound (docs/spec/ontos-codec.md §4.2). The vocabulary is append-only: a name never changes meaning, but a later version may add names, so a switch over it needs a default case.

const (
	// LimitDecodeDepth: the value about to be read is nested deeper than
	// Limits.MaxDepth, counting the root as depth 0.
	LimitDecodeDepth DecodeLimitKind = "decode_depth"
	// LimitAtomBytes: a declared atom byte length exceeds Limits.MaxAtomBytes.
	LimitAtomBytes DecodeLimitKind = "atom_bytes"
	// LimitTupleArity: a declared tuple arity exceeds Limits.MaxTupleArity.
	LimitTupleArity DecodeLimitKind = "tuple_arity"
	// LimitNativeWidth: a valid uvarint length or arity does not fit in Go's int
	// (above 2^63-1, or 2^31-1 on a 32-bit build).
	LimitNativeWidth DecodeLimitKind = "native_width"
)

type Limits

type Limits struct {
	MaxDepth      int
	MaxAtomBytes  int
	MaxTupleArity int
}

Limits bounds decoding of untrusted input. Operational policy, not value semantics. Every field is enforced exactly as given, so a zero field is a bound of zero, not a request for the default: Limits{} admits only an empty atom or an empty tuple at the root. Start from DefaultLimits and override the fields you mean to set.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns the standard decode limits.

Jump to

Keyboard shortcuts

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