Documentation ¶
Overview ¶
Package vamp implements the Vampire binary data serialization format for Go.
While Vampire does not provide zero-copy characteristics it gives you "direct access" to values i.e., you can navigate any path down into nested structures without parsing too many other parts that come before the value you are looking for. This feature makes any value directly accessible – “perviam” in Latin. Just because “Vampire” sounds much cooler the format is called Vampire in the end. But vamp is the package name to Go for because its shorter and still associated with vampires.
Type Promotion ¶
When reading and writing data, Vamp supports type promotion between certain types to facilitate the preservation of backwards compatibility.
Type promotion between integers is supported if a source type can be stored in a target type without loss. E.g. uint16 can be promoted to int32 but no signed integer type can be promoted to any unsigned integer type.
float32 will be promoted to `float64`
For the machine-dependent integers int and uint, it depends on the current value whether it can be read from or written to a Vampire integer. If the current value is within the range of the target, the read or write is successful. Otherwise, it results in a runtime error.
Date and Duration ¶
By default as int64 <-> Unix time with millis.
Example ¶
type Animal struct { Age int `vamp:"age,size=8"` Name string `vamp:",size=8"` Owners []string `vamp:",size=8:8"` Male bool } // Use reflection to get the Vampire type; can be created with API, too animalType, _, _ := ReflectType(Animal{}) // We use a 16-bit offset for the buffers const BufferOffset = Size16 // Create write buffer starting with a Vampire header wbuf := NewWriteBuffer( animalType, BufferOffset, HeaderSize, // Start writing data behind the header HeaderFor(BufferOffset).Bytes(0), // Pass Vampire header as buffer ) // Write a record of type t to the buffer. Value has to match type t. animalType.Write(wbuf, Animal{ Age: 4, Name: "Lassie", Owners: []string{"Timmy", "Martin"}, Male: false, }) // How many bytes did we write fmt.Printf("With header: %d; Value data only: %d\n", len(wbuf.AllBytes()), len(wbuf.Bytes())) // Now extract the complete buffer, incl. header data := wbuf.AllBytes() // Wrap data into a read buffer that expects the header at byte 0 // Reads buffer's offset size from header (We could also directly read from wbuf) rbuf, _ := NewReadBufferHeader(animalType, data, false) // We need the specific type – record – to select a field rt := animalType.(*Record) // Slice the read buffer to field "Owners" with index 2 (3rd field) revert := rt.Field(rbuf, 2) // Read the Owners into a string array var owners []string rt.FieldType(2).Read(rbuf, &owners) // We want to unslice the buffer to return to the complete record rbuf.Unslice(revert) // Print what we got from the binary data fmt.Println(strings.Join(owners, " & "))
Output: With header: 35; Value data only: 31 Timmy & Martin
Index ¶
- Constants
- Variables
- func Equal(s, t Type, opts CompareOpts) bool
- func MustReflectType(val any) (Type, Fields)
- func ReflectType(val any) (Type, Fields, error)
- func ResetTypeBuffer(buf *Buffer, off SizeRange, prefix int)
- func TypeName(t Type) string
- func UseFieldsForStruct(s any, fs Fields) error
- func ValidName(s string) bool
- func WriteType(buf *Buffer, t Type) error
- func WriteTypeFile(name string, off SizeRange, t Type) error
- type Alt
- type AnyMarshaler
- type Array
- func (a *Array) Compare(t Type, opts CompareOpts) (bool, int)
- func (t *Array) DynNo() uint
- func (t *Array) Elem() Type
- func (t *Array) FixSize() uint
- func (a *Array) Hash(h hash.Hash, opts CompareOpts)
- func (t *Array) Index(buf *ReadBuffer, i uint) (revert uint)
- func (t *Array) Len(buf *ReadBuffer) uint
- func (t *Array) LenRange() SizeRange
- func (t *Array) PrintTo(w io.Writer) (n int, err error)
- func (t *Array) Read(buf *ReadBuffer, into any) error
- func (t *Array) ReadArray(buf *ReadBuffer, ls ArrayUnmarshaler) error
- func (t *Array) ReadMap(buf *ReadBuffer, m any) error
- func (t *Array) ReadOpt(buf *ReadBuffer, into any) (ok bool, err error)
- func (t *Array) String() string
- func (t *Array) VampRecordRead(rt *Record, buf *ReadBuffer) error
- func (t *Array) VampRecordWrite(rt *Record, buf *Buffer) error
- func (t *Array) VampUnionAlt() (uint, any)
- func (t *Array) Write(buf *Buffer, ls any) (err error)
- func (t *Array) WriteArray(buf *Buffer, ls ArrayMarshaler) error
- func (t *Array) WriteMap(buf *Buffer, m any) error
- func (t *Array) WriteOptNone(buf *Buffer) error
- func (t *Array) WriteOptOne(buf *Buffer, value any) error
- type ArrayMarshaler
- type ArrayUnmarshaler
- type Buffer
- type Bytes
- func (s Bytes) Compare(t Type, _ CompareOpts) (bool, int)
- func (t Bytes) DynNo() uint
- func (t Bytes) FixSize() uint
- func (s Bytes) Hash(h hash.Hash, _ CompareOpts)
- func (t Bytes) LenRange() SizeRange
- func (t Bytes) PrintTo(w io.Writer) (int, error)
- func (t Bytes) Read(buf *ReadBuffer, into any) (err error)
- func (t Bytes) ReadBytes(buf *ReadBuffer) []byte
- func (t Bytes) ReadString(buf *ReadBuffer) string
- func (t Bytes) String() string
- func (t *Bytes) VampFromUint8(v uint8) error
- func (t Bytes) VampToUint8() (uint8, error)
- func (t Bytes) VampUnionAlt() (uint, any)
- func (t Bytes) Write(buf *Buffer, value any) error
- func (t Bytes) WriteBytes(buf *Buffer, data []byte) error
- func (t Bytes) WriteString(buf *Buffer, s string) error
- type BytesReadMode
- type CompareOpts
- type Fields
- type Header
- type List
- func (l *List) Compare(t Type, opts CompareOpts) (bool, int)
- func (t *List) DynNo() uint
- func (t *List) Elem() Type
- func (t *List) FixSize() uint
- func (a *List) Hash(h hash.Hash, opts CompareOpts)
- func (t *List) LenRange() SizeRange
- func (t *List) ListLen(buf *ReadBuffer) (len uint, ok bool)
- func (t *List) Next(buf *ReadBuffer) (revert uint, ok bool)
- func (t *List) PrintTo(w io.Writer) (n int, err error)
- func (t *List) Read(buf *ReadBuffer, into any) error
- func (t *List) ReadList(buf *ReadBuffer, into ListUnmarshaler) error
- func (l *List) ReadStream(r io.Reader, offSize SizeRange, buffer []byte) *ReadStream
- func (t *List) String() string
- func (t *List) VampRecordRead(rt *Record, buf *ReadBuffer) error
- func (t *List) VampRecordWrite(rt *Record, buf *Buffer) error
- func (t *List) VampUnionAlt() (uint, any)
- func (t *List) Write(buf *Buffer, val any) error
- func (t *List) WriteList(buf *Buffer, ls ListMarshaler) error
- func (l *List) WriteStream(w io.Writer, offSize SizeRange, buffer []byte) (*WriteStream, error)
- type ListIterator
- type ListMarshaler
- type ListUnmarshaler
- type Path
- type PathElement
- type PathError
- type ReadBuffer
- type ReadStream
- type Record
- func (r *Record) Compare(t Type, opts CompareOpts) (bool, int)
- func (t *Record) DynNo() uint
- func (t *Record) Field(buf *ReadBuffer, i uint) (revert uint)
- func (t *Record) FieldName(i uint) string
- func (t *Record) FieldType(i uint) Type
- func (t *Record) FixSize() uint
- func (t *Record) HasNames() bool
- func (r *Record) Hash(h hash.Hash, opts CompareOpts)
- func (t *Record) NumField() uint
- func (t *Record) PrintTo(w io.Writer) (n int, err error)
- func (t *Record) Read(buf *ReadBuffer, out any) (err error)
- func (t *Record) ReadFields(buf *ReadBuffer, outs ...any) error
- func (t *Record) String() string
- func (t *Record) VampArrayFinish() error
- func (t *Record) VampArrayLen() uint
- func (t *Record) VampArrayReader(i uint) (any, error)
- func (t *Record) VampArrayResize(l uint) error
- func (t *Record) VampArrayWriter(i uint) (any, error)
- func (t *Record) VampUnionAlt() (uint, any)
- func (t *Record) Write(buf *Buffer, value any) error
- func (t *Record) WriteFields(buf *Buffer, values ...any) error
- type RecordMarshaler
- type RecordUnmarshaler
- type Reflect
- func (r Reflect) ArraySize(s SizeRange) (res Reflect)
- func (r Reflect) MustType(val any) (Type, Fields)
- func (r Reflect) MustUseStruct(s any) *Record
- func (r Reflect) StringSize(s SizeRange) Reflect
- func (r Reflect) Type(val any) (Type, Fields, error)
- func (r Reflect) UseStruct(s any) (*Record, error)
- type SizeRange
- type Type
- func Anonymized(t Type) Type
- func MustNamed(n string, t Type) Type
- func Named(n string, t Type) (Type, error)
- func ParseType(rd io.Reader) (typ Type, err error)
- func ParseTypeString(s string) (Type, error)
- func ReadType(buf *ReadBuffer) (t Type, err error)
- func ReadTypeFile(name string) (Type, error)
- type Union
- func (t *Union) Alt(i uint) Type
- func (t *Union) AltName(i uint) string
- func (t *Union) AltRange() SizeRange
- func (u *Union) Compare(t Type, opts CompareOpts) (bool, int)
- func (t *Union) DynNo() uint
- func (t *Union) FixSize() uint
- func (u *Union) Hash(h hash.Hash, opts CompareOpts)
- func (t *Union) NumAlts() uint
- func (t *Union) PrintTo(w io.Writer) (n int, err error)
- func (t *Union) Read(buf *ReadBuffer, into any) error
- func (t *Union) ReadAlt(buf *ReadBuffer, altInto func(uint) (any, error)) (any, error)
- func (t *Union) SetAltReader(f func(uint, any) (any, error)) *Union
- func (t *Union) String() string
- func (t *Union) VampRecordRead(rt *Record, buf *ReadBuffer) error
- func (t *Union) VampRecordWrite(rt *Record, buf *Buffer) error
- func (t *Union) VampUnionAlt() (uint, any)
- func (t *Union) Write(buf *Buffer, value any) error
- func (t *Union) WriteAlt(buf *Buffer, alt uint, value any) error
- type UnionMarshaler
- type UnionUnmarshaler
- type WriteStream
Examples ¶
Constants ¶
const ( // Version of the implemented Vampire encoding schema Version = 0 // Size of Vampire data header in byte HeaderSize = len(Header{}) )
Variables ¶
var ( Bool boolType Int8 int8Type Int16 int16Type Int32 int32Type Int64 int64Type Uint8 uint8Type Uint16 uint16Type Uint32 uint32Type Uint64 uint64Type Float32 float32Type Float64 float64Type )
Vampire Basic Types
Each <N>Type variable implements Type and represents a Vampire type that corresponds directly to a Go type <n> where <n> is <N> folded to lower case e.g., Bool corresponds to Go's bool type.
Marshaler and Unmarshaler interfaces are not explicitly defined for basic types. To implement user defined (un-)marshaling to or from <N>Type a value has to implement methods that match the following convention:
The marshaler for writing <n> to <N>Type:
interface{ VampTo<N>() (<n>, error) }
The unmarshaler for reading <n> from <N>Type:
interface{ VampFrom<N>(<n>) error }
var Any anyType
var IOBytesMode = CheckUTF8
Functions ¶
func Equal ¶ added in v0.8.0
func Equal(s, t Type, opts CompareOpts) bool
func MustReflectType ¶ added in v0.7.0
func ResetTypeBuffer ¶
func TypeName ¶ added in v0.4.0
TypeName returns the name attached to a type, if any. Otherwise TypeName returns the empty string.
func UseFieldsForStruct ¶ added in v0.8.0
Types ¶
type Alt ¶
func (Alt) VampUnionAlt ¶
type AnyMarshaler ¶
type Array ¶ added in v0.5.0
type Array struct {
// contains filtered or unexported fields
}
Example ¶
a := []string{"boo", "bar", "baz"} t, _, _ := ReflectType(a) fmt.Println(t) buf := NewWriteBuffer(t, Size16, 0, nil) t.Write(buf, a) var out []string t.Read(&buf.ReadBuffer, &out) fmt.Printf("%[1]T=%[1]v\n", out)
Output: [Size32 "Size16"] []string=[boo bar baz]
func NewOptional ¶ added in v0.5.0
NewOptional returns an ArrayType with length of Size8 – the smallest sufficient size range. This is the recommended way to represent optional values.
Example ¶
et := NewBytes(Size8, ReadRaw) ot := NewOptional(et) buf := NewWriteBuffer(ot, Size8, 0, nil) ot.WriteOptOne(buf, "Hello, optional!") var out string ok, _ := ot.ReadOpt(&buf.ReadBuffer, &out) fmt.Println(ok, out) buf.Reset(ot, Size16, 0) ot.WriteOptNone(buf) out = "-" ok, _ = ot.ReadOpt(&buf.ReadBuffer, &out) fmt.Println(ok, out)
Output: true Hello, optional! false -
func (*Array) Compare ¶ added in v0.8.0
func (a *Array) Compare(t Type, opts CompareOpts) (bool, int)
func (*Array) Len ¶ added in v0.5.0
func (t *Array) Len(buf *ReadBuffer) uint
func (*Array) ReadArray ¶ added in v0.5.0
func (t *Array) ReadArray(buf *ReadBuffer, ls ArrayUnmarshaler) error
func (*Array) ReadOpt ¶ added in v0.5.0
func (t *Array) ReadOpt(buf *ReadBuffer, into any) (ok bool, err error)
func (*Array) VampRecordRead ¶ added in v0.5.0
func (t *Array) VampRecordRead(rt *Record, buf *ReadBuffer) error
func (*Array) VampRecordWrite ¶ added in v0.5.0
func (*Array) VampUnionAlt ¶ added in v0.5.0
func (*Array) WriteArray ¶ added in v0.5.0
func (t *Array) WriteArray(buf *Buffer, ls ArrayMarshaler) error
func (*Array) WriteOptNone ¶ added in v0.5.0
type ArrayMarshaler ¶
type ArrayUnmarshaler ¶
type Buffer ¶
type Buffer struct {
ReadBuffer
}
type Bytes ¶ added in v0.5.0
type Bytes struct {
// contains filtered or unexported fields
}
mode is a vamp but not a Vampire thing (Hash; Equals)
func NewBytes ¶ added in v0.5.0
func NewBytes(size SizeRange, mode BytesReadMode) Bytes
func (Bytes) Read ¶ added in v0.5.0
func (t Bytes) Read(buf *ReadBuffer, into any) (err error)
User Defined Unmarshaling
Unmarshaling from byte array:
interface{ VampFromBytes([]byte) error } // must copy its argument
Unmarshaling from string:
interface{ VampFromString(string) error }
func (Bytes) ReadBytes ¶ added in v0.5.0
func (t Bytes) ReadBytes(buf *ReadBuffer) []byte
func (Bytes) ReadString ¶ added in v0.5.0
func (t Bytes) ReadString(buf *ReadBuffer) string
func (*Bytes) VampFromUint8 ¶ added in v0.5.0
func (Bytes) VampToUint8 ¶ added in v0.5.0
func (Bytes) VampUnionAlt ¶ added in v0.5.0
type BytesReadMode ¶ added in v0.7.0
type BytesReadMode uint8
const ( // Bytes will be read as string if the bytes are valid UTF-8 CheckUTF8 BytesReadMode = iota // Bytes will be read as []byte ReadRaw // Bytes will be read as string. It is not checked if bytes are valid UTF-8. ReadString )
Bytes Reading Mode
The bytes mode controls how a Vampire array is unmarshaled when the target type is not determined e.g., read into an *any.
type CompareOpts ¶ added in v0.8.0
type CompareOpts int
const ( // Compare takes Record field names into account FieldNames CompareOpts = (1 << iota) // Compare takes Union alt names into account AltNames )
type Fields ¶ added in v0.8.0
type Fields struct {
// contains filtered or unexported fields
}
Fields denotes a set of fields from a Go struct. Fields are computed when a vamp record type is computed by reflection from a Go struct, e.g. with Reflect.Type.
func (Fields) Get ¶ added in v0.8.0
Get copies the field values of from to an any slice and returs it. If reuse is not nil and has sufficient capacty it will be used to store the values. The result of Get is meant to be used with Record.WriteFields.
func (Fields) Set ¶ added in v0.8.0
Set writes pointers to the fields of to to an any slice and returns it. If reuse is not nil and has sufficient capacty it will be used to store the pointers. The result of Set is meant to be used with Record.ReadFields.
type Header ¶
type Header [4]byte
Header is the VMP header that can be written to buffers to mark the data to be Vampire encoded and to provide the offsetSize used for the data.
The VMP header is "VMPx" where x encodes the vampire version and the offset size.s
Example ¶
fmt.Println(HeaderFor(Size16))
Output: Vampire-v0+16bit
func ParseHeader ¶
type List ¶ added in v0.5.0
type List struct {
// contains filtered or unexported fields
}
Example ¶
a := []string{"boo", "bar", "baz"} t := NewList(Size32, NewString(Size16)) fmt.Println(t) buf := NewWriteBuffer(t, Size16, 0, nil) t.Write(buf, a) var out any t.Read(&buf.ReadBuffer, &out) fmt.Printf("%[1]T=%[1]v\n", out)
Output: (Size32 "Size16") []interface {}=[boo bar baz]
func (*List) Next ¶ added in v0.5.0
func (t *List) Next(buf *ReadBuffer) (revert uint, ok bool)
TODO Test ListType.Next()
func (*List) ReadList ¶ added in v0.5.0
func (t *List) ReadList(buf *ReadBuffer, into ListUnmarshaler) error
func (*List) ReadStream ¶ added in v0.5.0
func (*List) VampRecordRead ¶ added in v0.5.0
func (t *List) VampRecordRead(rt *Record, buf *ReadBuffer) error
func (*List) VampRecordWrite ¶ added in v0.5.0
func (*List) VampUnionAlt ¶ added in v0.5.0
func (*List) WriteList ¶ added in v0.5.0
func (t *List) WriteList(buf *Buffer, ls ListMarshaler) error
func (*List) WriteStream ¶ added in v0.5.0
type ListIterator ¶
type ListMarshaler ¶
type ListMarshaler interface {
VampListIter() ListIterator
}
type ListUnmarshaler ¶
type PathElement ¶ added in v0.2.0
type PathError ¶ added in v0.2.0
type PathError struct {
// contains filtered or unexported fields
}
Example ¶
rt := NewRecord( NewString(Size16), Float64, Bool, ) buf := NewWriteBuffer(rt, Size16, 0, nil) err := rt.WriteFields(buf, "next fails", "", true) fmt.Println(err)
Output: .1:vampire cannot write string as float64
type ReadBuffer ¶
type ReadBuffer struct {
// contains filtered or unexported fields
}
ReadBuffer adapts a Vampire encoded byte array to be used for read-only access. One has to use the specific Type that matches the encoded data to read the data from the buffer.
func NewReadBuffer ¶
func NewReadBuffer(t Type, off SizeRange, buf []byte) *ReadBuffer
NewReadBuffer wraps buf for read access when the offsetSize used for the encoded data is known. The first byte of the data of Type t must be buf[0].
func NewReadBufferHeader ¶ added in v0.8.0
func NewReadBufferHeader(t Type, buf []byte, search bool) (*ReadBuffer, error)
NewReadBufferHeader wraps buf for read access when the offsetSize is provided by a VMP Header in buf. When search is true NewReadBufferHeader scans for the first valid VMP header and uses it's offsetSize. Otherwise the VMP header must start at buf[0].
func NewTypeReadBuffer ¶
func NewTypeReadBuffer(buf []byte, search bool) (*ReadBuffer, error)
func NewTypeReadBufferOffSize ¶
func NewTypeReadBufferOffSize(off SizeRange, buf []byte) *ReadBuffer
func (*ReadBuffer) AllBytes ¶
func (b *ReadBuffer) AllBytes() []byte
func (*ReadBuffer) Bytes ¶
func (b *ReadBuffer) Bytes() []byte
func (*ReadBuffer) Offset ¶
func (b *ReadBuffer) Offset() SizeRange
func (*ReadBuffer) Unslice ¶
func (b *ReadBuffer) Unslice(revert uint)
type ReadStream ¶ added in v0.5.0
type ReadStream struct {
// contains filtered or unexported fields
}
func (*ReadStream) Buffer ¶ added in v0.5.0
func (rs *ReadStream) Buffer() *ReadBuffer
func (*ReadStream) Err ¶ added in v0.5.0
func (rs *ReadStream) Err() error
func (*ReadStream) Head ¶ added in v0.5.0
func (rs *ReadStream) Head() (length int64, data []byte, err error)
Head returns the list's length hint and the raw head data. If the list's actual length is unknown or exceeds the SizeRange of the List, the length hint will be -1.
func (*ReadStream) Next ¶ added in v0.5.0
func (rs *ReadStream) Next() bool
func (*ReadStream) Read ¶ added in v0.5.0
func (rs *ReadStream) Read(into any) error
type Record ¶ added in v0.5.0
type Record struct {
// contains filtered or unexported fields
}
TODO Slice to field
Example ¶
r := struct { Name string Age uint8 }{ Name: "John Doe", Age: 44, } t, _, _ := ReflectType(r) fmt.Println(t) buf := NewWriteBuffer(t, Size16, 0, nil) t.Write(buf, r) var out any t.Read(&buf.ReadBuffer, &out) fmt.Printf("%[1]T=%[1]v\n", out)
Output: {Name:"Size16" Age:uint8} map[string]interface {}=map[Age:44 Name:John Doe]
func MustUseStruct ¶ added in v0.8.0
func (*Record) Compare ¶ added in v0.8.0
func (r *Record) Compare(t Type, opts CompareOpts) (bool, int)
func (*Record) Field ¶ added in v0.5.0
func (t *Record) Field(buf *ReadBuffer, i uint) (revert uint)
func (*Record) ReadFields ¶ added in v0.5.0
func (t *Record) ReadFields(buf *ReadBuffer, outs ...any) error
func (*Record) VampArrayFinish ¶ added in v0.5.0
func (*Record) VampArrayLen ¶ added in v0.5.0
func (*Record) VampArrayReader ¶ added in v0.5.0
func (*Record) VampArrayResize ¶ added in v0.5.0
func (*Record) VampArrayWriter ¶ added in v0.5.0
func (*Record) VampUnionAlt ¶ added in v0.5.0
type RecordMarshaler ¶
type RecordUnmarshaler ¶
type RecordUnmarshaler interface {
VampRecordRead(*Record, *ReadBuffer) error
}
type Reflect ¶
type Reflect struct {
// contains filtered or unexported fields
}
func DefaultReflect ¶
func DefaultReflect() Reflect
DefaultReflect uses Size16 for strings and Size32 for arrays.
func (Reflect) MustUseStruct ¶ added in v0.8.0
func (Reflect) StringSize ¶
func (Reflect) UseStruct ¶ added in v0.8.0
UseStruct computes the vamp record type of a Go struct by reflection and calls UseFieldsForStruct on the computed fields for eficcient use without implementing RecordMarshaler and RecordUnmarshaler.
type SizeRange ¶
type SizeRange uint8
SizeRanges are used in vamp to define the valid range for buffer offsets, string length and any sort of ranges of size in byte that is used in Vampire encoding.
func SizeRangeFromBits ¶ added in v0.7.1
func SizeRangeFromMax ¶ added in v0.8.0
func (SizeRange) Check ¶
Check checks if size is withing the SizeRange s. Only if size is not in s' range an error is returned.
func (SizeRange) Max ¶
Max returns the maximum uint of SizeRange s. SizeRange s is expected to be s.Valid()==true.
func (SizeRange) Size ¶
Size returns the number of bytes used for the size range in Vampire encoding. SizeRange s is expected to be s.Valid()==true.
func (*SizeRange) VampFromUint8 ¶
SizeRange can be decoded from a Vampire Uint8.
func (SizeRange) VampToUint8 ¶
SizeRange can be encoded as Vampire Uint8.
type Type ¶
type Type interface { FixSize() uint // fix size of the type in byte DynNo() uint // number of dynamic elements of the type // Write the value to buffer if its structure matches Type Write(b *Buffer, value any) error // Read buffers content into argument if its structure matches Type Read(b *ReadBuffer, into any) error // Compare this type with type t. A type w is less than or equal to type r // if vampire data written by w can be ready with type r. Only if neither w // is less than or equal to r nor r is less than or equal to w is comparable // false. Otherwise, comparison is 0 for equal types, less than 0 if this // type is less than t or greater than 0 if this t is less than this type. Compare(t Type, opts CompareOpts) (comparable bool, comparison int) Hash(h hash.Hash, opts CompareOpts) fmt.Stringer PrintTo(w io.Writer) (int, error) }
Type is the interface of all Vampire types that are used to define the structure of marshaled data. Types can be created as Array, List, Record or Union form other types. Basic types are booleans, signed and unsigned integers with 8, 16, 32 or 64 bit and float with 32 or 64 bit. The special Bytes types are used to hold any binary data, especially UTF-8 strings. Finally there is the Any type that can hold any Vampire value as a tuple of a value and its type.
func Anonymized ¶ added in v0.7.0
Anonymized returns the unnamed type of t. If t is already unnamed it is returned.
func Named ¶ added in v0.4.0
Named attaches a name to a type. Type names are irrelevant for the encoded data end exist for documentation only. Passing a named type to the constructors of record or union types will assign the name to the respective record field or union variant.
func ParseTypeString ¶ added in v0.7.0
Example ¶
typ, _ := ParseTypeString(`<Size8 Foo:[Size8 "Size8"] | float32 | {ID:uint32 Name:"Size16"} >`) fmt.Println(typ)
Output: <Size8 Foo:[Size8 "Size8"]|float32|{ID:uint32 Name:"Size16"}>
func ReadType ¶
func ReadType(buf *ReadBuffer) (t Type, err error)
func ReadTypeFile ¶ added in v0.7.1
type Union ¶ added in v0.5.0
type Union struct { // When not nil it will be used in [Union.Read] to select the read target // depending on the union's alt field. The original into argument of the // Read method is passed to AltTarget and the returend reader then is used // to read the alt type into. If reader is nil the union is skipped. AltTarget func(alt uint, into any) (reader any, err error) // contains filtered or unexported fields }
Example ¶
type address struct { Street string No string } type geocoos struct { Lat, Lon float64 } addrType, _, _ := ReflectType(address{}) geoType, _, _ := ReflectType(geocoos{}) placeType := NewUnion(Size8, addrType, MustNamed("geo", geoType), ) fmt.Println(placeType) buf := NewWriteBuffer(placeType, Size16, 0, nil) placeType.Write(buf, Alt{S: 0, V: address{Street: "Justroad", No: "33a"}}) var out any placeType.Read(&buf.ReadBuffer, &out) fmt.Printf("%[1]T=%[1]v\n", out) buf.Reset(placeType, Size16, 0) placeType.Write(buf, Alt{S: 1, V: geocoos{Lat: 33.321, Lon: 44.123}}) placeType.Read(&buf.ReadBuffer, &out) fmt.Printf("%[1]T=%[1]v\n", out)
Output: <Size8 {Street:"Size16" No:"Size16"}|geo:{Lat:float64 Lon:float64}> map[string]interface {}=map[No:33a Street:Justroad] map[string]interface {}=map[Lat:33.321 Lon:44.123]
func (*Union) Compare ¶ added in v0.8.0
func (u *Union) Compare(t Type, opts CompareOpts) (bool, int)
func (*Union) SetAltReader ¶ added in v0.7.0
func (*Union) VampRecordRead ¶ added in v0.5.0
func (t *Union) VampRecordRead(rt *Record, buf *ReadBuffer) error
func (*Union) VampRecordWrite ¶ added in v0.5.0
func (*Union) VampUnionAlt ¶ added in v0.5.0
type UnionMarshaler ¶
type UnionUnmarshaler ¶
type WriteStream ¶ added in v0.5.0
type WriteStream struct {
// contains filtered or unexported fields
}
func (*WriteStream) Flush ¶ added in v0.5.0
func (ws *WriteStream) Flush() error
func (*WriteStream) Write ¶ added in v0.5.0
func (ws *WriteStream) Write(elem any) error
func (*WriteStream) WriteLast ¶ added in v0.5.0
func (ws *WriteStream) WriteLast(elem any) error
func (*WriteStream) WriteWithNext ¶ added in v0.5.0
func (ws *WriteStream) WriteWithNext(elem any) error
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
vamp
The vamp CLI tool converts type definitions between vamp and text encoding and outputs vamp data as JSON.
|
The vamp CLI tool converts type definitions between vamp and text encoding and outputs vamp data as JSON. |
examples
|
|
cs
Package cs demonstartes how to use Vampire for client/server protocol.
|
Package cs demonstartes how to use Vampire for client/server protocol. |
polymorphic
Package polymorphic demonstartes how to use unions.
|
Package polymorphic demonstartes how to use unions. |
selfcontained
Package selfcontained demonstrates how to use the any type to create a self-contained vmp file that can be read without an external type definition.
|
Package selfcontained demonstrates how to use the any type to create a self-contained vmp file that can be read without an external type definition. |