f49fd502d3
This CL introduces generation of specialized APIs directly into the generated packages for certain well-known types. This follows the pattern set forth by the other language implementations that have specialized generated support for certain well-known types. Overview of new API: package anypb func MarshalFrom(*Any, proto.Message, proto.MarshalOptions) error func UnmarshalTo(*Any, proto.Message, proto.UnmarshalOptions) error func UnmarshalNew(*Any, proto.UnmarshalOptions) (proto.Message, error) func (*Any) MessageIs(proto.Message) bool func (*Any) MessageName() protoreflect.FullName func (*Any) MarshalFrom(proto.Message) error func (*Any) UnmarshalTo(proto.Message) error func (*Any) UnmarshalNew() (proto.Message, error) package timestamppb func Now() *Timestamp func New(time.Time) *Timestamp func (*Timestamp) AsTime() time.Time func (*Timestamp) IsValid() bool func (*Timestamp) CheckValid() error package durationpb func New(time.Duration) *Duration func (*Duration) AsDuration() time.Duration func (*Duration) IsValid() bool func (*Duration) CheckValid() error package structpb func NewStruct(map[string]interface{}) (*Struct, error) func (*Struct) AsMap() map[string]interface{} func (*Struct) MarshalJSON() ([]byte, error) func (*Struct) UnmarshalJSON(b []byte) error func NewList([]interface{}) (*ListValue, error) func (*ListValue) AsSlice() []interface{} func (*ListValue) MarshalJSON() ([]byte, error) func (*ListValue) UnmarshalJSON(b []byte) error func NewValue(interface{}) (*Value, error) func NewNullValue() *Value func NewBoolValue(bool) *Value func NewNumberValue(float64) *Value func NewStringValue(string) *Value func NewStructValue(*Struct) *Value func NewListValue(*ListValue) *Value func (*Value) AsInterface() interface{} func (*Value) MarshalJSON() ([]byte, error) func (*Value) UnmarshalJSON(b []byte) error package fieldmaskpb func New(proto.Message, ...string) (*FieldMask, error) func Union(*FieldMask, *FieldMask, ...*FieldMask) *FieldMask func Intersect(*FieldMask, *FieldMask, ...*FieldMask) *FieldMask func (*FieldMask) IsValid(proto.Message) bool func (*FieldMask) Append(proto.Message, ...string) error func (*FieldMask) Normalize() package wrapperspb func Bool(bool) *BoolValue func Int32(int32) *Int32Value func Int64(int64) *Int64Value func UInt32(uint32) *UInt32Value func UInt64(uint64) *UInt64Value func Float(float32) *FloatValue func Double(float64) *DoubleValue func String(string) *StringValue func Bytes([]byte) *BytesValue This functionality expands upon and supersedes the older github.com/golang/protobuf/ptypes package, which provided helpers for Any, Timestamp, and Duration. Comparison with older ptypes package: * ptypes.AnyMessageName is replaced by anypb.Any.MessageName. The former returned an error for malformed type URLs, while the latter simply returns an empty string. * ptypes.Is is replaced by anypb.Any.MessageIs. * ptypes.Empty has no direct replacement as it is equivalent to: mt, err := protoregistry.GlobalTypes.FindMessageByURL(any.GetTypeUrl()) if err != nil { return nil, err } return mt.New().Interface(), nil Analysis of user code revealed that this function is seldom used. * ptypes.MarshalAny is replaced by anypb.Any.MarshalFrom. The former creates a new Any message and returns it, while the latter is a method that modifies the receiver. * ptypes.UnmarshalAny is replaced by anypb.Any.UnmarshalTo. * ptypes.DynamicAny is loosely replaced by anypb.Any.UnmarshalNew. The DynamicAny type is a custom proto.Message that is special to ptypes.UnmarshalAny where it would allocate a new message and store it into the DynamicAny instance. The UnmarshalNew method accomplishes the equivalent functionality in a more direct fashion. * ptypes.TimestampNow is replaced by timestamppb.Now. * ptypes.TimestampProto is replaced by timestamppb.New. The former returned an error if the timestamp was outside the 10000-year range recommended by timestamp.proto, while the latter always succeeded. To preserve the behavior of the former validation check, the replacement can additionally call the timestamppb.Timestamp.CheckValid method. * ptypes.Timestamp is replaced by timestamppb.Timestamp.AsTime. The former returned an error if the timestamp was outside the 10000-year range recommended by timestamp.proto, while the latter always succeeded. To preserve the behavior of the former validation check, the replacement can additionally call the timestamppb.Timestamp.CheckValid method. * ptypes.TimestampString has no direct replacement as it is equivalent to: ts.AsTime().Format(time.RFC3339Nano) * ptypes.DurationProto is replaced by durationpb.New. * ptypes.Duration is replaced by durationpb.Duration.AsDuration. The former returned an error if the duration would overflow when converting to a time.Duration, while the latter uses saturation arithmetic (similiar to the time package itself). Underflow resulted in time.Duration(math.MinInt64), while overflow resulted in time.Duration(math.MaxInt64). To preserve the behavior of former validation checks, the replacement can call the durationpb.Duration.CheckValid method and check whether the duration is fixed to one of the overflow values. Change-Id: Ia996b1037a1fcafced7c7e10e9408ef7fa22863a Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/225298 Reviewed-by: Damien Neil <dneil@google.com> |
||
---|---|---|
cmd/protoc-gen-go | ||
compiler/protogen | ||
encoding | ||
internal | ||
proto | ||
reflect | ||
runtime | ||
testing | ||
types | ||
.gitignore | ||
.travis.yml | ||
AUTHORS | ||
CONTRIBUTING.md | ||
CONTRIBUTORS | ||
go.mod | ||
go.sum | ||
integration_test.go | ||
LICENSE | ||
PATENTS | ||
README.md | ||
regenerate.bash | ||
release.bash | ||
test.bash |
Go support for Protocol Buffers
This project hosts the Go implementation for protocol buffers, which is a language-neutral, platform-neutral, extensible mechanism for serializing structured data. The protocol buffer language is a language for specifying the schema for structured data. This schema is compiled into language specific bindings. This project provides both a tool to generate Go code for the protocol buffer language, and also the runtime implementation to handle serialization of messages in Go. See the protocol buffer developer guide for more information about protocol buffers themselves.
This project is comprised of two components:
-
Code generator: The
protoc-gen-go
tool is a compiler plugin toprotoc
, the protocol buffer compiler. It augments theprotoc
compiler so that it knows how to generate Go specific code for a given.proto
file. -
Runtime library: The
protobuf
module contains a set of Go packages that form the runtime implementation of protobufs in Go. This provides the set of interfaces that define what a message is and functionality to serialize message in various formats (e.g., wire, JSON, and text).
See the developer guide for protocol buffers in Go for a general guide for how to get started using protobufs in Go.
This project is the second major revision of the Go protocol buffer API
implemented by the
google.golang.org/protobuf
module. The first major version is implemented by the
github.com/golang/protobuf
module.
Package index
Summary of the packages provided by this module:
proto
: Packageproto
provides functions operating on protobuf messages such as cloning, merging, and checking equality, as well as binary serialization.encoding/protojson
: Packageprotojson
serializes protobuf messages as JSON.encoding/prototext
: Packageprototext
serializes protobuf messages as the text format.encoding/protowire
: Packageprotowire
parses and formats the low-level raw wire encoding. Most users should use packageproto
to serialize messages in the wire format.reflect/protoreflect
: Packageprotoreflect
provides interfaces to dynamically manipulate protobuf messages.reflect/protoregistry
: Packageprotoregistry
provides data structures to register and lookup protobuf descriptor types.reflect/protodesc
: Packageprotodesc
provides functionality for convertingdescriptorpb.FileDescriptorProto
messages to/from the reflectiveprotoreflect.FileDescriptor
.testing/protocmp
: Packageprotocmp
provides protobuf specific options for thecmp
package.testing/protopack
: Packageprotopack
aids manual encoding and decoding of the wire format.testing/prototest
: Packageprototest
exercises the protobuf reflection implementation for concrete message types.types/dynamicpb
: Packagedynamicpb
creates protobuf messages at runtime from protobuf descriptors.types/known/anypb
: Packageanypb
is the generated package forgoogle/protobuf/any.proto
.types/known/timestamppb
: Packagetimestamppb
is the generated package forgoogle/protobuf/timestamp.proto
.types/known/durationpb
: Packagedurationpb
is the generated package forgoogle/protobuf/duration.proto
.types/known/wrapperspb
: Packagewrapperspb
is the generated package forgoogle/protobuf/wrappers.proto
.types/known/structpb
: Packagestructpb
is the generated package forgoogle/protobuf/struct.proto
.types/known/fieldmaskpb
: Packagefieldmaskpb
is the generated package forgoogle/protobuf/field_mask.proto
.types/known/apipb
: Packageapipb
is the generated package forgoogle/protobuf/api.proto
.types/known/typepb
: Packagetypepb
is the generated package forgoogle/protobuf/type.proto
.types/known/sourcecontextpb
: Packagesourcecontextpb
is the generated package forgoogle/protobuf/source_context.proto
.types/known/emptypb
: Packageemptypb
is the generated package forgoogle/protobuf/empty.proto
.types/descriptorpb
: Packagedescriptorpb
is the generated package forgoogle/protobuf/descriptor.proto
.types/pluginpb
: Packagepluginpb
is the generated package forgoogle/protobuf/compiler/plugin.proto
.compiler/protogen
: Packageprotogen
provides support for writing protoc plugins.cmd/protoc-gen-go
: Theprotoc-gen-go
binary is a protoc plugin to generate a Go protocol buffer package.
Reporting issues
The issue tracker for this project is currently located at golang/protobuf.
Please report any issues there with a sufficient description of the bug or feature request. Bug reports should ideally be accompanied by a minimal reproduction of the issue. Irreproducible bugs are difficult to diagnose and fix (and likely to be closed after some period of time). Bug reports must specify the version of the Go protocol buffer module and also the version of the protocol buffer toolchain being used.
Contributing
This project is open-source and accepts contributions. See the contribution guide for more information.
Compatibility
This module and the generated code are expected to be stable over time. However, we reserve the right to make breaking changes without notice for the following reasons:
- Security: A security issue in the specification or implementation may come to light whose resolution requires breaking compatibility. We reserve the right to address such issues.
- Unspecified behavior: There are some aspects of the protocol buffer specification that are undefined. Programs that depend on unspecified behavior may break in future releases.
- Specification changes: It may become necessary to address an inconsistency, incompleteness, or change in the protocol buffer specification, which may affect the behavior of existing programs. We reserve the right to address such changes.
- Bugs: If a package has a bug that violates correctness, a program depending on the buggy behavior may break if the bug is fixed. We reserve the right to fix such bugs.
- Generated additions: We reserve the right to add new declarations to
generated Go packages of
.proto
files. This includes declared constants, variables, functions, types, fields in structs, and methods on types. This may break attempts at injecting additional code on top of what is generated byprotoc-gen-go
. Such practice is not supported by this project. - Internal changes: We reserve the right to add, modify, and remove
internal code, which includes all unexported declarations, the
protoc-gen-go/internal_gengo
package, theruntime/protoimpl
package, and all packages underinternal
.
Any breaking changes outside of these will be announced 6 months in advance to protobuf@googlegroups.com.
Users should use generated code produced by a version of
protoc-gen-go
that is identical to the runtime version provided by the
protobuf module. This
project promises that the runtime remains compatible with code produced by a
version of the generator that is no older than 1 year from the version of the
runtime used, according to the release dates of the minor version. Generated
code is expected to use a runtime version that is at least as new as the
generator used to produce it. Generated code contains references to
protoimpl.EnforceVersion
to statically ensure that the generated code and runtime do not drift
sufficiently far apart.
Historical legacy
This project is the second major revision
(released in 2020)
of the Go protocol buffer API implemented by the
google.golang.org/protobuf
module. The first major version
(released publicly in 2010)
is implemented by the
github.com/golang/protobuf
module.
The first version predates the release of Go 1 by several years. It has a long history as one of the first core pieces of infrastructure software ever written in Go. As such, the Go protobuf project was one of many pioneers for determining what the Go language should even look like and what would eventually be considered good design patterns and “idiomatic” Go (by simultaneously being both positive and negative examples of it).
Consider the changing signature of the proto.Unmarshal
function as an example
of Go language and library evolution throughout the life of this project:
// 2007/09/25 - Conception of Go
// 2008/11/12
export func UnMarshal(r io.Read, pb_e reflect.Empty) *os.Error
// 2008/11/13
export func UnMarshal(buf *[]byte, pb_e reflect.Empty) *os.Error
// 2008/11/24
export func UnMarshal(buf *[]byte, pb_e interface{}) *os.Error
// 2008/12/18
export func UnMarshal(buf []byte, pb_e interface{}) *os.Error
// 2009/01/20
func UnMarshal(buf []byte, pb_e interface{}) *os.Error
// 2009/04/17
func UnMarshal(buf []byte, pb_e interface{}) os.Error
// 2009/05/22
func Unmarshal(buf []byte, pb_e interface{}) os.Error
// 2011/11/03
func Unmarshal(buf []byte, pb_e interface{}) error
// 2012/03/28 - Release of Go 1
// 2012/06/12
func Unmarshal(buf []byte, pb Message) error
These changes demonstrate the difficulty of determining what the right API is for any new technology. It takes time multiplied by many users to determine what is best; even then, “best” is often still somewhere over the horizon.
The change on June 6th, 2012 added a degree of type-safety to Go protobufs by declaring a new interface that all protobuf messages were required to implement:
type Message interface {
Reset()
String() string
ProtoMessage()
}
This interface reduced the set of types that can be passed to proto.Unmarshal
from the universal set of all possible Go types to those with a special
ProtoMessage
marker method. The intention of this change is to limit the
protobuf API to only operate on protobuf data types (i.e., protobuf messages).
For example, there is no sensible operation if a Go channel were passed to the
protobuf API as a channel cannot be serialized. The restricted interface would
prevent that.
This interface does not behaviorally describe what a protobuf message is, but
acts as a marker with an undocumented expectation that protobuf messages must be
a Go struct with a specific layout of fields with formatted tags. This
expectation is not statically enforced by the Go language, for it is an
implementation detail checked dynamically at runtime using Go reflection. Back
in 2012, the only types with this marker were those generated by
protoc-gen-go
. Since protoc-gen-go
would always generate messages with the
proper layout of fields, this was deemed an acceptable and dramatic improvement
over interface{}
.
Over the next 10 years,
use of Go would skyrocket and use of
protobufs in Go would skyrocket as well. With increased popularity also came
more diverse usages and requirements for Go protobufs and an increased number of
custom proto.Message
implementations that were not generated by
protoc-gen-go
.
The increasingly diverse ecosystem of Go types implementing the proto.Message
interface led to incompatibilities, which often occurred when:
-
Passing custom
proto.Message
types to the protobuf APIs: A concrete message implementation might work with some top-level functions (e.g.,proto.Marshal
), but cause others (e.g.,proto.Equal
) to choke and panic. This occurs because the type only had partial support for being an actual message by only implementing theproto.Marshaler
interface or having malformed struct field tags that happened to work with one function, but not another. -
Using Go reflection on any
proto.Message
types: A common desire is to write general-purpose code that operates on any protobuf message. For example, a microservice might want to populate atrace_id
field if it is present in a message. To accomplish this, one would use Go reflection to introspect the message type, and assume it were a pointer to a Go struct with a field namedTraceId
(as would be commonly produced byprotoc-gen-go
). If the concrete message type did not match this expectation, it either failed to work or even resulted in a panic. Such was the case for concrete message types that might be backed by a Go map instead of a Go struct.
Both of these issues are solved by following the idiom that interfaces should describe behavior, not data. This means that the interface itself should provide sufficient functionality through its methods that users can introspect and interact with all aspects of a protobuf message through a principled API. This feature is called protobuf reflection. Just as how Go reflection provides an API for programmatically interacting with any arbitrary Go value, protobuf reflection provides an API for programmatically interacting with any arbitrary protobuf message.
Since an interface cannot be extended in a backwards compatible way, this
suggested the need for a new major version that defines a new proto.Message
interface:
type Message interface {
ProtoReflect() protoreflect.Message
}
The new
proto.Message
interface contains a single ProtoReflect
method that returns a
protoreflect.Message
,
which is a reflective view over a protobuf message. In addition to making a
breaking change to the proto.Message
interface, we took this opportunity to
cleanup the supporting functionality that operate on a proto.Message
, split up
complicated functionality apart into manageable packages, and to hide
implementation details away from the public API.
The goal for this major revision is to improve upon all the benefits of, while addressing all the shortcomings of the old API. We hope that it will serve the Go ecosystem well for the next 10 years and beyond.