Back to

Package codec

v0.0.0 (2c305a9)
Latest Go to latest
Published: May 1, 2020 | Licenses: MIT , Apache-2.0 | Module:


Package codec contains all the logic required for interacting with the protobuf raw encoding. It also contains all the encoding and field type specific constants required for using the molecule library.


Package Files


var ErrBadWireType = errors.New("proto: bad wiretype")

ErrBadWireType is returned when decoding a wire-type from a buffer that is not valid.

var ErrOverflow = errors.New("proto: integer overflow")

ErrOverflow is returned when an integer is too large to be represented.

func DecodeZigZag32

func DecodeZigZag32(v uint64) int32

DecodeZigZag32 decodes a signed 32-bit integer from the given zig-zag encoded value.

func DecodeZigZag64

func DecodeZigZag64(v uint64) int64

DecodeZigZag64 decodes a signed 64-bit integer from the given zig-zag encoded value.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields

Buffer is a reader and a writer that wraps a slice of bytes and also provides API for decoding and encoding the protobuf binary format.

Its operation is similar to that of a bytes.Buffer: writing pushes data to the end of the buffer while reading pops data from the head of the buffer. So the same buffer can be used to both read and write.

func NewBuffer

func NewBuffer(buf []byte) *Buffer

NewBuffer creates a new buffer with the given slice of bytes as the buffer's initial contents.

func (*Buffer) Bytes

func (cb *Buffer) Bytes() []byte

Bytes returns the slice of bytes remaining in the buffer. Note that this does not perform a copy: if the contents of the returned slice are modified, the modifications will be visible to subsequent reads via the buffer.

func (*Buffer) DecodeFixed32

func (cb *Buffer) DecodeFixed32() (x uint64, err error)

DecodeFixed32 reads a 32-bit integer from the Buffer. This is the format for the fixed32, sfixed32, and float protocol buffer types.

func (*Buffer) DecodeFixed64

func (cb *Buffer) DecodeFixed64() (x uint64, err error)

DecodeFixed64 reads a 64-bit integer from the Buffer. This is the format for the fixed64, sfixed64, and double protocol buffer types.

func (*Buffer) DecodeRawBytes

func (cb *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error)

DecodeRawBytes reads a count-delimited byte buffer from the Buffer. This is the format used for the bytes protocol buffer type and for embedded messages.

func (*Buffer) DecodeTagAndWireType

func (cb *Buffer) DecodeTagAndWireType() (tag int32, wireType WireType, err error)

DecodeTagAndWireType decodes a field tag and wire type from input. This reads a varint and then extracts the two fields from the varint value read.

func (*Buffer) DecodeVarint

func (cb *Buffer) DecodeVarint() (uint64, error)

DecodeVarint reads a varint-encoded integer from the Buffer. This is the format for the int32, int64, uint32, uint64, bool, and enum protocol buffer types.

func (*Buffer) EOF

func (cb *Buffer) EOF() bool

EOF returns true if there are no more bytes remaining to read.

func (*Buffer) Len

func (cb *Buffer) Len() int

Len returns the remaining number of bytes in the buffer.

func (*Buffer) Read

func (cb *Buffer) Read(dest []byte) (int, error)

Read implements the io.Reader interface. If there are no bytes remaining in the buffer, it will return 0, io.EOF. Otherwise, it reads max(len(dest), cb.Len()) bytes from input and copies them into dest. It returns the number of bytes copied and a nil error in this case.

func (*Buffer) ReadGroup

func (cb *Buffer) ReadGroup(alloc bool) ([]byte, error)

ReadGroup reads the input until a "group end" tag is found and returns the data up to that point. Subsequent reads from the buffer will read data after the group end tag. If alloc is true, the data is copied to a new slice before being returned. Otherwise, the returned slice is a view into the buffer's underlying byte slice.

This function correctly handles nested groups: if a "group start" tag is found, then that group's end tag will be included in the returned data.

func (*Buffer) Reset

func (cb *Buffer) Reset(buf []byte)

Reset resets this buffer back to empty. Any subsequent writes/encodes to the buffer will allocate a new backing slice of bytes.

func (*Buffer) Skip

func (cb *Buffer) Skip(count int) error

Skip attempts to skip the given number of bytes in the input. If the input has fewer bytes than the given count, false is returned and the buffer is unchanged. Otherwise, the given number of bytes are skipped and true is returned.

func (*Buffer) SkipGroup

func (cb *Buffer) SkipGroup() error

SkipGroup is like ReadGroup, except that it discards the data and just advances the buffer to point to the input right *after* the "group end" tag.

type FieldType

type FieldType int32

FieldType represents a protobuf field type.

const (
	FieldType_DOUBLE   FieldType = 1
	FieldType_FLOAT    FieldType = 2
	FieldType_INT64    FieldType = 3
	FieldType_UINT64   FieldType = 4
	FieldType_INT32    FieldType = 5
	FieldType_FIXED64  FieldType = 6
	FieldType_FIXED32  FieldType = 7
	FieldType_BOOL     FieldType = 8
	FieldType_STRING   FieldType = 9
	FieldType_GROUP    FieldType = 10
	FieldType_MESSAGE  FieldType = 11
	FieldType_BYTES    FieldType = 12
	FieldType_UINT32   FieldType = 13
	FieldType_ENUM     FieldType = 14
	FieldType_SFIXED32 FieldType = 15
	FieldType_SFIXED64 FieldType = 16
	FieldType_SINT32   FieldType = 17
	FieldType_SINT64   FieldType = 18

type WireType

type WireType int8

WireType represents a protobuf encoding wire type.

const (
	WireVarint     WireType = 0
	WireFixed64    WireType = 1
	WireBytes      WireType = 2
	WireStartGroup WireType = 3
	WireEndGroup   WireType = 4
	WireFixed32    WireType = 5

Constants that identify the encoding of a value on the wire.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier