Documentation
¶
Overview ¶
Package sbe provides SBE (Simple Binary Encoding) marshaling for protobuf messages.
Messages are encoded following the FIX SBE standard. Proto descriptors with custom SBE annotations define the message layout:
option (sbe.schema_id) = 1;
option (sbe.version) = 0;
message Order {
option (sbe.template_id) = 1;
uint64 order_id = 1;
string symbol = 2 [(sbe.length) = 8];
}
A Codec is created from proto file descriptors and produces standard SBE binary compatible with any SBE implementation.
Supported proto field types:
- Scalar integers (int32, int64, uint32, uint64, sint32, sint64, fixed*, sfixed*)
- bool, float, double, enum
- string and bytes (require [(sbe.length)] annotation for fixed size)
- Nested messages (encoded as SBE composites, inlined at fixed offsets)
- repeated messages (encoded as SBE repeating groups)
Unsupported: map fields, oneof, repeated scalars.
Concurrency ¶
A Codec is safe for concurrent use after construction. Its internal template tables are read-only once NewCodec returns.
A View returned by Codec.View is a value; copies are independent and safe to read from multiple goroutines, provided the underlying buffer is not mutated. View accessors read from that buffer directly — see the View documentation for the trust model.
Encoder and Decoder (the SOFH-framed stream wrappers) hold a reusable scratch buffer and are not safe for concurrent use; create one per goroutine, or guard with a mutex.
Index ¶
- Constants
- Variables
- func ProtoToXML(files ...protoreflect.FileDescriptor) ([]byte, error)
- func XMLToProto(xmlData []byte) ([]byte, error)
- type Codec
- func (c *Codec) Marshal(msg proto.Message) ([]byte, error)
- func (c *Codec) NewDecoder(r io.Reader) *Decoder
- func (c *Codec) NewEncoder(w io.Writer) *Encoder
- func (c *Codec) Unmarshal(data []byte, msg proto.Message) error
- func (c *Codec) UnmarshalDescriptor(data []byte, desc protoreflect.MessageDescriptor) (*dynamicpb.Message, error)
- func (c *Codec) View(data []byte) (View, error)
- type Decoder
- type Encoder
- type GroupView
- type View
- func (v View) Bool(name string) bool
- func (v View) Bytes(name string) []byte
- func (v View) Composite(name string) View
- func (v View) Enum(name string) int
- func (v View) Float(name string) float64
- func (v View) Group(name string) GroupView
- func (v View) Int(name string) int64
- func (v View) String(name string) string
- func (v View) Uint(name string) uint64
- type XMLComposite
- type XMLEnum
- type XMLField
- type XMLGroup
- type XMLMessage
- type XMLRef
- type XMLSchema
- type XMLType
- type XMLTypes
- type XMLValidValue
Constants ¶
const DefaultMaxFrameSize = 16 << 20
DefaultMaxFrameSize caps the SBE body size at 16 MiB by default.
Variables ¶
var ErrFramingCorrupt = errors.New("sbe: stream framing corrupt")
ErrFramingCorrupt is returned when the SOFH framing is invalid (wrong encoding type, length less than the header, frame larger than MaxFrameSize, etc.). The byte stream is desynchronized after this error and the decoder is permanently unusable.
Functions ¶
func ProtoToXML ¶
func ProtoToXML(files ...protoreflect.FileDescriptor) ([]byte, error)
ProtoToXML converts proto file descriptors with SBE annotations to an SBE XML schema.
func XMLToProto ¶
XMLToProto converts an SBE XML schema to a .proto file with SBE annotations.
Types ¶
type Codec ¶
type Codec struct {
// contains filtered or unexported fields
}
Codec encodes and decodes proto.Message values using the SBE binary format. A Codec is safe for concurrent use after creation.
func NewCodec ¶
func NewCodec(files ...protoreflect.FileDescriptor) (*Codec, error)
NewCodec creates an SBE codec from proto file descriptors. Each file must have (sbe.schema_id) and (sbe.version) file options, and each message to be encoded must have an (sbe.template_id) message option.
func (*Codec) UnmarshalDescriptor ¶
func (c *Codec) UnmarshalDescriptor(data []byte, desc protoreflect.MessageDescriptor) (*dynamicpb.Message, error)
UnmarshalDescriptor decodes SBE binary into a new dynamicpb.Message.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads SOFH-framed SBE messages. The internal scratch buffer is reused across Decode calls; Codec.Unmarshal copies parsed values into the destination message, so the buffer is the decoder's to overwrite.
func (*Decoder) SetMaxFrameSize ¶
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes SBE messages framed with a Simple Open Framing Header.
type GroupView ¶
type GroupView struct {
// contains filtered or unexported fields
}
GroupView provides access to entries in an SBE repeating group.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View provides zero-allocation read access to SBE-encoded data. Field values are read directly from the underlying byte buffer.
Strings returned by View.String are backed by the original buffer (via unsafe.String) and are only valid while that buffer is alive.
Trust model ¶
View is designed for the hot path and trades safety for speed. Once the constructing Codec.View call returns successfully, accessor methods (View.Int, View.Uint, View.String, View.Composite, View.Group, etc.) panic on schema mismatch — unknown field name, wrong primitive type, or a field accessed as the wrong category. They are intended for callers that own both the schema and the buffer.
Do not call View accessors with arbitrary, attacker-controlled bytes. For untrusted input, use Codec.Unmarshal or Codec.UnmarshalDescriptor, which return an error on every malformed-input path. View's constructor validates the message header and rejects buffers shorter than the template's declared block length, but accessor-level checks are intentionally skipped.
func (View) Group ¶
Group returns a GroupView for a repeating group field. Groups are located by walking the data sequentially from the end of the root block.
type XMLComposite ¶
type XMLComposite struct {
Name string `xml:"name,attr"`
Description string `xml:"description,attr,omitempty"`
Types []XMLType `xml:"type"`
Refs []XMLRef `xml:"ref"`
}
XMLComposite represents an SBE composite type definition.
type XMLEnum ¶
type XMLEnum struct {
Name string `xml:"name,attr"`
EncodingType string `xml:"encodingType,attr"`
Description string `xml:"description,attr,omitempty"`
ValidValues []XMLValidValue `xml:"validValue"`
}
XMLEnum represents an SBE enum type definition.
type XMLField ¶
type XMLField struct {
Name string `xml:"name,attr"`
ID uint32 `xml:"id,attr"`
Type string `xml:"type,attr"`
}
XMLField represents a field within an SBE message or group.
type XMLGroup ¶
type XMLGroup struct {
Name string `xml:"name,attr"`
ID uint32 `xml:"id,attr"`
Fields []XMLField `xml:"field"`
}
XMLGroup represents an SBE repeating group.
type XMLMessage ¶
type XMLMessage struct {
Name string `xml:"name,attr"`
ID uint32 `xml:"id,attr"`
Description string `xml:"description,attr,omitempty"`
Fields []XMLField `xml:"field"`
Groups []XMLGroup `xml:"group"`
}
XMLMessage represents an SBE message (template).
type XMLSchema ¶
type XMLSchema struct {
XMLName xml.Name `xml:"messageSchema"`
Package string `xml:"package,attr"`
ID uint32 `xml:"id,attr"`
Version uint32 `xml:"version,attr"`
ByteOrder string `xml:"byteOrder,attr"`
Description string `xml:"description,attr"`
Types XMLTypes `xml:"types"`
Messages []XMLMessage `xml:"message"`
}
XMLSchema represents an SBE XML message schema.
func ParseXMLSchema ¶
ParseXMLSchema parses an SBE XML schema.
type XMLType ¶
type XMLType struct {
Name string `xml:"name,attr"`
PrimitiveType string `xml:"primitiveType,attr"`
Length uint32 `xml:"length,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
}
XMLType represents an SBE simple type definition.
type XMLTypes ¶
type XMLTypes struct {
Types []XMLType `xml:"type"`
Composites []XMLComposite `xml:"composite"`
Enums []XMLEnum `xml:"enum"`
}
XMLTypes holds the type definitions within an SBE schema.
type XMLValidValue ¶
XMLValidValue represents one value in an SBE enum.