Documentation
¶
Overview ¶
encoding exposes functionality to convert between an arbitray maps and native Go structs in service of the Vanflow protocol.
The Vanflow protocol uses the amqp-value section to represent record types in map structures. Typically go amqp clients perform the marshaling to and from byte representation to some arbitrary map types, usually map[any]any. This package provides mapping to and from that map type to Go types.
All vanflow record types have an associated record type codepoint. Types need to be registered with that codepoint using MustRegisterRecord before they can be used.
When decoding to a struct, encoding will look for `vflow` struct fields to establish a mapping between vanflow record attribute codepoints and struct fields. If a record attribute is required as part of the record, appending ",required" to the vflow tag value will enforce that the field is set.
For Example:
type ExampleRecord struct { ExampleID int64 `vflow:"99,required"` ExampleVal string `vflow:"100"` }
The associated record attribute set would look like this:
map[any]any{ uint32(0): ExampleRecordCodepoint, uint32(99): int64(1), uint32(100): "test", }
Index ¶
Constants ¶
const ( // ErrAttributeNotSet can be returned from a RecordAttributeEncoder // when the field should not be set ErrAttributeNotSet = encodingError("record attribute not set") )
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
func Decode(recordset RecordAttributeSet) (interface{}, error)
Decode a record from its record attribute set. Decode is only aware of record types registered with MustRegisterRecord. Input must be a map type containing uint32 keys (map[uint32]X or map[any]X)
func MustRegisterRecord ¶
func MustRegisterRecord(codepoint uint32, record interface{})
MustRegisterRecord registers a record type for Encoding/Decoding panics if the type is not compatible, or if the codepoint or type has already been registered.
Types ¶
type RecordAttributeDecoder ¶
type RecordAttributeDecoder interface {
DecodeRecordAttribute(attr interface{}) error
}
RecordAttributeDecoder is implemented by record attribute types that can handle their own decoding.
type RecordAttributeEncoder ¶
type RecordAttributeEncoder interface {
EncodeRecordAttribute() (interface{}, error)
}
RecordAttributeEncoder is implemented by record attribute types to overwrite the default encoding behavior
type RecordAttributeSet ¶
type RecordAttributeSet = map[interface{}]interface{}
func Encode ¶
func Encode(record any) (RecordAttributeSet, error)
Encode a record into a record attribute set so that it can be sent over the vanflow protocol. Only records types that have been registered with MustRegisterRecord can be encoded.