Package protobuf (v1.6.2) [up] [repository]

encoder.go

errors.go

model.go

wire.go

Index

Variables

var (
	// ErrOutOfBounds is returned when attempting to read past the end of the buffer.
	ErrOutOfBounds = errors.New("data is out of bounds")

	// ErrMalformedVarint is returned when a varint is unterminated or overflows.
	ErrMalformedVarint = errors.New("malformed varint")

	// ErrBufferTooSmall is returned when a buffer does not contain enough bytes for a fixed-size value.
	ErrBufferTooSmall = errors.New("buffer is too small")

	// ErrInvalidWireType is returned when an unknown or unsupported wire type is encountered.
	ErrInvalidWireType = errors.New("invalid wire type")
)

Functions

func DecodeFixed32(buffer []byte) (uint32, int, error)

DecodeFixed32 decodes a 32-bit little-endian integer from the buffer.

func DecodeFixed64(buffer []byte) (uint64, int, error)

DecodeFixed64 decodes a 64-bit little-endian integer from the buffer.

func DecodeLengthPrefixed(buffer []byte) (uint64, int, error)

DecodeLengthPrefixed decodes a length-prefixed field from the buffer. It returns the length of the data, the number of bytes read for the length header, and an error if any.

func DecodeVarint(buffer []byte) (uint64, int)

DecodeVarint reads a varint from the buffer and returns the decoded uint64 and the number of bytes read. A negative number of bytes indicates an overflow. A zero indicates an unterminated varint.

func EncodeFixed32(value uint32) []byte

EncodeFixed32 encodes a uint32 into 4 bytes (little-endian).

func EncodeFixed64(value uint64) []byte

EncodeFixed64 encodes a uint64 into 8 bytes (little-endian).

func EncodeVarint(value uint64) []byte

EncodeVarint encodes a uint64 into varint bytes.

Types

type Field

Field represents a single, decoded field in a protobuf message.

type Field struct {
	Tag     Tag
	Numeric uint64
	Bytes   []byte
	Message Message
}

Functions

func Bytes(fieldNum uint32, value []byte) *Field

Bytes creates a new Bytes field and returns a pointer to it.

func Embed(fieldNum uint32, value ...*Field) *Field

Embed creates a new embedded message field from the provided sub-fields.

func Fixed32(fieldNum uint32, value uint32) *Field

Fixed32 creates a new Fixed32 field and returns a pointer to it.

func Fixed64(fieldNum uint32, value uint64) *Field

Fixed64 creates a new Fixed64 field and returns a pointer to it.

func String(fieldNum uint32, value string) *Field

String creates a new String (WireBytes) field and returns a pointer to it.

func Varint(fieldNum uint32, value uint64) *Field

Varint creates a new Varint field and returns a pointer to it.

type Iterator

Iterator provides a stateful, memory-efficient way to loop over all occurrences of a specific field number within a message.

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

Methods

func (it *Iterator) Field() *Field

Field returns a pointer to the current field the iterator is pointing to.

func (it *Iterator) Next() bool

Next advances the iterator to the next matching field. It returns false when there are no more matching fields.

type Message

Message is a named type for a slice of field pointers, representing a decoded protobuf message.

type Message []*Field

Functions

func DecodeMessage(data []byte) (Message, error)

DecodeMessage populates a message by decoding the protobuf wire format data.

Methods

func (m Message) Encode() ([]byte, error)

Encode serializes the message into the protobuf wire format.

func (m Message) Field(fieldNum uint32) (*Field, bool)

Field finds and returns the first field matching the given field number. The boolean return value is false if no matching field is found.

func (m Message) Iterator(fieldNum uint32) *Iterator

Iterator creates a new iterator to loop over all fields with the given number.

type Tag

Tag represents a field's tag (Field Number + Wire Type).

type Tag struct {
	Number uint32
	Type   Type
}

Functions

func DecodeTag(buffer []byte) (Tag, int, error)

DecodeTag decodes a varint from the input buffer and returns it as a Tag struct.

type Type

Type represents the type of data encoding on the wire.

type Type uint8

Constants

const (
	WireVarint     Type = 0
	WireFixed64    Type = 1
	WireBytes      Type = 2
	WireStartGroup Type = 3 // Deprecated
	WireEndGroup   Type = 4 // Deprecated
	WireFixed32    Type = 5
)