Top.Mail.Ru
package cbor — KubFly SDK for Go

KubFly SDK for Go / codec/cbor

package cbor

import "kubfly.io/sdk/codec/cbor"

Package cbor is the CBOR generator (RFC 8949) — io.kubfly.sdk.codec.cbor of the Java SDK: every data item is written straight into a buffer at an offset, the offset after it given back, nothing allocated; a nil buffer writes nothing and counts the bytes, as the JSON generator of codec/json.

var cbor cbor.Buffer
cbor.StartMap()
cbor.Text("name")
cbor.Text("orders")
cbor.Text("count")
cbor.Int64(7)
cbor.EndMap() // A2 65 count 07 64 name 66 orders

Every item in its deterministic form (RFC 8949, 4.2.1, Core Deterministic Encoding): the integers, the lengths and the tags in the fewest bytes; the lengths always definite; a float in the shortest float — half, single or double — that holds it exactly, NaN as 0xF97E00; a text in UTF-8, each byte that is not UTF-8 as U+FFFD. The keys of a map in the order of their bytes and none repeated is what Buffer adds: here a map is its header with the count of its pairs, and its pairs are written in their order by the caller, as an array is its header and its items. The bytes are those of the Java SDK, to the byte (sdk/spec/codec/cbor.tsv).

Constants

const MaxHead = 9

MaxHead is the most bytes of the head of an item: an integer, a length, a tag.

Functions

func WriteArrayHeader

func WriteArrayHeader(buffer []byte, offset int, count uint64) int

WriteArrayHeader writes the head of an array of a count of items; the items follow.

func WriteBool

func WriteBool(buffer []byte, offset int, value bool) int

WriteBool writes a boolean: 0xF4 or 0xF5.

func WriteBytes

func WriteBytes(buffer []byte, offset int, value []byte) int

WriteBytes writes a string of bytes: its length, then the bytes.

func WriteFloat64

func WriteFloat64(buffer []byte, offset int, value float64) int

WriteFloat64 writes a float in the shortest float that holds it exactly: half, single or double; NaN as a half.

func WriteInt64

func WriteInt64(buffer []byte, offset int, value int64) int

WriteInt64 writes an integer: an unsigned one when it is not negative, a negative one when it is.

func WriteMapHeader

func WriteMapHeader(buffer []byte, offset int, count uint64) int

WriteMapHeader writes the head of a map of a count of pairs; the pairs follow, a key then its value — for the deterministic form, the keys in the order of their bytes and none repeated.

func WriteNull

func WriteNull(buffer []byte, offset int) int

WriteNull writes null: 0xF6.

func WriteString

func WriteString(buffer []byte, offset int, value string) int

WriteString writes a string of text: its length in bytes, then its UTF-8, each byte that is not UTF-8 as U+FFFD.

func WriteTag

func WriteTag(buffer []byte, offset int, number uint64) int

WriteTag writes a tag; the item it tags follows (1 — a time of the epoch, 32 — a URI…).

func WriteUint64

func WriteUint64(buffer []byte, offset int, value uint64) int

WriteUint64 writes an unsigned integer.

Types

type Buffer

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

Buffer is CBOR written with the generator into a buffer of its own that grows, in the deterministic form of RFC 8949 (4.2.1) whole — CborBuffer of the Java SDK: an array or a map is opened and closed without its count known in advance — its head, in the fewest bytes, goes before its items when it closes — and the pairs of a map are put into the order of the bytes of their keys. The structure is checked as it goes; a fault of the code that writes (a close of what is not open, a key without its value, a key repeated) panics. Kept and Reset, it writes without allocating: the buffer, the stacks and the room for sorting grow to the largest of the items and stay. Several items one after another at the top are a CBOR sequence (RFC 8742). Not safe for concurrent use. The zero value is ready.

func (*Buffer) Bool

func (b *Buffer) Bool(value bool)

Bool writes a boolean.

func (*Buffer) ByteString

func (b *Buffer) ByteString(value []byte)

ByteString writes a string of bytes.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes are the bytes written, valid until the next write.

func (*Buffer) EndArray

func (b *Buffer) EndArray()

EndArray closes the array open: its head before its items.

func (*Buffer) EndMap

func (b *Buffer) EndMap()

EndMap closes the map open: its pairs in the order of the bytes of their keys, its head before them.

func (*Buffer) Float64

func (b *Buffer) Float64(value float64)

Float64 writes a float, in the shortest float that holds it exactly.

func (*Buffer) Int64

func (b *Buffer) Int64(value int64)

Int64 writes an integer.

func (*Buffer) Len

func (b *Buffer) Len() int

Len is the number of bytes written.

func (*Buffer) Null

func (b *Buffer) Null()

Null writes null.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset forgets what was written, keeps the buffer.

func (*Buffer) StartArray

func (b *Buffer) StartArray()

StartArray opens an array.

func (*Buffer) StartMap

func (b *Buffer) StartMap()

StartMap opens a map: a key, its value, the next key…

func (*Buffer) Tag

func (b *Buffer) Tag(number uint64)

Tag writes a tag of the next item (1 — a time of the epoch, 32 — a URI…): the two are one item.

func (*Buffer) Text

func (b *Buffer) Text(value string)

Text writes a string of text.

func (*Buffer) Uint64

func (b *Buffer) Uint64(value uint64)

Uint64 writes an unsigned integer.

type Event

type Event uint8

Event is what Parser.Next read.

const (
	// EventNone is before the first event.
	EventNone Event = iota
	// EventStartArray is the beginning of an array: its items follow.
	EventStartArray
	// EventEndArray is the end of an array.
	EventEndArray
	// EventStartMap is the beginning of a map: its pairs follow, a key then its value, each any item (Key).
	EventStartMap
	// EventEndMap is the end of a map.
	EventEndMap
	// EventInteger is an integer, unsigned or negative: 65 bits with the sign (Int64, BigInt).
	EventInteger
	// EventFloat is a float: half, single or double (Float64).
	EventFloat
	// EventText is a string of text (Text).
	EventText
	// EventBytes is a string of bytes (Bytes).
	EventBytes
	// EventTag is a tag (Tag) of the item that follows: the two are one item.
	EventTag
	// EventTrue is true.
	EventTrue
	// EventFalse is false.
	EventFalse
	// EventNull is null.
	EventNull
	// EventUndefined is undefined.
	EventUndefined
	// EventSimple is another simple value (Simple).
	EventSimple
	// EventEndDocument is the end: after the item, or after the last item of a sequence.
	EventEndDocument
)

The events.

func (Event) String

func (e Event) String() string

String is the name of the event, as CborEvent of the Java SDK: START_ARRAY … END_DOCUMENT.

type Parser

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

Parser is a streaming parser of CBOR (RFC 8949), strict and without a tree — CborParser of the Java SDK: Next reads item by item. Well-formed and valid, no more and no less: every major type, the lengths definite and indefinite (strings in chunks of their own type, read as one), floats half, single and double, tags, simple values; no reserved additional information, no simple value of two bytes below 32, no break outside an indefinite length; a text strict UTF-8, each chunk whole; the keys of a map not repeated (as their bytes). NewParser reads one item, NewSequence a CBOR sequence (RFC 8742), NewDeterministic one item in the deterministic form of RFC 8949 (4.2.1), as the generator writes it: the fewest bytes of every head, definite lengths only, the shortest float, the keys in the order of their bytes. Every failure — not CBOR, beyond the codec.Limits (a string by Text) — is a *codec.Error with the offset, the same as in every SDK (sdk/spec/codec/cbor-parser.tsv), and every call after it gives it again. Not safe for concurrent use.

func NewDeterministic

func NewDeterministic(item []byte, limits codec.Limits) (*Parser, error)

NewDeterministic is a parser of one item in the deterministic form of RFC 8949 (4.2.1): anything else fails.

func NewParser

func NewParser(item []byte, limits codec.Limits) (*Parser, error)

NewParser is a parser of one item, before its first event: an error if the item is longer than the limit. The item is not copied and is not to be changed while it is read. Limits that are not positive are a programming error, a panic.

func NewSequence

func NewSequence(items []byte, limits codec.Limits) (*Parser, error)

NewSequence is a parser of a CBOR sequence (RFC 8742): items one after another, none at all too.

func (*Parser) BigInt

func (p *Parser) BigInt() *big.Int

BigInt is the EventInteger read last, exact.

func (*Parser) Bytes

func (p *Parser) Bytes() []byte

Bytes are the EventBytes read last, its chunks joined: a copy.

func (*Parser) Depth

func (p *Parser) Depth() int

Depth is the depth of the event read last: of a start, with its array or map (that of the item is 1); of an end, without it.

func (*Parser) Event

func (p *Parser) Event() Event

Event is the event read last: EventNone before the first.

func (*Parser) Float64

func (p *Parser) Float64() float64

Float64 is the EventFloat read last.

func (*Parser) Int64

func (p *Parser) Int64() (int64, error)

Int64 is the EventInteger read last as an int64; an error if it is beyond 64 bits with the sign.

func (*Parser) Key

func (p *Parser) Key() bool

Key tells whether the item read last — a scalar, the start of an array or a map, or a tag — is a key of the map open.

func (*Parser) Negative

func (p *Parser) Negative() bool

Negative tells whether the EventInteger read last is negative.

func (*Parser) Next

func (p *Parser) Next() (event Event, err error)

Next reads the next event: those of the item, or of the items of a sequence, then EventEndDocument, given again on every call after it. An error if the bytes are not CBOR or are beyond the limits.

func (*Parser) Offset

func (p *Parser) Offset() int64

Offset is the offset of the event read last: its first byte.

func (*Parser) Simple

func (p *Parser) Simple() int

Simple is the EventSimple read last: 0 to 19, or 32 to 255.

func (*Parser) Skip

func (p *Parser) Skip() error

Skip reads past an item: at a tag, the item it tags; at a start, its array or map up to its end; at anything else, nothing. An error if the bytes fail.

func (*Parser) Tag

func (p *Parser) Tag() uint64

Tag is the number of the EventTag read last.

func (*Parser) Text

func (p *Parser) Text() string

Text is the EventText read last, its chunks joined.

func (*Parser) Unsigned

func (p *Parser) Unsigned() uint64

Unsigned is the argument of the EventInteger read last: the value of an unsigned one, -1 - value of a negative one.