Documentation
ΒΆ
Overview ΒΆ
Package glint implements a hierarchical binary serialization format
Example ΒΆ
package main
import (
"fmt"
"github.com/kungfusheep/glint"
)
func main() {
// Define your struct
type Person struct {
Name string `glint:"name"`
Age int `glint:"age"`
Tags []string `glint:"tags"`
}
// Create encoder and decoder once (thread-safe, reusable)
encoder := glint.NewEncoder[Person]()
decoder := glint.NewDecoder[Person]()
// Create some data
alice := Person{
Name: "TestUser",
Age: 32,
Tags: []string{"engineer", "go", "serialization"},
}
// Encode to binary
buffer := glint.NewBufferFromPool()
defer buffer.ReturnToPool()
encoder.Marshal(&alice, buffer)
encoded := buffer.Bytes
fmt.Printf("Encoded %d bytes\n", len(encoded))
// Decode from binary
var decoded Person
err := decoder.Unmarshal(encoded, &decoded)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Decoded: %+v\n", decoded)
}
Output: Encoded 60 bytes Decoded: {Name:TestUser Age:32 Tags:[engineer go serialization]}
Index ΒΆ
- Constants
- Variables
- func AppendDynamicValue(v any, b *Buffer)
- func DynamicValue(value any) []byte
- func Flags(document []byte) uint
- func HashBytes(document []byte) []byte
- func Print(bytes []byte) []byte
- func PrintMap(r *Reader, schema *PrinterSchemaField, nestLevel int)
- func PrintSchema(schema *PrinterSchema, nestLevel int)
- func PrintSlice(r *Reader, field *PrinterSchemaField, nestLevel int)
- func PrintStruct(r *Reader, schema *PrinterSchema, nestLevel int)
- func ReadDynamicBool(b []byte) (bool, bool)
- func ReadDynamicBoolSlice(b []byte) ([]bool, bool)
- func ReadDynamicBytes(b []byte) ([]byte, bool)
- func ReadDynamicBytesSlice(b []byte) ([][]byte, bool)
- func ReadDynamicFloat32(b []byte) (float32, bool)
- func ReadDynamicFloat32Slice(b []byte) ([]float32, bool)
- func ReadDynamicFloat64(b []byte) (float64, bool)
- func ReadDynamicFloat64Slice(b []byte) ([]float64, bool)
- func ReadDynamicInt(b []byte) (int, bool)
- func ReadDynamicInt8(b []byte) (int8, bool)
- func ReadDynamicInt8Slice(b []byte) ([]int8, bool)
- func ReadDynamicInt16(b []byte) (int16, bool)
- func ReadDynamicInt16Slice(b []byte) ([]int16, bool)
- func ReadDynamicInt32(b []byte) (int32, bool)
- func ReadDynamicInt32Slice(b []byte) ([]int32, bool)
- func ReadDynamicInt64(b []byte) (int64, bool)
- func ReadDynamicInt64Slice(b []byte) ([]int64, bool)
- func ReadDynamicIntSlice(b []byte) ([]int, bool)
- func ReadDynamicSlice(r *Reader, wire WireType) any
- func ReadDynamicString(b []byte) (string, bool)
- func ReadDynamicStringSlice(b []byte) ([]string, bool)
- func ReadDynamicTime(b []byte) (time.Time, bool)
- func ReadDynamicTimeSlice(b []byte) ([]time.Time, bool)
- func ReadDynamicUint(b []byte) (uint, bool)
- func ReadDynamicUint8(b []byte) (uint8, bool)
- func ReadDynamicUint8Slice(b []byte) ([]uint8, bool)
- func ReadDynamicUint16(b []byte) (uint16, bool)
- func ReadDynamicUint16Slice(b []byte) ([]uint16, bool)
- func ReadDynamicUint32(b []byte) (uint32, bool)
- func ReadDynamicUint32Slice(b []byte) ([]uint32, bool)
- func ReadDynamicUint64(b []byte) (uint64, bool)
- func ReadDynamicUint64Slice(b []byte) ([]uint64, bool)
- func ReadDynamicUintSlice(b []byte) ([]uint, bool)
- func ReadDynamicValue(b []byte) any
- func ReadDynamicValueFromReader(r *Reader) any
- func SPrint(bytes []byte) string
- func SPrintMap(r *Reader, schema *PrinterSchemaField, nestLevel int) string
- func SPrintSlice(r *Reader, field *PrinterSchemaField, nestLevel int) string
- func SPrintStruct(r *Reader, schema *PrinterSchema, nestLevel int) string
- func SPrintStructVerbose(r *Reader, schema *PrinterSchema, nestLevel int, printBytes bool) string
- func SPrintStructVerboseWithColors(r *Reader, schema *PrinterSchema, nestLevel int, printBytes bool, ...) string
- func SPrintStructWithColors(r *Reader, schema *PrinterSchema, nestLevel int, useColors bool) string
- func SchemaBytes(t any) []byte
- func SchemaBytesUsingTag(t any, tagName string) []byte
- func Walk(doc []byte, visitor Visitor) error
- func WireTypeToReflectType(k WireType) reflect.Type
- type Buffer
- func (b *Buffer) AppendBool(value bool)
- func (b *Buffer) AppendBytes(value []byte)
- func (b *Buffer) AppendFloat32(value float32)
- func (b *Buffer) AppendFloat64(value float64)
- func (b *Buffer) AppendInt(value int)
- func (b *Buffer) AppendInt8(value int8)
- func (b *Buffer) AppendInt16(value int16)
- func (b *Buffer) AppendInt32(value int32)
- func (b *Buffer) AppendInt64(value int64)
- func (b *Buffer) AppendString(value string)
- func (b *Buffer) AppendTime(t time.Time)
- func (b *Buffer) AppendUint(value uint)
- func (b *Buffer) AppendUint8(value uint8)
- func (b *Buffer) AppendUint16(value uint16)
- func (b *Buffer) AppendUint32(value uint32)
- func (b *Buffer) AppendUint64(value uint64)
- func (b *Buffer) Reset()
- func (b *Buffer) ReturnToPool()
- type DecodeInstructionLookup
- type DecodeLimits
- type Decoder
- type DecoderContext
- type Document
- type DocumentBuilder
- func (d *DocumentBuilder) AppendBool(name string, value bool) *DocumentBuilder
- func (d *DocumentBuilder) AppendBytes(name string, value []byte) *DocumentBuilder
- func (d *DocumentBuilder) AppendFloat32(name string, value float32) *DocumentBuilder
- func (d *DocumentBuilder) AppendFloat64(name string, value float64) *DocumentBuilder
- func (d *DocumentBuilder) AppendInt(name string, value int) *DocumentBuilder
- func (d *DocumentBuilder) AppendInt8(name string, value int8) *DocumentBuilder
- func (d *DocumentBuilder) AppendInt16(name string, value int16) *DocumentBuilder
- func (d *DocumentBuilder) AppendInt32(name string, value int32) *DocumentBuilder
- func (d *DocumentBuilder) AppendInt64(name string, value int64) *DocumentBuilder
- func (d *DocumentBuilder) AppendNestedDocument(name string, value *DocumentBuilder) *DocumentBuilder
- func (d *DocumentBuilder) AppendSlice(name string, value SliceBuilder) *DocumentBuilder
- func (d *DocumentBuilder) AppendString(name, value string) *DocumentBuilder
- func (d *DocumentBuilder) AppendTime(name string, value time.Time) *DocumentBuilder
- func (d *DocumentBuilder) AppendUint(name string, value uint) *DocumentBuilder
- func (d *DocumentBuilder) AppendUint8(name string, value uint8) *DocumentBuilder
- func (d *DocumentBuilder) AppendUint16(name string, value uint16) *DocumentBuilder
- func (d *DocumentBuilder) AppendUint32(name string, value uint32) *DocumentBuilder
- func (d *DocumentBuilder) AppendUint64(name string, value uint64) *DocumentBuilder
- func (d *DocumentBuilder) Bytes() []byte
- func (d *DocumentBuilder) WriteTo(b *Buffer)
- type Encoder
- type PrinterDocument
- type PrinterSchema
- type PrinterSchemaField
- type Reader
- func (r *Reader) BytesFromMark() []byte
- func (r *Reader) BytesLeft() uint
- func (r *Reader) Mark() uint
- func (r *Reader) Read(l uint) []byte
- func (r *Reader) ReadBool() bool
- func (r *Reader) ReadBoolSlice() []bool
- func (r *Reader) ReadByte() byte
- func (r *Reader) ReadBytesSlice() [][]byte
- func (r *Reader) ReadFloat32() float32
- func (r *Reader) ReadFloat32Slice() []float32
- func (r *Reader) ReadFloat64() float64
- func (r *Reader) ReadFloat64Slice() []float64
- func (r *Reader) ReadInt() int
- func (r *Reader) ReadInt8() int8
- func (r *Reader) ReadInt8Slice() []int8
- func (r *Reader) ReadInt16() int16
- func (r *Reader) ReadInt16Slice() []int16
- func (r *Reader) ReadInt32() int32
- func (r *Reader) ReadInt32Slice() []int32
- func (r *Reader) ReadInt64() int64
- func (r *Reader) ReadInt64Slice() []int64
- func (r *Reader) ReadIntSlice() []int
- func (r *Reader) ReadString() string
- func (r *Reader) ReadStringSlice() []string
- func (r *Reader) ReadTime() time.Time
- func (r *Reader) ReadTimeSlice() []time.Time
- func (r *Reader) ReadUint() uint
- func (r *Reader) ReadUint8() uint8
- func (r *Reader) ReadUint8Slice() []uint8
- func (r *Reader) ReadUint16() uint16
- func (r *Reader) ReadUint16Slice() []uint16
- func (r *Reader) ReadUint32() uint32
- func (r *Reader) ReadUint32Slice() []uint32
- func (r *Reader) ReadUint64() uint64
- func (r *Reader) ReadUint64Slice() []uint64
- func (r *Reader) ReadUintSlice() []uint
- func (r *Reader) ReadVarint() uint
- func (r *Reader) ReadZigzagVarint() int
- func (r *Reader) Remaining() []byte
- func (r *Reader) ResetMark()
- func (r *Reader) SetMark()
- func (r *Reader) Skip(l uint)
- func (r *Reader) SkipVarint()
- func (r *Reader) Unread(l uint)
- type SliceBuilder
- func (s *SliceBuilder) AppendBoolSlice(value []bool)
- func (s *SliceBuilder) AppendBytesSlice(value [][]byte)
- func (s *SliceBuilder) AppendFloat32Slice(value []float32)
- func (s *SliceBuilder) AppendFloat64Slice(value []float64)
- func (s *SliceBuilder) AppendInt8Slice(value []int8)
- func (s *SliceBuilder) AppendInt16Slice(value []int16)
- func (s *SliceBuilder) AppendInt16SliceDelta(value []int16)
- func (s *SliceBuilder) AppendInt32Slice(value []int32)
- func (s *SliceBuilder) AppendInt32SliceDelta(value []int32)
- func (s *SliceBuilder) AppendInt64Slice(value []int64)
- func (s *SliceBuilder) AppendInt64SliceDelta(value []int64)
- func (s *SliceBuilder) AppendIntSlice(value []int)
- func (s *SliceBuilder) AppendIntSliceDelta(value []int)
- func (s *SliceBuilder) AppendNestedDocumentSlice(value []DocumentBuilder)
- func (s *SliceBuilder) AppendSlice(value []SliceBuilder)
- func (s *SliceBuilder) AppendStringSlice(value []string)
- func (s *SliceBuilder) AppendTimeSlice(value []time.Time)
- func (s *SliceBuilder) AppendUint8Slice(value []uint8)
- func (s *SliceBuilder) AppendUint16Slice(value []uint16)
- func (s *SliceBuilder) AppendUint16SliceDelta(value []uint16)
- func (s *SliceBuilder) AppendUint32Slice(value []uint32)
- func (s *SliceBuilder) AppendUint32SliceDelta(value []uint32)
- func (s *SliceBuilder) AppendUint64Slice(value []uint64)
- func (s *SliceBuilder) AppendUint64SliceDelta(value []uint64)
- func (s *SliceBuilder) AppendUintSlice(value []uint)
- func (s *SliceBuilder) AppendUintSliceDelta(value []uint)
- type SliceEncoder
- type TrustHeader
- type Trustee
- type Visitor
- type Walker
- type WireType
Examples ΒΆ
Constants ΒΆ
const ( Red = "\033[91m" Orange = "\033[38;5;208m" Yellow = "\033[93m" Green = "\033[92m" Blue = "\033[94m" Purple = "\033[95m" Cyan = "\033[96m" White = "\033[97m" Reset = "\033[0m" )
Variables ΒΆ
var ( ErrInvalidDocument = errors.New("invalid glint document") ErrSchemaNotFound = errors.New("schema parse error. document was supplied with no schema and there are no cached instructions for the hash") )
Unmarshal Errors
var DefaultLimits = DecodeLimits{
MaxByteSliceLen: 100 * 1024 * 1024,
MaxSliceInitCap: 10000,
MaxSchemaSize: 1024 * 1024,
MaxStringLen: 50 * 1024 * 1024,
}
DefaultLimits provides sensible defaults for most use cases
var ErrSkipVisit = errors.New("skip visit")
ErrSkipVisit is returned by a visitor to indicate that the walker should skip visiting the current field
Functions ΒΆ
func AppendDynamicValue ΒΆ
AppendDynamicValue encodes a value with type prefix into the buffer
func DynamicValue ΒΆ
DynamicValue encodes any value to bytes with type information
func PrintMap ΒΆ
func PrintMap(r *Reader, schema *PrinterSchemaField, nestLevel int)
PrintMap prints a map within a document
func PrintSchema ΒΆ
func PrintSchema(schema *PrinterSchema, nestLevel int)
PrintSchema prints the schema of a document
func PrintSlice ΒΆ
func PrintSlice(r *Reader, field *PrinterSchemaField, nestLevel int)
PrintSlice prints a slice to stdout
func PrintStruct ΒΆ
func PrintStruct(r *Reader, schema *PrinterSchema, nestLevel int)
PrintStruct prints a struct within a document, or the top level of the document itself
func ReadDynamicBool ΒΆ
ReadDynamicBool reads a bool from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a bool
func ReadDynamicBoolSlice ΒΆ
ReadDynamicBoolSlice attempts to decode a []bool after checking the wire type. Returns false if the data doesn't represent a bool slice.
func ReadDynamicBytes ΒΆ
ReadDynamicBytes reads a []byte from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []byte
func ReadDynamicBytesSlice ΒΆ
ReadDynamicBytesSlice attempts to decode a [][]byte after checking the wire type. Returns false if the data doesn't represent a bytes slice.
func ReadDynamicFloat32 ΒΆ
ReadDynamicFloat32 reads a float32 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a float32
func ReadDynamicFloat32Slice ΒΆ
ReadDynamicFloat32Slice attempts to decode a []float32 after checking the wire type. Returns false if the data doesn't represent a float32 slice.
func ReadDynamicFloat64 ΒΆ
ReadDynamicFloat64 reads a float64 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a float64
func ReadDynamicFloat64Slice ΒΆ
ReadDynamicFloat64Slice attempts to decode a []float64 after checking the wire type. Returns false if the data doesn't represent a float64 slice.
func ReadDynamicInt ΒΆ
ReadDynamicInt reads an int from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an int
func ReadDynamicInt8 ΒΆ
ReadDynamicInt8 reads an int8 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an int8
func ReadDynamicInt8Slice ΒΆ
ReadDynamicInt8Slice reads a []int8 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []int8
func ReadDynamicInt16 ΒΆ
ReadDynamicInt16 reads an int16 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an int16
func ReadDynamicInt16Slice ΒΆ
ReadDynamicInt16Slice reads a []int16 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []int16
func ReadDynamicInt32 ΒΆ
ReadDynamicInt32 reads an int32 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an int32
func ReadDynamicInt32Slice ΒΆ
ReadDynamicInt32Slice reads a []int32 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []int32
func ReadDynamicInt64 ΒΆ
ReadDynamicInt64 reads an int64 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an int64
func ReadDynamicInt64Slice ΒΆ
ReadDynamicInt64Slice reads a []int64 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []int64
func ReadDynamicIntSlice ΒΆ
ReadDynamicIntSlice reads a []int from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []int
func ReadDynamicSlice ΒΆ
ReadDynamicSlice decodes a typed slice after examining the wire type
func ReadDynamicString ΒΆ
ReadDynamicString reads a string from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a string
func ReadDynamicStringSlice ΒΆ
ReadDynamicStringSlice reads a []string from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []string
func ReadDynamicTime ΒΆ
ReadDynamicTime reads a time.Time from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a time.Time
func ReadDynamicTimeSlice ΒΆ
ReadDynamicTimeSlice attempts to decode a []time.Time after checking the wire type. Returns false if the data doesn't represent a time slice.
func ReadDynamicUint ΒΆ
ReadDynamicUint reads an uint from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an uint
func ReadDynamicUint8 ΒΆ
ReadDynamicUint8 reads an uint8 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an uint8
func ReadDynamicUint8Slice ΒΆ
ReadDynamicUint8Slice reads a []uint8 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []uint8
func ReadDynamicUint16 ΒΆ
ReadDynamicUint16 reads an uint16 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an uint16
func ReadDynamicUint16Slice ΒΆ
ReadDynamicUint16Slice reads a []uint16 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []uint16
func ReadDynamicUint32 ΒΆ
ReadDynamicUint32 reads an uint32 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an uint32
func ReadDynamicUint32Slice ΒΆ
ReadDynamicUint32Slice reads a []uint32 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []uint32
func ReadDynamicUint64 ΒΆ
ReadDynamicUint64 reads an uint64 from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not an uint64
func ReadDynamicUint64Slice ΒΆ
ReadDynamicUint64Slice attempts to decode a []uint64 after checking the wire type. Returns false if the data doesn't represent a uint64 slice.
func ReadDynamicUintSlice ΒΆ
ReadDynamicUintSlice reads a []uint from a byte array based on the wire type provided by reading the first varint, it will return false if the wire type was not a []uint
func ReadDynamicValue ΒΆ
ReadDynamicValue decodes a value using the wire type from the first varint
func ReadDynamicValueFromReader ΒΆ
ReadDynamicValueFromReader extracts a typed value after reading the wire type indicator
func SPrint ΒΆ
SPrint returns a string representation of a glint document, similar to Print but returns the string instead of printing to stdout
func SPrintMap ΒΆ
func SPrintMap(r *Reader, schema *PrinterSchemaField, nestLevel int) string
SPrintMap returns a string representation of a map
func SPrintSlice ΒΆ
func SPrintSlice(r *Reader, field *PrinterSchemaField, nestLevel int) string
SPrintSlice returns a string representation of a slice
func SPrintStruct ΒΆ
func SPrintStruct(r *Reader, schema *PrinterSchema, nestLevel int) string
SPrintStruct returns a string representation of a struct within a document
func SPrintStructVerbose ΒΆ
func SPrintStructVerbose(r *Reader, schema *PrinterSchema, nestLevel int, printBytes bool) string
SPrintStructVerbose returns a string representation of a struct within a document with the option to print the bytes for values
func SPrintStructVerboseWithColors ΒΆ
func SPrintStructVerboseWithColors(r *Reader, schema *PrinterSchema, nestLevel int, printBytes bool, useColors bool) string
SPrintStructVerboseWithColors returns a string representation of a struct with optional color support
func SPrintStructWithColors ΒΆ
func SPrintStructWithColors(r *Reader, schema *PrinterSchema, nestLevel int, useColors bool) string
SPrintStructWithColors returns a string representation of a struct with optional color support
func SchemaBytes ΒΆ
SchemaBytes generates a binary schema from struct tags marked 'glint'
func SchemaBytesUsingTag ΒΆ
SchemaBytesUsingTag creates a schema using custom field tag names
func WireTypeToReflectType ΒΆ
WireTypeToReflectType converts glint wire types back to Go reflection types
Types ΒΆ
type Buffer ΒΆ
type Buffer struct {
Bytes []byte
TrustedSchema bool // when true, omits schema body for trusted connections
}
Buffer accumulates encoded data during serialization. Supports only append operations for efficiency.
func NewBufferFromPool ΒΆ
func NewBufferFromPool() *Buffer
NewBufferFromPool obtains a reset Buffer from the pool. Call ReturnToPool when finished. For existing memory, create directly: `buf := Buffer{mySlice[:0]}` - pooling is optional.
func NewBufferFromPoolWithCap ΒΆ
NewBufferFromPoolWithCap acquires a pooled Buffer with guaranteed capacity. Call ReturnToPool after use.
func NewBufferWithTrust ΒΆ
NewBufferWithTrust acquires a pooled Buffer and enables trust mode if schema hashes match. Remember to call ReturnToPool after use.
func (*Buffer) AppendBool ΒΆ
AppendBool encodes a boolean as a single byte: 1 for true, 0 for false.
func (*Buffer) AppendBytes ΒΆ
AppendBytes encodes a byte slice with length prefix into the buffer.
func (*Buffer) AppendFloat32 ΒΆ
AppendFloat32 encodes a float32 by converting to uint32 bits
func (*Buffer) AppendFloat64 ΒΆ
AppendFloat64 encodes a float64 by converting to uint64 bits
func (*Buffer) AppendInt8 ΒΆ
AppendInt8 adds a signed byte to the buffer.
func (*Buffer) AppendInt16 ΒΆ
AppendInt16 encodes a signed int16 using zigzag encoding.
func (*Buffer) AppendInt32 ΒΆ
AppendInt32 encodes a signed int32 using zigzag encoding.
func (*Buffer) AppendInt64 ΒΆ
AppendInt64 encodes a signed int64 as a variable-length integer.
func (*Buffer) AppendString ΒΆ
AppendString encodes a string with length prefix into the buffer.
func (*Buffer) AppendTime ΒΆ
AppendTime encodes a time value using Go's binary marshaling
func (*Buffer) AppendUint ΒΆ
AppendUint encodes a uint as a variable-length integer.
func (*Buffer) AppendUint8 ΒΆ
AppendUint8 adds a single byte to the buffer.
func (*Buffer) AppendUint16 ΒΆ
AppendUint16 encodes a uint16 as a variable-length integer.
func (*Buffer) AppendUint32 ΒΆ
AppendUint32 encodes a uint32 as a variable-length integer.
func (*Buffer) AppendUint64 ΒΆ
AppendUint64 encodes a uint64 as a variable-length integer.
func (*Buffer) Reset ΒΆ
func (b *Buffer) Reset()
Reset clears the buffer contents but preserves allocated memory
func (*Buffer) ReturnToPool ΒΆ
func (b *Buffer) ReturnToPool()
ReturnToPool releases the buffer back to the pool. Using the buffer after this call results in undefined behavior.
type DecodeInstructionLookup ΒΆ
type DecodeInstructionLookup struct {
Added func(hash uint, contextID uint) // a callback to be told when an item has been added to the lookup along with a context ID
// contains filtered or unexported fields
}
DecodeInstructionLookup is the data structre we use for doing lookups of small names. there is a tipping point where this becomes less effective than a simple string map - see `smallThresh` for that threshold
type DecodeLimits ΒΆ
type DecodeLimits struct {
MaxByteSliceLen uint // Maximum byte slice length (0 = unlimited)
MaxSliceInitCap uint // Cap initial slice allocations to prevent huge upfront allocations
MaxSchemaSize uint // Maximum schema size in bytes
MaxStringLen uint // Maximum string length
}
DecodeLimits configures bounds checking during decoding to prevent memory exhaustion attacks
type Decoder ΒΆ
type Decoder[T any] struct { // contains filtered or unexported fields }
Decoder handles type-safe decoding of type T
Example ΒΆ
type child struct {
Name string `glint:"name"`
}
type Example struct {
Bool bool `glint:"bool"`
Int int `glint:"int"`
Int8 int8 `glint:"int8"`
Int16 int16 `glint:"int16"`
Int32 int32 `glint:"int32"`
Int64 int64 `glint:"int64"`
Uint uint `glint:"uint"`
Uint8 uint8 `glint:"uint8"`
Uint16 uint16 `glint:"uint16"`
Uint32 uint32 `glint:"uint32"`
Uint64 uint64 `glint:"uint64"`
Float32 float32 `glint:"float32"`
Float64 float64 `glint:"float64"`
String string `glint:"string"`
Bytes []byte `glint:"bytes"`
Struct child `glint:"struct"`
Time time.Time `glint:"time"`
}
test := Example{
Bool: true,
Int: -42,
Int8: -8,
Int16: -2048,
Int32: -1024,
Int64: -9223372036854775808,
Uint: 42,
Uint8: 8,
Uint16: 2048,
Uint32: 1024,
Uint64: 9223372036854775807,
Float32: 3.14,
Float64: 3.142069,
String: "greetings",
Bytes: []byte{42, 7, 255},
Struct: child{
Name: "TestUser",
},
Time: time.Now(),
}
encoder := newEncoder(Example{})
buf := Buffer{}
encoder.Marshal(&test, &buf)
// fmt.Println(buf.Bytes)
// Print(buf.Bytes)
/// Output:
// 1
func NewDecoder ΒΆ
NewDecoder constructs a decoder specialized for type T with default limits
func NewDecoderUsingTag ΒΆ
NewDecoderUsingTag is primarily for internal use. Like NewDecoder but accepts a custom struct tag name for framework integration (e.g. "rpc").
func NewDecoderWithLimits ΒΆ
func NewDecoderWithLimits[T any](limits DecodeLimits) *Decoder[T]
NewDecoderWithLimits constructs a decoder with custom bounds checking limits
func (*Decoder[T]) UnmarshalWithContext ΒΆ
func (d *Decoder[T]) UnmarshalWithContext(bytes []byte, v *T, context DecoderContext) error
UnmarshalWithContext performs decoding using the supplied context
type DecoderContext ΒΆ
type DecoderContext struct {
InstructionCache *DecodeInstructionLookup
ID uint
}
DecoderContext supports trusted schema mode with an instruction cache and caller-defined affinity ID
type Document ΒΆ
type Document []byte
Document is a type alias for []byte that implements fmt.Formatter for pretty printing glint documents
type DocumentBuilder ΒΆ
type DocumentBuilder struct {
// contains filtered or unexported fields
}
DocumentBuilder is a simple inline progressive builder. You add your properties and it builds the document up as you go along.
Example ΒΆ
package main
import (
"github.com/kungfusheep/glint"
)
func main() {
// Build documents without structs
doc := &glint.DocumentBuilder{}
doc.AppendString("name", "SampleUser").
AppendInt("age", 25).
AppendBool("active", true)
data := doc.Bytes()
// Print human-readable format
glint.Print(data)
// Output shows tree structure (actual output may vary):
// Glint Document
// ββ Schema
// β ββ String: name
// β ββ Int: age
// β ββ Bool: active
// ββ Values
// ββ name: SampleUser
// ββ age: 25
// ββ active: true
}
Output:
func (*DocumentBuilder) AppendBool ΒΆ
func (d *DocumentBuilder) AppendBool(name string, value bool) *DocumentBuilder
AppendBool adds a bool field to the document against a given name
func (*DocumentBuilder) AppendBytes ΒΆ
func (d *DocumentBuilder) AppendBytes(name string, value []byte) *DocumentBuilder
AppendBytes adds a bytes field to the document against a given name
func (*DocumentBuilder) AppendFloat32 ΒΆ
func (d *DocumentBuilder) AppendFloat32(name string, value float32) *DocumentBuilder
AppendFloat32 adds a float32 field to the document against a given name
func (*DocumentBuilder) AppendFloat64 ΒΆ
func (d *DocumentBuilder) AppendFloat64(name string, value float64) *DocumentBuilder
AppendFloat64 adds a float64 field to the document against a given name
func (*DocumentBuilder) AppendInt ΒΆ
func (d *DocumentBuilder) AppendInt(name string, value int) *DocumentBuilder
AppendInt adds a int field to the document against a given name
func (*DocumentBuilder) AppendInt8 ΒΆ
func (d *DocumentBuilder) AppendInt8(name string, value int8) *DocumentBuilder
AppendInt8 adds a int8 field to the document against a given name
func (*DocumentBuilder) AppendInt16 ΒΆ
func (d *DocumentBuilder) AppendInt16(name string, value int16) *DocumentBuilder
AppendInt16 adds a int16 field to the document against a given name
func (*DocumentBuilder) AppendInt32 ΒΆ
func (d *DocumentBuilder) AppendInt32(name string, value int32) *DocumentBuilder
AppendInt32 adds a int32 field to the document against a given name
func (*DocumentBuilder) AppendInt64 ΒΆ
func (d *DocumentBuilder) AppendInt64(name string, value int64) *DocumentBuilder
AppendInt64 adds a int64 field to the document against a given name
func (*DocumentBuilder) AppendNestedDocument ΒΆ
func (d *DocumentBuilder) AppendNestedDocument(name string, value *DocumentBuilder) *DocumentBuilder
AppendNestedDocument appends another document within this one. Equivalent of a nested struct.
func (*DocumentBuilder) AppendSlice ΒΆ
func (d *DocumentBuilder) AppendSlice(name string, value SliceBuilder) *DocumentBuilder
AppendSlice adds a slice field to the document against a given name
func (*DocumentBuilder) AppendString ΒΆ
func (d *DocumentBuilder) AppendString(name, value string) *DocumentBuilder
AppendString adds a string field to the document against a given name
func (*DocumentBuilder) AppendTime ΒΆ
func (d *DocumentBuilder) AppendTime(name string, value time.Time) *DocumentBuilder
AppendTime adds a time field to the document against a given name
func (*DocumentBuilder) AppendUint ΒΆ
func (d *DocumentBuilder) AppendUint(name string, value uint) *DocumentBuilder
AppendUint adds a uint field to the document against a given name
func (*DocumentBuilder) AppendUint8 ΒΆ
func (d *DocumentBuilder) AppendUint8(name string, value uint8) *DocumentBuilder
AppendUint8 adds a uint8 field to the document against a given name
func (*DocumentBuilder) AppendUint16 ΒΆ
func (d *DocumentBuilder) AppendUint16(name string, value uint16) *DocumentBuilder
AppendUint16 adds a uint16 field to the document against a given name
func (*DocumentBuilder) AppendUint32 ΒΆ
func (d *DocumentBuilder) AppendUint32(name string, value uint32) *DocumentBuilder
AppendUint32 adds a uint32 field to the document against a given name
func (*DocumentBuilder) AppendUint64 ΒΆ
func (d *DocumentBuilder) AppendUint64(name string, value uint64) *DocumentBuilder
AppendUint64 adds a uint64 field to the document against a given name
func (*DocumentBuilder) Bytes ΒΆ
func (d *DocumentBuilder) Bytes() []byte
Bytes returns the document as a byte array
func (*DocumentBuilder) WriteTo ΒΆ
func (d *DocumentBuilder) WriteTo(b *Buffer)
WriteTo writes the document to a buffer
type Encoder ΒΆ
type Encoder[T any] struct { // contains filtered or unexported fields }
Encoder handles type-safe encoding of type T
Example ΒΆ
type Point struct {
X int `glint:"x"`
Y int `glint:"y"`
}
encoder := newEncoder(Point{})
buf := NewBufferFromPool()
value := Point{
X: 10,
Y: 55,
}
encoder.Marshal(&value, buf)
fmt.Println(buf.Bytes)
Output: [0 59 198 217 19 6 2 1 120 2 1 121 20 110]
Example (Generic) ΒΆ
// Create a type-safe encoder for Person
encoder := NewEncoder[Person]()
// Create a person
person := Person{Name: "SampleUser", Age: 30}
// Encode the person
buf := NewBufferFromPool()
encoder.Marshal(&person, buf)
// Create a type-safe decoder for Person
decoder := NewDecoder[Person]()
// Decode into a new person
var decoded Person
err := decoder.Unmarshal(buf.Bytes, &decoded)
if err != nil {
panic(err)
}
fmt.Printf("%s is %d years old\n", decoded.Name, decoded.Age)
Output: SampleUser is 30 years old
Example (SliceGeneric) ΒΆ
// Create a type-safe encoder for People (which contains a slice)
encoder := NewEncoder[People]()
// Create some people
people := People{
Items: []Person{
{Name: "ExampleUser", Age: 25},
{Name: "Carol", Age: 35},
},
}
// Encode the people
buf := NewBufferFromPool()
encoder.Marshal(&people, buf)
// Create a type-safe decoder for People
decoder := NewDecoder[People]()
// Decode into a new People struct
var decoded People
err := decoder.Unmarshal(buf.Bytes, &decoded)
if err != nil {
panic(err)
}
for _, p := range decoded.Items {
fmt.Printf("%s is %d years old\n", p.Name, p.Age)
}
Output: ExampleUser is 25 years old Carol is 35 years old
func NewEncoder ΒΆ
NewEncoder builds an Encoder using type information extracted from a blank struct instance. Create only ONE encoder per type - the encoder is safe for concurrent use. The blank struct's type must exactly match what you'll pass to Marshal. e.g mystructEncoder := glint.NewEncoder(mystruct{})
Fields that we wish to encode, on `mystruct` in the example above, carry tags that look like this
type mystruct struct {
name string `glint:"name"`
age uint8 `glint:"age"`
}
When `Marshal` encodes data into the Buffer, these tag names are used as the schema field names
func (*Encoder[T]) ClearSchema ΒΆ
func (e *Encoder[T]) ClearSchema()
ClearSchema wipes all schema data from this encoder.
type PrinterDocument ΒΆ
PrinterDocument represents the top level parts of a glint document, intended for tooling purposes.
func NewPrinterDocument ΒΆ
func NewPrinterDocument(r *Reader) PrinterDocument
NewPrinterDocument reads a document from a Reader and returns a PrinterDocument
type PrinterSchema ΒΆ
type PrinterSchema struct {
Fields []PrinterSchemaField
NestedSchema *PrinterSchema
}
PrinterSchema represents the schema of a glint document, intended for tooling purposes.
func NewPrinterSchema ΒΆ
func NewPrinterSchema(r *Reader) PrinterSchema
NewPrinterSchema reads a schema from a Reader and returns a PrinterSchema
type PrinterSchemaField ΒΆ
type PrinterSchemaField struct {
TypeID WireType
Name string
IsSlice bool
IsPointer bool
NestedSchema *PrinterSchema
NestedSlice *PrinterSchemaField
MapType [2]WireType
}
PrinterSchemaField represents a single field in a schema, intended for tooling purposes.
func NewPrinterSchemaField ΒΆ
func NewPrinterSchemaField(r *Reader) PrinterSchemaField
NewPrinterSchemaField reads a single field from a Reader and returns a PrinterSchemaField
func NewRawPrinterSchemaField ΒΆ
func NewRawPrinterSchemaField(r *Reader) PrinterSchemaField
NewRawPrinterSchemaField is almost identical to NewPrinterSchemaField except it doesn't read a name. Used for when parsing nested struct schemas like arrays or maps.
func (*PrinterSchemaField) ReadSubSchema ΒΆ
func (f *PrinterSchemaField) ReadSubSchema(r *Reader)
ReadSubSchema reads a nested schema from a Reader and sets it on the PrinterSchemaField
type Reader ΒΆ
type Reader struct {
// contains filtered or unexported fields
}
Reader provides sequential access to encoded data with position tracking.
func (*Reader) BytesFromMark ΒΆ
BytesFromMark extracts data between the saved position and current location
func (*Reader) ReadBoolSlice ΒΆ
ReadBoolSlice decodes an array of boolean values
func (*Reader) ReadBytesSlice ΒΆ
ReadBytesSlice decodes a collection of length-prefixed byte arrays
func (*Reader) ReadFloat32 ΒΆ
ReadFloat32 decodes a float32 from its uint32 bit representation
func (*Reader) ReadFloat32Slice ΒΆ
ReadFloat32Slice decodes multiple float32 values
func (*Reader) ReadFloat64 ΒΆ
ReadFloat64 decodes a float64 from its uint64 bit representation
func (*Reader) ReadFloat64Slice ΒΆ
ReadFloat64Slice retrieves an array of float64 values
func (*Reader) ReadInt8Slice ΒΆ
ReadInt8Slice extracts an array of signed bytes
func (*Reader) ReadInt16Slice ΒΆ
ReadInt16Slice retrieves multiple zigzag-encoded int16s
func (*Reader) ReadInt32Slice ΒΆ
ReadInt32Slice decodes a collection of zigzag-encoded int32s
func (*Reader) ReadInt64Slice ΒΆ
ReadInt64Slice extracts an array of variable-length int64s
func (*Reader) ReadIntSlice ΒΆ
ReadIntSlice decodes an array of zigzag-encoded ints
func (*Reader) ReadString ΒΆ
ReadString decodes a length-prefixed string
func (*Reader) ReadStringSlice ΒΆ
ReadStringSlice decodes a length-prefixed array of strings
func (*Reader) ReadTimeSlice ΒΆ
ReadTimeSlice extracts multiple binary-encoded time values
func (*Reader) ReadUint8Slice ΒΆ
ReadUint8Slice returns a byte slice of the specified length
func (*Reader) ReadUint16 ΒΆ
ReadUint16 decodes a variable-length uint16
func (*Reader) ReadUint16Slice ΒΆ
ReadUint16Slice decodes an array of variable-length uint16s
func (*Reader) ReadUint32 ΒΆ
ReadUint32 decodes a variable-length uint32
func (*Reader) ReadUint32Slice ΒΆ
ReadUint32Slice extracts multiple uint32 values
func (*Reader) ReadUint64 ΒΆ
ReadUint64 decodes a variable-length uint64
func (*Reader) ReadUint64Slice ΒΆ
ReadUint64Slice decodes a sequence of uint64 values
func (*Reader) ReadUintSlice ΒΆ
ReadUintSlice extracts an array of variable-length uints
func (*Reader) ReadVarint ΒΆ
func (*Reader) ReadZigzagVarint ΒΆ
ReadZigzagVarint decodes a zigzag-encoded variable integer.
func (*Reader) SetMark ΒΆ
func (r *Reader) SetMark()
SetMark saves the current position for later reference
func (*Reader) SkipVarint ΒΆ
func (r *Reader) SkipVarint()
SkipVarint bypasses a variable-length integer without decoding
type SliceBuilder ΒΆ
type SliceBuilder struct {
// contains filtered or unexported fields
}
SliceBuilder can build various types of glint slices and be appended into a DocumentBuilder
func (*SliceBuilder) AppendBoolSlice ΒΆ
func (s *SliceBuilder) AppendBoolSlice(value []bool)
AppendBoolSlice appends a bool slice to this slice builder
func (*SliceBuilder) AppendBytesSlice ΒΆ
func (s *SliceBuilder) AppendBytesSlice(value [][]byte)
AppendBytesSlice appends a bytes slice to this slice builder
func (*SliceBuilder) AppendFloat32Slice ΒΆ
func (s *SliceBuilder) AppendFloat32Slice(value []float32)
AppendFloat32Slice appends a float32 slice to this slice builder
func (*SliceBuilder) AppendFloat64Slice ΒΆ
func (s *SliceBuilder) AppendFloat64Slice(value []float64)
AppendFloat64Slice appends a float64 slice to this slice builder
func (*SliceBuilder) AppendInt8Slice ΒΆ
func (s *SliceBuilder) AppendInt8Slice(value []int8)
AppendInt8Slice appends a int8 slice to this slice builder
func (*SliceBuilder) AppendInt16Slice ΒΆ
func (s *SliceBuilder) AppendInt16Slice(value []int16)
AppendInt16Slice appends a int16 slice to this slice builder
func (*SliceBuilder) AppendInt16SliceDelta ΒΆ
func (s *SliceBuilder) AppendInt16SliceDelta(value []int16)
AppendInt16SliceDelta appends a int16 slice using delta encoding
func (*SliceBuilder) AppendInt32Slice ΒΆ
func (s *SliceBuilder) AppendInt32Slice(value []int32)
AppendInt32Slice appends a int32 slice to this slice builder
func (*SliceBuilder) AppendInt32SliceDelta ΒΆ
func (s *SliceBuilder) AppendInt32SliceDelta(value []int32)
AppendInt32SliceDelta appends a int32 slice using delta encoding
func (*SliceBuilder) AppendInt64Slice ΒΆ
func (s *SliceBuilder) AppendInt64Slice(value []int64)
AppendInt64Slice appends a int64 slice to this slice builder
func (*SliceBuilder) AppendInt64SliceDelta ΒΆ
func (s *SliceBuilder) AppendInt64SliceDelta(value []int64)
AppendInt64SliceDelta appends a int64 slice using delta encoding
func (*SliceBuilder) AppendIntSlice ΒΆ
func (s *SliceBuilder) AppendIntSlice(value []int)
AppendIntSlice appends a int slice to this slice builder
func (*SliceBuilder) AppendIntSliceDelta ΒΆ
func (s *SliceBuilder) AppendIntSliceDelta(value []int)
AppendIntSliceDelta appends a int slice using delta encoding
func (*SliceBuilder) AppendNestedDocumentSlice ΒΆ
func (s *SliceBuilder) AppendNestedDocumentSlice(value []DocumentBuilder)
AppendNestedDocumentSlice appends a nesteddocument slice
func (*SliceBuilder) AppendSlice ΒΆ
func (s *SliceBuilder) AppendSlice(value []SliceBuilder)
func (*SliceBuilder) AppendStringSlice ΒΆ
func (s *SliceBuilder) AppendStringSlice(value []string)
AppendStringSlice appends a string slice to this slice builder
func (*SliceBuilder) AppendTimeSlice ΒΆ
func (s *SliceBuilder) AppendTimeSlice(value []time.Time)
AppendTimeSlice appends a time slice to this slice builder
func (*SliceBuilder) AppendUint8Slice ΒΆ
func (s *SliceBuilder) AppendUint8Slice(value []uint8)
AppendUint8Slice appends a uint8 slice to this slice builder
func (*SliceBuilder) AppendUint16Slice ΒΆ
func (s *SliceBuilder) AppendUint16Slice(value []uint16)
AppendUint16Slice appends a uint16 slice to this slice builder
func (*SliceBuilder) AppendUint16SliceDelta ΒΆ
func (s *SliceBuilder) AppendUint16SliceDelta(value []uint16)
AppendUint16SliceDelta appends a uint16 slice using delta encoding
func (*SliceBuilder) AppendUint32Slice ΒΆ
func (s *SliceBuilder) AppendUint32Slice(value []uint32)
AppendUint32Slice appends a uint32 slice to this slice builder
func (*SliceBuilder) AppendUint32SliceDelta ΒΆ
func (s *SliceBuilder) AppendUint32SliceDelta(value []uint32)
AppendUint32SliceDelta appends a uint32 slice using delta encoding
func (*SliceBuilder) AppendUint64Slice ΒΆ
func (s *SliceBuilder) AppendUint64Slice(value []uint64)
AppendUint64Slice appends a uint64 slice to this slice builder
func (*SliceBuilder) AppendUint64SliceDelta ΒΆ
func (s *SliceBuilder) AppendUint64SliceDelta(value []uint64)
AppendUint64SliceDelta appends a uint64 slice using delta encoding
func (*SliceBuilder) AppendUintSlice ΒΆ
func (s *SliceBuilder) AppendUintSlice(value []uint)
AppendUintSlice appends a uint slice to this slice builder
func (*SliceBuilder) AppendUintSliceDelta ΒΆ
func (s *SliceBuilder) AppendUintSliceDelta(value []uint)
AppendUintSliceDelta appends a uint slice using delta encoding
type SliceEncoder ΒΆ
type SliceEncoder struct {
// contains filtered or unexported fields
}
func NewSliceEncoder ΒΆ
func NewSliceEncoder(t any) *SliceEncoder
func NewSliceEncoderUsingTag ΒΆ
func NewSliceEncoderUsingTag(t any, usingTagName string) *SliceEncoder
func NewSliceEncoderUsingTagWithSchema ΒΆ
func NewSliceEncoderUsingTagWithSchema(t any, usingTagName string, sc *Buffer) *SliceEncoder
NewSliceEncoderUsingTagWithSchema allows us to create an instruction which can iterate over a slice of different data types at runtime
func (*SliceEncoder) ClearSchema ΒΆ
func (s *SliceEncoder) ClearSchema()
ClearSchema removes all data from the schema of this instance
func (*SliceEncoder) Marshal ΒΆ
func (s *SliceEncoder) Marshal(v any, w *Buffer)
Marshal executes the instruction set built up by NewSliceEncoder
func (*SliceEncoder) Schema ΒΆ
func (s *SliceEncoder) Schema() *Buffer
Schema returns the schema without any header information
type TrustHeader ΒΆ
type TrustHeader struct {
// contains filtered or unexported fields
}
TrustHeader enables HTTP-based trusted schema mode. Implements the KVP interface for HTTP headers.
func NewTrustHeader ΒΆ
func NewTrustHeader(d *decoderImpl) TrustHeader
NewTrustHeader creates an HTTP header for schema trust negotiation. Use with NewBufferWithTrust to skip schema transmission when both sides have matching schemas.
type Trustee ΒΆ
type Trustee interface {
Hash() uint32
}
Trustee interface enables schema trust verification. HTTPTrustee provides a default HTTP-based implementation.
func HTTPTrustee ΒΆ
HTTPTrustee implements trust verification via the X-Glint-Trust HTTP header. Use with NewBufferWithTrust to enable schema omission for trusted requests.
type Visitor ΒΆ
type Visitor interface {
VisitFlags(flags byte) error
VisitSchemaHash(hash []byte) error
VisitField(name string, wire WireType, body Reader) (Reader, error)
VisitArrayStart(name string, wire WireType, length int) error
VisitArrayEnd(name string) error
VisitStructStart(name string) error
VisitStructEnd(name string) error
}
Visitor is an interface that can be implemented to walk a document
type Walker ΒΆ
type Walker struct {
// contains filtered or unexported fields
}
Walker walks a document
type WireType ΒΆ
type WireType uint
const ( WireBool WireType = 1 WireInt WireType = 2 WireInt8 WireType = 3 WireInt16 WireType = 4 WireInt32 WireType = 5 WireInt64 WireType = 6 WireUint WireType = 7 WireUint8 WireType = 8 WireUint16 WireType = 9 WireUint32 WireType = 10 WireUint64 WireType = 11 WireFloat32 WireType = 12 WireFloat64 WireType = 13 WireString WireType = 14 WireBytes WireType = 15 WireStruct WireType = 16 WireMap WireType = 17 WireTime WireType = 18 // maximum value 31 (5-bit limit) WireTypeMask = 0b00011111 WireSliceFlag WireType = 1 << 5 // marks slice fields WirePtrFlag WireType = 1 << 6 // marks nullable fields WireDeltaFlag WireType = 1 << 7 // delta encoding for numeric slices )
func ReflectKindToWireType ΒΆ
ReflectKindToWireType converts Go reflection types to glint wire types