Package proto
Overview ▹
Index ▹
Constants
Constants that identify the encoding of a value on the wire.
const ( WireVarint = 0 WireFixed64 = 1 WireBytes = 2 WireStartGroup = 3 WireEndGroup = 4 WireFixed32 = 5 )
ProtoPackageIsVersion1 is referenced from generated protocol buffer files to assert that that code is compatible with this version of the proto package.
const GoGoProtoPackageIsVersion1 = true
ProtoPackageIsVersion2 is referenced from generated protocol buffer files to assert that that code is compatible with this version of the proto package.
const GoGoProtoPackageIsVersion2 = true
Variables
var ( // ErrNil is the error returned if Marshal is called with nil. ErrNil = errors.New("proto: Marshal called with nil") // ErrTooLarge is the error returned if Marshal is called with a // message that encodes to >2GB. ErrTooLarge = errors.New("proto: message encodes to over 2 GB") )
ErrInternalBadWireType is returned by generated code when an incorrect wire type is encountered. It does not get returned to user code.
var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message.
var ErrMissingExtension = errors.New("proto: missing extension")
func AppendExtension ¶
func AppendExtension(e Message, tag int32, buf []byte)
func Bool ¶
func Bool(v bool) *bool
Bool is a helper routine that allocates a new bool value to store v and returns a pointer to it.
func BytesToExtensionsMap ¶
func BytesToExtensionsMap(buf []byte) (map[int32]Extension, error)
func ClearAllExtensions ¶
func ClearAllExtensions(pb Message)
ClearAllExtensions clears all extensions from pb.
func ClearExtension ¶
func ClearExtension(pb Message, extension *ExtensionDesc)
ClearExtension removes the given extension from pb.
func CompactText ¶
func CompactText(w io.Writer, pb Message) error
CompactText writes a given protocol buffer in compact text format (one line).
func CompactTextString ¶
func CompactTextString(pb Message) string
CompactTextString is the same as CompactText, but returns the string directly.
func DecodeVarint ¶
func DecodeVarint(buf []byte) (x uint64, n int)
DecodeVarint reads a varint-encoded integer from the slice. It returns the integer and the number of bytes consumed, or zero if there is not enough. This is the format for the int32, int64, uint32, uint64, bool, and enum protocol buffer types.
func EncodeExtensionMap ¶
func EncodeExtensionMap(m map[int32]Extension, data []byte) (n int, err error)
func EncodeInternalExtension ¶
func EncodeInternalExtension(m extendableProto, data []byte) (n int, err error)
func EncodeVarint ¶
func EncodeVarint(x uint64) []byte
EncodeVarint returns the varint encoding of x. This is the format for the int32, int64, uint32, uint64, bool, and enum protocol buffer types. Not used by the package itself, but helpful to clients wishing to use the same encoding.
func EnumName ¶
func EnumName(m map[int32]string, v int32) string
EnumName is a helper function to simplify printing protocol buffer enums by name. Given an enum map and a value, it returns a useful string.
func EnumValueMap ¶
func EnumValueMap(enumType string) map[string]int32
EnumValueMap returns the mapping from names to integers of the enum type enumType, or a nil if not found.
func Equal ¶
func Equal(a, b Message) bool
Equal returns true iff protocol buffers a and b are equal. The arguments must both be pointers to protocol buffer structs.
Equality is defined in this way:
- Two messages are equal iff they are the same type, corresponding fields are equal, unknown field sets are equal, and extensions sets are equal. - Two set scalar fields are equal iff their values are equal. If the fields are of a floating-point type, remember that NaN != x for all x, including NaN. If the message is defined in a proto3 .proto file, fields are not "set"; specifically, zero length proto3 "bytes" fields are equal (nil == {}). - Two repeated fields are equal iff their lengths are the same, and their corresponding elements are equal. Note a "bytes" field, although represented by []byte, is not a repeated field and the rule for the scalar fields described above applies. - Two unset fields are equal. - Two unknown field sets are equal if their current encoded state is equal. - Two extension sets are equal iff they have corresponding elements that are pairwise equal. - Two map fields are equal iff their lengths are the same, and they contain the same set of elements. Zero-length map fields are equal. - Every other combination of things are not equal.
The return value is undefined if a and b are not protocol buffers.
func ExtensionDescs ¶
func ExtensionDescs(pb Message) ([]*ExtensionDesc, error)
ExtensionDescs returns a new slice containing pb's extension descriptors, in undefined order. For non-registered extensions, ExtensionDescs returns an incomplete descriptor containing just the Field field, which defines the extension's field number.
func FileDescriptor ¶
func FileDescriptor(filename string) []byte
FileDescriptor returns the compressed FileDescriptorProto for a .proto file.
func Float32 ¶
func Float32(v float32) *float32
Float32 is a helper routine that allocates a new float32 value to store v and returns a pointer to it.
func Float64 ¶
func Float64(v float64) *float64
Float64 is a helper routine that allocates a new float64 value to store v and returns a pointer to it.
func GetBoolExtension ¶
func GetBoolExtension(pb Message, extension *ExtensionDesc, ifnotset bool) bool
func GetExtension ¶
func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error)
GetExtension parses and returns the given extension of pb. If the extension is not present and has no default value it returns ErrMissingExtension.
func GetExtensions ¶
func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error)
GetExtensions returns a slice of the extensions present in pb that are also listed in es. The returned slice has the same length as es; missing extensions will appear as nil elements.
func GetRawExtension ¶
func GetRawExtension(m map[int32]Extension, id int32) ([]byte, error)
func GetUnsafeExtension ¶
func GetUnsafeExtension(pb Message, fieldNum int32) (interface{}, error)
func GetUnsafeExtensionsMap ¶
func GetUnsafeExtensionsMap(extendable Message) map[int32]Extension
func HasExtension ¶
func HasExtension(pb Message, extension *ExtensionDesc) bool
HasExtension returns whether the given extension is present in pb.
func Int ¶
func Int(v int) *int32
Int is a helper routine that allocates a new int32 value to store v and returns a pointer to it, but unlike Int32 its argument value is an int.
func Int32 ¶
func Int32(v int32) *int32
Int32 is a helper routine that allocates a new int32 value to store v and returns a pointer to it.
func Int64 ¶
func Int64(v int64) *int64
Int64 is a helper routine that allocates a new int64 value to store v and returns a pointer to it.
func Marshal ¶
func Marshal(pb Message) ([]byte, error)
Marshal takes the protocol buffer and encodes it into the wire format, returning the data.
func MarshalJSONEnum ¶
func MarshalJSONEnum(m map[int32]string, value int32) ([]byte, error)
func MarshalMessageSet ¶
func MarshalMessageSet(exts interface{}) ([]byte, error)
MarshalMessageSet encodes the extension map represented by m in the message set wire format. It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option.
func MarshalMessageSetJSON ¶
func MarshalMessageSetJSON(exts interface{}) ([]byte, error)
MarshalMessageSetJSON encodes the extension map represented by m in JSON format. It is called by generated MarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
func MarshalText ¶
func MarshalText(w io.Writer, pb Message) error
MarshalText writes a given protocol buffer in text format. The only errors returned are from w.
func MarshalTextString ¶
func MarshalTextString(pb Message) string
MarshalTextString is the same as MarshalText, but returns the string directly.
func Merge ¶
func Merge(dst, src Message)
Merge merges src into dst. Required and optional fields that are set in src will be set to that value in dst. Elements of repeated fields will be appended. Merge panics if src and dst are not the same type, or if dst is nil.
func MessageName ¶
func MessageName(x Message) string
MessageName returns the fully-qualified proto name for the given message type.
func MessageType ¶
func MessageType(name string) reflect.Type
MessageType returns the message type (pointer to struct) for a named message.
func RegisterEnum ¶
func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32)
RegisterEnum is called from the generated code to install the enum descriptor maps into the global table to aid parsing text format protocol buffers.
func RegisterExtension ¶
func RegisterExtension(desc *ExtensionDesc)
RegisterExtension is called from the generated code.
func RegisterFile ¶
func RegisterFile(filename string, fileDescriptor []byte)
RegisterFile is called from generated code and maps from the full file name of a .proto file to its compressed FileDescriptorProto.
func RegisterMessageSetType ¶
func RegisterMessageSetType(m Message, fieldNum int32, name string)
RegisterMessageSetType is called from the generated code.
func RegisterType ¶
func RegisterType(x Message, name string)
RegisterType is called from generated code and maps from the fully qualified proto name to the type (pointer to struct) of the protocol buffer.
func RegisteredExtensions ¶
func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc
RegisteredExtensions returns a map of the registered extensions of a protocol buffer struct, indexed by the extension number. The argument pb should be a nil pointer to the struct type.
func SetDefaults ¶
func SetDefaults(pb Message)
SetDefaults sets unset protocol buffer fields to their default values. It only modifies fields that are both unset and have defined defaults. It recursively sets default values in any non-nil sub-messages.
func SetExtension ¶
func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error
SetExtension sets the specified extension of pb to the specified value.
func SetRawExtension ¶
func SetRawExtension(base Message, id int32, b []byte)
SetRawExtension is for testing only.
func SetUnsafeExtension ¶
func SetUnsafeExtension(pb Message, fieldNum int32, value interface{}) error
func Size ¶
func Size(pb Message) (n int)
Size returns the encoded size of a protocol buffer.
func SizeOfExtensionMap ¶
func SizeOfExtensionMap(m map[int32]Extension) (n int)
func SizeOfInternalExtension ¶
func SizeOfInternalExtension(m extendableProto) (n int)
func SizeVarint ¶
func SizeVarint(x uint64) int
SizeVarint returns the varint encoding size of an integer.
func Skip ¶
func Skip(data []byte) (n int, err error)
func String ¶
func String(v string) *string
String is a helper routine that allocates a new string value to store v and returns a pointer to it.
func StringFromExtensionsBytes ¶
func StringFromExtensionsBytes(ext []byte) string
func StringFromExtensionsMap ¶
func StringFromExtensionsMap(m map[int32]Extension) string
func StringFromInternalExtension ¶
func StringFromInternalExtension(m extendableProto) string
func Uint32 ¶
func Uint32(v uint32) *uint32
Uint32 is a helper routine that allocates a new uint32 value to store v and returns a pointer to it.
func Uint64 ¶
func Uint64(v uint64) *uint64
Uint64 is a helper routine that allocates a new uint64 value to store v and returns a pointer to it.
func Unmarshal ¶
func Unmarshal(buf []byte, pb Message) error
Unmarshal parses the protocol buffer representation in buf and places the decoded result in pb. If the struct underlying pb does not match the data in buf, the results can be unpredictable.
Unmarshal resets pb before starting to unmarshal, so any existing data in pb is always removed. Use UnmarshalMerge to preserve and append to existing data.
func UnmarshalJSONEnum ¶
func UnmarshalJSONEnum(m map[string]int32, data []byte, enumName string) (int32, error)
UnmarshalJSONEnum is a helper function to simplify recovering enum int values from their JSON-encoded representation. Given a map from the enum's symbolic names to its int values, and a byte buffer containing the JSON-encoded value, it returns an int32 that can be cast to the enum type by the caller.
The function can deal with both JSON representations, numeric and symbolic.
func UnmarshalMerge ¶
func UnmarshalMerge(buf []byte, pb Message) error
UnmarshalMerge parses the protocol buffer representation in buf and writes the decoded result to pb. If the struct underlying pb does not match the data in buf, the results can be unpredictable.
UnmarshalMerge merges into existing data in pb. Most code should use Unmarshal instead.
func UnmarshalMessageSet ¶
func UnmarshalMessageSet(buf []byte, exts interface{}) error
UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format. It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
func UnmarshalMessageSetJSON ¶
func UnmarshalMessageSetJSON(buf []byte, exts interface{}) error
UnmarshalMessageSetJSON decodes the extension map encoded in buf in JSON format. It is called by generated UnmarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
func UnmarshalText ¶
func UnmarshalText(s string, pb Message) error
UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb before starting to unmarshal, so any existing data in pb is always removed. If a required field is not set and no other error occurs, UnmarshalText returns *RequiredNotSetError.
type Buffer ¶
A Buffer is a buffer manager for marshaling and unmarshaling protocol buffers. It may be reused between invocations to reduce memory usage. It is not necessary to use a Buffer; the global functions Marshal and Unmarshal create a temporary Buffer and are fine for most applications.
type Buffer struct {
// contains filtered or unexported fields
}
func NewBuffer ¶
func NewBuffer(e []byte) *Buffer
NewBuffer allocates a new Buffer and initializes its internal data to the contents of the argument slice.
func (*Buffer) Bytes ¶
func (p *Buffer) Bytes() []byte
Bytes returns the contents of the Buffer.
func (*Buffer) DebugPrint ¶
func (p *Buffer) DebugPrint(s string, b []byte)
DebugPrint dumps the encoded data in b in a debugging format with a header including the string s. Used in testing but made available for general debugging.
func (*Buffer) DecodeFixed32 ¶
func (p *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 (p *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) DecodeGroup ¶
func (p *Buffer) DecodeGroup(pb Message) error
DecodeGroup reads a tag-delimited group from the Buffer.
func (*Buffer) DecodeMessage ¶
func (p *Buffer) DecodeMessage(pb Message) error
DecodeMessage reads a count-delimited message from the Buffer.
func (*Buffer) DecodeRawBytes ¶
func (p *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) DecodeStringBytes ¶
func (p *Buffer) DecodeStringBytes() (s string, err error)
DecodeStringBytes reads an encoded string from the Buffer. This is the format used for the proto2 string type.
func (*Buffer) DecodeVarint ¶
func (p *Buffer) DecodeVarint() (x uint64, err 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) DecodeZigzag32 ¶
func (p *Buffer) DecodeZigzag32() (x uint64, err error)
DecodeZigzag32 reads a zigzag-encoded 32-bit integer from the Buffer. This is the format used for the sint32 protocol buffer type.
func (*Buffer) DecodeZigzag64 ¶
func (p *Buffer) DecodeZigzag64() (x uint64, err error)
DecodeZigzag64 reads a zigzag-encoded 64-bit integer from the Buffer. This is the format used for the sint64 protocol buffer type.
func (*Buffer) EncodeFixed32 ¶
func (p *Buffer) EncodeFixed32(x uint64) error
EncodeFixed32 writes a 32-bit integer to the Buffer. This is the format for the fixed32, sfixed32, and float protocol buffer types.
func (*Buffer) EncodeFixed64 ¶
func (p *Buffer) EncodeFixed64(x uint64) error
EncodeFixed64 writes a 64-bit integer to the Buffer. This is the format for the fixed64, sfixed64, and double protocol buffer types.
func (*Buffer) EncodeMessage ¶
func (p *Buffer) EncodeMessage(pb Message) error
EncodeMessage writes the protocol buffer to the Buffer, prefixed by a varint-encoded length.
func (*Buffer) EncodeRawBytes ¶
func (p *Buffer) EncodeRawBytes(b []byte) error
EncodeRawBytes writes a count-delimited byte buffer to the Buffer. This is the format used for the bytes protocol buffer type and for embedded messages.
func (*Buffer) EncodeStringBytes ¶
func (p *Buffer) EncodeStringBytes(s string) error
EncodeStringBytes writes an encoded string to the Buffer. This is the format used for the proto2 string type.
func (*Buffer) EncodeVarint ¶
func (p *Buffer) EncodeVarint(x uint64) error
EncodeVarint writes a varint-encoded integer to the Buffer. This is the format for the int32, int64, uint32, uint64, bool, and enum protocol buffer types.
func (*Buffer) EncodeZigzag32 ¶
func (p *Buffer) EncodeZigzag32(x uint64) error
EncodeZigzag32 writes a zigzag-encoded 32-bit integer to the Buffer. This is the format used for the sint32 protocol buffer type.
func (*Buffer) EncodeZigzag64 ¶
func (p *Buffer) EncodeZigzag64(x uint64) error
EncodeZigzag64 writes a zigzag-encoded 64-bit integer to the Buffer. This is the format used for the sint64 protocol buffer type.
func (*Buffer) Marshal ¶
func (p *Buffer) Marshal(pb Message) error
Marshal takes the protocol buffer and encodes it into the wire format, writing the result to the Buffer.
func (*Buffer) Reset ¶
func (p *Buffer) Reset()
Reset resets the Buffer, ready for marshaling a new protocol buffer.
func (*Buffer) SetBuf ¶
func (p *Buffer) SetBuf(s []byte)
SetBuf replaces the internal buffer with the slice, ready for unmarshaling the contents of the slice.
func (*Buffer) Unmarshal ¶
func (p *Buffer) Unmarshal(pb Message) error
Unmarshal parses the protocol buffer representation in the Buffer and places the decoded result in pb. If the struct underlying pb does not match the data in the buffer, the results can be unpredictable.
Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal.
type Extension ¶
Extension represents an extension in a message.
type Extension struct {
// contains filtered or unexported fields
}
func NewExtension ¶
func NewExtension(e []byte) Extension
func (*Extension) Compare ¶
func (this *Extension) Compare(that *Extension) int
func (*Extension) Equal ¶
func (this *Extension) Equal(that *Extension) bool
func (Extension) GoString ¶
func (this Extension) GoString() string
type ExtensionDesc ¶
ExtensionDesc represents an extension specification. Used in generated code from the protocol compiler.
type ExtensionDesc struct { ExtendedType Message // nil pointer to the type that is being extended ExtensionType interface{} // nil pointer to the extension type Field int32 // field number Name string // fully-qualified name of extension, for text formatting Tag string // protobuf tag style Filename string // name of the file in which the extension is defined }
type ExtensionRange ¶
ExtensionRange represents a range of message extensions for a protocol buffer. Used in code generated by the protocol compiler.
type ExtensionRange struct {
Start, End int32 // both inclusive
}
type Marshaler ¶
Marshaler is the interface representing objects that can marshal themselves.
type Marshaler interface { Marshal() ([]byte, error) }
type Message ¶
Message is implemented by generated protocol buffer messages.
type Message interface { Reset() String() string ProtoMessage() }
func Clone ¶
func Clone(pb Message) Message
Clone returns a deep copy of a protocol buffer.
type OneofProperties ¶
OneofProperties represents information about a specific field in a oneof.
type OneofProperties struct { Type reflect.Type // pointer to generated struct type for this oneof field Field int // struct field number of the containing oneof in the message Prop *Properties }
type ParseError ¶
type ParseError struct { Message string Line int // 1-based line number Offset int // 0-based byte offset from start of input }
func (*ParseError) Error ¶
func (p *ParseError) Error() string
type Properties ¶
Properties represents the protocol-specific behavior of a single struct field.
type Properties struct { Name string // name of the field, for error messages OrigName string // original name before protocol compiler (always set) JSONName string // name to use for JSON; determined by protoc Wire string WireType int Tag int Required bool Optional bool Repeated bool Packed bool // relevant for repeated primitives only Enum string // set for enum types only Default string // default value HasDefault bool // whether an explicit default was provided CustomType string StdTime bool StdDuration bool // contains filtered or unexported fields }
func (*Properties) Init ¶
func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField)
Init populates the properties from a protocol buffer struct tag.
func (*Properties) Parse ¶
func (p *Properties) Parse(s string)
Parse populates p by parsing a string in the protobuf struct field tag style.
func (*Properties) String ¶
func (p *Properties) String() string
String formats the properties in the protobuf struct field tag style.
type RequiredNotSetError ¶
RequiredNotSetError is the error returned if Marshal is called with a protocol buffer struct whose required fields have not all been initialized. It is also the error returned if Unmarshal is called with an encoded protocol buffer that does not include all the required fields.
When printed, RequiredNotSetError reports the first unset required field in a message. If the field cannot be precisely determined, it is reported as "{Unknown}".
type RequiredNotSetError struct {
// contains filtered or unexported fields
}
func NewRequiredNotSetError ¶
func NewRequiredNotSetError(field string) *RequiredNotSetError
func (*RequiredNotSetError) Error ¶
func (e *RequiredNotSetError) Error() string
type Sizer ¶
type Sizer interface { Size() int }
type Stats ¶
Stats records allocation details about the protocol buffer encoders and decoders. Useful for tuning the library itself.
type Stats struct { Emalloc uint64 // mallocs in encode Dmalloc uint64 // mallocs in decode Encode uint64 // number of encodes Decode uint64 // number of decodes Chit uint64 // number of cache hits Cmiss uint64 // number of cache misses Size uint64 // number of sizes }
func GetStats ¶
func GetStats() Stats
GetStats returns a copy of the global Stats structure.
type StructProperties ¶
StructProperties represents properties for all the fields of a struct. decoderTags and decoderOrigNames should only be used by the decoder.
type StructProperties struct { Prop []*Properties // properties for each field // OneofTypes contains information about the oneof fields in this message. // It is keyed by the original name of a field. OneofTypes map[string]*OneofProperties // contains filtered or unexported fields }
func GetProperties ¶
func GetProperties(t reflect.Type) *StructProperties
GetProperties returns the list of properties for the type represented by t. t must represent a generated struct type of a protocol message.
func (*StructProperties) Len ¶
func (sp *StructProperties) Len() int
func (*StructProperties) Less ¶
func (sp *StructProperties) Less(i, j int) bool
func (*StructProperties) Swap ¶
func (sp *StructProperties) Swap(i, j int)
type TextMarshaler ¶
TextMarshaler is a configurable text format marshaler.
type TextMarshaler struct { Compact bool // use compact text format (one line). ExpandAny bool // expand google.protobuf.Any messages of known types }
func (*TextMarshaler) Marshal ¶
func (tm *TextMarshaler) Marshal(w io.Writer, pb Message) error
Marshal writes a given protocol buffer in text format. The only errors returned are from w.
func (*TextMarshaler) Text ¶
func (tm *TextMarshaler) Text(pb Message) string
Text is the same as Marshal, but returns the string directly.
type Unmarshaler ¶
Unmarshaler is the interface representing objects that can unmarshal themselves. The method should reset the receiver before decoding starts. The argument points to data that may be overwritten, so implementations should not keep references to the buffer.
type Unmarshaler interface { Unmarshal([]byte) error }
type XXX_InternalExtensions ¶
XXX_InternalExtensions is an internal representation of proto extensions.
Each generated message struct type embeds an anonymous XXX_InternalExtensions field, thus gaining the unexported 'extensions' method, which can be called only from the proto package.
The methods of XXX_InternalExtensions are not concurrency safe in general, but calls to logically read-only methods such as has and get may be executed concurrently.
type XXX_InternalExtensions struct {
// contains filtered or unexported fields
}
func NewUnsafeXXX_InternalExtensions ¶
func NewUnsafeXXX_InternalExtensions(m map[int32]Extension) XXX_InternalExtensions
Subdirectories
Name | Synopsis |
---|---|
.. | |
proto3_proto | Package proto3_proto is a generated protocol buffer package. |