ltvgo

package module
v0.0.0-...-45d4590 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 14 Imported by: 0

README

TODO

  • Clean up duplicate code between the []byte and stream coders (where appropriate).
  • Write tests to verify compatibility with JSON marshal tags.
  • Documentation
  • More Examples
  • Hone performance

Documentation

Index

Examples

Constants

View Source
const MaxNestingDepth = 10000

Maximum that structs/arrays can be nested in this library.

View Source
const NopTag byte = 0xFF

Variables

This section is empty.

Functions

func Marshal

func Marshal(v any) ([]byte, error)
Example
type ColorGroup struct {
	ID     int
	Name   string
	Colors []string
}
group := ColorGroup{
	ID:     1,
	Name:   "Reds",
	Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}

b, err := Marshal(group)
if err != nil {
	fmt.Println("error:", err)
}

fmt.Println(hex.EncodeToString(b))
Output:

1041024944a00141044e616d654104526564734106436f6c6f72732041074372696d736f6e410352656441045275627941064d61726f6f6e3030

func Unmarshal

func Unmarshal(data []byte, v any) error

func Valid

func Valid(data []byte) bool

Valid reports whether data is a valid LiteVector buffer.

func Validate

func Validate(data []byte) error

Types

type Decoder

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

A LiteVector buffer decoder

func NewDecoder

func NewDecoder(buf []byte) *Decoder

func (*Decoder) Next

func (s *Decoder) Next() (LtvDesc, error)

Read the next tag or tag and length prefix from the stream. On return, the scanner will be positioned over the value.

func (*Decoder) ReadValue

func (s *Decoder) ReadValue(d LtvDesc) (any, error)

Read a single (generic) value from a data stream based on the given descriptor.

func (*Decoder) Skip

func (s *Decoder) Skip(d LtvDesc) error

Skip the value currently under the scanner

func (*Decoder) ValidateAndSkip

func (s *Decoder) ValidateAndSkip(d LtvDesc) error

Skip the value currently under the scanner with additional validation checks.

func (*Decoder) Value

func (s *Decoder) Value() (any, error)

Read the next value from a data stream.

type Encoder

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

func NewEncoder

func NewEncoder() *Encoder

func (*Encoder) Bytes

func (e *Encoder) Bytes() []byte

Return the underlying buffer. This is only valid until the next buffer modification. Callers that want to hold onto this value should make a copy.

func (*Encoder) RawWrite

func (e *Encoder) RawWrite(data []byte)

func (*Encoder) RawWriteByte

func (e *Encoder) RawWriteByte(data byte)

func (*Encoder) RawWriteUint16

func (e *Encoder) RawWriteUint16(v uint16)

Passthrough write Uint16 endian corrected

func (*Encoder) RawWriteUint32

func (e *Encoder) RawWriteUint32(v uint32)

Passthrough write Uint32, endian corrected

func (*Encoder) RawWriteUint64

func (e *Encoder) RawWriteUint64(v uint64)

Passthrough write Uint64, endian corrected

func (*Encoder) Reset

func (e *Encoder) Reset()

func (*Encoder) WriteBool

func (e *Encoder) WriteBool(v bool)

func (*Encoder) WriteBoolVec

func (e *Encoder) WriteBoolVec(v []bool)

func (*Encoder) WriteBytes

func (e *Encoder) WriteBytes(v []byte)

func (*Encoder) WriteF32

func (e *Encoder) WriteF32(v float32)

func (*Encoder) WriteF32Vec

func (e *Encoder) WriteF32Vec(v []float32)

func (*Encoder) WriteF64

func (e *Encoder) WriteF64(v float64)

func (*Encoder) WriteF64Vec

func (e *Encoder) WriteF64Vec(v []float64)

func (*Encoder) WriteI8

func (e *Encoder) WriteI8(v int8)

func (*Encoder) WriteI8Vec

func (e *Encoder) WriteI8Vec(v []int8)

func (*Encoder) WriteI16

func (e *Encoder) WriteI16(v int16)

func (*Encoder) WriteI16Vec

func (e *Encoder) WriteI16Vec(v []int16)

func (*Encoder) WriteI32

func (e *Encoder) WriteI32(v int32)

func (*Encoder) WriteI32Vec

func (e *Encoder) WriteI32Vec(v []int32)

func (*Encoder) WriteI64

func (e *Encoder) WriteI64(v int64)

func (*Encoder) WriteI64Vec

func (e *Encoder) WriteI64Vec(v []int64)

func (*Encoder) WriteInt

func (e *Encoder) WriteInt(v int64)

Write int with Goldilocks fitting

func (*Encoder) WriteListEnd

func (e *Encoder) WriteListEnd()

func (*Encoder) WriteListStart

func (e *Encoder) WriteListStart()

func (*Encoder) WriteNil

func (e *Encoder) WriteNil()

func (*Encoder) WriteNop

func (e *Encoder) WriteNop()

func (*Encoder) WriteString

func (e *Encoder) WriteString(s string)

func (*Encoder) WriteStructEnd

func (e *Encoder) WriteStructEnd()

func (*Encoder) WriteStructStart

func (e *Encoder) WriteStructStart()

func (*Encoder) WriteTag

func (e *Encoder) WriteTag(t TypeCode, s SizeCode)

func (*Encoder) WriteU8

func (e *Encoder) WriteU8(v uint8)

func (*Encoder) WriteU8Vec

func (e *Encoder) WriteU8Vec(v []uint8)

func (*Encoder) WriteU16

func (e *Encoder) WriteU16(v uint16)

func (*Encoder) WriteU16Vec

func (e *Encoder) WriteU16Vec(v []uint16)

func (*Encoder) WriteU32

func (e *Encoder) WriteU32(v uint32)

func (*Encoder) WriteU32Vec

func (e *Encoder) WriteU32Vec(v []uint32)

func (*Encoder) WriteU64

func (e *Encoder) WriteU64(v uint64)

func (*Encoder) WriteU64Vec

func (e *Encoder) WriteU64Vec(v []uint64)

func (*Encoder) WriteUint

func (e *Encoder) WriteUint(v uint64)

Write uint with Goldilocks fitting

func (*Encoder) WriteVectorPrefix

func (e *Encoder) WriteVectorPrefix(t TypeCode, count int)

Write the tag and length for a typed vector.

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type LtvDesc

type LtvDesc struct {
	TypeCode TypeCode
	SizeCode SizeCode
	Length   uint64
	Offset   int
}

A LiteVector element descriptor

type LtvElementDesc

type LtvElementDesc struct {
	Tag          byte
	TypeCode     TypeCode
	SizeCode     SizeCode
	Length       uint64
	TagOffset    int
	ValueOffset  int
	Role         LtvElementRole
	FirstElement bool
	Depth        int
}

A LiteVector element descriptor

type LtvElementRole

type LtvElementRole int
const (
	RoleValue LtvElementRole = iota
	RoleListEnd
	RoleStructEnd
	RoleStructKey
	RoleStructValue
)

type LtvEncoder

type LtvEncoder interface {
	WriteNop()
	WriteNil()
	WriteStructStart()
	WriteStructEnd()
	WriteListStart()
	WriteListEnd()
	WriteBool(bool)
	WriteU8(uint8)
	WriteU16(uint16)
	WriteU32(uint32)
	WriteU64(uint64)
	WriteI8(int8)
	WriteI16(int16)
	WriteI32(int32)
	WriteI64(int64)

	WriteF32(float32)
	WriteF64(float64)

	WriteInt(int64)
	WriteUint(uint64)
	WriteString(string)
	WriteBytes([]byte)

	WriteBoolVec([]bool)
	WriteU8Vec([]uint8)
	WriteU16Vec([]uint16)
	WriteU32Vec([]uint32)
	WriteU64Vec([]uint64)
	WriteI8Vec([]int8)
	WriteI16Vec([]int16)
	WriteI32Vec([]int32)
	WriteI64Vec([]int64)
	WriteF32Vec([]float32)
	WriteF64Vec([]float64)
}

A interface for a generic LiteVector encoder

type Marshaler

type Marshaler interface {
	MarshalLTV() ([]byte, error)
}

type MarshalerError

type MarshalerError struct {
	Type reflect.Type
	Err  error
	// contains filtered or unexported fields
}

A MarshalerError represents an error from calling a MarshalLTV or MarshalText method.

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

func (*MarshalerError) Unwrap

func (e *MarshalerError) Unwrap() error

Unwrap returns the underlying error.

type SizeCode

type SizeCode uint8
const (
	SizeSingle SizeCode = 0
	Size1      SizeCode = 1
	Size2      SizeCode = 2
	Size4      SizeCode = 3
	Size8      SizeCode = 4
)

type StreamDecoder

type StreamDecoder struct {

	// If set to true, will return NOP tags.
	ReturnNops bool

	// The maximum length supported by the ReadValue function
	MaxValueLength uint64
	// contains filtered or unexported fields
}

func NewStreamDecoder

func NewStreamDecoder(r io.Reader) *StreamDecoder

func (*StreamDecoder) Next

func (s *StreamDecoder) Next() (LtvElementDesc, error)

Read the next tag or tag and length prefix from the stream. On return, the scanner will be positioned over the value.

func (*StreamDecoder) Read

func (s *StreamDecoder) Read(buf []byte) (int, error)

Read into buffer from the underlying stream.

func (*StreamDecoder) ReadByte

func (s *StreamDecoder) ReadByte() (byte, error)

Read a byte from the underlying stream and return the byte or error.

func (*StreamDecoder) ReadFull

func (s *StreamDecoder) ReadFull(buf []byte) error

Read the full size of the buffer from the underlying stream.

func (*StreamDecoder) ReadValue

func (s *StreamDecoder) ReadValue(d LtvElementDesc) (any, error)

Read a single (generic) value from a data stream based on the passed descriptor.

func (*StreamDecoder) Skip

func (s *StreamDecoder) Skip(d LtvElementDesc) error

Skip the value currently under the scanner

func (*StreamDecoder) SkipValue

func (s *StreamDecoder) SkipValue(d LtvElementDesc) error

Skip the value of a tag

func (*StreamDecoder) ValidateAndSkip

func (s *StreamDecoder) ValidateAndSkip(d LtvElementDesc) error

Skip the value currently under the scanner with additional validation checks. Current implementation is inefficient to the point of being an abomination. It should be refactored with fire...

func (*StreamDecoder) Value

func (s *StreamDecoder) Value() (any, error)

Read the next value from a data stream.

type StreamEncoder

type StreamEncoder struct {
	Werr error
	// contains filtered or unexported fields
}

func NewStreamEncoder

func NewStreamEncoder(w io.Writer) *StreamEncoder

func (*StreamEncoder) RawWrite

func (e *StreamEncoder) RawWrite(b []byte)

Passthrough write a byte array to the underlying writer with error caching and offset tracking.

func (*StreamEncoder) RawWriteByte

func (e *StreamEncoder) RawWriteByte(v byte)

Passthrough write byte

func (*StreamEncoder) RawWriteUint16

func (e *StreamEncoder) RawWriteUint16(v uint16)

Passthrough write Uint16 endian corrected

func (*StreamEncoder) RawWriteUint32

func (e *StreamEncoder) RawWriteUint32(v uint32)

Passthrough write Uint32, endian corrected

func (*StreamEncoder) RawWriteUint64

func (e *StreamEncoder) RawWriteUint64(v uint64)

Passthrough write Uint64, endian corrected

func (*StreamEncoder) Reset

func (e *StreamEncoder) Reset()

Reset the error state and offset of the encoder.

func (*StreamEncoder) SetOffset

func (e *StreamEncoder) SetOffset(offset int)

Manually set the "offset" of the stream encoder.

func (*StreamEncoder) WriteBool

func (e *StreamEncoder) WriteBool(v bool)

func (*StreamEncoder) WriteBoolVec

func (e *StreamEncoder) WriteBoolVec(v []bool)

func (*StreamEncoder) WriteBytes

func (e *StreamEncoder) WriteBytes(v []byte)

func (*StreamEncoder) WriteF32

func (e *StreamEncoder) WriteF32(v float32)

func (*StreamEncoder) WriteF32Vec

func (e *StreamEncoder) WriteF32Vec(v []float32)

func (*StreamEncoder) WriteF64

func (e *StreamEncoder) WriteF64(v float64)

func (*StreamEncoder) WriteF64Vec

func (e *StreamEncoder) WriteF64Vec(v []float64)

func (*StreamEncoder) WriteI8

func (e *StreamEncoder) WriteI8(v int8)

func (*StreamEncoder) WriteI8Vec

func (e *StreamEncoder) WriteI8Vec(v []int8)

func (*StreamEncoder) WriteI16

func (e *StreamEncoder) WriteI16(v int16)

func (*StreamEncoder) WriteI16Vec

func (e *StreamEncoder) WriteI16Vec(v []int16)

func (*StreamEncoder) WriteI32

func (e *StreamEncoder) WriteI32(v int32)

func (*StreamEncoder) WriteI32Vec

func (e *StreamEncoder) WriteI32Vec(v []int32)

func (*StreamEncoder) WriteI64

func (e *StreamEncoder) WriteI64(v int64)

func (*StreamEncoder) WriteI64Vec

func (e *StreamEncoder) WriteI64Vec(v []int64)

func (*StreamEncoder) WriteInt

func (e *StreamEncoder) WriteInt(v int64)

Write int with Goldilocks fitting

func (*StreamEncoder) WriteListEnd

func (e *StreamEncoder) WriteListEnd()

func (*StreamEncoder) WriteListStart

func (e *StreamEncoder) WriteListStart()

func (*StreamEncoder) WriteNil

func (e *StreamEncoder) WriteNil()

func (*StreamEncoder) WriteNop

func (e *StreamEncoder) WriteNop()

func (*StreamEncoder) WriteString

func (e *StreamEncoder) WriteString(s string)

func (*StreamEncoder) WriteStructEnd

func (e *StreamEncoder) WriteStructEnd()

func (*StreamEncoder) WriteStructStart

func (e *StreamEncoder) WriteStructStart()

func (*StreamEncoder) WriteTag

func (e *StreamEncoder) WriteTag(t TypeCode, s SizeCode)

func (*StreamEncoder) WriteU8

func (e *StreamEncoder) WriteU8(v uint8)

func (*StreamEncoder) WriteU8Vec

func (e *StreamEncoder) WriteU8Vec(v []uint8)

func (*StreamEncoder) WriteU16

func (e *StreamEncoder) WriteU16(v uint16)

func (*StreamEncoder) WriteU16Vec

func (e *StreamEncoder) WriteU16Vec(v []uint16)

func (*StreamEncoder) WriteU32

func (e *StreamEncoder) WriteU32(v uint32)

func (*StreamEncoder) WriteU32Vec

func (e *StreamEncoder) WriteU32Vec(v []uint32)

func (*StreamEncoder) WriteU64

func (e *StreamEncoder) WriteU64(v uint64)

func (*StreamEncoder) WriteU64Vec

func (e *StreamEncoder) WriteU64Vec(v []uint64)

func (*StreamEncoder) WriteUint

func (e *StreamEncoder) WriteUint(v uint64)

Write uint with Goldilocks fitting

func (*StreamEncoder) WriteVectorPrefix

func (e *StreamEncoder) WriteVectorPrefix(t TypeCode, count int)

Write the tag and length for a typed vector.

type StructEncoder

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

func NewStructEncoder

func NewStructEncoder(enc LtvEncoder) StructEncoder

func (StructEncoder) Bool

func (s StructEncoder) Bool(key string, v bool)

func (StructEncoder) BoolVec

func (s StructEncoder) BoolVec(key string, v []bool)

func (StructEncoder) Bytes

func (s StructEncoder) Bytes(key string, v []byte)

func (StructEncoder) EndStruct

func (s StructEncoder) EndStruct()

func (StructEncoder) F32Vec

func (s StructEncoder) F32Vec(key string, v []float32)

func (StructEncoder) F64Vec

func (s StructEncoder) F64Vec(key string, v []float64)

func (StructEncoder) I8

func (s StructEncoder) I8(key string, v int8)

func (StructEncoder) I8Vec

func (s StructEncoder) I8Vec(key string, v []int8)

func (StructEncoder) I16

func (s StructEncoder) I16(key string, v int16)

func (StructEncoder) I16Vec

func (s StructEncoder) I16Vec(key string, v []int16)

func (StructEncoder) I32

func (s StructEncoder) I32(key string, v int32)

func (StructEncoder) I32Vec

func (s StructEncoder) I32Vec(key string, v []int32)

func (StructEncoder) I64

func (s StructEncoder) I64(key string, v int64)

func (StructEncoder) I64Vec

func (s StructEncoder) I64Vec(key string, v []int64)

func (StructEncoder) Int

func (s StructEncoder) Int(key string, v int64)

func (StructEncoder) Nil

func (s StructEncoder) Nil(key string)

func (StructEncoder) StartStruct

func (s StructEncoder) StartStruct()

func (StructEncoder) String

func (s StructEncoder) String(key string, v string)

func (StructEncoder) U8

func (s StructEncoder) U8(key string, v uint8)

func (StructEncoder) U8Vec

func (s StructEncoder) U8Vec(key string, v []uint8)

func (StructEncoder) U16

func (s StructEncoder) U16(key string, v uint16)

func (StructEncoder) U16Vec

func (s StructEncoder) U16Vec(key string, v []uint16)

func (StructEncoder) U32

func (s StructEncoder) U32(key string, v uint32)

func (StructEncoder) U32Vec

func (s StructEncoder) U32Vec(key string, v []uint32)

func (StructEncoder) U64

func (s StructEncoder) U64(key string, v uint64)

func (StructEncoder) U64Vec

func (s StructEncoder) U64Vec(key string, v []uint64)

func (StructEncoder) Uint

func (s StructEncoder) Uint(key string, v uint64)

type TypeCode

type TypeCode uint8
const (
	Nil    TypeCode = 0
	Struct TypeCode = 1
	List   TypeCode = 2
	End    TypeCode = 3
	String TypeCode = 4
	Bool   TypeCode = 5
	U8     TypeCode = 6
	U16    TypeCode = 7
	U32    TypeCode = 8
	U64    TypeCode = 9
	I8     TypeCode = 10
	I16    TypeCode = 11
	I32    TypeCode = 12
	I64    TypeCode = 13
	F32    TypeCode = 14
	F64    TypeCode = 15
)

func (TypeCode) Size

func (t TypeCode) Size() int

func (TypeCode) String

func (t TypeCode) String() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Desc   LtvDesc      // LiteVector descriptor
	GoType reflect.Type // Go value type it could not be assigned to
	Struct string       // name of the struct type containing the field
	Field  string       // the full path from root node to the field
}

An UnmarshalTypeError describes a LiteVector value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalLTV([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a LiteVector description of themselves. UnmarshalLTV must copy whatever it wishes to retain after returning.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

type UnsupportedValueError

type UnsupportedValueError struct {
	Value reflect.Value
	Str   string
}

An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.

func (*UnsupportedValueError) Error

func (e *UnsupportedValueError) Error() string

Directories

Path Synopsis
cmd
json2ltv command
Utility that converts JSON to it's LiteVector representation.
Utility that converts JSON to it's LiteVector representation.
ltv2dict command
ltv2json command
Utility that converts LiteVector data to it's JSON representation.
Utility that converts LiteVector data to it's JSON representation.
ltvdump command

Jump to

Keyboard shortcuts

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