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

KubFly SDK for Go / codec/smile

package smile

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

Package smile is the Smile generator and parser — io.kubfly.sdk.codec.smile of the Java SDK: Smile 1.0, the binary JSON of Jackson that the bulk API of Elasticsearch and OpenSearch takes (application/smile). Every token 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 smile smile.Buffer
smile.Header()
smile.StartObject()
smile.Name("message")
smile.Text("Shipped")
smile.EndObject()
smile.End()
out.Write(smile.Bytes())

The tokens of the documents of KubFly: the header, objects, arrays, names, strings, integers (small ones in the token itself, the others as zig-zag variable-length integers), floats (64 bits in 10 bytes of 7 bits), binary (7 bits a byte), booleans, null and the end of a document in a stream (0xFF, by which the bulk API splits them); Parser reads every token of the specification. NaN and the infinities are the strings "NaN", "Infinity" and "-Infinity", as in the canonical JSON; each byte that is not UTF-8 is U+FFFD. The bytes are those of the Java SDK, to the byte (sdk/spec/codec/smile.tsv).

Shared names — a name already written in the document as a reference back by its index — need the names the document holds: Buffer keeps them. WriteName writes a name in full, which a reader takes with shared names on or off: the functions measure and write exactly, in two passes too. Shared values are not written.

Constants

const End = 0xFF

End is the end of a document in a stream of documents.

const MaxNumber = 11

MaxNumber is the most bytes of the token of a number: an integer or a float.

Functions

func WriteBinary

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

WriteBinary writes bytes as binary encoded 7 bits a byte, which never holds the end marker 0xFF: 0xE8, the length, then 7 bytes in 8, the last ones in one more than they are, the bits of the last at its right.

func WriteBool

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

WriteBool writes a boolean.

func WriteEnd

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

WriteEnd writes the end of a document in a stream: 0xFF.

func WriteEndArray

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

WriteEndArray writes the end of an array.

func WriteEndObject

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

WriteEndObject writes the end of an object.

func WriteFloat64

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

WriteFloat64 writes a float: its 64 bits in 10 bytes of 7 bits, the high ones first; NaN and the infinities as strings.

func WriteHeader

func WriteHeader(buffer []byte, offset int, sharedNames bool) int

WriteHeader writes the header of a document: ":)\n", version 0, shared values off, raw binary off.

func WriteInt64

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

WriteInt64 writes an integer: from -16 to 15 in the token itself, an int32 as a 32-bit zig-zag integer, the others as a 64-bit one.

func WriteName

func WriteName(buffer []byte, offset int, name string) int

WriteName writes the name of a member in full, never a reference: a short name in its token, a long one with an end marker; a reader with shared names takes every name but the empty one into its table.

func WriteNull

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

WriteNull writes null.

func WriteStartArray

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

WriteStartArray writes the start of an array.

func WriteStartObject

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

WriteStartObject writes the start of an object.

func WriteString

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

WriteString writes a string.

Types

type Buffer

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

Buffer is Smile documents written with the generator into a buffer of their own that grows, with shared names — SmileBuffer of the Java SDK: a name already written in the document is a reference back by its index. The structure is checked as it goes (a value before the header or without its name, a name outside an object, a close of what is not open panic: they are faults of the code that writes). Kept and Reset, it writes without allocating: the buffer grows to the largest of the documents and stays, the table of the names is its own. Several documents one after another, each from its Header to its End, are a stream of them — a bulk of Elasticsearch. Not safe for concurrent use. The zero value is ready.

func (*Buffer) Binary

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

Binary writes bytes, as binary encoded 7 bits a byte.

func (*Buffer) Bool

func (b *Buffer) Bool(value bool)

Bool writes a boolean.

func (*Buffer) Bytes

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

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

func (*Buffer) End

func (b *Buffer) End()

End ends a document in a stream: 0xFF.

func (*Buffer) EndArray

func (b *Buffer) EndArray()

EndArray closes the array open.

func (*Buffer) EndObject

func (b *Buffer) EndObject()

EndObject closes the object open.

func (*Buffer) Float64

func (b *Buffer) Float64(value float64)

Float64 writes a float; NaN and the infinities as strings.

func (*Buffer) Header

func (b *Buffer) Header()

Header starts a document: its header, a new table of shared names.

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) Name

func (b *Buffer) Name(name string)

Name writes the name of the next member of the object open: a reference back when the document has it already.

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) StartObject

func (b *Buffer) StartObject()

StartObject opens an object.

func (*Buffer) Text

func (b *Buffer) Text(value string)

Text writes a string.

type Event

type Event uint8

Event is what Parser.Next read.

const (
	// EventNone is before the first event.
	EventNone Event = iota
	// EventStartObject is the beginning of an object: its members follow, each an EventName and a value.
	EventStartObject
	// EventEndObject is the end of an object.
	EventEndObject
	// EventStartArray is the beginning of an array: its values follow.
	EventStartArray
	// EventEndArray is the end of an array.
	EventEndArray
	// EventName is the name of a member of an object, String: its value follows.
	EventName
	// EventString is a string, String.
	EventString
	// EventNumber is a number: an integer, a float, a double, a big integer or a big decimal.
	EventNumber
	// EventBytes is bytes, binary 7-bit encoded or raw: Bytes.
	EventBytes
	// EventTrue is true.
	EventTrue
	// EventFalse is false.
	EventFalse
	// EventNull is null.
	EventNull
	// EventEndDocument is the end of the stream: after the value of its last document.
	EventEndDocument
)

The events.

func (Event) String

func (e Event) String() string

String is the name of the event, as SmileEvent of the Java SDK: START_OBJECT … END_DOCUMENT.

type Parser

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

Parser is a streaming parser of Smile 1.0 — the binary JSON of Jackson — strict and without a tree, SmileParser of the Java SDK: Next reads event by event. A stream of documents, as the bulk API of Elasticsearch takes them: each with its header and one value, ended by 0xFF or followed by the next header; their values come one after another, then EventEndDocument. Every token of the specification: shared names and shared string values (when the header has them), strings tiny, short and long, integers small, of 32 and 64 bits and big, floats, doubles, big decimals, binary 7-bit encoded and raw (when the header has it). Strict: the header of version 0 with its reserved bit clear; no reserved token; a variable-length integer within its bits; a float or a double with its unused bits clear; ASCII where the token says so and strict UTF-8 elsewhere; a reference only to a shared name or value already read; the names of an object not repeated. Every failure — not Smile, beyond the codec.Limits (a name by Names, a string and bytes by Text) — is a *codec.Error with the offset, the same as in every SDK (sdk/spec/codec/smile-parser.tsv), and every call after it gives it again. Not safe for concurrent use.

func NewParser

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

NewParser is a parser of a stream of documents, before its first event: an error if the stream is longer than the limit. The stream 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 (*Parser) BigDecimal

func (p *Parser) BigDecimal() (*big.Int, int32, error)

BigDecimal is the EventNumber read last as a big decimal — its unscaled value and its scale, the value being unscaled × 10^-scale — a big decimal or an integer; an error if it is a float or a double.

func (*Parser) BigInt

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

BigInt is the EventNumber read last as a big integer; an error if it is not an integer.

func (*Parser) Bytes

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

Bytes are the EventBytes read last, a copy.

func (*Parser) Decimal

func (p *Parser) Decimal() bool

Decimal tells whether the EventNumber read last is a big decimal.

func (*Parser) Depth

func (p *Parser) Depth() int

Depth is the depth of the event read last: of a start, with its object or array (that of a document 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 EventNumber read last as a float64: the nearest one.

func (*Parser) Int64

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

Int64 is the EventNumber read last as an integer of 64 bits; an error if it is not an integer, or is beyond 64 bits.

func (*Parser) Integer

func (p *Parser) Integer() bool

Integer tells whether the EventNumber read last is an integer: small, of 32 or 64 bits, or big.

func (*Parser) Next

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

Next reads the next event: those of the values of the documents, then EventEndDocument, given again on every call after it. An error if the stream is not Smile or is 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) Skip

func (p *Parser) Skip() error

Skip reads past a value: at an EventName, the value of its member; at a start, its object or array up to its end; at anything else, nothing. An error if the stream fails.

func (*Parser) String

func (p *Parser) String() string

String is the text of the EventName or the EventString read last.