Documentation ¶
Overview ¶
Package protowire parses and formats the raw wire encoding. See https://developers.google.com/protocol-buffers/docs/encoding.
For marshaling and unmarshaling entire protobuf messages, use the "google.golang.org/protobuf/proto" package instead.
Index ¶
- func AppendBytes(b []byte, v []byte) []byte
- func AppendFixed32(b []byte, v uint32) []byte
- func AppendFixed64(b []byte, v uint64) []byte
- func AppendGroup(b []byte, num FieldNumber, v []byte) []byte
- func AppendString(b []byte, v string) []byte
- func AppendTag(b []byte, num FieldNumber, typ ProtobufType) []byte
- func AppendVarint(b []byte, v uint64) []byte
- func ConsumeBytes(b []byte) (v []byte, n int)
- func ConsumeField(b []byte) (FieldNumber, ProtobufType, int)
- func ConsumeFieldValue(num FieldNumber, typ ProtobufType, b []byte) (n int)
- func ConsumeFixed32(b []byte) (v uint32, n int)
- func ConsumeFixed64(b []byte) (v uint64, n int)
- func ConsumeGroup(num FieldNumber, b []byte) (v []byte, n int)
- func ConsumeString(b []byte) (v string, n int)
- func ConsumeTag(b []byte) (FieldNumber, ProtobufType, int)
- func ConsumeVarint(b []byte) (v uint64, n int)
- func DecodeBool(x uint64) bool
- func DecodeTag(x uint64) (FieldNumber, ProtobufType)
- func DecodeZigZag(x uint64) int64
- func EncodeBool(x bool) uint64
- func EncodeTag(num FieldNumber, typ ProtobufType) uint64
- func EncodeZigZag(x int64) uint64
- func ParseError(n int) error
- func SizeBytes(n int) int
- func SizeFixed32() int
- func SizeFixed64() int
- func SizeGroup(num FieldNumber, n int) int
- func SizeTag(num FieldNumber) int
- func SizeVarint(v uint64) int
- type FieldNumber
- type ProtobufType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendBytes ¶
AppendBytes appends v to b as a length-prefixed bytes value.
func AppendFixed32 ¶
AppendFixed32 appends v to b as a little-endian uint32.
func AppendFixed64 ¶
AppendFixed64 appends v to b as a little-endian uint64.
func AppendGroup ¶
func AppendGroup(b []byte, num FieldNumber, v []byte) []byte
AppendGroup appends v to b as group value, with a trailing end group marker. The value v must not contain the end marker.
func AppendString ¶
AppendString appends v to b as a length-prefixed bytes value.
func AppendTag ¶
func AppendTag(b []byte, num FieldNumber, typ ProtobufType) []byte
AppendTag encodes num and typ as a varint-encoded tag and appends it to b.
func AppendVarint ¶
AppendVarint appends v to b as a varint-encoded uint64.
func ConsumeBytes ¶
ConsumeBytes parses b as a length-prefixed bytes value, reporting its length. This returns a negative length upon an error (see ParseError).
func ConsumeField ¶
func ConsumeField(b []byte) (FieldNumber, ProtobufType, int)
ConsumeField parses an entire field record (both tag and value) and returns the field number, the wire type, and the total length. This returns a negative length upon an error (see ParseError).
The total length includes the tag header and the end group marker (if the field is a group).
func ConsumeFieldValue ¶
func ConsumeFieldValue(num FieldNumber, typ ProtobufType, b []byte) (n int)
ConsumeFieldValue parses a field value and returns its length. This assumes that the field FieldNumber and wire ProtobufType have already been parsed. This returns a negative length upon an error (see ParseError).
When parsing a group, the length includes the end group marker and the end group is verified to match the starting field number.
func ConsumeFixed32 ¶
ConsumeFixed32 parses b as a little-endian uint32, reporting its length. This returns a negative length upon an error (see ParseError).
func ConsumeFixed64 ¶
ConsumeFixed64 parses b as a little-endian uint64, reporting its length. This returns a negative length upon an error (see ParseError).
func ConsumeGroup ¶
func ConsumeGroup(num FieldNumber, b []byte) (v []byte, n int)
ConsumeGroup parses b as a group value until the trailing end group marker, and verifies that the end marker matches the provided num. The value v does not contain the end marker, while the length does contain the end marker. This returns a negative length upon an error (see ParseError).
func ConsumeString ¶
ConsumeString parses b as a length-prefixed bytes value, reporting its length. This returns a negative length upon an error (see ParseError).
func ConsumeTag ¶
func ConsumeTag(b []byte) (FieldNumber, ProtobufType, int)
ConsumeTag parses b as a varint-encoded tag, reporting its length. This returns a negative length upon an error (see ParseError).
func ConsumeVarint ¶
ConsumeVarint parses b as a varint-encoded uint64, reporting its length. This returns a negative length upon an error (see ParseError).
func DecodeBool ¶
DecodeBool decodes a uint64 as a bool.
Input: { 0, 1, 2, …} Output: {false, true, true, …}
func DecodeTag ¶
func DecodeTag(x uint64) (FieldNumber, ProtobufType)
DecodeTag decodes the field FieldNumber and wire ProtobufType from its unified form. The FieldNumber is -1 if the decoded field number overflows int32. Other than overflow, this does not check for field number validity.
func DecodeZigZag ¶
DecodeZigZag decodes a zig-zag-encoded uint64 as an int64.
Input: {…, 5, 3, 1, 0, 2, 4, 6, …} Output: {…, -3, -2, -1, 0, +1, +2, +3, …}
func EncodeTag ¶
func EncodeTag(num FieldNumber, typ ProtobufType) uint64
EncodeTag encodes the field FieldNumber and wire ProtobufType into its unified form.
func EncodeZigZag ¶
EncodeZigZag encodes an int64 as a zig-zag-encoded uint64.
Input: {…, -3, -2, -1, 0, +1, +2, +3, …} Output: {…, 5, 3, 1, 0, 2, 4, 6, …}
func ParseError ¶
ParseError converts an error code into an error value. This returns nil if n is a non-negative number.
func SizeBytes ¶
SizeBytes returns the encoded size of a length-prefixed bytes value, given only the length.
func SizeFixed32 ¶
func SizeFixed32() int
SizeFixed32 returns the encoded size of a fixed32; which is always 4.
func SizeFixed64 ¶
func SizeFixed64() int
SizeFixed64 returns the encoded size of a fixed64; which is always 8.
func SizeGroup ¶
func SizeGroup(num FieldNumber, n int) int
SizeGroup returns the encoded size of a group, given only the length.
func SizeTag ¶
func SizeTag(num FieldNumber) int
func SizeVarint ¶
SizeVarint returns the encoded size of a varint. The size is guaranteed to be within 1 and 10, inclusive.
Types ¶
type FieldNumber ¶
type FieldNumber int32
FieldNumber represents the field number.
const ( MinValidNumber FieldNumber = 1 FirstReservedNumber FieldNumber = 19000 LastReservedNumber FieldNumber = 19999 MaxValidNumber FieldNumber = 1<<29 - 1 )
func (FieldNumber) IsValid ¶
func (n FieldNumber) IsValid() bool
IsValid reports whether the field number is semantically valid.
Note that while numbers within the reserved range are semantically invalid, they are syntactically valid in the wire format. Implementations may treat records with reserved field numbers as unknown.
type ProtobufType ¶
type ProtobufType int8
ProtobufType represents the wire type.
const ( VarintType ProtobufType = 0 Fixed32Type ProtobufType = 5 Fixed64Type ProtobufType = 1 BytesType ProtobufType = 2 StartGroupType ProtobufType = 3 EndGroupType ProtobufType = 4 )