cbor

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 4 Imported by: 0

README

cbor

import "github.com/kitsunium/sdk/pkg/v1/data/codec/cbor"

Package cbor registers the CBOR codec with the SDK's codec registry — and no other codec — when it is imported (ADR 0134):

import _ "github.com/kitsunium/sdk/pkg/v1/data/codec/cbor"

The codec is RFC 8949 written on the standard library: maps, arrays, the integers and floats, text and byte strings, tags 0 to 3 for times and big numbers and any other as a Tag, struct fields by their cbor tag or their json tag. Map pairs are written sorted by their encoded key, the deterministic encoding of RFC 8949 §4.2.1, so one value always encodes to the same bytes; NewDeterministic adds the shortest floats and reads only that encoding, for bytes that are signed. A time.Duration is an integer of int64 nanoseconds, the rule of every binary format (#1126): no standard tag carries a duration. Input is checked whole — well-formed, valid, within its bounds — before the target is written. It streams.

Everything that dispatches through the registry by format name then reads and writes it: the codec package's Marshal and Unmarshal, config.FSSource and i18n.LoadFS. Importing github.com/kitsunium/sdk/pkg/v1/data/codec instead registers every format the SDK ships; this package links this one codec, the core package declaring its codes and the standard library, and nothing else. Importing both is harmless: a format is registered by the package that implements it, which Go initialises once however many packages import it.

Errors

Failures carry the range 0.3.6.*; match them with errs.HasCode, or the sentinels with errors.Is:

CodeMarshalFailed    0.3.6.1  a value CBOR cannot carry, invalid UTF-8, nesting past the bound
CodeUnmarshalFailed  0.3.6.2  input that is not one well-formed item, or an item the target cannot hold
CodeDuplicateKey     0.3.6.3  NewStrict: a map repeats a key
CodeUnknownMember    0.3.6.4  NewStrict: a member no struct field claims
CodeNotDeterministic 0.3.6.5  NewDeterministic: a document not in core deterministic encoding

No error message quotes the input.

Index

Constants

const CodeDepthExceeded errs.Code = 198151

CodeDepthExceeded is 0.3.6.7: arrays, maps and tags nested past 32 levels.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORDepthExceeded.

Code 0.3.6.7

const CodeDuplicateKey errs.Code = 198147

CodeDuplicateKey is 0.3.6.3: NewStrict's instance found a map that repeats a key.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORDuplicateKey.

Code 0.3.6.3

const CodeMarshalFailed errs.Code = 198145

CodeMarshalFailed is 0.3.6.1: a value CBOR cannot carry, invalid UTF-8, nesting past the bound.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORMarshalFailed.

Code 0.3.6.1

const CodeNotDeterministic errs.Code = 198149

CodeNotDeterministic is 0.3.6.5: NewDeterministic's instance found a document not in core deterministic encoding.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORNotDeterministic.

Code 0.3.6.5

const CodeSizeExceeded errs.Code = 198150

CodeSizeExceeded is 0.3.6.6: an array, map or chunked string past its 1 048 576-entry bound, or an item past NewBoundedDecoder's ceiling.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORSizeExceeded.

Code 0.3.6.6

const CodeUnknownMember errs.Code = 198148

CodeUnknownMember is 0.3.6.4: NewStrict's instance found a member no struct field claims.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORUnknownMember.

Code 0.3.6.4

const CodeUnmarshalFailed errs.Code = 198146

CodeUnmarshalFailed is 0.3.6.2: input that is not one well-formed item, or an item the target cannot hold.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.CodeCBORUnmarshalFailed.

Code 0.3.6.2

const Format = "cbor"

Format is the name CBOR is registered under. It is an untyped constant, so it goes wherever a format name is taken — the codec package's Marshal, config.FSSource's string, i18n.LoadFS's codec.Format — without a conversion.

Variables

var DepthExceeded *errs.Error

DepthExceeded is the sentinel of CodeDepthExceeded.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.DepthExceeded.

Code 0.3.6.7 · reason DEPTH_EXCEEDED · public "CBOR input nests too deeply"

var DuplicateKey *errs.Error

DuplicateKey is the sentinel of CodeDuplicateKey.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.DuplicateKey.

Code 0.3.6.3 · reason DUPLICATE_KEY · public "CBOR document repeats a map key"

var MarshalFailed *errs.Error

MarshalFailed is the sentinel of CodeMarshalFailed.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.MarshalFailed.

Code 0.3.6.1 · reason MARSHAL_FAILED · public "CBOR encoding failed"

var NotDeterministic *errs.Error

NotDeterministic is the sentinel of CodeNotDeterministic.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.NotDeterministic.

Code 0.3.6.5 · reason NOT_DETERMINISTIC · public "CBOR document is not in deterministic encoding"

var SizeExceeded *errs.Error

SizeExceeded is the sentinel of CodeSizeExceeded.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.SizeExceeded.

Code 0.3.6.6 · reason SIZE_EXCEEDED · public "CBOR input exceeds a size limit"

var UnknownMember *errs.Error

UnknownMember is the sentinel of CodeUnknownMember.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.UnknownMember.

Code 0.3.6.4 · reason UNKNOWN_MEMBER · public "CBOR document carries an unknown member"

var UnmarshalFailed *errs.Error

UnmarshalFailed is the sentinel of CodeUnmarshalFailed.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/cbor.UnmarshalFailed.

Code 0.3.6.2 · reason UNMARSHAL_FAILED · public "CBOR decoding failed"

func NewDeterministic

func NewDeterministic() codec.Codec

NewDeterministic returns a fresh CBOR codec, not registered, for bytes that are signed or hashed: it writes RFC 8949's core deterministic encoding — every float in its shortest exact form — and refuses with CodeNotDeterministic a document not in it. Into an interface it decodes every tag but a bignum as a Tag, so a document round-trips to its bytes.

func NewStrict

func NewStrict() codec.Codec

NewStrict returns a fresh CBOR codec, not registered, that reads a document one way or not at all: a map repeating a key is refused with CodeDuplicateKey, a member no struct field claims with CodeUnknownMember. The registered format keeps last-wins and skips unknown members.

type Tag

Tag is a CBOR tag: a number and the item it encloses, for an application's own tags — COSE, CWT.

type Tag = svccbor.TagValue

Tag is an alias of github.com/kitsunium/sdk/internal/service/data/codec/cbor.TagValue, declared as:

type Tag struct {
	// Number is the tag number.
	Number  uint64
	// Content is the enclosed data item.
	Content any
}

Generated from docs/api by tools/genindex.

Documentation

Overview

Package cbor registers the CBOR codec with the SDK's codec registry — and no other codec — when it is imported (ADR 0134):

import _ "github.com/kitsunium/sdk/pkg/v1/data/codec/cbor"

The codec is RFC 8949 written on the standard library: maps, arrays, the integers and floats, text and byte strings, tags 0 to 3 for times and big numbers and any other as a Tag, struct fields by their cbor tag or their json tag. Map pairs are written sorted by their encoded key, the deterministic encoding of RFC 8949 §4.2.1, so one value always encodes to the same bytes; NewDeterministic adds the shortest floats and reads only that encoding, for bytes that are signed. A time.Duration is an integer of int64 nanoseconds, the rule of every binary format (#1126): no standard tag carries a duration. Input is checked whole — well-formed, valid, within its bounds — before the target is written. It streams.

Everything that dispatches through the registry by format name then reads and writes it: the codec package's Marshal and Unmarshal, config.FSSource and i18n.LoadFS. Importing github.com/kitsunium/sdk/pkg/v1/data/codec instead registers every format the SDK ships; this package links this one codec, the core package declaring its codes and the standard library, and nothing else. Importing both is harmless: a format is registered by the package that implements it, which Go initialises once however many packages import it.

Errors

Failures carry the range 0.3.6.*; match them with errs.HasCode, or the sentinels with errors.Is:

CodeMarshalFailed    0.3.6.1  a value CBOR cannot carry, invalid UTF-8, nesting past the bound
CodeUnmarshalFailed  0.3.6.2  input that is not one well-formed item, or an item the target cannot hold
CodeDuplicateKey     0.3.6.3  NewStrict: a map repeats a key
CodeUnknownMember    0.3.6.4  NewStrict: a member no struct field claims
CodeNotDeterministic 0.3.6.5  NewDeterministic: a document not in core deterministic encoding

No error message quotes the input.

Index

Constants

View Source
const (
	// CodeMarshalFailed is 0.3.6.1: a value CBOR cannot carry, invalid UTF-8,
	// nesting past the bound.
	CodeMarshalFailed errs.Code = corecbor.CodeCBORMarshalFailed
	// CodeUnmarshalFailed is 0.3.6.2: input that is not one well-formed item,
	// or an item the target cannot hold.
	CodeUnmarshalFailed errs.Code = corecbor.CodeCBORUnmarshalFailed
	// CodeDuplicateKey is 0.3.6.3: NewStrict's instance found a map that
	// repeats a key.
	CodeDuplicateKey errs.Code = corecbor.CodeCBORDuplicateKey
	// CodeUnknownMember is 0.3.6.4: NewStrict's instance found a member
	// no struct field claims.
	CodeUnknownMember errs.Code = corecbor.CodeCBORUnknownMember
	// CodeNotDeterministic is 0.3.6.5: NewDeterministic's instance found a
	// document not in core deterministic encoding.
	CodeNotDeterministic errs.Code = corecbor.CodeCBORNotDeterministic
	// CodeSizeExceeded is 0.3.6.6: an array, map or chunked string past its 1 048 576-entry bound, or an item past NewBoundedDecoder's ceiling.
	CodeSizeExceeded errs.Code = corecbor.CodeCBORSizeExceeded
	// CodeDepthExceeded is 0.3.6.7: arrays, maps and tags nested past 32 levels.
	CodeDepthExceeded errs.Code = corecbor.CodeCBORDepthExceeded
)

The error codes, range 0.3.6.*, declared in the core (ADR 0160).

View Source
const Format = "cbor"

Format is the name CBOR is registered under. It is an untyped constant, so it goes wherever a format name is taken — the codec package's Marshal, config.FSSource's string, i18n.LoadFS's codec.Format — without a conversion.

Variables

View Source
var (
	// MarshalFailed is the sentinel of [CodeMarshalFailed].
	MarshalFailed = corecbor.MarshalFailed
	// UnmarshalFailed is the sentinel of [CodeUnmarshalFailed].
	UnmarshalFailed = corecbor.UnmarshalFailed
	// DuplicateKey is the sentinel of [CodeDuplicateKey].
	DuplicateKey = corecbor.DuplicateKey
	// UnknownMember is the sentinel of [CodeUnknownMember].
	UnknownMember = corecbor.UnknownMember
	// NotDeterministic is the sentinel of [CodeNotDeterministic].
	NotDeterministic = corecbor.NotDeterministic
	// SizeExceeded is the sentinel of [CodeSizeExceeded].
	SizeExceeded = corecbor.SizeExceeded
	// DepthExceeded is the sentinel of [CodeDepthExceeded].
	DepthExceeded = corecbor.DepthExceeded
)

The sentinels, for errors.Is: each carries the code of the same name.

Functions

func NewDeterministic added in v0.12.0

func NewDeterministic() codec.Codec

NewDeterministic returns a fresh CBOR codec, not registered, for bytes that are signed or hashed: it writes RFC 8949's core deterministic encoding — every float in its shortest exact form — and refuses with CodeNotDeterministic a document not in it. Into an interface it decodes every tag but a bignum as a Tag, so a document round-trips to its bytes.

func NewStrict added in v0.12.0

func NewStrict() codec.Codec

NewStrict returns a fresh CBOR codec, not registered, that reads a document one way or not at all: a map repeating a key is refused with CodeDuplicateKey, a member no struct field claims with CodeUnknownMember. The registered format keeps last-wins and skips unknown members.

Types

type Tag added in v0.12.0

type Tag = svccbor.TagValue

Tag is a CBOR tag: a number and the item it encloses, for an application's own tags — COSE, CWT.

Jump to

Keyboard shortcuts

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