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 ¶
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).
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 ( // 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
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
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.