Bool is a boolean.
type Bool bool
Bytes is a length-prefixed bytes.
type Bytes []byte
Denormalized is a denormalized varint value, where a varint is encoded using more bytes than is strictly necessary. The number of extra bytes alone is sufficient to losslessly represent the denormalized varint.
The value may be one of Tag, Bool, Varint, Svarint, or Uvarint, where the varint representation of each token is denormalized.
Alternatively, the value may be one of String, Bytes, or LengthPrefix, where the varint representation of the length-prefix is denormalized.
type Denormalized struct { Count uint // number of extra bytes Value Token }
Float32 is a 32-bit fixed-width floating point number.
type Float32 float32
Float64 is a 64-bit fixed-width floating point number.
type Float64 float64
Int32 is a signed 32-bit fixed-width integer.
type Int32 int32
Int64 is a signed 64-bit fixed-width integer.
type Int64 int64
LengthPrefix is a length-prefixed message.
type LengthPrefix Message
Message is an ordered sequence of Token values, where certain tokens may contain other tokens. It is functionally a concrete syntax tree that losslessly represents any arbitrary wire data (including invalid input).
type Message []Token
func (m Message) Format(s fmt.State, r rune)
Format implements a custom formatter to visualize the syntax tree. Using "%#v" formats the Message in Go source code.
func (m Message) Marshal() []byte
Marshal encodes a syntax tree into the protobuf wire format.
Example message definition:
message MyMessage { string field1 = 1; int64 field2 = 2; repeated float32 field3 = 3; }
Example encoded message:
b := Message{ Tag{1, BytesType}, String("Hello, world!"), Tag{2, VarintType}, Varint(-10), Tag{3, BytesType}, LengthPrefix{ Float32(1.1), Float32(2.2), Float32(3.3), }, }.Marshal()
Resulting wire data:
0x0000 0a 0d 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 10 |..Hello, world!.| 0x0010 f6 ff ff ff ff ff ff ff ff 01 1a 0c cd cc 8c 3f |...............?| 0x0020 cd cc 0c 40 33 33 53 40 |...@33S@|
func (m Message) Size() int
Size reports the size in bytes of the marshaled message.
func (m *Message) Unmarshal(in []byte)
Unmarshal parses the input protobuf wire data as a syntax tree. Any parsing error results in the remainder of the input being concatenated to the message as a Raw type.
Each tag (a tuple of the field number and wire type) encountered is inserted into the syntax tree as a Tag.
The contents of each wire type is mapped to the following Go types:
Since the wire format is not self-describing, this function cannot parse sub-messages and will leave them as the Bytes type. Further manual parsing can be performed as such:
var m, m1, m2 Message m.Unmarshal(b) m1.Unmarshal(m[3].(Bytes)) m[3] = LengthPrefix(m1) m2.Unmarshal(m[3].(LengthPrefix)[1].(Bytes)) m[3].(LengthPrefix)[1] = LengthPrefix(m2)
Unmarshal is useful for debugging the protobuf wire format.
func (m *Message) UnmarshalAbductive(in []byte, desc protoreflect.MessageDescriptor)
UnmarshalAbductive is like Message.UnmarshalDescriptor, but infers abductively whether any unknown bytes values is a message based on whether it is a syntactically well-formed message.
Note that the protobuf wire format is not fully self-describing, so abductive inference may attempt to expand a bytes value as a message that is not actually a message. It is a best-effort guess.
func (m *Message) UnmarshalDescriptor(in []byte, desc protoreflect.MessageDescriptor)
UnmarshalDescriptor parses the input protobuf wire data as a syntax tree using the provided message descriptor for more accurate parsing of fields. It operates like Message.Unmarshal, but may use a wider range of Go types to represent the wire data.
The contents of each wire type is mapped to one of the following Go types:
If the field is unknown, it uses the same mapping as Message.Unmarshal. Known sub-messages are parsed as a Message and packed repeated fields are parsed as a LengthPrefix.
Number is the field number; aliased from the protowire package for convenience.
type Number = protowire.Number
Number type constants; copied from the protowire package for convenience.
const ( MinValidNumber Number = protowire.MinValidNumber FirstReservedNumber Number = protowire.FirstReservedNumber LastReservedNumber Number = protowire.LastReservedNumber MaxValidNumber Number = protowire.MaxValidNumber )
Raw are bytes directly appended to output.
type Raw []byte
String is a length-prefixed string.
type String string
Svarint is a signed varint using zig-zag encoding.
type Svarint int64
Tag is a tuple of the field number and the wire type.
type Tag struct { Number Number Type Type }
Token is any other type (e.g., Message, Tag, Varint, Float32, etc).
type Token token
Type is the wire type; aliased from the protowire package for convenience.
type Type = protowire.Type
Wire type constants; copied from the protowire package for convenience.
const ( VarintType Type = protowire.VarintType Fixed32Type Type = protowire.Fixed32Type Fixed64Type Type = protowire.Fixed64Type BytesType Type = protowire.BytesType StartGroupType Type = protowire.StartGroupType EndGroupType Type = protowire.EndGroupType )
Uint32 is an unsigned 32-bit fixed-width integer.
type Uint32 uint32
Uint64 is an unsigned 64-bit fixed-width integer.
type Uint64 uint64
Uvarint is a unsigned varint.
type Uvarint uint64
Varint is a signed varint using 64-bit two's complement encoding.
type Varint int64