Migrate away from queue type in faas project

The queue type now resides in the provider, so that there is
no risk of a circular reference.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd)
2021-07-26 17:21:20 +01:00
committed by Alex Ellis
parent 06a51373e2
commit 58394bb1de
101 changed files with 10232 additions and 1763 deletions

View File

@ -1,14 +1,14 @@
module github.com/openfaas/faas/gateway module github.com/openfaas/faas/gateway
go 1.15 go 1.16
require ( require (
github.com/docker/distribution v2.7.1+incompatible github.com/docker/distribution v2.7.1+incompatible
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/nats-io/nats-server/v2 v2.3.2 // indirect github.com/nats-io/nats-server/v2 v2.3.2 // indirect
github.com/nats-io/nats-streaming-server v0.22.0 // indirect github.com/nats-io/nats-streaming-server v0.22.0 // indirect
github.com/openfaas/faas-provider v0.17.3 github.com/openfaas/faas-provider v0.18.6
github.com/openfaas/nats-queue-worker v0.0.0-20200512211843-8e9eefd5a320 github.com/openfaas/nats-queue-worker v0.0.0-20210726161954-ada9a31504c9
github.com/prometheus/client_golang v1.9.0 github.com/prometheus/client_golang v1.9.0
github.com/prometheus/client_model v0.2.0 github.com/prometheus/client_model v0.2.0
go.uber.org/goleak v1.1.10 go.uber.org/goleak v1.1.10

View File

@ -95,6 +95,9 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
@ -104,6 +107,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -241,10 +245,12 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/openfaas/faas-provider v0.17.3 h1:LN76lrXUKAx27o5X8l+daKWEzsdiW2E99jMOlI1SO5Q= github.com/openfaas/faas v0.0.0-20200422113858-a7c6c3920078/go.mod h1:E0m2rLup0Vvxg53BKxGgaYAGcZa3Xl+vvL7vSi5yQ14=
github.com/openfaas/faas-provider v0.17.3/go.mod h1:fq1JL0mX4rNvVVvRLaLRJ3H6o667sHuyP5p/7SZEe98= github.com/openfaas/faas-provider v0.18.6 h1:wypzvPKZqta8t4rx3W6Dm14ommBCc+rQ4DKDiBdGB7M=
github.com/openfaas/nats-queue-worker v0.0.0-20200512211843-8e9eefd5a320 h1:rgNxl8VqfRh/2iZLO0OjcOo7D+BUcZrBMTPBUYCB1pY= github.com/openfaas/faas-provider v0.18.6/go.mod h1:fq1JL0mX4rNvVVvRLaLRJ3H6o667sHuyP5p/7SZEe98=
github.com/openfaas/nats-queue-worker v0.0.0-20200512211843-8e9eefd5a320/go.mod h1:BfT8MB890hbhbtPid+X/oU0HAznGFS581NiG2hkr8Rc= github.com/openfaas/nats-queue-worker v0.0.0-20210726161233-3fa550b705fe/go.mod h1:njdij5dt/fm8EMrpUFiAW5u6tg7higDN+Xi+LXclDas=
github.com/openfaas/nats-queue-worker v0.0.0-20210726161954-ada9a31504c9 h1:dpG1UcgTesGfLetgT3ns1cAhP8XMDZqxnxTW1MlnwSc=
github.com/openfaas/nats-queue-worker v0.0.0-20210726161954-ada9a31504c9/go.mod h1:ajlN2z+D8JPBq3kWNv4WLT6mtKPqlgeE3dYEx39d1tk=
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis= github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
@ -491,6 +497,9 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -12,15 +12,16 @@ import (
"strings" "strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
ftypes "github.com/openfaas/faas-provider/types"
"github.com/openfaas/faas/gateway/metrics" "github.com/openfaas/faas/gateway/metrics"
"github.com/openfaas/faas/gateway/queue"
"github.com/openfaas/faas/gateway/scaling" "github.com/openfaas/faas/gateway/scaling"
) )
const queueAnnotation = "com.openfaas.queue" const queueAnnotation = "com.openfaas.queue"
// MakeQueuedProxy accepts work onto a queue // MakeQueuedProxy accepts work onto a queue
func MakeQueuedProxy(metrics metrics.MetricOptions, queuer queue.RequestQueuer, pathTransformer URLPathTransformer, defaultNS string, functionQuery scaling.FunctionQuery) http.HandlerFunc { func MakeQueuedProxy(metrics metrics.MetricOptions, queuer ftypes.RequestQueuer, pathTransformer URLPathTransformer, defaultNS string, functionQuery scaling.FunctionQuery) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if r.Body != nil { if r.Body != nil {
defer r.Body.Close() defer r.Body.Close()
@ -48,7 +49,7 @@ func MakeQueuedProxy(metrics metrics.MetricOptions, queuer queue.RequestQueuer,
return return
} }
req := &queue.Request{ req := &ftypes.QueueRequest{
Function: name, Function: name,
Body: body, Body: body,
Method: r.Method, Method: r.Method,

View File

@ -13,6 +13,7 @@ import (
"strings" "strings"
"sync" "sync"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl" "google.golang.org/protobuf/runtime/protoimpl"
@ -62,14 +63,7 @@ func FileDescriptor(s filePath) fileDescGZIP {
// Find the descriptor in the v2 registry. // Find the descriptor in the v2 registry.
var b []byte var b []byte
if fd, _ := protoregistry.GlobalFiles.FindFileByPath(s); fd != nil { if fd, _ := protoregistry.GlobalFiles.FindFileByPath(s); fd != nil {
if fd, ok := fd.(interface{ ProtoLegacyRawDesc() []byte }); ok { b, _ = Marshal(protodesc.ToFileDescriptorProto(fd))
b = fd.ProtoLegacyRawDesc()
} else {
// TODO: Use protodesc.ToFileDescriptorProto to construct
// a descriptorpb.FileDescriptorProto and marshal it.
// However, doing so causes the proto package to have a dependency
// on descriptorpb, leading to cyclic dependency issues.
}
} }
// Locally cache the raw descriptor form for the file. // Locally cache the raw descriptor form for the file.

View File

@ -19,6 +19,8 @@ const urlPrefix = "type.googleapis.com/"
// AnyMessageName returns the message name contained in an anypb.Any message. // AnyMessageName returns the message name contained in an anypb.Any message.
// Most type assertions should use the Is function instead. // Most type assertions should use the Is function instead.
//
// Deprecated: Call the any.MessageName method instead.
func AnyMessageName(any *anypb.Any) (string, error) { func AnyMessageName(any *anypb.Any) (string, error) {
name, err := anyMessageName(any) name, err := anyMessageName(any)
return string(name), err return string(name), err
@ -38,6 +40,8 @@ func anyMessageName(any *anypb.Any) (protoreflect.FullName, error) {
} }
// MarshalAny marshals the given message m into an anypb.Any message. // MarshalAny marshals the given message m into an anypb.Any message.
//
// Deprecated: Call the anypb.New function instead.
func MarshalAny(m proto.Message) (*anypb.Any, error) { func MarshalAny(m proto.Message) (*anypb.Any, error) {
switch dm := m.(type) { switch dm := m.(type) {
case DynamicAny: case DynamicAny:
@ -58,6 +62,9 @@ func MarshalAny(m proto.Message) (*anypb.Any, error) {
// Empty returns a new message of the type specified in an anypb.Any message. // Empty returns a new message of the type specified in an anypb.Any message.
// It returns protoregistry.NotFound if the corresponding message type could not // It returns protoregistry.NotFound if the corresponding message type could not
// be resolved in the global registry. // be resolved in the global registry.
//
// Deprecated: Use protoregistry.GlobalTypes.FindMessageByName instead
// to resolve the message name and create a new instance of it.
func Empty(any *anypb.Any) (proto.Message, error) { func Empty(any *anypb.Any) (proto.Message, error) {
name, err := anyMessageName(any) name, err := anyMessageName(any)
if err != nil { if err != nil {
@ -76,6 +83,8 @@ func Empty(any *anypb.Any) (proto.Message, error) {
// //
// The target message m may be a *DynamicAny message. If the underlying message // The target message m may be a *DynamicAny message. If the underlying message
// type could not be resolved, then this returns protoregistry.NotFound. // type could not be resolved, then this returns protoregistry.NotFound.
//
// Deprecated: Call the any.UnmarshalTo method instead.
func UnmarshalAny(any *anypb.Any, m proto.Message) error { func UnmarshalAny(any *anypb.Any, m proto.Message) error {
if dm, ok := m.(*DynamicAny); ok { if dm, ok := m.(*DynamicAny); ok {
if dm.Message == nil { if dm.Message == nil {
@ -100,6 +109,8 @@ func UnmarshalAny(any *anypb.Any, m proto.Message) error {
} }
// Is reports whether the Any message contains a message of the specified type. // Is reports whether the Any message contains a message of the specified type.
//
// Deprecated: Call the any.MessageIs method instead.
func Is(any *anypb.Any, m proto.Message) bool { func Is(any *anypb.Any, m proto.Message) bool {
if any == nil || m == nil { if any == nil || m == nil {
return false return false
@ -119,6 +130,9 @@ func Is(any *anypb.Any, m proto.Message) bool {
// var x ptypes.DynamicAny // var x ptypes.DynamicAny
// if err := ptypes.UnmarshalAny(a, &x); err != nil { ... } // if err := ptypes.UnmarshalAny(a, &x); err != nil { ... }
// fmt.Printf("unmarshaled message: %v", x.Message) // fmt.Printf("unmarshaled message: %v", x.Message)
//
// Deprecated: Use the any.UnmarshalNew method instead to unmarshal
// the any message contents into a new instance of the underlying message.
type DynamicAny struct{ proto.Message } type DynamicAny struct{ proto.Message }
func (m DynamicAny) String() string { func (m DynamicAny) String() string {

View File

@ -3,4 +3,8 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package ptypes provides functionality for interacting with well-known types. // Package ptypes provides functionality for interacting with well-known types.
//
// Deprecated: Well-known types have specialized functionality directly
// injected into the generated packages for each message type.
// See the deprecation notice for each function for the suggested alternative.
package ptypes package ptypes

View File

@ -21,6 +21,8 @@ const (
// Duration converts a durationpb.Duration to a time.Duration. // Duration converts a durationpb.Duration to a time.Duration.
// Duration returns an error if dur is invalid or overflows a time.Duration. // Duration returns an error if dur is invalid or overflows a time.Duration.
//
// Deprecated: Call the dur.AsDuration and dur.CheckValid methods instead.
func Duration(dur *durationpb.Duration) (time.Duration, error) { func Duration(dur *durationpb.Duration) (time.Duration, error) {
if err := validateDuration(dur); err != nil { if err := validateDuration(dur); err != nil {
return 0, err return 0, err
@ -39,6 +41,8 @@ func Duration(dur *durationpb.Duration) (time.Duration, error) {
} }
// DurationProto converts a time.Duration to a durationpb.Duration. // DurationProto converts a time.Duration to a durationpb.Duration.
//
// Deprecated: Call the durationpb.New function instead.
func DurationProto(d time.Duration) *durationpb.Duration { func DurationProto(d time.Duration) *durationpb.Duration {
nanos := d.Nanoseconds() nanos := d.Nanoseconds()
secs := nanos / 1e9 secs := nanos / 1e9

View File

@ -33,6 +33,8 @@ const (
// //
// A nil Timestamp returns an error. The first return value in that case is // A nil Timestamp returns an error. The first return value in that case is
// undefined. // undefined.
//
// Deprecated: Call the ts.AsTime and ts.CheckValid methods instead.
func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) { func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) {
// Don't return the zero value on error, because corresponds to a valid // Don't return the zero value on error, because corresponds to a valid
// timestamp. Instead return whatever time.Unix gives us. // timestamp. Instead return whatever time.Unix gives us.
@ -46,6 +48,8 @@ func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) {
} }
// TimestampNow returns a google.protobuf.Timestamp for the current time. // TimestampNow returns a google.protobuf.Timestamp for the current time.
//
// Deprecated: Call the timestamppb.Now function instead.
func TimestampNow() *timestamppb.Timestamp { func TimestampNow() *timestamppb.Timestamp {
ts, err := TimestampProto(time.Now()) ts, err := TimestampProto(time.Now())
if err != nil { if err != nil {
@ -56,6 +60,8 @@ func TimestampNow() *timestamppb.Timestamp {
// TimestampProto converts the time.Time to a google.protobuf.Timestamp proto. // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
// It returns an error if the resulting Timestamp is invalid. // It returns an error if the resulting Timestamp is invalid.
//
// Deprecated: Call the timestamppb.New function instead.
func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) { func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) {
ts := &timestamppb.Timestamp{ ts := &timestamppb.Timestamp{
Seconds: t.Unix(), Seconds: t.Unix(),
@ -69,6 +75,9 @@ func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) {
// TimestampString returns the RFC 3339 string for valid Timestamps. // TimestampString returns the RFC 3339 string for valid Timestamps.
// For invalid Timestamps, it returns an error message in parentheses. // For invalid Timestamps, it returns an error message in parentheses.
//
// Deprecated: Call the ts.AsTime method instead,
// followed by a call to the Format method on the time.Time value.
func TimestampString(ts *timestamppb.Timestamp) string { func TimestampString(ts *timestamppb.Timestamp) string {
t, err := Timestamp(ts) t, err := Timestamp(ts)
if err != nil { if err != nil {

View File

@ -2,6 +2,22 @@ package types
import "time" import "time"
// Secret for underlying orchestrator
type Secret struct {
// Name of the secret
Name string `json:"name"`
// Namespace if applicable for the secret
Namespace string `json:"namespace,omitempty"`
// Value is a string representing the string's value
Value string `json:"value,omitempty"`
// RawValue can be used to provide binary data when
// Value is not set
RawValue []byte `json:"rawValue,omitempty"`
}
// FunctionDeployment represents a request to create or update a Function. // FunctionDeployment represents a request to create or update a Function.
type FunctionDeployment struct { type FunctionDeployment struct {
@ -46,13 +62,6 @@ type FunctionDeployment struct {
ReadOnlyRootFilesystem bool `json:"readOnlyRootFilesystem,omitempty"` ReadOnlyRootFilesystem bool `json:"readOnlyRootFilesystem,omitempty"`
} }
// Secret for underlying orchestrator
type Secret struct {
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Value string `json:"value,omitempty"`
}
// FunctionResources Memory and CPU // FunctionResources Memory and CPU
type FunctionResources struct { type FunctionResources struct {
Memory string `json:"memory,omitempty"` Memory string `json:"memory,omitempty"`

View File

@ -1,7 +1,4 @@
// Copyright (c) Alex Ellis 2017. All rights reserved. package types
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package queue
import ( import (
"net/http" "net/http"
@ -9,7 +6,7 @@ import (
) )
// Request for asynchronous processing // Request for asynchronous processing
type Request struct { type QueueRequest struct {
// Header from HTTP request // Header from HTTP request
Header http.Header Header http.Header
@ -41,5 +38,5 @@ type Request struct {
// RequestQueuer can public a request to be executed asynchronously // RequestQueuer can public a request to be executed asynchronously
type RequestQueuer interface { type RequestQueuer interface {
Queue(req *Request) error Queue(req *QueueRequest) error
} }

View File

@ -8,7 +8,7 @@ import (
"time" "time"
stan "github.com/nats-io/stan.go" stan "github.com/nats-io/stan.go"
"github.com/openfaas/faas/gateway/queue" ftypes "github.com/openfaas/faas-provider/types"
) )
// NATSQueue queue for work // NATSQueue queue for work
@ -32,7 +32,7 @@ type NATSQueue struct {
} }
// Queue request for processing // Queue request for processing
func (q *NATSQueue) Queue(req *queue.Request) error { func (q *NATSQueue) Queue(req *ftypes.QueueRequest) error {
fmt.Printf("NatsQueue - submitting request: %s.\n", req.Function) fmt.Printf("NatsQueue - submitting request: %s.\n", req.Function)
out, err := json.Marshal(req) out, err := json.Marshal(req)

View File

@ -6,14 +6,13 @@ package prototext
import ( import (
"fmt" "fmt"
"strings"
"unicode/utf8" "unicode/utf8"
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/encoding/text" "google.golang.org/protobuf/internal/encoding/text"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/fieldnum"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/pragma" "google.golang.org/protobuf/internal/pragma"
"google.golang.org/protobuf/internal/set" "google.golang.org/protobuf/internal/set"
"google.golang.org/protobuf/internal/strs" "google.golang.org/protobuf/internal/strs"
@ -23,6 +22,7 @@ import (
) )
// Unmarshal reads the given []byte into the given proto.Message. // Unmarshal reads the given []byte into the given proto.Message.
// The provided message must be mutable (e.g., a non-nil pointer to a message).
func Unmarshal(b []byte, m proto.Message) error { func Unmarshal(b []byte, m proto.Message) error {
return UnmarshalOptions{}.Unmarshal(b, m) return UnmarshalOptions{}.Unmarshal(b, m)
} }
@ -51,9 +51,17 @@ type UnmarshalOptions struct {
} }
} }
// Unmarshal reads the given []byte and populates the given proto.Message using options in // Unmarshal reads the given []byte and populates the given proto.Message
// UnmarshalOptions object. // using options in the UnmarshalOptions object.
// The provided message must be mutable (e.g., a non-nil pointer to a message).
func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error { func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
return o.unmarshal(b, m)
}
// unmarshal is a centralized function that all unmarshal operations go through.
// For profiling purposes, avoid changing the name of this function or
// introducing other code paths for unmarshal that do not go through this.
func (o UnmarshalOptions) unmarshal(b []byte, m proto.Message) error {
proto.Reset(m) proto.Reset(m)
if o.Resolver == nil { if o.Resolver == nil {
@ -101,7 +109,7 @@ func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error {
return errors.New("no support for proto1 MessageSets") return errors.New("no support for proto1 MessageSets")
} }
if messageDesc.FullName() == "google.protobuf.Any" { if messageDesc.FullName() == genid.Any_message_fullname {
return d.unmarshalAny(m, checkDelims) return d.unmarshalAny(m, checkDelims)
} }
@ -151,21 +159,11 @@ func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error {
switch tok.NameKind() { switch tok.NameKind() {
case text.IdentName: case text.IdentName:
name = pref.Name(tok.IdentName()) name = pref.Name(tok.IdentName())
fd = fieldDescs.ByName(name) fd = fieldDescs.ByTextName(string(name))
if fd == nil {
// The proto name of a group field is in all lowercase,
// while the textproto field name is the group message name.
gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
fd = gd
}
} else if fd.Kind() == pref.GroupKind && fd.Message().Name() != name {
fd = nil // reset since field name is actually the message name
}
case text.TypeName: case text.TypeName:
// Handle extensions only. This code path is not for Any. // Handle extensions only. This code path is not for Any.
xt, xtErr = d.findExtension(pref.FullName(tok.TypeName())) xt, xtErr = d.opts.Resolver.FindExtensionByName(pref.FullName(tok.TypeName()))
case text.FieldNumber: case text.FieldNumber:
isFieldNumberName = true isFieldNumberName = true
@ -262,15 +260,6 @@ func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error {
return nil return nil
} }
// findExtension returns protoreflect.ExtensionType from the Resolver if found.
func (d decoder) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
xt, err := d.opts.Resolver.FindExtensionByName(xtName)
if err == nil {
return xt, nil
}
return messageset.FindMessageSetExtension(d.opts.Resolver, xtName)
}
// unmarshalSingular unmarshals a non-repeated field value specified by the // unmarshalSingular unmarshals a non-repeated field value specified by the
// given FieldDescriptor. // given FieldDescriptor.
func (d decoder) unmarshalSingular(fd pref.FieldDescriptor, m pref.Message) error { func (d decoder) unmarshalSingular(fd pref.FieldDescriptor, m pref.Message) error {
@ -531,14 +520,13 @@ Loop:
return d.unexpectedTokenError(tok) return d.unexpectedTokenError(tok)
} }
name := tok.IdentName() switch name := pref.Name(tok.IdentName()); name {
switch name { case genid.MapEntry_Key_field_name:
case "key":
if !tok.HasSeparator() { if !tok.HasSeparator() {
return d.syntaxError(tok.Pos(), "missing field separator :") return d.syntaxError(tok.Pos(), "missing field separator :")
} }
if key.IsValid() { if key.IsValid() {
return d.newError(tok.Pos(), `map entry "key" cannot be repeated`) return d.newError(tok.Pos(), "map entry %q cannot be repeated", name)
} }
val, err := d.unmarshalScalar(fd.MapKey()) val, err := d.unmarshalScalar(fd.MapKey())
if err != nil { if err != nil {
@ -546,14 +534,14 @@ Loop:
} }
key = val.MapKey() key = val.MapKey()
case "value": case genid.MapEntry_Value_field_name:
if kind := fd.MapValue().Kind(); (kind != pref.MessageKind) && (kind != pref.GroupKind) { if kind := fd.MapValue().Kind(); (kind != pref.MessageKind) && (kind != pref.GroupKind) {
if !tok.HasSeparator() { if !tok.HasSeparator() {
return d.syntaxError(tok.Pos(), "missing field separator :") return d.syntaxError(tok.Pos(), "missing field separator :")
} }
} }
if pval.IsValid() { if pval.IsValid() {
return d.newError(tok.Pos(), `map entry "value" cannot be repeated`) return d.newError(tok.Pos(), "map entry %q cannot be repeated", name)
} }
pval, err = unmarshalMapValue() pval, err = unmarshalMapValue()
if err != nil { if err != nil {
@ -590,13 +578,9 @@ Loop:
func (d decoder) unmarshalAny(m pref.Message, checkDelims bool) error { func (d decoder) unmarshalAny(m pref.Message, checkDelims bool) error {
var typeURL string var typeURL string
var bValue []byte var bValue []byte
var seenTypeUrl bool
// hasFields tracks which valid fields have been seen in the loop below in var seenValue bool
// order to flag an error if there are duplicates or conflicts. It may var isExpanded bool
// contain the strings "type_url", "value" and "expanded". The literal
// "expanded" is used to indicate that the expanded form has been
// encountered already.
hasFields := map[string]bool{}
if checkDelims { if checkDelims {
tok, err := d.Read() tok, err := d.Read()
@ -635,12 +619,12 @@ Loop:
return d.syntaxError(tok.Pos(), "missing field separator :") return d.syntaxError(tok.Pos(), "missing field separator :")
} }
switch tok.IdentName() { switch name := pref.Name(tok.IdentName()); name {
case "type_url": case genid.Any_TypeUrl_field_name:
if hasFields["type_url"] { if seenTypeUrl {
return d.newError(tok.Pos(), "duplicate Any type_url field") return d.newError(tok.Pos(), "duplicate %v field", genid.Any_TypeUrl_field_fullname)
} }
if hasFields["expanded"] { if isExpanded {
return d.newError(tok.Pos(), "conflict with [%s] field", typeURL) return d.newError(tok.Pos(), "conflict with [%s] field", typeURL)
} }
tok, err := d.Read() tok, err := d.Read()
@ -650,15 +634,15 @@ Loop:
var ok bool var ok bool
typeURL, ok = tok.String() typeURL, ok = tok.String()
if !ok { if !ok {
return d.newError(tok.Pos(), "invalid Any type_url: %v", tok.RawString()) return d.newError(tok.Pos(), "invalid %v field value: %v", genid.Any_TypeUrl_field_fullname, tok.RawString())
} }
hasFields["type_url"] = true seenTypeUrl = true
case "value": case genid.Any_Value_field_name:
if hasFields["value"] { if seenValue {
return d.newError(tok.Pos(), "duplicate Any value field") return d.newError(tok.Pos(), "duplicate %v field", genid.Any_Value_field_fullname)
} }
if hasFields["expanded"] { if isExpanded {
return d.newError(tok.Pos(), "conflict with [%s] field", typeURL) return d.newError(tok.Pos(), "conflict with [%s] field", typeURL)
} }
tok, err := d.Read() tok, err := d.Read()
@ -667,22 +651,22 @@ Loop:
} }
s, ok := tok.String() s, ok := tok.String()
if !ok { if !ok {
return d.newError(tok.Pos(), "invalid Any value: %v", tok.RawString()) return d.newError(tok.Pos(), "invalid %v field value: %v", genid.Any_Value_field_fullname, tok.RawString())
} }
bValue = []byte(s) bValue = []byte(s)
hasFields["value"] = true seenValue = true
default: default:
if !d.opts.DiscardUnknown { if !d.opts.DiscardUnknown {
return d.newError(tok.Pos(), "invalid field name %q in google.protobuf.Any message", tok.RawString()) return d.newError(tok.Pos(), "invalid field name %q in %v message", tok.RawString(), genid.Any_message_fullname)
} }
} }
case text.TypeName: case text.TypeName:
if hasFields["expanded"] { if isExpanded {
return d.newError(tok.Pos(), "cannot have more than one type") return d.newError(tok.Pos(), "cannot have more than one type")
} }
if hasFields["type_url"] { if seenTypeUrl {
return d.newError(tok.Pos(), "conflict with type_url field") return d.newError(tok.Pos(), "conflict with type_url field")
} }
typeURL = tok.TypeName() typeURL = tok.TypeName()
@ -691,21 +675,21 @@ Loop:
if err != nil { if err != nil {
return err return err
} }
hasFields["expanded"] = true isExpanded = true
default: default:
if !d.opts.DiscardUnknown { if !d.opts.DiscardUnknown {
return d.newError(tok.Pos(), "invalid field name %q in google.protobuf.Any message", tok.RawString()) return d.newError(tok.Pos(), "invalid field name %q in %v message", tok.RawString(), genid.Any_message_fullname)
} }
} }
} }
fds := m.Descriptor().Fields() fds := m.Descriptor().Fields()
if len(typeURL) > 0 { if len(typeURL) > 0 {
m.Set(fds.ByNumber(fieldnum.Any_TypeUrl), pref.ValueOfString(typeURL)) m.Set(fds.ByNumber(genid.Any_TypeUrl_field_number), pref.ValueOfString(typeURL))
} }
if len(bValue) > 0 { if len(bValue) > 0 {
m.Set(fds.ByNumber(fieldnum.Any_Value), pref.ValueOfBytes(bValue)) m.Set(fds.ByNumber(genid.Any_Value_field_number), pref.ValueOfBytes(bValue))
} }
return nil return nil
} }

View File

@ -6,7 +6,6 @@ package prototext
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"unicode/utf8" "unicode/utf8"
@ -14,12 +13,13 @@ import (
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/encoding/text" "google.golang.org/protobuf/internal/encoding/text"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/fieldnum"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/mapsort" "google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/order"
"google.golang.org/protobuf/internal/pragma" "google.golang.org/protobuf/internal/pragma"
"google.golang.org/protobuf/internal/strs" "google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/reflect/protoregistry"
) )
@ -102,6 +102,13 @@ func (o MarshalOptions) Format(m proto.Message) string {
// MarshalOptions object. Do not depend on the output being stable. It may // MarshalOptions object. Do not depend on the output being stable. It may
// change over time across different versions of the program. // change over time across different versions of the program.
func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) { func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
return o.marshal(m)
}
// marshal is a centralized function that all marshal operations go through.
// For profiling purposes, avoid changing the name of this function or
// introducing other code paths for marshal that do not go through this.
func (o MarshalOptions) marshal(m proto.Message) ([]byte, error) {
var delims = [2]byte{'{', '}'} var delims = [2]byte{'{', '}'}
if o.Multiline && o.Indent == "" { if o.Multiline && o.Indent == "" {
@ -155,42 +162,22 @@ func (e encoder) marshalMessage(m pref.Message, inclDelims bool) error {
} }
// Handle Any expansion. // Handle Any expansion.
if messageDesc.FullName() == "google.protobuf.Any" { if messageDesc.FullName() == genid.Any_message_fullname {
if e.marshalAny(m) { if e.marshalAny(m) {
return nil return nil
} }
// If unable to expand, continue on to marshal Any as a regular message. // If unable to expand, continue on to marshal Any as a regular message.
} }
// Marshal known fields. // Marshal fields.
fieldDescs := messageDesc.Fields() var err error
size := fieldDescs.Len() order.RangeFields(m, order.IndexNameFieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
for i := 0; i < size; { if err = e.marshalField(fd.TextName(), v, fd); err != nil {
fd := fieldDescs.Get(i) return false
if od := fd.ContainingOneof(); od != nil {
fd = m.WhichOneof(od)
i += od.Fields().Len()
} else {
i++
} }
return true
if fd == nil || !m.Has(fd) { })
continue if err != nil {
}
name := fd.Name()
// Use type name for group field name.
if fd.Kind() == pref.GroupKind {
name = fd.Message().Name()
}
val := m.Get(fd)
if err := e.marshalField(string(name), val, fd); err != nil {
return err
}
}
// Marshal extensions.
if err := e.marshalExtensions(m); err != nil {
return err return err
} }
@ -283,18 +270,18 @@ func (e encoder) marshalList(name string, list pref.List, fd pref.FieldDescripto
// marshalMap marshals the given protoreflect.Map as multiple name-value fields. // marshalMap marshals the given protoreflect.Map as multiple name-value fields.
func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor) error { func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor) error {
var err error var err error
mapsort.Range(mmap, fd.MapKey().Kind(), func(key pref.MapKey, val pref.Value) bool { order.RangeEntries(mmap, order.GenericKeyOrder, func(key pref.MapKey, val pref.Value) bool {
e.WriteName(name) e.WriteName(name)
e.StartMessage() e.StartMessage()
defer e.EndMessage() defer e.EndMessage()
e.WriteName("key") e.WriteName(string(genid.MapEntry_Key_field_name))
err = e.marshalSingular(key.Value(), fd.MapKey()) err = e.marshalSingular(key.Value(), fd.MapKey())
if err != nil { if err != nil {
return false return false
} }
e.WriteName("value") e.WriteName(string(genid.MapEntry_Value_field_name))
err = e.marshalSingular(val, fd.MapValue()) err = e.marshalSingular(val, fd.MapValue())
if err != nil { if err != nil {
return false return false
@ -304,48 +291,6 @@ func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor)
return err return err
} }
// marshalExtensions marshals extension fields.
func (e encoder) marshalExtensions(m pref.Message) error {
type entry struct {
key string
value pref.Value
desc pref.FieldDescriptor
}
// Get a sorted list based on field key first.
var entries []entry
m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
if !fd.IsExtension() {
return true
}
// For MessageSet extensions, the name used is the parent message.
name := fd.FullName()
if messageset.IsMessageSetExtension(fd) {
name = name.Parent()
}
entries = append(entries, entry{
key: string(name),
value: v,
desc: fd,
})
return true
})
// Sort extensions lexicographically.
sort.Slice(entries, func(i, j int) bool {
return entries[i].key < entries[j].key
})
// Write out sorted list.
for _, entry := range entries {
// Extension field name is the proto field name enclosed in [].
name := "[" + entry.key + "]"
if err := e.marshalField(name, entry.value, entry.desc); err != nil {
return err
}
}
return nil
}
// marshalUnknown parses the given []byte and marshals fields out. // marshalUnknown parses the given []byte and marshals fields out.
// This function assumes proper encoding in the given []byte. // This function assumes proper encoding in the given []byte.
func (e encoder) marshalUnknown(b []byte) { func (e encoder) marshalUnknown(b []byte) {
@ -392,7 +337,7 @@ func (e encoder) marshalUnknown(b []byte) {
func (e encoder) marshalAny(any pref.Message) bool { func (e encoder) marshalAny(any pref.Message) bool {
// Construct the embedded message. // Construct the embedded message.
fds := any.Descriptor().Fields() fds := any.Descriptor().Fields()
fdType := fds.ByNumber(fieldnum.Any_TypeUrl) fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
typeURL := any.Get(fdType).String() typeURL := any.Get(fdType).String()
mt, err := e.opts.Resolver.FindMessageByURL(typeURL) mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
if err != nil { if err != nil {
@ -401,7 +346,7 @@ func (e encoder) marshalAny(any pref.Message) bool {
m := mt.New().Interface() m := mt.New().Interface()
// Unmarshal bytes into embedded message. // Unmarshal bytes into embedded message.
fdValue := fds.ByNumber(fieldnum.Any_Value) fdValue := fds.ByNumber(genid.Any_Value_field_number)
value := any.Get(fdValue) value := any.Get(fdValue)
err = proto.UnmarshalOptions{ err = proto.UnmarshalOptions{
AllowPartial: true, AllowPartial: true,

View File

@ -42,6 +42,8 @@ func formatListOpt(vs list, isRoot, allowMulti bool) string {
name = "FileImports" name = "FileImports"
case pref.Descriptor: case pref.Descriptor:
name = reflect.ValueOf(vs).MethodByName("Get").Type().Out(0).Name() + "s" name = reflect.ValueOf(vs).MethodByName("Get").Type().Out(0).Name() + "s"
default:
name = reflect.ValueOf(vs).Elem().Type().Name()
} }
start, end = name+"{", "}" start, end = name+"{", "}"
} }

View File

@ -26,6 +26,14 @@ func Bool() bool {
return randSeed%2 == 1 return randSeed%2 == 1
} }
// Intn returns a deterministically random integer between 0 and n-1, inclusive.
func Intn(n int) int {
if n <= 0 {
panic("must be positive")
}
return int(randSeed % uint64(n))
}
// randSeed is a best-effort at an approximate hash of the Go binary. // randSeed is a best-effort at an approximate hash of the Go binary.
var randSeed = binaryHash() var randSeed = binaryHash()

View File

@ -11,10 +11,9 @@ import (
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry"
) )
// The MessageSet wire format is equivalent to a message defiend as follows, // The MessageSet wire format is equivalent to a message defined as follows,
// where each Item defines an extension field with a field number of 'type_id' // where each Item defines an extension field with a field number of 'type_id'
// and content of 'message'. MessageSet extensions must be non-repeated message // and content of 'message'. MessageSet extensions must be non-repeated message
// fields. // fields.
@ -48,33 +47,17 @@ func IsMessageSet(md pref.MessageDescriptor) bool {
return ok && xmd.IsMessageSet() return ok && xmd.IsMessageSet()
} }
// IsMessageSetExtension reports this field extends a MessageSet. // IsMessageSetExtension reports this field properly extends a MessageSet.
func IsMessageSetExtension(fd pref.FieldDescriptor) bool { func IsMessageSetExtension(fd pref.FieldDescriptor) bool {
if fd.Name() != ExtensionName { switch {
case fd.Name() != ExtensionName:
return false
case !IsMessageSet(fd.ContainingMessage()):
return false
case fd.FullName().Parent() != fd.Message().FullName():
return false return false
} }
if fd.FullName().Parent() != fd.Message().FullName() { return true
return false
}
return IsMessageSet(fd.ContainingMessage())
}
// FindMessageSetExtension locates a MessageSet extension field by name.
// In text and JSON formats, the extension name used is the message itself.
// The extension field name is derived by appending ExtensionName.
func FindMessageSetExtension(r preg.ExtensionTypeResolver, s pref.FullName) (pref.ExtensionType, error) {
name := s.Append(ExtensionName)
xt, err := r.FindExtensionByName(name)
if err != nil {
if err == preg.NotFound {
return nil, err
}
return nil, errors.Wrap(err, "%q", name)
}
if !IsMessageSetExtension(xt.TypeDescriptor()) {
return nil, preg.NotFound
}
return xt, nil
} }
// SizeField returns the size of a MessageSet item field containing an extension // SizeField returns the size of a MessageSet item field containing an extension

View File

@ -104,7 +104,7 @@ func Unmarshal(tag string, goType reflect.Type, evs pref.EnumValueDescriptors) p
case strings.HasPrefix(s, "json="): case strings.HasPrefix(s, "json="):
jsonName := s[len("json="):] jsonName := s[len("json="):]
if jsonName != strs.JSONCamelCase(string(f.L0.FullName.Name())) { if jsonName != strs.JSONCamelCase(string(f.L0.FullName.Name())) {
f.L1.JSONName.Init(jsonName) f.L1.StringName.InitJSON(jsonName)
} }
case s == "packed": case s == "packed":
f.L1.HasPacked = true f.L1.HasPacked = true

View File

@ -32,7 +32,6 @@ type Encoder struct {
encoderState encoderState
indent string indent string
newline string // set to "\n" if len(indent) > 0
delims [2]byte delims [2]byte
outputASCII bool outputASCII bool
} }
@ -61,7 +60,6 @@ func NewEncoder(indent string, delims [2]byte, outputASCII bool) (*Encoder, erro
return nil, errors.New("indent may only be composed of space and tab characters") return nil, errors.New("indent may only be composed of space and tab characters")
} }
e.indent = indent e.indent = indent
e.newline = "\n"
} }
switch delims { switch delims {
case [2]byte{0, 0}: case [2]byte{0, 0}:
@ -126,7 +124,7 @@ func appendString(out []byte, in string, outputASCII bool) []byte {
// are used to represent both the proto string and bytes type. // are used to represent both the proto string and bytes type.
r = rune(in[0]) r = rune(in[0])
fallthrough fallthrough
case r < ' ' || r == '"' || r == '\\': case r < ' ' || r == '"' || r == '\\' || r == 0x7f:
out = append(out, '\\') out = append(out, '\\')
switch r { switch r {
case '"', '\\': case '"', '\\':
@ -143,7 +141,7 @@ func appendString(out []byte, in string, outputASCII bool) []byte {
out = strconv.AppendUint(out, uint64(r), 16) out = strconv.AppendUint(out, uint64(r), 16)
} }
in = in[n:] in = in[n:]
case outputASCII && r >= utf8.RuneSelf: case r >= utf8.RuneSelf && (outputASCII || r <= 0x009f):
out = append(out, '\\') out = append(out, '\\')
if r <= math.MaxUint16 { if r <= math.MaxUint16 {
out = append(out, 'u') out = append(out, 'u')
@ -168,7 +166,7 @@ func appendString(out []byte, in string, outputASCII bool) []byte {
// escaping. If no characters need escaping, this returns the input length. // escaping. If no characters need escaping, this returns the input length.
func indexNeedEscapeInString(s string) int { func indexNeedEscapeInString(s string) int {
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if c := s[i]; c < ' ' || c == '"' || c == '\'' || c == '\\' || c >= utf8.RuneSelf { if c := s[i]; c < ' ' || c == '"' || c == '\'' || c == '\\' || c >= 0x7f {
return i return i
} }
} }

View File

@ -1,13 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Any.
const (
Any_TypeUrl = 1 // optional string
Any_Value = 2 // optional bytes
)

View File

@ -1,35 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Api.
const (
Api_Name = 1 // optional string
Api_Methods = 2 // repeated google.protobuf.Method
Api_Options = 3 // repeated google.protobuf.Option
Api_Version = 4 // optional string
Api_SourceContext = 5 // optional google.protobuf.SourceContext
Api_Mixins = 6 // repeated google.protobuf.Mixin
Api_Syntax = 7 // optional google.protobuf.Syntax
)
// Field numbers for google.protobuf.Method.
const (
Method_Name = 1 // optional string
Method_RequestTypeUrl = 2 // optional string
Method_RequestStreaming = 3 // optional bool
Method_ResponseTypeUrl = 4 // optional string
Method_ResponseStreaming = 5 // optional bool
Method_Options = 6 // repeated google.protobuf.Option
Method_Syntax = 7 // optional google.protobuf.Syntax
)
// Field numbers for google.protobuf.Mixin.
const (
Mixin_Name = 1 // optional string
Mixin_Root = 2 // optional string
)

View File

@ -1,240 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.FileDescriptorSet.
const (
FileDescriptorSet_File = 1 // repeated google.protobuf.FileDescriptorProto
)
// Field numbers for google.protobuf.FileDescriptorProto.
const (
FileDescriptorProto_Name = 1 // optional string
FileDescriptorProto_Package = 2 // optional string
FileDescriptorProto_Dependency = 3 // repeated string
FileDescriptorProto_PublicDependency = 10 // repeated int32
FileDescriptorProto_WeakDependency = 11 // repeated int32
FileDescriptorProto_MessageType = 4 // repeated google.protobuf.DescriptorProto
FileDescriptorProto_EnumType = 5 // repeated google.protobuf.EnumDescriptorProto
FileDescriptorProto_Service = 6 // repeated google.protobuf.ServiceDescriptorProto
FileDescriptorProto_Extension = 7 // repeated google.protobuf.FieldDescriptorProto
FileDescriptorProto_Options = 8 // optional google.protobuf.FileOptions
FileDescriptorProto_SourceCodeInfo = 9 // optional google.protobuf.SourceCodeInfo
FileDescriptorProto_Syntax = 12 // optional string
)
// Field numbers for google.protobuf.DescriptorProto.
const (
DescriptorProto_Name = 1 // optional string
DescriptorProto_Field = 2 // repeated google.protobuf.FieldDescriptorProto
DescriptorProto_Extension = 6 // repeated google.protobuf.FieldDescriptorProto
DescriptorProto_NestedType = 3 // repeated google.protobuf.DescriptorProto
DescriptorProto_EnumType = 4 // repeated google.protobuf.EnumDescriptorProto
DescriptorProto_ExtensionRange = 5 // repeated google.protobuf.DescriptorProto.ExtensionRange
DescriptorProto_OneofDecl = 8 // repeated google.protobuf.OneofDescriptorProto
DescriptorProto_Options = 7 // optional google.protobuf.MessageOptions
DescriptorProto_ReservedRange = 9 // repeated google.protobuf.DescriptorProto.ReservedRange
DescriptorProto_ReservedName = 10 // repeated string
)
// Field numbers for google.protobuf.DescriptorProto.ExtensionRange.
const (
DescriptorProto_ExtensionRange_Start = 1 // optional int32
DescriptorProto_ExtensionRange_End = 2 // optional int32
DescriptorProto_ExtensionRange_Options = 3 // optional google.protobuf.ExtensionRangeOptions
)
// Field numbers for google.protobuf.DescriptorProto.ReservedRange.
const (
DescriptorProto_ReservedRange_Start = 1 // optional int32
DescriptorProto_ReservedRange_End = 2 // optional int32
)
// Field numbers for google.protobuf.ExtensionRangeOptions.
const (
ExtensionRangeOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.FieldDescriptorProto.
const (
FieldDescriptorProto_Name = 1 // optional string
FieldDescriptorProto_Number = 3 // optional int32
FieldDescriptorProto_Label = 4 // optional google.protobuf.FieldDescriptorProto.Label
FieldDescriptorProto_Type = 5 // optional google.protobuf.FieldDescriptorProto.Type
FieldDescriptorProto_TypeName = 6 // optional string
FieldDescriptorProto_Extendee = 2 // optional string
FieldDescriptorProto_DefaultValue = 7 // optional string
FieldDescriptorProto_OneofIndex = 9 // optional int32
FieldDescriptorProto_JsonName = 10 // optional string
FieldDescriptorProto_Options = 8 // optional google.protobuf.FieldOptions
FieldDescriptorProto_Proto3Optional = 17 // optional bool
)
// Field numbers for google.protobuf.OneofDescriptorProto.
const (
OneofDescriptorProto_Name = 1 // optional string
OneofDescriptorProto_Options = 2 // optional google.protobuf.OneofOptions
)
// Field numbers for google.protobuf.EnumDescriptorProto.
const (
EnumDescriptorProto_Name = 1 // optional string
EnumDescriptorProto_Value = 2 // repeated google.protobuf.EnumValueDescriptorProto
EnumDescriptorProto_Options = 3 // optional google.protobuf.EnumOptions
EnumDescriptorProto_ReservedRange = 4 // repeated google.protobuf.EnumDescriptorProto.EnumReservedRange
EnumDescriptorProto_ReservedName = 5 // repeated string
)
// Field numbers for google.protobuf.EnumDescriptorProto.EnumReservedRange.
const (
EnumDescriptorProto_EnumReservedRange_Start = 1 // optional int32
EnumDescriptorProto_EnumReservedRange_End = 2 // optional int32
)
// Field numbers for google.protobuf.EnumValueDescriptorProto.
const (
EnumValueDescriptorProto_Name = 1 // optional string
EnumValueDescriptorProto_Number = 2 // optional int32
EnumValueDescriptorProto_Options = 3 // optional google.protobuf.EnumValueOptions
)
// Field numbers for google.protobuf.ServiceDescriptorProto.
const (
ServiceDescriptorProto_Name = 1 // optional string
ServiceDescriptorProto_Method = 2 // repeated google.protobuf.MethodDescriptorProto
ServiceDescriptorProto_Options = 3 // optional google.protobuf.ServiceOptions
)
// Field numbers for google.protobuf.MethodDescriptorProto.
const (
MethodDescriptorProto_Name = 1 // optional string
MethodDescriptorProto_InputType = 2 // optional string
MethodDescriptorProto_OutputType = 3 // optional string
MethodDescriptorProto_Options = 4 // optional google.protobuf.MethodOptions
MethodDescriptorProto_ClientStreaming = 5 // optional bool
MethodDescriptorProto_ServerStreaming = 6 // optional bool
)
// Field numbers for google.protobuf.FileOptions.
const (
FileOptions_JavaPackage = 1 // optional string
FileOptions_JavaOuterClassname = 8 // optional string
FileOptions_JavaMultipleFiles = 10 // optional bool
FileOptions_JavaGenerateEqualsAndHash = 20 // optional bool
FileOptions_JavaStringCheckUtf8 = 27 // optional bool
FileOptions_OptimizeFor = 9 // optional google.protobuf.FileOptions.OptimizeMode
FileOptions_GoPackage = 11 // optional string
FileOptions_CcGenericServices = 16 // optional bool
FileOptions_JavaGenericServices = 17 // optional bool
FileOptions_PyGenericServices = 18 // optional bool
FileOptions_PhpGenericServices = 42 // optional bool
FileOptions_Deprecated = 23 // optional bool
FileOptions_CcEnableArenas = 31 // optional bool
FileOptions_ObjcClassPrefix = 36 // optional string
FileOptions_CsharpNamespace = 37 // optional string
FileOptions_SwiftPrefix = 39 // optional string
FileOptions_PhpClassPrefix = 40 // optional string
FileOptions_PhpNamespace = 41 // optional string
FileOptions_PhpMetadataNamespace = 44 // optional string
FileOptions_RubyPackage = 45 // optional string
FileOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.MessageOptions.
const (
MessageOptions_MessageSetWireFormat = 1 // optional bool
MessageOptions_NoStandardDescriptorAccessor = 2 // optional bool
MessageOptions_Deprecated = 3 // optional bool
MessageOptions_MapEntry = 7 // optional bool
MessageOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.FieldOptions.
const (
FieldOptions_Ctype = 1 // optional google.protobuf.FieldOptions.CType
FieldOptions_Packed = 2 // optional bool
FieldOptions_Jstype = 6 // optional google.protobuf.FieldOptions.JSType
FieldOptions_Lazy = 5 // optional bool
FieldOptions_Deprecated = 3 // optional bool
FieldOptions_Weak = 10 // optional bool
FieldOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.OneofOptions.
const (
OneofOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.EnumOptions.
const (
EnumOptions_AllowAlias = 2 // optional bool
EnumOptions_Deprecated = 3 // optional bool
EnumOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.EnumValueOptions.
const (
EnumValueOptions_Deprecated = 1 // optional bool
EnumValueOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.ServiceOptions.
const (
ServiceOptions_Deprecated = 33 // optional bool
ServiceOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.MethodOptions.
const (
MethodOptions_Deprecated = 33 // optional bool
MethodOptions_IdempotencyLevel = 34 // optional google.protobuf.MethodOptions.IdempotencyLevel
MethodOptions_UninterpretedOption = 999 // repeated google.protobuf.UninterpretedOption
)
// Field numbers for google.protobuf.UninterpretedOption.
const (
UninterpretedOption_Name = 2 // repeated google.protobuf.UninterpretedOption.NamePart
UninterpretedOption_IdentifierValue = 3 // optional string
UninterpretedOption_PositiveIntValue = 4 // optional uint64
UninterpretedOption_NegativeIntValue = 5 // optional int64
UninterpretedOption_DoubleValue = 6 // optional double
UninterpretedOption_StringValue = 7 // optional bytes
UninterpretedOption_AggregateValue = 8 // optional string
)
// Field numbers for google.protobuf.UninterpretedOption.NamePart.
const (
UninterpretedOption_NamePart_NamePart = 1 // required string
UninterpretedOption_NamePart_IsExtension = 2 // required bool
)
// Field numbers for google.protobuf.SourceCodeInfo.
const (
SourceCodeInfo_Location = 1 // repeated google.protobuf.SourceCodeInfo.Location
)
// Field numbers for google.protobuf.SourceCodeInfo.Location.
const (
SourceCodeInfo_Location_Path = 1 // repeated int32
SourceCodeInfo_Location_Span = 2 // repeated int32
SourceCodeInfo_Location_LeadingComments = 3 // optional string
SourceCodeInfo_Location_TrailingComments = 4 // optional string
SourceCodeInfo_Location_LeadingDetachedComments = 6 // repeated string
)
// Field numbers for google.protobuf.GeneratedCodeInfo.
const (
GeneratedCodeInfo_Annotation = 1 // repeated google.protobuf.GeneratedCodeInfo.Annotation
)
// Field numbers for google.protobuf.GeneratedCodeInfo.Annotation.
const (
GeneratedCodeInfo_Annotation_Path = 1 // repeated int32
GeneratedCodeInfo_Annotation_SourceFile = 2 // optional string
GeneratedCodeInfo_Annotation_Begin = 3 // optional int32
GeneratedCodeInfo_Annotation_End = 4 // optional int32
)

View File

@ -1,7 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fieldnum contains constants for field numbers of fields in messages
// declared in descriptor.proto and any of the well-known types.
package fieldnum

View File

@ -1,13 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Duration.
const (
Duration_Seconds = 1 // optional int64
Duration_Nanos = 2 // optional int32
)

View File

@ -1,10 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Empty.
const ()

View File

@ -1,12 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.FieldMask.
const (
FieldMask_Paths = 1 // repeated string
)

View File

@ -1,12 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.SourceContext.
const (
SourceContext_FileName = 1 // optional string
)

View File

@ -1,33 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Struct.
const (
Struct_Fields = 1 // repeated google.protobuf.Struct.FieldsEntry
)
// Field numbers for google.protobuf.Struct.FieldsEntry.
const (
Struct_FieldsEntry_Key = 1 // optional string
Struct_FieldsEntry_Value = 2 // optional google.protobuf.Value
)
// Field numbers for google.protobuf.Value.
const (
Value_NullValue = 1 // optional google.protobuf.NullValue
Value_NumberValue = 2 // optional double
Value_StringValue = 3 // optional string
Value_BoolValue = 4 // optional bool
Value_StructValue = 5 // optional google.protobuf.Struct
Value_ListValue = 6 // optional google.protobuf.ListValue
)
// Field numbers for google.protobuf.ListValue.
const (
ListValue_Values = 1 // repeated google.protobuf.Value
)

View File

@ -1,13 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Timestamp.
const (
Timestamp_Seconds = 1 // optional int64
Timestamp_Nanos = 2 // optional int32
)

View File

@ -1,53 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.Type.
const (
Type_Name = 1 // optional string
Type_Fields = 2 // repeated google.protobuf.Field
Type_Oneofs = 3 // repeated string
Type_Options = 4 // repeated google.protobuf.Option
Type_SourceContext = 5 // optional google.protobuf.SourceContext
Type_Syntax = 6 // optional google.protobuf.Syntax
)
// Field numbers for google.protobuf.Field.
const (
Field_Kind = 1 // optional google.protobuf.Field.Kind
Field_Cardinality = 2 // optional google.protobuf.Field.Cardinality
Field_Number = 3 // optional int32
Field_Name = 4 // optional string
Field_TypeUrl = 6 // optional string
Field_OneofIndex = 7 // optional int32
Field_Packed = 8 // optional bool
Field_Options = 9 // repeated google.protobuf.Option
Field_JsonName = 10 // optional string
Field_DefaultValue = 11 // optional string
)
// Field numbers for google.protobuf.Enum.
const (
Enum_Name = 1 // optional string
Enum_Enumvalue = 2 // repeated google.protobuf.EnumValue
Enum_Options = 3 // repeated google.protobuf.Option
Enum_SourceContext = 4 // optional google.protobuf.SourceContext
Enum_Syntax = 5 // optional google.protobuf.Syntax
)
// Field numbers for google.protobuf.EnumValue.
const (
EnumValue_Name = 1 // optional string
EnumValue_Number = 2 // optional int32
EnumValue_Options = 3 // repeated google.protobuf.Option
)
// Field numbers for google.protobuf.Option.
const (
Option_Name = 1 // optional string
Option_Value = 2 // optional google.protobuf.Any
)

View File

@ -1,52 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package fieldnum
// Field numbers for google.protobuf.DoubleValue.
const (
DoubleValue_Value = 1 // optional double
)
// Field numbers for google.protobuf.FloatValue.
const (
FloatValue_Value = 1 // optional float
)
// Field numbers for google.protobuf.Int64Value.
const (
Int64Value_Value = 1 // optional int64
)
// Field numbers for google.protobuf.UInt64Value.
const (
UInt64Value_Value = 1 // optional uint64
)
// Field numbers for google.protobuf.Int32Value.
const (
Int32Value_Value = 1 // optional int32
)
// Field numbers for google.protobuf.UInt32Value.
const (
UInt32Value_Value = 1 // optional uint32
)
// Field numbers for google.protobuf.BoolValue.
const (
BoolValue_Value = 1 // optional bool
)
// Field numbers for google.protobuf.StringValue.
const (
StringValue_Value = 1 // optional string
)
// Field numbers for google.protobuf.BytesValue.
const (
BytesValue_Value = 1 // optional bytes
)

View File

@ -1,40 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fieldsort defines an ordering of fields.
//
// The ordering defined by this package matches the historic behavior of the proto
// package, placing extensions first and oneofs last.
//
// There is no guarantee about stability of the wire encoding, and users should not
// depend on the order defined in this package as it is subject to change without
// notice.
package fieldsort
import (
"google.golang.org/protobuf/reflect/protoreflect"
)
// Less returns true if field a comes before field j in ordered wire marshal output.
func Less(a, b protoreflect.FieldDescriptor) bool {
ea := a.IsExtension()
eb := b.IsExtension()
oa := a.ContainingOneof()
ob := b.ContainingOneof()
switch {
case ea != eb:
return ea
case oa != nil && ob != nil:
if oa == ob {
return a.Number() < b.Number()
}
return oa.Index() < ob.Index()
case oa != nil && !oa.IsSynthetic():
return false
case ob != nil && !ob.IsSynthetic():
return true
default:
return a.Number() < b.Number()
}
}

View File

@ -3,11 +3,14 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package filedesc provides functionality for constructing descriptors. // Package filedesc provides functionality for constructing descriptors.
//
// The types in this package implement interfaces in the protoreflect package
// related to protobuf descripriptors.
package filedesc package filedesc
import ( import (
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/fieldnum" "google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry" preg "google.golang.org/protobuf/reflect/protoregistry"
@ -126,24 +129,24 @@ func (db *Builder) unmarshalCounts(b []byte, isFile bool) {
b = b[m:] b = b[m:]
if isFile { if isFile {
switch num { switch num {
case fieldnum.FileDescriptorProto_EnumType: case genid.FileDescriptorProto_EnumType_field_number:
db.NumEnums++ db.NumEnums++
case fieldnum.FileDescriptorProto_MessageType: case genid.FileDescriptorProto_MessageType_field_number:
db.unmarshalCounts(v, false) db.unmarshalCounts(v, false)
db.NumMessages++ db.NumMessages++
case fieldnum.FileDescriptorProto_Extension: case genid.FileDescriptorProto_Extension_field_number:
db.NumExtensions++ db.NumExtensions++
case fieldnum.FileDescriptorProto_Service: case genid.FileDescriptorProto_Service_field_number:
db.NumServices++ db.NumServices++
} }
} else { } else {
switch num { switch num {
case fieldnum.DescriptorProto_EnumType: case genid.DescriptorProto_EnumType_field_number:
db.NumEnums++ db.NumEnums++
case fieldnum.DescriptorProto_NestedType: case genid.DescriptorProto_NestedType_field_number:
db.unmarshalCounts(v, false) db.unmarshalCounts(v, false)
db.NumMessages++ db.NumMessages++
case fieldnum.DescriptorProto_Extension: case genid.DescriptorProto_Extension_field_number:
db.NumExtensions++ db.NumExtensions++
} }
} }

View File

@ -13,6 +13,8 @@ import (
"google.golang.org/protobuf/internal/descfmt" "google.golang.org/protobuf/internal/descfmt"
"google.golang.org/protobuf/internal/descopts" "google.golang.org/protobuf/internal/descopts"
"google.golang.org/protobuf/internal/encoding/defval" "google.golang.org/protobuf/internal/encoding/defval"
"google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/pragma" "google.golang.org/protobuf/internal/pragma"
"google.golang.org/protobuf/internal/strs" "google.golang.org/protobuf/internal/strs"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
@ -98,15 +100,6 @@ func (fd *File) lazyInitOnce() {
fd.mu.Unlock() fd.mu.Unlock()
} }
// ProtoLegacyRawDesc is a pseudo-internal API for allowing the v1 code
// to be able to retrieve the raw descriptor.
//
// WARNING: This method is exempt from the compatibility promise and may be
// removed in the future without warning.
func (fd *File) ProtoLegacyRawDesc() []byte {
return fd.builder.RawDescriptor
}
// GoPackagePath is a pseudo-internal API for determining the Go package path // GoPackagePath is a pseudo-internal API for determining the Go package path
// that this file descriptor is declared in. // that this file descriptor is declared in.
// //
@ -206,7 +199,7 @@ type (
Number pref.FieldNumber Number pref.FieldNumber
Cardinality pref.Cardinality // must be consistent with Message.RequiredNumbers Cardinality pref.Cardinality // must be consistent with Message.RequiredNumbers
Kind pref.Kind Kind pref.Kind
JSONName jsonName StringName stringName
IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
IsWeak bool // promoted from google.protobuf.FieldOptions IsWeak bool // promoted from google.protobuf.FieldOptions
HasPacked bool // promoted from google.protobuf.FieldOptions HasPacked bool // promoted from google.protobuf.FieldOptions
@ -276,8 +269,9 @@ func (fd *Field) Options() pref.ProtoMessage {
func (fd *Field) Number() pref.FieldNumber { return fd.L1.Number } func (fd *Field) Number() pref.FieldNumber { return fd.L1.Number }
func (fd *Field) Cardinality() pref.Cardinality { return fd.L1.Cardinality } func (fd *Field) Cardinality() pref.Cardinality { return fd.L1.Cardinality }
func (fd *Field) Kind() pref.Kind { return fd.L1.Kind } func (fd *Field) Kind() pref.Kind { return fd.L1.Kind }
func (fd *Field) HasJSONName() bool { return fd.L1.JSONName.has } func (fd *Field) HasJSONName() bool { return fd.L1.StringName.hasJSON }
func (fd *Field) JSONName() string { return fd.L1.JSONName.get(fd) } func (fd *Field) JSONName() string { return fd.L1.StringName.getJSON(fd) }
func (fd *Field) TextName() string { return fd.L1.StringName.getText(fd) }
func (fd *Field) HasPresence() bool { func (fd *Field) HasPresence() bool {
return fd.L1.Cardinality != pref.Repeated && (fd.L0.ParentFile.L1.Syntax == pref.Proto2 || fd.L1.Message != nil || fd.L1.ContainingOneof != nil) return fd.L1.Cardinality != pref.Repeated && (fd.L0.ParentFile.L1.Syntax == pref.Proto2 || fd.L1.Message != nil || fd.L1.ContainingOneof != nil)
} }
@ -302,13 +296,13 @@ func (fd *Field) MapKey() pref.FieldDescriptor {
if !fd.IsMap() { if !fd.IsMap() {
return nil return nil
} }
return fd.Message().Fields().ByNumber(1) return fd.Message().Fields().ByNumber(genid.MapEntry_Key_field_number)
} }
func (fd *Field) MapValue() pref.FieldDescriptor { func (fd *Field) MapValue() pref.FieldDescriptor {
if !fd.IsMap() { if !fd.IsMap() {
return nil return nil
} }
return fd.Message().Fields().ByNumber(2) return fd.Message().Fields().ByNumber(genid.MapEntry_Value_field_number)
} }
func (fd *Field) HasDefault() bool { return fd.L1.Default.has } func (fd *Field) HasDefault() bool { return fd.L1.Default.has }
func (fd *Field) Default() pref.Value { return fd.L1.Default.get(fd) } func (fd *Field) Default() pref.Value { return fd.L1.Default.get(fd) }
@ -372,7 +366,7 @@ type (
} }
ExtensionL2 struct { ExtensionL2 struct {
Options func() pref.ProtoMessage Options func() pref.ProtoMessage
JSONName jsonName StringName stringName
IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto IsProto3Optional bool // promoted from google.protobuf.FieldDescriptorProto
IsPacked bool // promoted from google.protobuf.FieldOptions IsPacked bool // promoted from google.protobuf.FieldOptions
Default defaultValue Default defaultValue
@ -390,8 +384,9 @@ func (xd *Extension) Options() pref.ProtoMessage {
func (xd *Extension) Number() pref.FieldNumber { return xd.L1.Number } func (xd *Extension) Number() pref.FieldNumber { return xd.L1.Number }
func (xd *Extension) Cardinality() pref.Cardinality { return xd.L1.Cardinality } func (xd *Extension) Cardinality() pref.Cardinality { return xd.L1.Cardinality }
func (xd *Extension) Kind() pref.Kind { return xd.L1.Kind } func (xd *Extension) Kind() pref.Kind { return xd.L1.Kind }
func (xd *Extension) HasJSONName() bool { return xd.lazyInit().JSONName.has } func (xd *Extension) HasJSONName() bool { return xd.lazyInit().StringName.hasJSON }
func (xd *Extension) JSONName() string { return xd.lazyInit().JSONName.get(xd) } func (xd *Extension) JSONName() string { return xd.lazyInit().StringName.getJSON(xd) }
func (xd *Extension) TextName() string { return xd.lazyInit().StringName.getText(xd) }
func (xd *Extension) HasPresence() bool { return xd.L1.Cardinality != pref.Repeated } func (xd *Extension) HasPresence() bool { return xd.L1.Cardinality != pref.Repeated }
func (xd *Extension) HasOptionalKeyword() bool { func (xd *Extension) HasOptionalKeyword() bool {
return (xd.L0.ParentFile.L1.Syntax == pref.Proto2 && xd.L1.Cardinality == pref.Optional) || xd.lazyInit().IsProto3Optional return (xd.L0.ParentFile.L1.Syntax == pref.Proto2 && xd.L1.Cardinality == pref.Optional) || xd.lazyInit().IsProto3Optional
@ -505,27 +500,50 @@ func (d *Base) Syntax() pref.Syntax { return d.L0.ParentFile.Syn
func (d *Base) IsPlaceholder() bool { return false } func (d *Base) IsPlaceholder() bool { return false }
func (d *Base) ProtoInternal(pragma.DoNotImplement) {} func (d *Base) ProtoInternal(pragma.DoNotImplement) {}
type jsonName struct { type stringName struct {
has bool hasJSON bool
once sync.Once once sync.Once
name string nameJSON string
nameText string
} }
// Init initializes the name. It is exported for use by other internal packages. // InitJSON initializes the name. It is exported for use by other internal packages.
func (js *jsonName) Init(s string) { func (s *stringName) InitJSON(name string) {
js.has = true s.hasJSON = true
js.name = s s.nameJSON = name
} }
func (js *jsonName) get(fd pref.FieldDescriptor) string { func (s *stringName) lazyInit(fd pref.FieldDescriptor) *stringName {
if !js.has { s.once.Do(func() {
js.once.Do(func() { if fd.IsExtension() {
js.name = strs.JSONCamelCase(string(fd.Name())) // For extensions, JSON and text are formatted the same way.
}) var name string
} if messageset.IsMessageSetExtension(fd) {
return js.name name = string("[" + fd.FullName().Parent() + "]")
} else {
name = string("[" + fd.FullName() + "]")
}
s.nameJSON = name
s.nameText = name
} else {
// Format the JSON name.
if !s.hasJSON {
s.nameJSON = strs.JSONCamelCase(string(fd.Name()))
}
// Format the text name.
s.nameText = string(fd.Name())
if fd.Kind() == pref.GroupKind {
s.nameText = string(fd.Message().Name())
}
}
})
return s
} }
func (s *stringName) getJSON(fd pref.FieldDescriptor) string { return s.lazyInit(fd).nameJSON }
func (s *stringName) getText(fd pref.FieldDescriptor) string { return s.lazyInit(fd).nameText }
func DefaultValue(v pref.Value, ev pref.EnumValueDescriptor) defaultValue { func DefaultValue(v pref.Value, ev pref.EnumValueDescriptor) defaultValue {
dv := defaultValue{has: v.IsValid(), val: v, enum: ev} dv := defaultValue{has: v.IsValid(), val: v, enum: ev}
if b, ok := v.Interface().([]byte); ok { if b, ok := v.Interface().([]byte); ok {

View File

@ -8,7 +8,7 @@ import (
"sync" "sync"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/fieldnum" "google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/strs" "google.golang.org/protobuf/internal/strs"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
) )
@ -107,7 +107,7 @@ func (fd *File) unmarshalSeed(b []byte) {
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FileDescriptorProto_Syntax: case genid.FileDescriptorProto_Syntax_field_number:
switch string(v) { switch string(v) {
case "proto2": case "proto2":
fd.L1.Syntax = pref.Proto2 fd.L1.Syntax = pref.Proto2
@ -116,36 +116,36 @@ func (fd *File) unmarshalSeed(b []byte) {
default: default:
panic("invalid syntax") panic("invalid syntax")
} }
case fieldnum.FileDescriptorProto_Name: case genid.FileDescriptorProto_Name_field_number:
fd.L1.Path = sb.MakeString(v) fd.L1.Path = sb.MakeString(v)
case fieldnum.FileDescriptorProto_Package: case genid.FileDescriptorProto_Package_field_number:
fd.L1.Package = pref.FullName(sb.MakeString(v)) fd.L1.Package = pref.FullName(sb.MakeString(v))
case fieldnum.FileDescriptorProto_EnumType: case genid.FileDescriptorProto_EnumType_field_number:
if prevField != fieldnum.FileDescriptorProto_EnumType { if prevField != genid.FileDescriptorProto_EnumType_field_number {
if numEnums > 0 { if numEnums > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
posEnums = len(b0) - len(b) - n - m posEnums = len(b0) - len(b) - n - m
} }
numEnums++ numEnums++
case fieldnum.FileDescriptorProto_MessageType: case genid.FileDescriptorProto_MessageType_field_number:
if prevField != fieldnum.FileDescriptorProto_MessageType { if prevField != genid.FileDescriptorProto_MessageType_field_number {
if numMessages > 0 { if numMessages > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
posMessages = len(b0) - len(b) - n - m posMessages = len(b0) - len(b) - n - m
} }
numMessages++ numMessages++
case fieldnum.FileDescriptorProto_Extension: case genid.FileDescriptorProto_Extension_field_number:
if prevField != fieldnum.FileDescriptorProto_Extension { if prevField != genid.FileDescriptorProto_Extension_field_number {
if numExtensions > 0 { if numExtensions > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
posExtensions = len(b0) - len(b) - n - m posExtensions = len(b0) - len(b) - n - m
} }
numExtensions++ numExtensions++
case fieldnum.FileDescriptorProto_Service: case genid.FileDescriptorProto_Service_field_number:
if prevField != fieldnum.FileDescriptorProto_Service { if prevField != genid.FileDescriptorProto_Service_field_number {
if numServices > 0 { if numServices > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
@ -233,9 +233,9 @@ func (ed *Enum) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd pref.Desc
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.EnumDescriptorProto_Name: case genid.EnumDescriptorProto_Name_field_number:
ed.L0.FullName = appendFullName(sb, pd.FullName(), v) ed.L0.FullName = appendFullName(sb, pd.FullName(), v)
case fieldnum.EnumDescriptorProto_Value: case genid.EnumDescriptorProto_Value_field_number:
numValues++ numValues++
} }
default: default:
@ -260,7 +260,7 @@ func (ed *Enum) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd pref.Desc
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.EnumDescriptorProto_Value: case genid.EnumDescriptorProto_Value_field_number:
ed.L2.Values.List[i].unmarshalFull(v, sb, pf, ed, i) ed.L2.Values.List[i].unmarshalFull(v, sb, pf, ed, i)
i++ i++
} }
@ -288,33 +288,33 @@ func (md *Message) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd pref.D
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.DescriptorProto_Name: case genid.DescriptorProto_Name_field_number:
md.L0.FullName = appendFullName(sb, pd.FullName(), v) md.L0.FullName = appendFullName(sb, pd.FullName(), v)
case fieldnum.DescriptorProto_EnumType: case genid.DescriptorProto_EnumType_field_number:
if prevField != fieldnum.DescriptorProto_EnumType { if prevField != genid.DescriptorProto_EnumType_field_number {
if numEnums > 0 { if numEnums > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
posEnums = len(b0) - len(b) - n - m posEnums = len(b0) - len(b) - n - m
} }
numEnums++ numEnums++
case fieldnum.DescriptorProto_NestedType: case genid.DescriptorProto_NestedType_field_number:
if prevField != fieldnum.DescriptorProto_NestedType { if prevField != genid.DescriptorProto_NestedType_field_number {
if numMessages > 0 { if numMessages > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
posMessages = len(b0) - len(b) - n - m posMessages = len(b0) - len(b) - n - m
} }
numMessages++ numMessages++
case fieldnum.DescriptorProto_Extension: case genid.DescriptorProto_Extension_field_number:
if prevField != fieldnum.DescriptorProto_Extension { if prevField != genid.DescriptorProto_Extension_field_number {
if numExtensions > 0 { if numExtensions > 0 {
panic("non-contiguous repeated field") panic("non-contiguous repeated field")
} }
posExtensions = len(b0) - len(b) - n - m posExtensions = len(b0) - len(b) - n - m
} }
numExtensions++ numExtensions++
case fieldnum.DescriptorProto_Options: case genid.DescriptorProto_Options_field_number:
md.unmarshalSeedOptions(v) md.unmarshalSeedOptions(v)
} }
prevField = num prevField = num
@ -375,9 +375,9 @@ func (md *Message) unmarshalSeedOptions(b []byte) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.MessageOptions_MapEntry: case genid.MessageOptions_MapEntry_field_number:
md.L1.IsMapEntry = protowire.DecodeBool(v) md.L1.IsMapEntry = protowire.DecodeBool(v)
case fieldnum.MessageOptions_MessageSetWireFormat: case genid.MessageOptions_MessageSetWireFormat_field_number:
md.L1.IsMessageSet = protowire.DecodeBool(v) md.L1.IsMessageSet = protowire.DecodeBool(v)
} }
default: default:
@ -400,20 +400,20 @@ func (xd *Extension) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd pref
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldDescriptorProto_Number: case genid.FieldDescriptorProto_Number_field_number:
xd.L1.Number = pref.FieldNumber(v) xd.L1.Number = pref.FieldNumber(v)
case fieldnum.FieldDescriptorProto_Label: case genid.FieldDescriptorProto_Label_field_number:
xd.L1.Cardinality = pref.Cardinality(v) xd.L1.Cardinality = pref.Cardinality(v)
case fieldnum.FieldDescriptorProto_Type: case genid.FieldDescriptorProto_Type_field_number:
xd.L1.Kind = pref.Kind(v) xd.L1.Kind = pref.Kind(v)
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldDescriptorProto_Name: case genid.FieldDescriptorProto_Name_field_number:
xd.L0.FullName = appendFullName(sb, pd.FullName(), v) xd.L0.FullName = appendFullName(sb, pd.FullName(), v)
case fieldnum.FieldDescriptorProto_Extendee: case genid.FieldDescriptorProto_Extendee_field_number:
xd.L1.Extendee = PlaceholderMessage(makeFullName(sb, v)) xd.L1.Extendee = PlaceholderMessage(makeFullName(sb, v))
} }
default: default:
@ -436,7 +436,7 @@ func (sd *Service) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd pref.D
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.ServiceDescriptorProto_Name: case genid.ServiceDescriptorProto_Name_field_number:
sd.L0.FullName = appendFullName(sb, pd.FullName(), v) sd.L0.FullName = appendFullName(sb, pd.FullName(), v)
} }
default: default:

View File

@ -10,7 +10,7 @@ import (
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/descopts" "google.golang.org/protobuf/internal/descopts"
"google.golang.org/protobuf/internal/fieldnum" "google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/strs" "google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
@ -143,35 +143,35 @@ func (fd *File) unmarshalFull(b []byte) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FileDescriptorProto_PublicDependency: case genid.FileDescriptorProto_PublicDependency_field_number:
fd.L2.Imports[v].IsPublic = true fd.L2.Imports[v].IsPublic = true
case fieldnum.FileDescriptorProto_WeakDependency: case genid.FileDescriptorProto_WeakDependency_field_number:
fd.L2.Imports[v].IsWeak = true fd.L2.Imports[v].IsWeak = true
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FileDescriptorProto_Dependency: case genid.FileDescriptorProto_Dependency_field_number:
path := sb.MakeString(v) path := sb.MakeString(v)
imp, _ := fd.builder.FileRegistry.FindFileByPath(path) imp, _ := fd.builder.FileRegistry.FindFileByPath(path)
if imp == nil { if imp == nil {
imp = PlaceholderFile(path) imp = PlaceholderFile(path)
} }
fd.L2.Imports = append(fd.L2.Imports, pref.FileImport{FileDescriptor: imp}) fd.L2.Imports = append(fd.L2.Imports, pref.FileImport{FileDescriptor: imp})
case fieldnum.FileDescriptorProto_EnumType: case genid.FileDescriptorProto_EnumType_field_number:
fd.L1.Enums.List[enumIdx].unmarshalFull(v, sb) fd.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
enumIdx++ enumIdx++
case fieldnum.FileDescriptorProto_MessageType: case genid.FileDescriptorProto_MessageType_field_number:
fd.L1.Messages.List[messageIdx].unmarshalFull(v, sb) fd.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
messageIdx++ messageIdx++
case fieldnum.FileDescriptorProto_Extension: case genid.FileDescriptorProto_Extension_field_number:
fd.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb) fd.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
extensionIdx++ extensionIdx++
case fieldnum.FileDescriptorProto_Service: case genid.FileDescriptorProto_Service_field_number:
fd.L1.Services.List[serviceIdx].unmarshalFull(v, sb) fd.L1.Services.List[serviceIdx].unmarshalFull(v, sb)
serviceIdx++ serviceIdx++
case fieldnum.FileDescriptorProto_Options: case genid.FileDescriptorProto_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:
@ -196,13 +196,13 @@ func (ed *Enum) unmarshalFull(b []byte, sb *strs.Builder) {
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.EnumDescriptorProto_Value: case genid.EnumDescriptorProto_Value_field_number:
rawValues = append(rawValues, v) rawValues = append(rawValues, v)
case fieldnum.EnumDescriptorProto_ReservedName: case genid.EnumDescriptorProto_ReservedName_field_number:
ed.L2.ReservedNames.List = append(ed.L2.ReservedNames.List, pref.Name(sb.MakeString(v))) ed.L2.ReservedNames.List = append(ed.L2.ReservedNames.List, pref.Name(sb.MakeString(v)))
case fieldnum.EnumDescriptorProto_ReservedRange: case genid.EnumDescriptorProto_ReservedRange_field_number:
ed.L2.ReservedRanges.List = append(ed.L2.ReservedRanges.List, unmarshalEnumReservedRange(v)) ed.L2.ReservedRanges.List = append(ed.L2.ReservedRanges.List, unmarshalEnumReservedRange(v))
case fieldnum.EnumDescriptorProto_Options: case genid.EnumDescriptorProto_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:
@ -228,9 +228,9 @@ func unmarshalEnumReservedRange(b []byte) (r [2]pref.EnumNumber) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.EnumDescriptorProto_EnumReservedRange_Start: case genid.EnumDescriptorProto_EnumReservedRange_Start_field_number:
r[0] = pref.EnumNumber(v) r[0] = pref.EnumNumber(v)
case fieldnum.EnumDescriptorProto_EnumReservedRange_End: case genid.EnumDescriptorProto_EnumReservedRange_End_field_number:
r[1] = pref.EnumNumber(v) r[1] = pref.EnumNumber(v)
} }
default: default:
@ -255,17 +255,17 @@ func (vd *EnumValue) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.EnumValueDescriptorProto_Number: case genid.EnumValueDescriptorProto_Number_field_number:
vd.L1.Number = pref.EnumNumber(v) vd.L1.Number = pref.EnumNumber(v)
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.EnumValueDescriptorProto_Name: case genid.EnumValueDescriptorProto_Name_field_number:
// NOTE: Enum values are in the same scope as the enum parent. // NOTE: Enum values are in the same scope as the enum parent.
vd.L0.FullName = appendFullName(sb, pd.Parent().FullName(), v) vd.L0.FullName = appendFullName(sb, pd.Parent().FullName(), v)
case fieldnum.EnumValueDescriptorProto_Options: case genid.EnumValueDescriptorProto_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:
@ -289,29 +289,29 @@ func (md *Message) unmarshalFull(b []byte, sb *strs.Builder) {
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.DescriptorProto_Field: case genid.DescriptorProto_Field_field_number:
rawFields = append(rawFields, v) rawFields = append(rawFields, v)
case fieldnum.DescriptorProto_OneofDecl: case genid.DescriptorProto_OneofDecl_field_number:
rawOneofs = append(rawOneofs, v) rawOneofs = append(rawOneofs, v)
case fieldnum.DescriptorProto_ReservedName: case genid.DescriptorProto_ReservedName_field_number:
md.L2.ReservedNames.List = append(md.L2.ReservedNames.List, pref.Name(sb.MakeString(v))) md.L2.ReservedNames.List = append(md.L2.ReservedNames.List, pref.Name(sb.MakeString(v)))
case fieldnum.DescriptorProto_ReservedRange: case genid.DescriptorProto_ReservedRange_field_number:
md.L2.ReservedRanges.List = append(md.L2.ReservedRanges.List, unmarshalMessageReservedRange(v)) md.L2.ReservedRanges.List = append(md.L2.ReservedRanges.List, unmarshalMessageReservedRange(v))
case fieldnum.DescriptorProto_ExtensionRange: case genid.DescriptorProto_ExtensionRange_field_number:
r, rawOptions := unmarshalMessageExtensionRange(v) r, rawOptions := unmarshalMessageExtensionRange(v)
opts := md.L0.ParentFile.builder.optionsUnmarshaler(&descopts.ExtensionRange, rawOptions) opts := md.L0.ParentFile.builder.optionsUnmarshaler(&descopts.ExtensionRange, rawOptions)
md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, r) md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, r)
md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, opts) md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, opts)
case fieldnum.DescriptorProto_EnumType: case genid.DescriptorProto_EnumType_field_number:
md.L1.Enums.List[enumIdx].unmarshalFull(v, sb) md.L1.Enums.List[enumIdx].unmarshalFull(v, sb)
enumIdx++ enumIdx++
case fieldnum.DescriptorProto_NestedType: case genid.DescriptorProto_NestedType_field_number:
md.L1.Messages.List[messageIdx].unmarshalFull(v, sb) md.L1.Messages.List[messageIdx].unmarshalFull(v, sb)
messageIdx++ messageIdx++
case fieldnum.DescriptorProto_Extension: case genid.DescriptorProto_Extension_field_number:
md.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb) md.L1.Extensions.List[extensionIdx].unmarshalFull(v, sb)
extensionIdx++ extensionIdx++
case fieldnum.DescriptorProto_Options: case genid.DescriptorProto_Options_field_number:
md.unmarshalOptions(v) md.unmarshalOptions(v)
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
@ -347,9 +347,9 @@ func (md *Message) unmarshalOptions(b []byte) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.MessageOptions_MapEntry: case genid.MessageOptions_MapEntry_field_number:
md.L1.IsMapEntry = protowire.DecodeBool(v) md.L1.IsMapEntry = protowire.DecodeBool(v)
case fieldnum.MessageOptions_MessageSetWireFormat: case genid.MessageOptions_MessageSetWireFormat_field_number:
md.L1.IsMessageSet = protowire.DecodeBool(v) md.L1.IsMessageSet = protowire.DecodeBool(v)
} }
default: default:
@ -368,9 +368,9 @@ func unmarshalMessageReservedRange(b []byte) (r [2]pref.FieldNumber) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.DescriptorProto_ReservedRange_Start: case genid.DescriptorProto_ReservedRange_Start_field_number:
r[0] = pref.FieldNumber(v) r[0] = pref.FieldNumber(v)
case fieldnum.DescriptorProto_ReservedRange_End: case genid.DescriptorProto_ReservedRange_End_field_number:
r[1] = pref.FieldNumber(v) r[1] = pref.FieldNumber(v)
} }
default: default:
@ -390,16 +390,16 @@ func unmarshalMessageExtensionRange(b []byte) (r [2]pref.FieldNumber, rawOptions
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.DescriptorProto_ExtensionRange_Start: case genid.DescriptorProto_ExtensionRange_Start_field_number:
r[0] = pref.FieldNumber(v) r[0] = pref.FieldNumber(v)
case fieldnum.DescriptorProto_ExtensionRange_End: case genid.DescriptorProto_ExtensionRange_End_field_number:
r[1] = pref.FieldNumber(v) r[1] = pref.FieldNumber(v)
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.DescriptorProto_ExtensionRange_Options: case genid.DescriptorProto_ExtensionRange_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:
@ -425,13 +425,13 @@ func (fd *Field) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Des
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldDescriptorProto_Number: case genid.FieldDescriptorProto_Number_field_number:
fd.L1.Number = pref.FieldNumber(v) fd.L1.Number = pref.FieldNumber(v)
case fieldnum.FieldDescriptorProto_Label: case genid.FieldDescriptorProto_Label_field_number:
fd.L1.Cardinality = pref.Cardinality(v) fd.L1.Cardinality = pref.Cardinality(v)
case fieldnum.FieldDescriptorProto_Type: case genid.FieldDescriptorProto_Type_field_number:
fd.L1.Kind = pref.Kind(v) fd.L1.Kind = pref.Kind(v)
case fieldnum.FieldDescriptorProto_OneofIndex: case genid.FieldDescriptorProto_OneofIndex_field_number:
// In Message.unmarshalFull, we allocate slices for both // In Message.unmarshalFull, we allocate slices for both
// the field and oneof descriptors before unmarshaling either // the field and oneof descriptors before unmarshaling either
// of them. This ensures pointers to slice elements are stable. // of them. This ensures pointers to slice elements are stable.
@ -441,22 +441,22 @@ func (fd *Field) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Des
panic("oneof type already set") panic("oneof type already set")
} }
fd.L1.ContainingOneof = od fd.L1.ContainingOneof = od
case fieldnum.FieldDescriptorProto_Proto3Optional: case genid.FieldDescriptorProto_Proto3Optional_field_number:
fd.L1.IsProto3Optional = protowire.DecodeBool(v) fd.L1.IsProto3Optional = protowire.DecodeBool(v)
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldDescriptorProto_Name: case genid.FieldDescriptorProto_Name_field_number:
fd.L0.FullName = appendFullName(sb, pd.FullName(), v) fd.L0.FullName = appendFullName(sb, pd.FullName(), v)
case fieldnum.FieldDescriptorProto_JsonName: case genid.FieldDescriptorProto_JsonName_field_number:
fd.L1.JSONName.Init(sb.MakeString(v)) fd.L1.StringName.InitJSON(sb.MakeString(v))
case fieldnum.FieldDescriptorProto_DefaultValue: case genid.FieldDescriptorProto_DefaultValue_field_number:
fd.L1.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveMessages fd.L1.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveMessages
case fieldnum.FieldDescriptorProto_TypeName: case genid.FieldDescriptorProto_TypeName_field_number:
rawTypeName = v rawTypeName = v
case fieldnum.FieldDescriptorProto_Options: case genid.FieldDescriptorProto_Options_field_number:
fd.unmarshalOptions(v) fd.unmarshalOptions(v)
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
@ -488,10 +488,10 @@ func (fd *Field) unmarshalOptions(b []byte) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldOptions_Packed: case genid.FieldOptions_Packed_field_number:
fd.L1.HasPacked = true fd.L1.HasPacked = true
fd.L1.IsPacked = protowire.DecodeBool(v) fd.L1.IsPacked = protowire.DecodeBool(v)
case fieldnum.FieldOptions_Weak: case genid.FieldOptions_Weak_field_number:
fd.L1.IsWeak = protowire.DecodeBool(v) fd.L1.IsWeak = protowire.DecodeBool(v)
case FieldOptions_EnforceUTF8: case FieldOptions_EnforceUTF8:
fd.L1.HasEnforceUTF8 = true fd.L1.HasEnforceUTF8 = true
@ -518,9 +518,9 @@ func (od *Oneof) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.Des
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.OneofDescriptorProto_Name: case genid.OneofDescriptorProto_Name_field_number:
od.L0.FullName = appendFullName(sb, pd.FullName(), v) od.L0.FullName = appendFullName(sb, pd.FullName(), v)
case fieldnum.OneofDescriptorProto_Options: case genid.OneofDescriptorProto_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:
@ -543,20 +543,20 @@ func (xd *Extension) unmarshalFull(b []byte, sb *strs.Builder) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldDescriptorProto_Proto3Optional: case genid.FieldDescriptorProto_Proto3Optional_field_number:
xd.L2.IsProto3Optional = protowire.DecodeBool(v) xd.L2.IsProto3Optional = protowire.DecodeBool(v)
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldDescriptorProto_JsonName: case genid.FieldDescriptorProto_JsonName_field_number:
xd.L2.JSONName.Init(sb.MakeString(v)) xd.L2.StringName.InitJSON(sb.MakeString(v))
case fieldnum.FieldDescriptorProto_DefaultValue: case genid.FieldDescriptorProto_DefaultValue_field_number:
xd.L2.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveExtensions xd.L2.Default.val = pref.ValueOfBytes(v) // temporarily store as bytes; later resolved in resolveExtensions
case fieldnum.FieldDescriptorProto_TypeName: case genid.FieldDescriptorProto_TypeName_field_number:
rawTypeName = v rawTypeName = v
case fieldnum.FieldDescriptorProto_Options: case genid.FieldDescriptorProto_Options_field_number:
xd.unmarshalOptions(v) xd.unmarshalOptions(v)
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
@ -586,7 +586,7 @@ func (xd *Extension) unmarshalOptions(b []byte) {
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.FieldOptions_Packed: case genid.FieldOptions_Packed_field_number:
xd.L2.IsPacked = protowire.DecodeBool(v) xd.L2.IsPacked = protowire.DecodeBool(v)
} }
default: default:
@ -608,9 +608,9 @@ func (sd *Service) unmarshalFull(b []byte, sb *strs.Builder) {
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.ServiceDescriptorProto_Method: case genid.ServiceDescriptorProto_Method_field_number:
rawMethods = append(rawMethods, v) rawMethods = append(rawMethods, v)
case fieldnum.ServiceDescriptorProto_Options: case genid.ServiceDescriptorProto_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:
@ -641,22 +641,22 @@ func (md *Method) unmarshalFull(b []byte, sb *strs.Builder, pf *File, pd pref.De
v, m := protowire.ConsumeVarint(b) v, m := protowire.ConsumeVarint(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.MethodDescriptorProto_ClientStreaming: case genid.MethodDescriptorProto_ClientStreaming_field_number:
md.L1.IsStreamingClient = protowire.DecodeBool(v) md.L1.IsStreamingClient = protowire.DecodeBool(v)
case fieldnum.MethodDescriptorProto_ServerStreaming: case genid.MethodDescriptorProto_ServerStreaming_field_number:
md.L1.IsStreamingServer = protowire.DecodeBool(v) md.L1.IsStreamingServer = protowire.DecodeBool(v)
} }
case protowire.BytesType: case protowire.BytesType:
v, m := protowire.ConsumeBytes(b) v, m := protowire.ConsumeBytes(b)
b = b[m:] b = b[m:]
switch num { switch num {
case fieldnum.MethodDescriptorProto_Name: case genid.MethodDescriptorProto_Name_field_number:
md.L0.FullName = appendFullName(sb, pd.FullName(), v) md.L0.FullName = appendFullName(sb, pd.FullName(), v)
case fieldnum.MethodDescriptorProto_InputType: case genid.MethodDescriptorProto_InputType_field_number:
md.L1.Input = PlaceholderMessage(makeFullName(sb, v)) md.L1.Input = PlaceholderMessage(makeFullName(sb, v))
case fieldnum.MethodDescriptorProto_OutputType: case genid.MethodDescriptorProto_OutputType_field_number:
md.L1.Output = PlaceholderMessage(makeFullName(sb, v)) md.L1.Output = PlaceholderMessage(makeFullName(sb, v))
case fieldnum.MethodDescriptorProto_Options: case genid.MethodDescriptorProto_Options_field_number:
rawOptions = appendOptions(rawOptions, v) rawOptions = appendOptions(rawOptions, v)
} }
default: default:

View File

@ -10,6 +10,8 @@ import (
"sort" "sort"
"sync" "sync"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/descfmt" "google.golang.org/protobuf/internal/descfmt"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
@ -185,10 +187,7 @@ func (p *FieldRanges) CheckValid(isMessageSet bool) error {
// Unlike the FieldNumber.IsValid method, it allows ranges that cover the // Unlike the FieldNumber.IsValid method, it allows ranges that cover the
// reserved number range. // reserved number range.
func isValidFieldNumber(n protoreflect.FieldNumber, isMessageSet bool) bool { func isValidFieldNumber(n protoreflect.FieldNumber, isMessageSet bool) bool {
if isMessageSet { return protowire.MinValidNumber <= n && (n <= protowire.MaxValidNumber || isMessageSet)
return protowire.MinValidNumber <= n && n <= math.MaxInt32
}
return protowire.MinValidNumber <= n && n <= protowire.MaxValidNumber
} }
// CheckOverlap reports an error if p and q overlap. // CheckOverlap reports an error if p and q overlap.
@ -249,6 +248,7 @@ type OneofFields struct {
once sync.Once once sync.Once
byName map[pref.Name]pref.FieldDescriptor // protected by once byName map[pref.Name]pref.FieldDescriptor // protected by once
byJSON map[string]pref.FieldDescriptor // protected by once byJSON map[string]pref.FieldDescriptor // protected by once
byText map[string]pref.FieldDescriptor // protected by once
byNum map[pref.FieldNumber]pref.FieldDescriptor // protected by once byNum map[pref.FieldNumber]pref.FieldDescriptor // protected by once
} }
@ -256,6 +256,7 @@ func (p *OneofFields) Len() int { return
func (p *OneofFields) Get(i int) pref.FieldDescriptor { return p.List[i] } func (p *OneofFields) Get(i int) pref.FieldDescriptor { return p.List[i] }
func (p *OneofFields) ByName(s pref.Name) pref.FieldDescriptor { return p.lazyInit().byName[s] } func (p *OneofFields) ByName(s pref.Name) pref.FieldDescriptor { return p.lazyInit().byName[s] }
func (p *OneofFields) ByJSONName(s string) pref.FieldDescriptor { return p.lazyInit().byJSON[s] } func (p *OneofFields) ByJSONName(s string) pref.FieldDescriptor { return p.lazyInit().byJSON[s] }
func (p *OneofFields) ByTextName(s string) pref.FieldDescriptor { return p.lazyInit().byText[s] }
func (p *OneofFields) ByNumber(n pref.FieldNumber) pref.FieldDescriptor { return p.lazyInit().byNum[n] } func (p *OneofFields) ByNumber(n pref.FieldNumber) pref.FieldDescriptor { return p.lazyInit().byNum[n] }
func (p *OneofFields) Format(s fmt.State, r rune) { descfmt.FormatList(s, r, p) } func (p *OneofFields) Format(s fmt.State, r rune) { descfmt.FormatList(s, r, p) }
func (p *OneofFields) ProtoInternal(pragma.DoNotImplement) {} func (p *OneofFields) ProtoInternal(pragma.DoNotImplement) {}
@ -265,11 +266,13 @@ func (p *OneofFields) lazyInit() *OneofFields {
if len(p.List) > 0 { if len(p.List) > 0 {
p.byName = make(map[pref.Name]pref.FieldDescriptor, len(p.List)) p.byName = make(map[pref.Name]pref.FieldDescriptor, len(p.List))
p.byJSON = make(map[string]pref.FieldDescriptor, len(p.List)) p.byJSON = make(map[string]pref.FieldDescriptor, len(p.List))
p.byText = make(map[string]pref.FieldDescriptor, len(p.List))
p.byNum = make(map[pref.FieldNumber]pref.FieldDescriptor, len(p.List)) p.byNum = make(map[pref.FieldNumber]pref.FieldDescriptor, len(p.List))
for _, f := range p.List { for _, f := range p.List {
// Field names and numbers are guaranteed to be unique. // Field names and numbers are guaranteed to be unique.
p.byName[f.Name()] = f p.byName[f.Name()] = f
p.byJSON[f.JSONName()] = f p.byJSON[f.JSONName()] = f
p.byText[f.TextName()] = f
p.byNum[f.Number()] = f p.byNum[f.Number()] = f
} }
} }
@ -278,9 +281,170 @@ func (p *OneofFields) lazyInit() *OneofFields {
} }
type SourceLocations struct { type SourceLocations struct {
// List is a list of SourceLocations.
// The SourceLocation.Next field does not need to be populated
// as it will be lazily populated upon first need.
List []pref.SourceLocation List []pref.SourceLocation
// File is the parent file descriptor that these locations are relative to.
// If non-nil, ByDescriptor verifies that the provided descriptor
// is a child of this file descriptor.
File pref.FileDescriptor
once sync.Once
byPath map[pathKey]int
} }
func (p *SourceLocations) Len() int { return len(p.List) } func (p *SourceLocations) Len() int { return len(p.List) }
func (p *SourceLocations) Get(i int) pref.SourceLocation { return p.List[i] } func (p *SourceLocations) Get(i int) pref.SourceLocation { return p.lazyInit().List[i] }
func (p *SourceLocations) byKey(k pathKey) pref.SourceLocation {
if i, ok := p.lazyInit().byPath[k]; ok {
return p.List[i]
}
return pref.SourceLocation{}
}
func (p *SourceLocations) ByPath(path pref.SourcePath) pref.SourceLocation {
return p.byKey(newPathKey(path))
}
func (p *SourceLocations) ByDescriptor(desc pref.Descriptor) pref.SourceLocation {
if p.File != nil && desc != nil && p.File != desc.ParentFile() {
return pref.SourceLocation{} // mismatching parent files
}
var pathArr [16]int32
path := pathArr[:0]
for {
switch desc.(type) {
case pref.FileDescriptor:
// Reverse the path since it was constructed in reverse.
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
}
return p.byKey(newPathKey(path))
case pref.MessageDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case pref.FileDescriptor:
path = append(path, int32(genid.FileDescriptorProto_MessageType_field_number))
case pref.MessageDescriptor:
path = append(path, int32(genid.DescriptorProto_NestedType_field_number))
default:
return pref.SourceLocation{}
}
case pref.FieldDescriptor:
isExtension := desc.(pref.FieldDescriptor).IsExtension()
path = append(path, int32(desc.Index()))
desc = desc.Parent()
if isExtension {
switch desc.(type) {
case pref.FileDescriptor:
path = append(path, int32(genid.FileDescriptorProto_Extension_field_number))
case pref.MessageDescriptor:
path = append(path, int32(genid.DescriptorProto_Extension_field_number))
default:
return pref.SourceLocation{}
}
} else {
switch desc.(type) {
case pref.MessageDescriptor:
path = append(path, int32(genid.DescriptorProto_Field_field_number))
default:
return pref.SourceLocation{}
}
}
case pref.OneofDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case pref.MessageDescriptor:
path = append(path, int32(genid.DescriptorProto_OneofDecl_field_number))
default:
return pref.SourceLocation{}
}
case pref.EnumDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case pref.FileDescriptor:
path = append(path, int32(genid.FileDescriptorProto_EnumType_field_number))
case pref.MessageDescriptor:
path = append(path, int32(genid.DescriptorProto_EnumType_field_number))
default:
return pref.SourceLocation{}
}
case pref.EnumValueDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case pref.EnumDescriptor:
path = append(path, int32(genid.EnumDescriptorProto_Value_field_number))
default:
return pref.SourceLocation{}
}
case pref.ServiceDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case pref.FileDescriptor:
path = append(path, int32(genid.FileDescriptorProto_Service_field_number))
default:
return pref.SourceLocation{}
}
case pref.MethodDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case pref.ServiceDescriptor:
path = append(path, int32(genid.ServiceDescriptorProto_Method_field_number))
default:
return pref.SourceLocation{}
}
default:
return pref.SourceLocation{}
}
}
}
func (p *SourceLocations) lazyInit() *SourceLocations {
p.once.Do(func() {
if len(p.List) > 0 {
// Collect all the indexes for a given path.
pathIdxs := make(map[pathKey][]int, len(p.List))
for i, l := range p.List {
k := newPathKey(l.Path)
pathIdxs[k] = append(pathIdxs[k], i)
}
// Update the next index for all locations.
p.byPath = make(map[pathKey]int, len(p.List))
for k, idxs := range pathIdxs {
for i := 0; i < len(idxs)-1; i++ {
p.List[idxs[i]].Next = idxs[i+1]
}
p.List[idxs[len(idxs)-1]].Next = 0
p.byPath[k] = idxs[0] // record the first location for this path
}
}
})
return p
}
func (p *SourceLocations) ProtoInternal(pragma.DoNotImplement) {} func (p *SourceLocations) ProtoInternal(pragma.DoNotImplement) {}
// pathKey is a comparable representation of protoreflect.SourcePath.
type pathKey struct {
arr [16]uint8 // first n-1 path segments; last element is the length
str string // used if the path does not fit in arr
}
func newPathKey(p pref.SourcePath) (k pathKey) {
if len(p) < len(k.arr) {
for i, ps := range p {
if ps < 0 || math.MaxUint8 <= ps {
return pathKey{str: p.String()}
}
k.arr[i] = uint8(ps)
}
k.arr[len(k.arr)-1] = uint8(len(p))
return k
}
return pathKey{str: p.String()}
}

View File

@ -142,6 +142,7 @@ type Fields struct {
once sync.Once once sync.Once
byName map[protoreflect.Name]*Field // protected by once byName map[protoreflect.Name]*Field // protected by once
byJSON map[string]*Field // protected by once byJSON map[string]*Field // protected by once
byText map[string]*Field // protected by once
byNum map[protoreflect.FieldNumber]*Field // protected by once byNum map[protoreflect.FieldNumber]*Field // protected by once
} }
@ -163,6 +164,12 @@ func (p *Fields) ByJSONName(s string) protoreflect.FieldDescriptor {
} }
return nil return nil
} }
func (p *Fields) ByTextName(s string) protoreflect.FieldDescriptor {
if d := p.lazyInit().byText[s]; d != nil {
return d
}
return nil
}
func (p *Fields) ByNumber(n protoreflect.FieldNumber) protoreflect.FieldDescriptor { func (p *Fields) ByNumber(n protoreflect.FieldNumber) protoreflect.FieldDescriptor {
if d := p.lazyInit().byNum[n]; d != nil { if d := p.lazyInit().byNum[n]; d != nil {
return d return d
@ -178,6 +185,7 @@ func (p *Fields) lazyInit() *Fields {
if len(p.List) > 0 { if len(p.List) > 0 {
p.byName = make(map[protoreflect.Name]*Field, len(p.List)) p.byName = make(map[protoreflect.Name]*Field, len(p.List))
p.byJSON = make(map[string]*Field, len(p.List)) p.byJSON = make(map[string]*Field, len(p.List))
p.byText = make(map[string]*Field, len(p.List))
p.byNum = make(map[protoreflect.FieldNumber]*Field, len(p.List)) p.byNum = make(map[protoreflect.FieldNumber]*Field, len(p.List))
for i := range p.List { for i := range p.List {
d := &p.List[i] d := &p.List[i]
@ -187,6 +195,9 @@ func (p *Fields) lazyInit() *Fields {
if _, ok := p.byJSON[d.JSONName()]; !ok { if _, ok := p.byJSON[d.JSONName()]; !ok {
p.byJSON[d.JSONName()] = d p.byJSON[d.JSONName()] = d
} }
if _, ok := p.byText[d.TextName()]; !ok {
p.byText[d.TextName()] = d
}
if _, ok := p.byNum[d.Number()]; !ok { if _, ok := p.byNum[d.Number()]; !ok {
p.byNum[d.Number()] = d p.byNum[d.Number()] = d
} }

View File

@ -0,0 +1,34 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_any_proto = "google/protobuf/any.proto"
// Names for google.protobuf.Any.
const (
Any_message_name protoreflect.Name = "Any"
Any_message_fullname protoreflect.FullName = "google.protobuf.Any"
)
// Field names for google.protobuf.Any.
const (
Any_TypeUrl_field_name protoreflect.Name = "type_url"
Any_Value_field_name protoreflect.Name = "value"
Any_TypeUrl_field_fullname protoreflect.FullName = "google.protobuf.Any.type_url"
Any_Value_field_fullname protoreflect.FullName = "google.protobuf.Any.value"
)
// Field numbers for google.protobuf.Any.
const (
Any_TypeUrl_field_number protoreflect.FieldNumber = 1
Any_Value_field_number protoreflect.FieldNumber = 2
)

View File

@ -0,0 +1,106 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_api_proto = "google/protobuf/api.proto"
// Names for google.protobuf.Api.
const (
Api_message_name protoreflect.Name = "Api"
Api_message_fullname protoreflect.FullName = "google.protobuf.Api"
)
// Field names for google.protobuf.Api.
const (
Api_Name_field_name protoreflect.Name = "name"
Api_Methods_field_name protoreflect.Name = "methods"
Api_Options_field_name protoreflect.Name = "options"
Api_Version_field_name protoreflect.Name = "version"
Api_SourceContext_field_name protoreflect.Name = "source_context"
Api_Mixins_field_name protoreflect.Name = "mixins"
Api_Syntax_field_name protoreflect.Name = "syntax"
Api_Name_field_fullname protoreflect.FullName = "google.protobuf.Api.name"
Api_Methods_field_fullname protoreflect.FullName = "google.protobuf.Api.methods"
Api_Options_field_fullname protoreflect.FullName = "google.protobuf.Api.options"
Api_Version_field_fullname protoreflect.FullName = "google.protobuf.Api.version"
Api_SourceContext_field_fullname protoreflect.FullName = "google.protobuf.Api.source_context"
Api_Mixins_field_fullname protoreflect.FullName = "google.protobuf.Api.mixins"
Api_Syntax_field_fullname protoreflect.FullName = "google.protobuf.Api.syntax"
)
// Field numbers for google.protobuf.Api.
const (
Api_Name_field_number protoreflect.FieldNumber = 1
Api_Methods_field_number protoreflect.FieldNumber = 2
Api_Options_field_number protoreflect.FieldNumber = 3
Api_Version_field_number protoreflect.FieldNumber = 4
Api_SourceContext_field_number protoreflect.FieldNumber = 5
Api_Mixins_field_number protoreflect.FieldNumber = 6
Api_Syntax_field_number protoreflect.FieldNumber = 7
)
// Names for google.protobuf.Method.
const (
Method_message_name protoreflect.Name = "Method"
Method_message_fullname protoreflect.FullName = "google.protobuf.Method"
)
// Field names for google.protobuf.Method.
const (
Method_Name_field_name protoreflect.Name = "name"
Method_RequestTypeUrl_field_name protoreflect.Name = "request_type_url"
Method_RequestStreaming_field_name protoreflect.Name = "request_streaming"
Method_ResponseTypeUrl_field_name protoreflect.Name = "response_type_url"
Method_ResponseStreaming_field_name protoreflect.Name = "response_streaming"
Method_Options_field_name protoreflect.Name = "options"
Method_Syntax_field_name protoreflect.Name = "syntax"
Method_Name_field_fullname protoreflect.FullName = "google.protobuf.Method.name"
Method_RequestTypeUrl_field_fullname protoreflect.FullName = "google.protobuf.Method.request_type_url"
Method_RequestStreaming_field_fullname protoreflect.FullName = "google.protobuf.Method.request_streaming"
Method_ResponseTypeUrl_field_fullname protoreflect.FullName = "google.protobuf.Method.response_type_url"
Method_ResponseStreaming_field_fullname protoreflect.FullName = "google.protobuf.Method.response_streaming"
Method_Options_field_fullname protoreflect.FullName = "google.protobuf.Method.options"
Method_Syntax_field_fullname protoreflect.FullName = "google.protobuf.Method.syntax"
)
// Field numbers for google.protobuf.Method.
const (
Method_Name_field_number protoreflect.FieldNumber = 1
Method_RequestTypeUrl_field_number protoreflect.FieldNumber = 2
Method_RequestStreaming_field_number protoreflect.FieldNumber = 3
Method_ResponseTypeUrl_field_number protoreflect.FieldNumber = 4
Method_ResponseStreaming_field_number protoreflect.FieldNumber = 5
Method_Options_field_number protoreflect.FieldNumber = 6
Method_Syntax_field_number protoreflect.FieldNumber = 7
)
// Names for google.protobuf.Mixin.
const (
Mixin_message_name protoreflect.Name = "Mixin"
Mixin_message_fullname protoreflect.FullName = "google.protobuf.Mixin"
)
// Field names for google.protobuf.Mixin.
const (
Mixin_Name_field_name protoreflect.Name = "name"
Mixin_Root_field_name protoreflect.Name = "root"
Mixin_Name_field_fullname protoreflect.FullName = "google.protobuf.Mixin.name"
Mixin_Root_field_fullname protoreflect.FullName = "google.protobuf.Mixin.root"
)
// Field numbers for google.protobuf.Mixin.
const (
Mixin_Name_field_number protoreflect.FieldNumber = 1
Mixin_Root_field_number protoreflect.FieldNumber = 2
)

View File

@ -0,0 +1,829 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_descriptor_proto = "google/protobuf/descriptor.proto"
// Names for google.protobuf.FileDescriptorSet.
const (
FileDescriptorSet_message_name protoreflect.Name = "FileDescriptorSet"
FileDescriptorSet_message_fullname protoreflect.FullName = "google.protobuf.FileDescriptorSet"
)
// Field names for google.protobuf.FileDescriptorSet.
const (
FileDescriptorSet_File_field_name protoreflect.Name = "file"
FileDescriptorSet_File_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorSet.file"
)
// Field numbers for google.protobuf.FileDescriptorSet.
const (
FileDescriptorSet_File_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.FileDescriptorProto.
const (
FileDescriptorProto_message_name protoreflect.Name = "FileDescriptorProto"
FileDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto"
)
// Field names for google.protobuf.FileDescriptorProto.
const (
FileDescriptorProto_Name_field_name protoreflect.Name = "name"
FileDescriptorProto_Package_field_name protoreflect.Name = "package"
FileDescriptorProto_Dependency_field_name protoreflect.Name = "dependency"
FileDescriptorProto_PublicDependency_field_name protoreflect.Name = "public_dependency"
FileDescriptorProto_WeakDependency_field_name protoreflect.Name = "weak_dependency"
FileDescriptorProto_MessageType_field_name protoreflect.Name = "message_type"
FileDescriptorProto_EnumType_field_name protoreflect.Name = "enum_type"
FileDescriptorProto_Service_field_name protoreflect.Name = "service"
FileDescriptorProto_Extension_field_name protoreflect.Name = "extension"
FileDescriptorProto_Options_field_name protoreflect.Name = "options"
FileDescriptorProto_SourceCodeInfo_field_name protoreflect.Name = "source_code_info"
FileDescriptorProto_Syntax_field_name protoreflect.Name = "syntax"
FileDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.name"
FileDescriptorProto_Package_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.package"
FileDescriptorProto_Dependency_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.dependency"
FileDescriptorProto_PublicDependency_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.public_dependency"
FileDescriptorProto_WeakDependency_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.weak_dependency"
FileDescriptorProto_MessageType_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.message_type"
FileDescriptorProto_EnumType_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.enum_type"
FileDescriptorProto_Service_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.service"
FileDescriptorProto_Extension_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.extension"
FileDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.options"
FileDescriptorProto_SourceCodeInfo_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.source_code_info"
FileDescriptorProto_Syntax_field_fullname protoreflect.FullName = "google.protobuf.FileDescriptorProto.syntax"
)
// Field numbers for google.protobuf.FileDescriptorProto.
const (
FileDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
FileDescriptorProto_Package_field_number protoreflect.FieldNumber = 2
FileDescriptorProto_Dependency_field_number protoreflect.FieldNumber = 3
FileDescriptorProto_PublicDependency_field_number protoreflect.FieldNumber = 10
FileDescriptorProto_WeakDependency_field_number protoreflect.FieldNumber = 11
FileDescriptorProto_MessageType_field_number protoreflect.FieldNumber = 4
FileDescriptorProto_EnumType_field_number protoreflect.FieldNumber = 5
FileDescriptorProto_Service_field_number protoreflect.FieldNumber = 6
FileDescriptorProto_Extension_field_number protoreflect.FieldNumber = 7
FileDescriptorProto_Options_field_number protoreflect.FieldNumber = 8
FileDescriptorProto_SourceCodeInfo_field_number protoreflect.FieldNumber = 9
FileDescriptorProto_Syntax_field_number protoreflect.FieldNumber = 12
)
// Names for google.protobuf.DescriptorProto.
const (
DescriptorProto_message_name protoreflect.Name = "DescriptorProto"
DescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.DescriptorProto"
)
// Field names for google.protobuf.DescriptorProto.
const (
DescriptorProto_Name_field_name protoreflect.Name = "name"
DescriptorProto_Field_field_name protoreflect.Name = "field"
DescriptorProto_Extension_field_name protoreflect.Name = "extension"
DescriptorProto_NestedType_field_name protoreflect.Name = "nested_type"
DescriptorProto_EnumType_field_name protoreflect.Name = "enum_type"
DescriptorProto_ExtensionRange_field_name protoreflect.Name = "extension_range"
DescriptorProto_OneofDecl_field_name protoreflect.Name = "oneof_decl"
DescriptorProto_Options_field_name protoreflect.Name = "options"
DescriptorProto_ReservedRange_field_name protoreflect.Name = "reserved_range"
DescriptorProto_ReservedName_field_name protoreflect.Name = "reserved_name"
DescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.name"
DescriptorProto_Field_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.field"
DescriptorProto_Extension_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.extension"
DescriptorProto_NestedType_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.nested_type"
DescriptorProto_EnumType_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.enum_type"
DescriptorProto_ExtensionRange_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.extension_range"
DescriptorProto_OneofDecl_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.oneof_decl"
DescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.options"
DescriptorProto_ReservedRange_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.reserved_range"
DescriptorProto_ReservedName_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.reserved_name"
)
// Field numbers for google.protobuf.DescriptorProto.
const (
DescriptorProto_Name_field_number protoreflect.FieldNumber = 1
DescriptorProto_Field_field_number protoreflect.FieldNumber = 2
DescriptorProto_Extension_field_number protoreflect.FieldNumber = 6
DescriptorProto_NestedType_field_number protoreflect.FieldNumber = 3
DescriptorProto_EnumType_field_number protoreflect.FieldNumber = 4
DescriptorProto_ExtensionRange_field_number protoreflect.FieldNumber = 5
DescriptorProto_OneofDecl_field_number protoreflect.FieldNumber = 8
DescriptorProto_Options_field_number protoreflect.FieldNumber = 7
DescriptorProto_ReservedRange_field_number protoreflect.FieldNumber = 9
DescriptorProto_ReservedName_field_number protoreflect.FieldNumber = 10
)
// Names for google.protobuf.DescriptorProto.ExtensionRange.
const (
DescriptorProto_ExtensionRange_message_name protoreflect.Name = "ExtensionRange"
DescriptorProto_ExtensionRange_message_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ExtensionRange"
)
// Field names for google.protobuf.DescriptorProto.ExtensionRange.
const (
DescriptorProto_ExtensionRange_Start_field_name protoreflect.Name = "start"
DescriptorProto_ExtensionRange_End_field_name protoreflect.Name = "end"
DescriptorProto_ExtensionRange_Options_field_name protoreflect.Name = "options"
DescriptorProto_ExtensionRange_Start_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ExtensionRange.start"
DescriptorProto_ExtensionRange_End_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ExtensionRange.end"
DescriptorProto_ExtensionRange_Options_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ExtensionRange.options"
)
// Field numbers for google.protobuf.DescriptorProto.ExtensionRange.
const (
DescriptorProto_ExtensionRange_Start_field_number protoreflect.FieldNumber = 1
DescriptorProto_ExtensionRange_End_field_number protoreflect.FieldNumber = 2
DescriptorProto_ExtensionRange_Options_field_number protoreflect.FieldNumber = 3
)
// Names for google.protobuf.DescriptorProto.ReservedRange.
const (
DescriptorProto_ReservedRange_message_name protoreflect.Name = "ReservedRange"
DescriptorProto_ReservedRange_message_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ReservedRange"
)
// Field names for google.protobuf.DescriptorProto.ReservedRange.
const (
DescriptorProto_ReservedRange_Start_field_name protoreflect.Name = "start"
DescriptorProto_ReservedRange_End_field_name protoreflect.Name = "end"
DescriptorProto_ReservedRange_Start_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ReservedRange.start"
DescriptorProto_ReservedRange_End_field_fullname protoreflect.FullName = "google.protobuf.DescriptorProto.ReservedRange.end"
)
// Field numbers for google.protobuf.DescriptorProto.ReservedRange.
const (
DescriptorProto_ReservedRange_Start_field_number protoreflect.FieldNumber = 1
DescriptorProto_ReservedRange_End_field_number protoreflect.FieldNumber = 2
)
// Names for google.protobuf.ExtensionRangeOptions.
const (
ExtensionRangeOptions_message_name protoreflect.Name = "ExtensionRangeOptions"
ExtensionRangeOptions_message_fullname protoreflect.FullName = "google.protobuf.ExtensionRangeOptions"
)
// Field names for google.protobuf.ExtensionRangeOptions.
const (
ExtensionRangeOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
ExtensionRangeOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.ExtensionRangeOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.ExtensionRangeOptions.
const (
ExtensionRangeOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Names for google.protobuf.FieldDescriptorProto.
const (
FieldDescriptorProto_message_name protoreflect.Name = "FieldDescriptorProto"
FieldDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto"
)
// Field names for google.protobuf.FieldDescriptorProto.
const (
FieldDescriptorProto_Name_field_name protoreflect.Name = "name"
FieldDescriptorProto_Number_field_name protoreflect.Name = "number"
FieldDescriptorProto_Label_field_name protoreflect.Name = "label"
FieldDescriptorProto_Type_field_name protoreflect.Name = "type"
FieldDescriptorProto_TypeName_field_name protoreflect.Name = "type_name"
FieldDescriptorProto_Extendee_field_name protoreflect.Name = "extendee"
FieldDescriptorProto_DefaultValue_field_name protoreflect.Name = "default_value"
FieldDescriptorProto_OneofIndex_field_name protoreflect.Name = "oneof_index"
FieldDescriptorProto_JsonName_field_name protoreflect.Name = "json_name"
FieldDescriptorProto_Options_field_name protoreflect.Name = "options"
FieldDescriptorProto_Proto3Optional_field_name protoreflect.Name = "proto3_optional"
FieldDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.name"
FieldDescriptorProto_Number_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.number"
FieldDescriptorProto_Label_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.label"
FieldDescriptorProto_Type_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.type"
FieldDescriptorProto_TypeName_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.type_name"
FieldDescriptorProto_Extendee_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.extendee"
FieldDescriptorProto_DefaultValue_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.default_value"
FieldDescriptorProto_OneofIndex_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.oneof_index"
FieldDescriptorProto_JsonName_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.json_name"
FieldDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.options"
FieldDescriptorProto_Proto3Optional_field_fullname protoreflect.FullName = "google.protobuf.FieldDescriptorProto.proto3_optional"
)
// Field numbers for google.protobuf.FieldDescriptorProto.
const (
FieldDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
FieldDescriptorProto_Number_field_number protoreflect.FieldNumber = 3
FieldDescriptorProto_Label_field_number protoreflect.FieldNumber = 4
FieldDescriptorProto_Type_field_number protoreflect.FieldNumber = 5
FieldDescriptorProto_TypeName_field_number protoreflect.FieldNumber = 6
FieldDescriptorProto_Extendee_field_number protoreflect.FieldNumber = 2
FieldDescriptorProto_DefaultValue_field_number protoreflect.FieldNumber = 7
FieldDescriptorProto_OneofIndex_field_number protoreflect.FieldNumber = 9
FieldDescriptorProto_JsonName_field_number protoreflect.FieldNumber = 10
FieldDescriptorProto_Options_field_number protoreflect.FieldNumber = 8
FieldDescriptorProto_Proto3Optional_field_number protoreflect.FieldNumber = 17
)
// Full and short names for google.protobuf.FieldDescriptorProto.Type.
const (
FieldDescriptorProto_Type_enum_fullname = "google.protobuf.FieldDescriptorProto.Type"
FieldDescriptorProto_Type_enum_name = "Type"
)
// Full and short names for google.protobuf.FieldDescriptorProto.Label.
const (
FieldDescriptorProto_Label_enum_fullname = "google.protobuf.FieldDescriptorProto.Label"
FieldDescriptorProto_Label_enum_name = "Label"
)
// Names for google.protobuf.OneofDescriptorProto.
const (
OneofDescriptorProto_message_name protoreflect.Name = "OneofDescriptorProto"
OneofDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.OneofDescriptorProto"
)
// Field names for google.protobuf.OneofDescriptorProto.
const (
OneofDescriptorProto_Name_field_name protoreflect.Name = "name"
OneofDescriptorProto_Options_field_name protoreflect.Name = "options"
OneofDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.OneofDescriptorProto.name"
OneofDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.OneofDescriptorProto.options"
)
// Field numbers for google.protobuf.OneofDescriptorProto.
const (
OneofDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
OneofDescriptorProto_Options_field_number protoreflect.FieldNumber = 2
)
// Names for google.protobuf.EnumDescriptorProto.
const (
EnumDescriptorProto_message_name protoreflect.Name = "EnumDescriptorProto"
EnumDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto"
)
// Field names for google.protobuf.EnumDescriptorProto.
const (
EnumDescriptorProto_Name_field_name protoreflect.Name = "name"
EnumDescriptorProto_Value_field_name protoreflect.Name = "value"
EnumDescriptorProto_Options_field_name protoreflect.Name = "options"
EnumDescriptorProto_ReservedRange_field_name protoreflect.Name = "reserved_range"
EnumDescriptorProto_ReservedName_field_name protoreflect.Name = "reserved_name"
EnumDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.name"
EnumDescriptorProto_Value_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.value"
EnumDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.options"
EnumDescriptorProto_ReservedRange_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.reserved_range"
EnumDescriptorProto_ReservedName_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.reserved_name"
)
// Field numbers for google.protobuf.EnumDescriptorProto.
const (
EnumDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
EnumDescriptorProto_Value_field_number protoreflect.FieldNumber = 2
EnumDescriptorProto_Options_field_number protoreflect.FieldNumber = 3
EnumDescriptorProto_ReservedRange_field_number protoreflect.FieldNumber = 4
EnumDescriptorProto_ReservedName_field_number protoreflect.FieldNumber = 5
)
// Names for google.protobuf.EnumDescriptorProto.EnumReservedRange.
const (
EnumDescriptorProto_EnumReservedRange_message_name protoreflect.Name = "EnumReservedRange"
EnumDescriptorProto_EnumReservedRange_message_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.EnumReservedRange"
)
// Field names for google.protobuf.EnumDescriptorProto.EnumReservedRange.
const (
EnumDescriptorProto_EnumReservedRange_Start_field_name protoreflect.Name = "start"
EnumDescriptorProto_EnumReservedRange_End_field_name protoreflect.Name = "end"
EnumDescriptorProto_EnumReservedRange_Start_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.EnumReservedRange.start"
EnumDescriptorProto_EnumReservedRange_End_field_fullname protoreflect.FullName = "google.protobuf.EnumDescriptorProto.EnumReservedRange.end"
)
// Field numbers for google.protobuf.EnumDescriptorProto.EnumReservedRange.
const (
EnumDescriptorProto_EnumReservedRange_Start_field_number protoreflect.FieldNumber = 1
EnumDescriptorProto_EnumReservedRange_End_field_number protoreflect.FieldNumber = 2
)
// Names for google.protobuf.EnumValueDescriptorProto.
const (
EnumValueDescriptorProto_message_name protoreflect.Name = "EnumValueDescriptorProto"
EnumValueDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.EnumValueDescriptorProto"
)
// Field names for google.protobuf.EnumValueDescriptorProto.
const (
EnumValueDescriptorProto_Name_field_name protoreflect.Name = "name"
EnumValueDescriptorProto_Number_field_name protoreflect.Name = "number"
EnumValueDescriptorProto_Options_field_name protoreflect.Name = "options"
EnumValueDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.EnumValueDescriptorProto.name"
EnumValueDescriptorProto_Number_field_fullname protoreflect.FullName = "google.protobuf.EnumValueDescriptorProto.number"
EnumValueDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.EnumValueDescriptorProto.options"
)
// Field numbers for google.protobuf.EnumValueDescriptorProto.
const (
EnumValueDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
EnumValueDescriptorProto_Number_field_number protoreflect.FieldNumber = 2
EnumValueDescriptorProto_Options_field_number protoreflect.FieldNumber = 3
)
// Names for google.protobuf.ServiceDescriptorProto.
const (
ServiceDescriptorProto_message_name protoreflect.Name = "ServiceDescriptorProto"
ServiceDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.ServiceDescriptorProto"
)
// Field names for google.protobuf.ServiceDescriptorProto.
const (
ServiceDescriptorProto_Name_field_name protoreflect.Name = "name"
ServiceDescriptorProto_Method_field_name protoreflect.Name = "method"
ServiceDescriptorProto_Options_field_name protoreflect.Name = "options"
ServiceDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.ServiceDescriptorProto.name"
ServiceDescriptorProto_Method_field_fullname protoreflect.FullName = "google.protobuf.ServiceDescriptorProto.method"
ServiceDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.ServiceDescriptorProto.options"
)
// Field numbers for google.protobuf.ServiceDescriptorProto.
const (
ServiceDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
ServiceDescriptorProto_Method_field_number protoreflect.FieldNumber = 2
ServiceDescriptorProto_Options_field_number protoreflect.FieldNumber = 3
)
// Names for google.protobuf.MethodDescriptorProto.
const (
MethodDescriptorProto_message_name protoreflect.Name = "MethodDescriptorProto"
MethodDescriptorProto_message_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto"
)
// Field names for google.protobuf.MethodDescriptorProto.
const (
MethodDescriptorProto_Name_field_name protoreflect.Name = "name"
MethodDescriptorProto_InputType_field_name protoreflect.Name = "input_type"
MethodDescriptorProto_OutputType_field_name protoreflect.Name = "output_type"
MethodDescriptorProto_Options_field_name protoreflect.Name = "options"
MethodDescriptorProto_ClientStreaming_field_name protoreflect.Name = "client_streaming"
MethodDescriptorProto_ServerStreaming_field_name protoreflect.Name = "server_streaming"
MethodDescriptorProto_Name_field_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto.name"
MethodDescriptorProto_InputType_field_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto.input_type"
MethodDescriptorProto_OutputType_field_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto.output_type"
MethodDescriptorProto_Options_field_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto.options"
MethodDescriptorProto_ClientStreaming_field_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto.client_streaming"
MethodDescriptorProto_ServerStreaming_field_fullname protoreflect.FullName = "google.protobuf.MethodDescriptorProto.server_streaming"
)
// Field numbers for google.protobuf.MethodDescriptorProto.
const (
MethodDescriptorProto_Name_field_number protoreflect.FieldNumber = 1
MethodDescriptorProto_InputType_field_number protoreflect.FieldNumber = 2
MethodDescriptorProto_OutputType_field_number protoreflect.FieldNumber = 3
MethodDescriptorProto_Options_field_number protoreflect.FieldNumber = 4
MethodDescriptorProto_ClientStreaming_field_number protoreflect.FieldNumber = 5
MethodDescriptorProto_ServerStreaming_field_number protoreflect.FieldNumber = 6
)
// Names for google.protobuf.FileOptions.
const (
FileOptions_message_name protoreflect.Name = "FileOptions"
FileOptions_message_fullname protoreflect.FullName = "google.protobuf.FileOptions"
)
// Field names for google.protobuf.FileOptions.
const (
FileOptions_JavaPackage_field_name protoreflect.Name = "java_package"
FileOptions_JavaOuterClassname_field_name protoreflect.Name = "java_outer_classname"
FileOptions_JavaMultipleFiles_field_name protoreflect.Name = "java_multiple_files"
FileOptions_JavaGenerateEqualsAndHash_field_name protoreflect.Name = "java_generate_equals_and_hash"
FileOptions_JavaStringCheckUtf8_field_name protoreflect.Name = "java_string_check_utf8"
FileOptions_OptimizeFor_field_name protoreflect.Name = "optimize_for"
FileOptions_GoPackage_field_name protoreflect.Name = "go_package"
FileOptions_CcGenericServices_field_name protoreflect.Name = "cc_generic_services"
FileOptions_JavaGenericServices_field_name protoreflect.Name = "java_generic_services"
FileOptions_PyGenericServices_field_name protoreflect.Name = "py_generic_services"
FileOptions_PhpGenericServices_field_name protoreflect.Name = "php_generic_services"
FileOptions_Deprecated_field_name protoreflect.Name = "deprecated"
FileOptions_CcEnableArenas_field_name protoreflect.Name = "cc_enable_arenas"
FileOptions_ObjcClassPrefix_field_name protoreflect.Name = "objc_class_prefix"
FileOptions_CsharpNamespace_field_name protoreflect.Name = "csharp_namespace"
FileOptions_SwiftPrefix_field_name protoreflect.Name = "swift_prefix"
FileOptions_PhpClassPrefix_field_name protoreflect.Name = "php_class_prefix"
FileOptions_PhpNamespace_field_name protoreflect.Name = "php_namespace"
FileOptions_PhpMetadataNamespace_field_name protoreflect.Name = "php_metadata_namespace"
FileOptions_RubyPackage_field_name protoreflect.Name = "ruby_package"
FileOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
FileOptions_JavaPackage_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.java_package"
FileOptions_JavaOuterClassname_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.java_outer_classname"
FileOptions_JavaMultipleFiles_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.java_multiple_files"
FileOptions_JavaGenerateEqualsAndHash_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.java_generate_equals_and_hash"
FileOptions_JavaStringCheckUtf8_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.java_string_check_utf8"
FileOptions_OptimizeFor_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.optimize_for"
FileOptions_GoPackage_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.go_package"
FileOptions_CcGenericServices_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.cc_generic_services"
FileOptions_JavaGenericServices_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.java_generic_services"
FileOptions_PyGenericServices_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.py_generic_services"
FileOptions_PhpGenericServices_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.php_generic_services"
FileOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.deprecated"
FileOptions_CcEnableArenas_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.cc_enable_arenas"
FileOptions_ObjcClassPrefix_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.objc_class_prefix"
FileOptions_CsharpNamespace_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.csharp_namespace"
FileOptions_SwiftPrefix_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.swift_prefix"
FileOptions_PhpClassPrefix_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.php_class_prefix"
FileOptions_PhpNamespace_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.php_namespace"
FileOptions_PhpMetadataNamespace_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.php_metadata_namespace"
FileOptions_RubyPackage_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.ruby_package"
FileOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.FileOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.FileOptions.
const (
FileOptions_JavaPackage_field_number protoreflect.FieldNumber = 1
FileOptions_JavaOuterClassname_field_number protoreflect.FieldNumber = 8
FileOptions_JavaMultipleFiles_field_number protoreflect.FieldNumber = 10
FileOptions_JavaGenerateEqualsAndHash_field_number protoreflect.FieldNumber = 20
FileOptions_JavaStringCheckUtf8_field_number protoreflect.FieldNumber = 27
FileOptions_OptimizeFor_field_number protoreflect.FieldNumber = 9
FileOptions_GoPackage_field_number protoreflect.FieldNumber = 11
FileOptions_CcGenericServices_field_number protoreflect.FieldNumber = 16
FileOptions_JavaGenericServices_field_number protoreflect.FieldNumber = 17
FileOptions_PyGenericServices_field_number protoreflect.FieldNumber = 18
FileOptions_PhpGenericServices_field_number protoreflect.FieldNumber = 42
FileOptions_Deprecated_field_number protoreflect.FieldNumber = 23
FileOptions_CcEnableArenas_field_number protoreflect.FieldNumber = 31
FileOptions_ObjcClassPrefix_field_number protoreflect.FieldNumber = 36
FileOptions_CsharpNamespace_field_number protoreflect.FieldNumber = 37
FileOptions_SwiftPrefix_field_number protoreflect.FieldNumber = 39
FileOptions_PhpClassPrefix_field_number protoreflect.FieldNumber = 40
FileOptions_PhpNamespace_field_number protoreflect.FieldNumber = 41
FileOptions_PhpMetadataNamespace_field_number protoreflect.FieldNumber = 44
FileOptions_RubyPackage_field_number protoreflect.FieldNumber = 45
FileOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Full and short names for google.protobuf.FileOptions.OptimizeMode.
const (
FileOptions_OptimizeMode_enum_fullname = "google.protobuf.FileOptions.OptimizeMode"
FileOptions_OptimizeMode_enum_name = "OptimizeMode"
)
// Names for google.protobuf.MessageOptions.
const (
MessageOptions_message_name protoreflect.Name = "MessageOptions"
MessageOptions_message_fullname protoreflect.FullName = "google.protobuf.MessageOptions"
)
// Field names for google.protobuf.MessageOptions.
const (
MessageOptions_MessageSetWireFormat_field_name protoreflect.Name = "message_set_wire_format"
MessageOptions_NoStandardDescriptorAccessor_field_name protoreflect.Name = "no_standard_descriptor_accessor"
MessageOptions_Deprecated_field_name protoreflect.Name = "deprecated"
MessageOptions_MapEntry_field_name protoreflect.Name = "map_entry"
MessageOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
MessageOptions_MessageSetWireFormat_field_fullname protoreflect.FullName = "google.protobuf.MessageOptions.message_set_wire_format"
MessageOptions_NoStandardDescriptorAccessor_field_fullname protoreflect.FullName = "google.protobuf.MessageOptions.no_standard_descriptor_accessor"
MessageOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.MessageOptions.deprecated"
MessageOptions_MapEntry_field_fullname protoreflect.FullName = "google.protobuf.MessageOptions.map_entry"
MessageOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.MessageOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.MessageOptions.
const (
MessageOptions_MessageSetWireFormat_field_number protoreflect.FieldNumber = 1
MessageOptions_NoStandardDescriptorAccessor_field_number protoreflect.FieldNumber = 2
MessageOptions_Deprecated_field_number protoreflect.FieldNumber = 3
MessageOptions_MapEntry_field_number protoreflect.FieldNumber = 7
MessageOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Names for google.protobuf.FieldOptions.
const (
FieldOptions_message_name protoreflect.Name = "FieldOptions"
FieldOptions_message_fullname protoreflect.FullName = "google.protobuf.FieldOptions"
)
// Field names for google.protobuf.FieldOptions.
const (
FieldOptions_Ctype_field_name protoreflect.Name = "ctype"
FieldOptions_Packed_field_name protoreflect.Name = "packed"
FieldOptions_Jstype_field_name protoreflect.Name = "jstype"
FieldOptions_Lazy_field_name protoreflect.Name = "lazy"
FieldOptions_Deprecated_field_name protoreflect.Name = "deprecated"
FieldOptions_Weak_field_name protoreflect.Name = "weak"
FieldOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
FieldOptions_Ctype_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.ctype"
FieldOptions_Packed_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.packed"
FieldOptions_Jstype_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.jstype"
FieldOptions_Lazy_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.lazy"
FieldOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.deprecated"
FieldOptions_Weak_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.weak"
FieldOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.FieldOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.FieldOptions.
const (
FieldOptions_Ctype_field_number protoreflect.FieldNumber = 1
FieldOptions_Packed_field_number protoreflect.FieldNumber = 2
FieldOptions_Jstype_field_number protoreflect.FieldNumber = 6
FieldOptions_Lazy_field_number protoreflect.FieldNumber = 5
FieldOptions_Deprecated_field_number protoreflect.FieldNumber = 3
FieldOptions_Weak_field_number protoreflect.FieldNumber = 10
FieldOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Full and short names for google.protobuf.FieldOptions.CType.
const (
FieldOptions_CType_enum_fullname = "google.protobuf.FieldOptions.CType"
FieldOptions_CType_enum_name = "CType"
)
// Full and short names for google.protobuf.FieldOptions.JSType.
const (
FieldOptions_JSType_enum_fullname = "google.protobuf.FieldOptions.JSType"
FieldOptions_JSType_enum_name = "JSType"
)
// Names for google.protobuf.OneofOptions.
const (
OneofOptions_message_name protoreflect.Name = "OneofOptions"
OneofOptions_message_fullname protoreflect.FullName = "google.protobuf.OneofOptions"
)
// Field names for google.protobuf.OneofOptions.
const (
OneofOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
OneofOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.OneofOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.OneofOptions.
const (
OneofOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Names for google.protobuf.EnumOptions.
const (
EnumOptions_message_name protoreflect.Name = "EnumOptions"
EnumOptions_message_fullname protoreflect.FullName = "google.protobuf.EnumOptions"
)
// Field names for google.protobuf.EnumOptions.
const (
EnumOptions_AllowAlias_field_name protoreflect.Name = "allow_alias"
EnumOptions_Deprecated_field_name protoreflect.Name = "deprecated"
EnumOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
EnumOptions_AllowAlias_field_fullname protoreflect.FullName = "google.protobuf.EnumOptions.allow_alias"
EnumOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.EnumOptions.deprecated"
EnumOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.EnumOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.EnumOptions.
const (
EnumOptions_AllowAlias_field_number protoreflect.FieldNumber = 2
EnumOptions_Deprecated_field_number protoreflect.FieldNumber = 3
EnumOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Names for google.protobuf.EnumValueOptions.
const (
EnumValueOptions_message_name protoreflect.Name = "EnumValueOptions"
EnumValueOptions_message_fullname protoreflect.FullName = "google.protobuf.EnumValueOptions"
)
// Field names for google.protobuf.EnumValueOptions.
const (
EnumValueOptions_Deprecated_field_name protoreflect.Name = "deprecated"
EnumValueOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
EnumValueOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.EnumValueOptions.deprecated"
EnumValueOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.EnumValueOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.EnumValueOptions.
const (
EnumValueOptions_Deprecated_field_number protoreflect.FieldNumber = 1
EnumValueOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Names for google.protobuf.ServiceOptions.
const (
ServiceOptions_message_name protoreflect.Name = "ServiceOptions"
ServiceOptions_message_fullname protoreflect.FullName = "google.protobuf.ServiceOptions"
)
// Field names for google.protobuf.ServiceOptions.
const (
ServiceOptions_Deprecated_field_name protoreflect.Name = "deprecated"
ServiceOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
ServiceOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.ServiceOptions.deprecated"
ServiceOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.ServiceOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.ServiceOptions.
const (
ServiceOptions_Deprecated_field_number protoreflect.FieldNumber = 33
ServiceOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Names for google.protobuf.MethodOptions.
const (
MethodOptions_message_name protoreflect.Name = "MethodOptions"
MethodOptions_message_fullname protoreflect.FullName = "google.protobuf.MethodOptions"
)
// Field names for google.protobuf.MethodOptions.
const (
MethodOptions_Deprecated_field_name protoreflect.Name = "deprecated"
MethodOptions_IdempotencyLevel_field_name protoreflect.Name = "idempotency_level"
MethodOptions_UninterpretedOption_field_name protoreflect.Name = "uninterpreted_option"
MethodOptions_Deprecated_field_fullname protoreflect.FullName = "google.protobuf.MethodOptions.deprecated"
MethodOptions_IdempotencyLevel_field_fullname protoreflect.FullName = "google.protobuf.MethodOptions.idempotency_level"
MethodOptions_UninterpretedOption_field_fullname protoreflect.FullName = "google.protobuf.MethodOptions.uninterpreted_option"
)
// Field numbers for google.protobuf.MethodOptions.
const (
MethodOptions_Deprecated_field_number protoreflect.FieldNumber = 33
MethodOptions_IdempotencyLevel_field_number protoreflect.FieldNumber = 34
MethodOptions_UninterpretedOption_field_number protoreflect.FieldNumber = 999
)
// Full and short names for google.protobuf.MethodOptions.IdempotencyLevel.
const (
MethodOptions_IdempotencyLevel_enum_fullname = "google.protobuf.MethodOptions.IdempotencyLevel"
MethodOptions_IdempotencyLevel_enum_name = "IdempotencyLevel"
)
// Names for google.protobuf.UninterpretedOption.
const (
UninterpretedOption_message_name protoreflect.Name = "UninterpretedOption"
UninterpretedOption_message_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption"
)
// Field names for google.protobuf.UninterpretedOption.
const (
UninterpretedOption_Name_field_name protoreflect.Name = "name"
UninterpretedOption_IdentifierValue_field_name protoreflect.Name = "identifier_value"
UninterpretedOption_PositiveIntValue_field_name protoreflect.Name = "positive_int_value"
UninterpretedOption_NegativeIntValue_field_name protoreflect.Name = "negative_int_value"
UninterpretedOption_DoubleValue_field_name protoreflect.Name = "double_value"
UninterpretedOption_StringValue_field_name protoreflect.Name = "string_value"
UninterpretedOption_AggregateValue_field_name protoreflect.Name = "aggregate_value"
UninterpretedOption_Name_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.name"
UninterpretedOption_IdentifierValue_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.identifier_value"
UninterpretedOption_PositiveIntValue_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.positive_int_value"
UninterpretedOption_NegativeIntValue_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.negative_int_value"
UninterpretedOption_DoubleValue_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.double_value"
UninterpretedOption_StringValue_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.string_value"
UninterpretedOption_AggregateValue_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.aggregate_value"
)
// Field numbers for google.protobuf.UninterpretedOption.
const (
UninterpretedOption_Name_field_number protoreflect.FieldNumber = 2
UninterpretedOption_IdentifierValue_field_number protoreflect.FieldNumber = 3
UninterpretedOption_PositiveIntValue_field_number protoreflect.FieldNumber = 4
UninterpretedOption_NegativeIntValue_field_number protoreflect.FieldNumber = 5
UninterpretedOption_DoubleValue_field_number protoreflect.FieldNumber = 6
UninterpretedOption_StringValue_field_number protoreflect.FieldNumber = 7
UninterpretedOption_AggregateValue_field_number protoreflect.FieldNumber = 8
)
// Names for google.protobuf.UninterpretedOption.NamePart.
const (
UninterpretedOption_NamePart_message_name protoreflect.Name = "NamePart"
UninterpretedOption_NamePart_message_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.NamePart"
)
// Field names for google.protobuf.UninterpretedOption.NamePart.
const (
UninterpretedOption_NamePart_NamePart_field_name protoreflect.Name = "name_part"
UninterpretedOption_NamePart_IsExtension_field_name protoreflect.Name = "is_extension"
UninterpretedOption_NamePart_NamePart_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.NamePart.name_part"
UninterpretedOption_NamePart_IsExtension_field_fullname protoreflect.FullName = "google.protobuf.UninterpretedOption.NamePart.is_extension"
)
// Field numbers for google.protobuf.UninterpretedOption.NamePart.
const (
UninterpretedOption_NamePart_NamePart_field_number protoreflect.FieldNumber = 1
UninterpretedOption_NamePart_IsExtension_field_number protoreflect.FieldNumber = 2
)
// Names for google.protobuf.SourceCodeInfo.
const (
SourceCodeInfo_message_name protoreflect.Name = "SourceCodeInfo"
SourceCodeInfo_message_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo"
)
// Field names for google.protobuf.SourceCodeInfo.
const (
SourceCodeInfo_Location_field_name protoreflect.Name = "location"
SourceCodeInfo_Location_field_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.location"
)
// Field numbers for google.protobuf.SourceCodeInfo.
const (
SourceCodeInfo_Location_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.SourceCodeInfo.Location.
const (
SourceCodeInfo_Location_message_name protoreflect.Name = "Location"
SourceCodeInfo_Location_message_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.Location"
)
// Field names for google.protobuf.SourceCodeInfo.Location.
const (
SourceCodeInfo_Location_Path_field_name protoreflect.Name = "path"
SourceCodeInfo_Location_Span_field_name protoreflect.Name = "span"
SourceCodeInfo_Location_LeadingComments_field_name protoreflect.Name = "leading_comments"
SourceCodeInfo_Location_TrailingComments_field_name protoreflect.Name = "trailing_comments"
SourceCodeInfo_Location_LeadingDetachedComments_field_name protoreflect.Name = "leading_detached_comments"
SourceCodeInfo_Location_Path_field_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.Location.path"
SourceCodeInfo_Location_Span_field_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.Location.span"
SourceCodeInfo_Location_LeadingComments_field_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.Location.leading_comments"
SourceCodeInfo_Location_TrailingComments_field_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.Location.trailing_comments"
SourceCodeInfo_Location_LeadingDetachedComments_field_fullname protoreflect.FullName = "google.protobuf.SourceCodeInfo.Location.leading_detached_comments"
)
// Field numbers for google.protobuf.SourceCodeInfo.Location.
const (
SourceCodeInfo_Location_Path_field_number protoreflect.FieldNumber = 1
SourceCodeInfo_Location_Span_field_number protoreflect.FieldNumber = 2
SourceCodeInfo_Location_LeadingComments_field_number protoreflect.FieldNumber = 3
SourceCodeInfo_Location_TrailingComments_field_number protoreflect.FieldNumber = 4
SourceCodeInfo_Location_LeadingDetachedComments_field_number protoreflect.FieldNumber = 6
)
// Names for google.protobuf.GeneratedCodeInfo.
const (
GeneratedCodeInfo_message_name protoreflect.Name = "GeneratedCodeInfo"
GeneratedCodeInfo_message_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo"
)
// Field names for google.protobuf.GeneratedCodeInfo.
const (
GeneratedCodeInfo_Annotation_field_name protoreflect.Name = "annotation"
GeneratedCodeInfo_Annotation_field_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo.annotation"
)
// Field numbers for google.protobuf.GeneratedCodeInfo.
const (
GeneratedCodeInfo_Annotation_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.GeneratedCodeInfo.Annotation.
const (
GeneratedCodeInfo_Annotation_message_name protoreflect.Name = "Annotation"
GeneratedCodeInfo_Annotation_message_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo.Annotation"
)
// Field names for google.protobuf.GeneratedCodeInfo.Annotation.
const (
GeneratedCodeInfo_Annotation_Path_field_name protoreflect.Name = "path"
GeneratedCodeInfo_Annotation_SourceFile_field_name protoreflect.Name = "source_file"
GeneratedCodeInfo_Annotation_Begin_field_name protoreflect.Name = "begin"
GeneratedCodeInfo_Annotation_End_field_name protoreflect.Name = "end"
GeneratedCodeInfo_Annotation_Path_field_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo.Annotation.path"
GeneratedCodeInfo_Annotation_SourceFile_field_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo.Annotation.source_file"
GeneratedCodeInfo_Annotation_Begin_field_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo.Annotation.begin"
GeneratedCodeInfo_Annotation_End_field_fullname protoreflect.FullName = "google.protobuf.GeneratedCodeInfo.Annotation.end"
)
// Field numbers for google.protobuf.GeneratedCodeInfo.Annotation.
const (
GeneratedCodeInfo_Annotation_Path_field_number protoreflect.FieldNumber = 1
GeneratedCodeInfo_Annotation_SourceFile_field_number protoreflect.FieldNumber = 2
GeneratedCodeInfo_Annotation_Begin_field_number protoreflect.FieldNumber = 3
GeneratedCodeInfo_Annotation_End_field_number protoreflect.FieldNumber = 4
)

View File

@ -0,0 +1,11 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package genid contains constants for declarations in descriptor.proto
// and the well-known types.
package genid
import protoreflect "google.golang.org/protobuf/reflect/protoreflect"
const GoogleProtobuf_package protoreflect.FullName = "google.protobuf"

View File

@ -0,0 +1,34 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_duration_proto = "google/protobuf/duration.proto"
// Names for google.protobuf.Duration.
const (
Duration_message_name protoreflect.Name = "Duration"
Duration_message_fullname protoreflect.FullName = "google.protobuf.Duration"
)
// Field names for google.protobuf.Duration.
const (
Duration_Seconds_field_name protoreflect.Name = "seconds"
Duration_Nanos_field_name protoreflect.Name = "nanos"
Duration_Seconds_field_fullname protoreflect.FullName = "google.protobuf.Duration.seconds"
Duration_Nanos_field_fullname protoreflect.FullName = "google.protobuf.Duration.nanos"
)
// Field numbers for google.protobuf.Duration.
const (
Duration_Seconds_field_number protoreflect.FieldNumber = 1
Duration_Nanos_field_number protoreflect.FieldNumber = 2
)

View File

@ -0,0 +1,19 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_empty_proto = "google/protobuf/empty.proto"
// Names for google.protobuf.Empty.
const (
Empty_message_name protoreflect.Name = "Empty"
Empty_message_fullname protoreflect.FullName = "google.protobuf.Empty"
)

View File

@ -0,0 +1,31 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_field_mask_proto = "google/protobuf/field_mask.proto"
// Names for google.protobuf.FieldMask.
const (
FieldMask_message_name protoreflect.Name = "FieldMask"
FieldMask_message_fullname protoreflect.FullName = "google.protobuf.FieldMask"
)
// Field names for google.protobuf.FieldMask.
const (
FieldMask_Paths_field_name protoreflect.Name = "paths"
FieldMask_Paths_field_fullname protoreflect.FullName = "google.protobuf.FieldMask.paths"
)
// Field numbers for google.protobuf.FieldMask.
const (
FieldMask_Paths_field_number protoreflect.FieldNumber = 1
)

View File

@ -0,0 +1,25 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package genid
// Go names of implementation-specific struct fields in generated messages.
const (
State_goname = "state"
SizeCache_goname = "sizeCache"
SizeCacheA_goname = "XXX_sizecache"
WeakFields_goname = "weakFields"
WeakFieldsA_goname = "XXX_weak"
UnknownFields_goname = "unknownFields"
UnknownFieldsA_goname = "XXX_unrecognized"
ExtensionFields_goname = "extensionFields"
ExtensionFieldsA_goname = "XXX_InternalExtensions"
ExtensionFieldsB_goname = "XXX_extensions"
WeakFieldPrefix_goname = "XXX_weak_"
)

View File

@ -0,0 +1,16 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package genid
import protoreflect "google.golang.org/protobuf/reflect/protoreflect"
// Generic field names and numbers for synthetic map entry messages.
const (
MapEntry_Key_field_name protoreflect.Name = "key"
MapEntry_Value_field_name protoreflect.Name = "value"
MapEntry_Key_field_number protoreflect.FieldNumber = 1
MapEntry_Value_field_number protoreflect.FieldNumber = 2
)

View File

@ -0,0 +1,31 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_source_context_proto = "google/protobuf/source_context.proto"
// Names for google.protobuf.SourceContext.
const (
SourceContext_message_name protoreflect.Name = "SourceContext"
SourceContext_message_fullname protoreflect.FullName = "google.protobuf.SourceContext"
)
// Field names for google.protobuf.SourceContext.
const (
SourceContext_FileName_field_name protoreflect.Name = "file_name"
SourceContext_FileName_field_fullname protoreflect.FullName = "google.protobuf.SourceContext.file_name"
)
// Field numbers for google.protobuf.SourceContext.
const (
SourceContext_FileName_field_number protoreflect.FieldNumber = 1
)

View File

@ -0,0 +1,116 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_struct_proto = "google/protobuf/struct.proto"
// Full and short names for google.protobuf.NullValue.
const (
NullValue_enum_fullname = "google.protobuf.NullValue"
NullValue_enum_name = "NullValue"
)
// Names for google.protobuf.Struct.
const (
Struct_message_name protoreflect.Name = "Struct"
Struct_message_fullname protoreflect.FullName = "google.protobuf.Struct"
)
// Field names for google.protobuf.Struct.
const (
Struct_Fields_field_name protoreflect.Name = "fields"
Struct_Fields_field_fullname protoreflect.FullName = "google.protobuf.Struct.fields"
)
// Field numbers for google.protobuf.Struct.
const (
Struct_Fields_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.Struct.FieldsEntry.
const (
Struct_FieldsEntry_message_name protoreflect.Name = "FieldsEntry"
Struct_FieldsEntry_message_fullname protoreflect.FullName = "google.protobuf.Struct.FieldsEntry"
)
// Field names for google.protobuf.Struct.FieldsEntry.
const (
Struct_FieldsEntry_Key_field_name protoreflect.Name = "key"
Struct_FieldsEntry_Value_field_name protoreflect.Name = "value"
Struct_FieldsEntry_Key_field_fullname protoreflect.FullName = "google.protobuf.Struct.FieldsEntry.key"
Struct_FieldsEntry_Value_field_fullname protoreflect.FullName = "google.protobuf.Struct.FieldsEntry.value"
)
// Field numbers for google.protobuf.Struct.FieldsEntry.
const (
Struct_FieldsEntry_Key_field_number protoreflect.FieldNumber = 1
Struct_FieldsEntry_Value_field_number protoreflect.FieldNumber = 2
)
// Names for google.protobuf.Value.
const (
Value_message_name protoreflect.Name = "Value"
Value_message_fullname protoreflect.FullName = "google.protobuf.Value"
)
// Field names for google.protobuf.Value.
const (
Value_NullValue_field_name protoreflect.Name = "null_value"
Value_NumberValue_field_name protoreflect.Name = "number_value"
Value_StringValue_field_name protoreflect.Name = "string_value"
Value_BoolValue_field_name protoreflect.Name = "bool_value"
Value_StructValue_field_name protoreflect.Name = "struct_value"
Value_ListValue_field_name protoreflect.Name = "list_value"
Value_NullValue_field_fullname protoreflect.FullName = "google.protobuf.Value.null_value"
Value_NumberValue_field_fullname protoreflect.FullName = "google.protobuf.Value.number_value"
Value_StringValue_field_fullname protoreflect.FullName = "google.protobuf.Value.string_value"
Value_BoolValue_field_fullname protoreflect.FullName = "google.protobuf.Value.bool_value"
Value_StructValue_field_fullname protoreflect.FullName = "google.protobuf.Value.struct_value"
Value_ListValue_field_fullname protoreflect.FullName = "google.protobuf.Value.list_value"
)
// Field numbers for google.protobuf.Value.
const (
Value_NullValue_field_number protoreflect.FieldNumber = 1
Value_NumberValue_field_number protoreflect.FieldNumber = 2
Value_StringValue_field_number protoreflect.FieldNumber = 3
Value_BoolValue_field_number protoreflect.FieldNumber = 4
Value_StructValue_field_number protoreflect.FieldNumber = 5
Value_ListValue_field_number protoreflect.FieldNumber = 6
)
// Oneof names for google.protobuf.Value.
const (
Value_Kind_oneof_name protoreflect.Name = "kind"
Value_Kind_oneof_fullname protoreflect.FullName = "google.protobuf.Value.kind"
)
// Names for google.protobuf.ListValue.
const (
ListValue_message_name protoreflect.Name = "ListValue"
ListValue_message_fullname protoreflect.FullName = "google.protobuf.ListValue"
)
// Field names for google.protobuf.ListValue.
const (
ListValue_Values_field_name protoreflect.Name = "values"
ListValue_Values_field_fullname protoreflect.FullName = "google.protobuf.ListValue.values"
)
// Field numbers for google.protobuf.ListValue.
const (
ListValue_Values_field_number protoreflect.FieldNumber = 1
)

View File

@ -0,0 +1,34 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_timestamp_proto = "google/protobuf/timestamp.proto"
// Names for google.protobuf.Timestamp.
const (
Timestamp_message_name protoreflect.Name = "Timestamp"
Timestamp_message_fullname protoreflect.FullName = "google.protobuf.Timestamp"
)
// Field names for google.protobuf.Timestamp.
const (
Timestamp_Seconds_field_name protoreflect.Name = "seconds"
Timestamp_Nanos_field_name protoreflect.Name = "nanos"
Timestamp_Seconds_field_fullname protoreflect.FullName = "google.protobuf.Timestamp.seconds"
Timestamp_Nanos_field_fullname protoreflect.FullName = "google.protobuf.Timestamp.nanos"
)
// Field numbers for google.protobuf.Timestamp.
const (
Timestamp_Seconds_field_number protoreflect.FieldNumber = 1
Timestamp_Nanos_field_number protoreflect.FieldNumber = 2
)

View File

@ -0,0 +1,184 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_type_proto = "google/protobuf/type.proto"
// Full and short names for google.protobuf.Syntax.
const (
Syntax_enum_fullname = "google.protobuf.Syntax"
Syntax_enum_name = "Syntax"
)
// Names for google.protobuf.Type.
const (
Type_message_name protoreflect.Name = "Type"
Type_message_fullname protoreflect.FullName = "google.protobuf.Type"
)
// Field names for google.protobuf.Type.
const (
Type_Name_field_name protoreflect.Name = "name"
Type_Fields_field_name protoreflect.Name = "fields"
Type_Oneofs_field_name protoreflect.Name = "oneofs"
Type_Options_field_name protoreflect.Name = "options"
Type_SourceContext_field_name protoreflect.Name = "source_context"
Type_Syntax_field_name protoreflect.Name = "syntax"
Type_Name_field_fullname protoreflect.FullName = "google.protobuf.Type.name"
Type_Fields_field_fullname protoreflect.FullName = "google.protobuf.Type.fields"
Type_Oneofs_field_fullname protoreflect.FullName = "google.protobuf.Type.oneofs"
Type_Options_field_fullname protoreflect.FullName = "google.protobuf.Type.options"
Type_SourceContext_field_fullname protoreflect.FullName = "google.protobuf.Type.source_context"
Type_Syntax_field_fullname protoreflect.FullName = "google.protobuf.Type.syntax"
)
// Field numbers for google.protobuf.Type.
const (
Type_Name_field_number protoreflect.FieldNumber = 1
Type_Fields_field_number protoreflect.FieldNumber = 2
Type_Oneofs_field_number protoreflect.FieldNumber = 3
Type_Options_field_number protoreflect.FieldNumber = 4
Type_SourceContext_field_number protoreflect.FieldNumber = 5
Type_Syntax_field_number protoreflect.FieldNumber = 6
)
// Names for google.protobuf.Field.
const (
Field_message_name protoreflect.Name = "Field"
Field_message_fullname protoreflect.FullName = "google.protobuf.Field"
)
// Field names for google.protobuf.Field.
const (
Field_Kind_field_name protoreflect.Name = "kind"
Field_Cardinality_field_name protoreflect.Name = "cardinality"
Field_Number_field_name protoreflect.Name = "number"
Field_Name_field_name protoreflect.Name = "name"
Field_TypeUrl_field_name protoreflect.Name = "type_url"
Field_OneofIndex_field_name protoreflect.Name = "oneof_index"
Field_Packed_field_name protoreflect.Name = "packed"
Field_Options_field_name protoreflect.Name = "options"
Field_JsonName_field_name protoreflect.Name = "json_name"
Field_DefaultValue_field_name protoreflect.Name = "default_value"
Field_Kind_field_fullname protoreflect.FullName = "google.protobuf.Field.kind"
Field_Cardinality_field_fullname protoreflect.FullName = "google.protobuf.Field.cardinality"
Field_Number_field_fullname protoreflect.FullName = "google.protobuf.Field.number"
Field_Name_field_fullname protoreflect.FullName = "google.protobuf.Field.name"
Field_TypeUrl_field_fullname protoreflect.FullName = "google.protobuf.Field.type_url"
Field_OneofIndex_field_fullname protoreflect.FullName = "google.protobuf.Field.oneof_index"
Field_Packed_field_fullname protoreflect.FullName = "google.protobuf.Field.packed"
Field_Options_field_fullname protoreflect.FullName = "google.protobuf.Field.options"
Field_JsonName_field_fullname protoreflect.FullName = "google.protobuf.Field.json_name"
Field_DefaultValue_field_fullname protoreflect.FullName = "google.protobuf.Field.default_value"
)
// Field numbers for google.protobuf.Field.
const (
Field_Kind_field_number protoreflect.FieldNumber = 1
Field_Cardinality_field_number protoreflect.FieldNumber = 2
Field_Number_field_number protoreflect.FieldNumber = 3
Field_Name_field_number protoreflect.FieldNumber = 4
Field_TypeUrl_field_number protoreflect.FieldNumber = 6
Field_OneofIndex_field_number protoreflect.FieldNumber = 7
Field_Packed_field_number protoreflect.FieldNumber = 8
Field_Options_field_number protoreflect.FieldNumber = 9
Field_JsonName_field_number protoreflect.FieldNumber = 10
Field_DefaultValue_field_number protoreflect.FieldNumber = 11
)
// Full and short names for google.protobuf.Field.Kind.
const (
Field_Kind_enum_fullname = "google.protobuf.Field.Kind"
Field_Kind_enum_name = "Kind"
)
// Full and short names for google.protobuf.Field.Cardinality.
const (
Field_Cardinality_enum_fullname = "google.protobuf.Field.Cardinality"
Field_Cardinality_enum_name = "Cardinality"
)
// Names for google.protobuf.Enum.
const (
Enum_message_name protoreflect.Name = "Enum"
Enum_message_fullname protoreflect.FullName = "google.protobuf.Enum"
)
// Field names for google.protobuf.Enum.
const (
Enum_Name_field_name protoreflect.Name = "name"
Enum_Enumvalue_field_name protoreflect.Name = "enumvalue"
Enum_Options_field_name protoreflect.Name = "options"
Enum_SourceContext_field_name protoreflect.Name = "source_context"
Enum_Syntax_field_name protoreflect.Name = "syntax"
Enum_Name_field_fullname protoreflect.FullName = "google.protobuf.Enum.name"
Enum_Enumvalue_field_fullname protoreflect.FullName = "google.protobuf.Enum.enumvalue"
Enum_Options_field_fullname protoreflect.FullName = "google.protobuf.Enum.options"
Enum_SourceContext_field_fullname protoreflect.FullName = "google.protobuf.Enum.source_context"
Enum_Syntax_field_fullname protoreflect.FullName = "google.protobuf.Enum.syntax"
)
// Field numbers for google.protobuf.Enum.
const (
Enum_Name_field_number protoreflect.FieldNumber = 1
Enum_Enumvalue_field_number protoreflect.FieldNumber = 2
Enum_Options_field_number protoreflect.FieldNumber = 3
Enum_SourceContext_field_number protoreflect.FieldNumber = 4
Enum_Syntax_field_number protoreflect.FieldNumber = 5
)
// Names for google.protobuf.EnumValue.
const (
EnumValue_message_name protoreflect.Name = "EnumValue"
EnumValue_message_fullname protoreflect.FullName = "google.protobuf.EnumValue"
)
// Field names for google.protobuf.EnumValue.
const (
EnumValue_Name_field_name protoreflect.Name = "name"
EnumValue_Number_field_name protoreflect.Name = "number"
EnumValue_Options_field_name protoreflect.Name = "options"
EnumValue_Name_field_fullname protoreflect.FullName = "google.protobuf.EnumValue.name"
EnumValue_Number_field_fullname protoreflect.FullName = "google.protobuf.EnumValue.number"
EnumValue_Options_field_fullname protoreflect.FullName = "google.protobuf.EnumValue.options"
)
// Field numbers for google.protobuf.EnumValue.
const (
EnumValue_Name_field_number protoreflect.FieldNumber = 1
EnumValue_Number_field_number protoreflect.FieldNumber = 2
EnumValue_Options_field_number protoreflect.FieldNumber = 3
)
// Names for google.protobuf.Option.
const (
Option_message_name protoreflect.Name = "Option"
Option_message_fullname protoreflect.FullName = "google.protobuf.Option"
)
// Field names for google.protobuf.Option.
const (
Option_Name_field_name protoreflect.Name = "name"
Option_Value_field_name protoreflect.Name = "value"
Option_Name_field_fullname protoreflect.FullName = "google.protobuf.Option.name"
Option_Value_field_fullname protoreflect.FullName = "google.protobuf.Option.value"
)
// Field numbers for google.protobuf.Option.
const (
Option_Name_field_number protoreflect.FieldNumber = 1
Option_Value_field_number protoreflect.FieldNumber = 2
)

View File

@ -0,0 +1,13 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package genid
import protoreflect "google.golang.org/protobuf/reflect/protoreflect"
// Generic field name and number for messages in wrappers.proto.
const (
WrapperValue_Value_field_name protoreflect.Name = "value"
WrapperValue_Value_field_number protoreflect.FieldNumber = 1
)

View File

@ -0,0 +1,175 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package genid
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const File_google_protobuf_wrappers_proto = "google/protobuf/wrappers.proto"
// Names for google.protobuf.DoubleValue.
const (
DoubleValue_message_name protoreflect.Name = "DoubleValue"
DoubleValue_message_fullname protoreflect.FullName = "google.protobuf.DoubleValue"
)
// Field names for google.protobuf.DoubleValue.
const (
DoubleValue_Value_field_name protoreflect.Name = "value"
DoubleValue_Value_field_fullname protoreflect.FullName = "google.protobuf.DoubleValue.value"
)
// Field numbers for google.protobuf.DoubleValue.
const (
DoubleValue_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.FloatValue.
const (
FloatValue_message_name protoreflect.Name = "FloatValue"
FloatValue_message_fullname protoreflect.FullName = "google.protobuf.FloatValue"
)
// Field names for google.protobuf.FloatValue.
const (
FloatValue_Value_field_name protoreflect.Name = "value"
FloatValue_Value_field_fullname protoreflect.FullName = "google.protobuf.FloatValue.value"
)
// Field numbers for google.protobuf.FloatValue.
const (
FloatValue_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.Int64Value.
const (
Int64Value_message_name protoreflect.Name = "Int64Value"
Int64Value_message_fullname protoreflect.FullName = "google.protobuf.Int64Value"
)
// Field names for google.protobuf.Int64Value.
const (
Int64Value_Value_field_name protoreflect.Name = "value"
Int64Value_Value_field_fullname protoreflect.FullName = "google.protobuf.Int64Value.value"
)
// Field numbers for google.protobuf.Int64Value.
const (
Int64Value_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.UInt64Value.
const (
UInt64Value_message_name protoreflect.Name = "UInt64Value"
UInt64Value_message_fullname protoreflect.FullName = "google.protobuf.UInt64Value"
)
// Field names for google.protobuf.UInt64Value.
const (
UInt64Value_Value_field_name protoreflect.Name = "value"
UInt64Value_Value_field_fullname protoreflect.FullName = "google.protobuf.UInt64Value.value"
)
// Field numbers for google.protobuf.UInt64Value.
const (
UInt64Value_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.Int32Value.
const (
Int32Value_message_name protoreflect.Name = "Int32Value"
Int32Value_message_fullname protoreflect.FullName = "google.protobuf.Int32Value"
)
// Field names for google.protobuf.Int32Value.
const (
Int32Value_Value_field_name protoreflect.Name = "value"
Int32Value_Value_field_fullname protoreflect.FullName = "google.protobuf.Int32Value.value"
)
// Field numbers for google.protobuf.Int32Value.
const (
Int32Value_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.UInt32Value.
const (
UInt32Value_message_name protoreflect.Name = "UInt32Value"
UInt32Value_message_fullname protoreflect.FullName = "google.protobuf.UInt32Value"
)
// Field names for google.protobuf.UInt32Value.
const (
UInt32Value_Value_field_name protoreflect.Name = "value"
UInt32Value_Value_field_fullname protoreflect.FullName = "google.protobuf.UInt32Value.value"
)
// Field numbers for google.protobuf.UInt32Value.
const (
UInt32Value_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.BoolValue.
const (
BoolValue_message_name protoreflect.Name = "BoolValue"
BoolValue_message_fullname protoreflect.FullName = "google.protobuf.BoolValue"
)
// Field names for google.protobuf.BoolValue.
const (
BoolValue_Value_field_name protoreflect.Name = "value"
BoolValue_Value_field_fullname protoreflect.FullName = "google.protobuf.BoolValue.value"
)
// Field numbers for google.protobuf.BoolValue.
const (
BoolValue_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.StringValue.
const (
StringValue_message_name protoreflect.Name = "StringValue"
StringValue_message_fullname protoreflect.FullName = "google.protobuf.StringValue"
)
// Field names for google.protobuf.StringValue.
const (
StringValue_Value_field_name protoreflect.Name = "value"
StringValue_Value_field_fullname protoreflect.FullName = "google.protobuf.StringValue.value"
)
// Field numbers for google.protobuf.StringValue.
const (
StringValue_Value_field_number protoreflect.FieldNumber = 1
)
// Names for google.protobuf.BytesValue.
const (
BytesValue_message_name protoreflect.Name = "BytesValue"
BytesValue_message_fullname protoreflect.FullName = "google.protobuf.BytesValue"
)
// Field names for google.protobuf.BytesValue.
const (
BytesValue_Value_field_name protoreflect.Name = "value"
BytesValue_Value_field_fullname protoreflect.FullName = "google.protobuf.BytesValue.value"
)
// Field numbers for google.protobuf.BytesValue.
const (
BytesValue_Value_field_number protoreflect.FieldNumber = 1
)

View File

@ -1,25 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package genname contains constants for generated names.
package genname
const (
State = "state"
SizeCache = "sizeCache"
SizeCacheA = "XXX_sizecache"
WeakFields = "weakFields"
WeakFieldsA = "XXX_weak"
UnknownFields = "unknownFields"
UnknownFieldsA = "XXX_unrecognized"
ExtensionFields = "extensionFields"
ExtensionFieldsA = "XXX_InternalExtensions"
ExtensionFieldsB = "XXX_extensions"
WeakFieldPrefix = "XXX_weak_"
)

View File

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
piface "google.golang.org/protobuf/runtime/protoiface" piface "google.golang.org/protobuf/runtime/protoiface"
@ -19,6 +20,12 @@ import (
// functions that we do not want to appear in godoc. // functions that we do not want to appear in godoc.
type Export struct{} type Export struct{}
// NewError formats a string according to the format specifier and arguments and
// returns an error that has a "proto" prefix.
func (Export) NewError(f string, x ...interface{}) error {
return errors.New(f, x...)
}
// enum is any enum type generated by protoc-gen-go // enum is any enum type generated by protoc-gen-go
// and must be a named int32 type. // and must be a named int32 type.
type enum = interface{} type enum = interface{}
@ -160,7 +167,7 @@ func (Export) MessageTypeOf(m message) pref.MessageType {
if mv := (Export{}).protoMessageV2Of(m); mv != nil { if mv := (Export{}).protoMessageV2Of(m); mv != nil {
return mv.ProtoReflect().Type() return mv.ProtoReflect().Type()
} }
return legacyLoadMessageInfo(reflect.TypeOf(m), "") return legacyLoadMessageType(reflect.TypeOf(m), "")
} }
// MessageStringOf returns the message value as a string, // MessageStringOf returns the message value as a string,

View File

@ -10,6 +10,7 @@ import (
"sync" "sync"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry" preg "google.golang.org/protobuf/reflect/protoregistry"
@ -20,6 +21,7 @@ type errInvalidUTF8 struct{}
func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" }
func (errInvalidUTF8) InvalidUTF8() bool { return true } func (errInvalidUTF8) InvalidUTF8() bool { return true }
func (errInvalidUTF8) Unwrap() error { return errors.Error }
// initOneofFieldCoders initializes the fast-path functions for the fields in a oneof. // initOneofFieldCoders initializes the fast-path functions for the fields in a oneof.
// //
@ -242,7 +244,7 @@ func consumeMessageInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldI
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
if p.Elem().IsNil() { if p.Elem().IsNil() {
p.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem()))) p.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem())))
@ -276,7 +278,7 @@ func consumeMessage(b []byte, m proto.Message, wtyp protowire.Type, opts unmarsh
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
Buf: v, Buf: v,
@ -420,7 +422,7 @@ func consumeGroup(b []byte, m proto.Message, num protowire.Number, wtyp protowir
} }
b, n := protowire.ConsumeGroup(num, b) b, n := protowire.ConsumeGroup(num, b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
Buf: b, Buf: b,
@ -494,7 +496,7 @@ func consumeMessageSliceInfo(b []byte, p pointer, wtyp protowire.Type, f *coderF
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
m := reflect.New(f.mi.GoReflectType.Elem()).Interface() m := reflect.New(f.mi.GoReflectType.Elem()).Interface()
mp := pointerOfIface(m) mp := pointerOfIface(m)
@ -550,7 +552,7 @@ func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp protowir
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
mp := reflect.New(goType.Elem()) mp := reflect.New(goType.Elem())
o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
@ -613,7 +615,7 @@ func consumeMessageSliceValue(b []byte, listv pref.Value, _ protowire.Number, wt
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return pref.Value{}, out, protowire.ParseError(n) return pref.Value{}, out, errDecode
} }
m := list.NewElement() m := list.NewElement()
o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
@ -681,7 +683,7 @@ func consumeGroupSliceValue(b []byte, listv pref.Value, num protowire.Number, wt
} }
b, n := protowire.ConsumeGroup(num, b) b, n := protowire.ConsumeGroup(num, b)
if n < 0 { if n < 0 {
return pref.Value{}, out, protowire.ParseError(n) return pref.Value{}, out, errDecode
} }
m := list.NewElement() m := list.NewElement()
o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
@ -767,7 +769,7 @@ func consumeGroupSlice(b []byte, p pointer, num protowire.Number, wtyp protowire
} }
b, n := protowire.ConsumeGroup(num, b) b, n := protowire.ConsumeGroup(num, b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
mp := reflect.New(goType.Elem()) mp := reflect.New(goType.Elem())
o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{

File diff suppressed because it is too large Load Diff

View File

@ -5,11 +5,11 @@
package impl package impl
import ( import (
"errors"
"reflect" "reflect"
"sort" "sort"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/genid"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
) )
@ -117,7 +117,7 @@ func consumeMap(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi *mapInfo
} }
b, n := protowire.ConsumeBytes(b) b, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
var ( var (
key = mapi.keyZero key = mapi.keyZero
@ -126,15 +126,15 @@ func consumeMap(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi *mapInfo
for len(b) > 0 { for len(b) > 0 {
num, wtyp, n := protowire.ConsumeTag(b) num, wtyp, n := protowire.ConsumeTag(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
if num > protowire.MaxValidNumber { if num > protowire.MaxValidNumber {
return out, errors.New("invalid field number") return out, errDecode
} }
b = b[n:] b = b[n:]
err := errUnknown err := errUnknown
switch num { switch num {
case 1: case genid.MapEntry_Key_field_number:
var v pref.Value var v pref.Value
var o unmarshalOutput var o unmarshalOutput
v, o, err = mapi.keyFuncs.unmarshal(b, key, num, wtyp, opts) v, o, err = mapi.keyFuncs.unmarshal(b, key, num, wtyp, opts)
@ -143,7 +143,7 @@ func consumeMap(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi *mapInfo
} }
key = v key = v
n = o.n n = o.n
case 2: case genid.MapEntry_Value_field_number:
var v pref.Value var v pref.Value
var o unmarshalOutput var o unmarshalOutput
v, o, err = mapi.valFuncs.unmarshal(b, val, num, wtyp, opts) v, o, err = mapi.valFuncs.unmarshal(b, val, num, wtyp, opts)
@ -156,7 +156,7 @@ func consumeMap(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi *mapInfo
if err == errUnknown { if err == errUnknown {
n = protowire.ConsumeFieldValue(num, wtyp, b) n = protowire.ConsumeFieldValue(num, wtyp, b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
} else if err != nil { } else if err != nil {
return out, err return out, err
@ -174,7 +174,7 @@ func consumeMapOfMessage(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi
} }
b, n := protowire.ConsumeBytes(b) b, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
var ( var (
key = mapi.keyZero key = mapi.keyZero
@ -183,10 +183,10 @@ func consumeMapOfMessage(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi
for len(b) > 0 { for len(b) > 0 {
num, wtyp, n := protowire.ConsumeTag(b) num, wtyp, n := protowire.ConsumeTag(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
if num > protowire.MaxValidNumber { if num > protowire.MaxValidNumber {
return out, errors.New("invalid field number") return out, errDecode
} }
b = b[n:] b = b[n:]
err := errUnknown err := errUnknown
@ -207,7 +207,7 @@ func consumeMapOfMessage(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi
var v []byte var v []byte
v, n = protowire.ConsumeBytes(b) v, n = protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
var o unmarshalOutput var o unmarshalOutput
o, err = f.mi.unmarshalPointer(v, pointerOfValue(val), 0, opts) o, err = f.mi.unmarshalPointer(v, pointerOfValue(val), 0, opts)
@ -220,7 +220,7 @@ func consumeMapOfMessage(b []byte, mapv reflect.Value, wtyp protowire.Type, mapi
if err == errUnknown { if err == errUnknown {
n = protowire.ConsumeFieldValue(num, wtyp, b) n = protowire.ConsumeFieldValue(num, wtyp, b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
} else if err != nil { } else if err != nil {
return out, err return out, err

View File

@ -11,7 +11,7 @@ import (
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/fieldsort" "google.golang.org/protobuf/internal/order"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
piface "google.golang.org/protobuf/runtime/protoiface" piface "google.golang.org/protobuf/runtime/protoiface"
) )
@ -27,6 +27,7 @@ type coderMessageInfo struct {
coderFields map[protowire.Number]*coderFieldInfo coderFields map[protowire.Number]*coderFieldInfo
sizecacheOffset offset sizecacheOffset offset
unknownOffset offset unknownOffset offset
unknownPtrKind bool
extensionOffset offset extensionOffset offset
needsInitCheck bool needsInitCheck bool
isMessageSet bool isMessageSet bool
@ -47,9 +48,20 @@ type coderFieldInfo struct {
} }
func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) { func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
mi.sizecacheOffset = si.sizecacheOffset mi.sizecacheOffset = invalidOffset
mi.unknownOffset = si.unknownOffset mi.unknownOffset = invalidOffset
mi.extensionOffset = si.extensionOffset mi.extensionOffset = invalidOffset
if si.sizecacheOffset.IsValid() && si.sizecacheType == sizecacheType {
mi.sizecacheOffset = si.sizecacheOffset
}
if si.unknownOffset.IsValid() && (si.unknownType == unknownFieldsAType || si.unknownType == unknownFieldsBType) {
mi.unknownOffset = si.unknownOffset
mi.unknownPtrKind = si.unknownType.Kind() == reflect.Ptr
}
if si.extensionOffset.IsValid() && si.extensionType == extensionFieldsType {
mi.extensionOffset = si.extensionOffset
}
mi.coderFields = make(map[protowire.Number]*coderFieldInfo) mi.coderFields = make(map[protowire.Number]*coderFieldInfo)
fields := mi.Desc.Fields() fields := mi.Desc.Fields()
@ -73,6 +85,27 @@ func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
var funcs pointerCoderFuncs var funcs pointerCoderFuncs
var childMessage *MessageInfo var childMessage *MessageInfo
switch { switch {
case ft == nil:
// This never occurs for generated message types.
// It implies that a hand-crafted type has missing Go fields
// for specific protobuf message fields.
funcs = pointerCoderFuncs{
size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
return 0
},
marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
return nil, nil
},
unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
panic("missing Go struct field for " + string(fd.FullName()))
},
isInit: func(p pointer, f *coderFieldInfo) error {
panic("missing Go struct field for " + string(fd.FullName()))
},
merge: func(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
panic("missing Go struct field for " + string(fd.FullName()))
},
}
case isOneof: case isOneof:
fieldOffset = offsetOf(fs, mi.Exporter) fieldOffset = offsetOf(fs, mi.Exporter)
case fd.IsWeak(): case fd.IsWeak():
@ -136,7 +169,7 @@ func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
sort.Slice(mi.orderedCoderFields, func(i, j int) bool { sort.Slice(mi.orderedCoderFields, func(i, j int) bool {
fi := fields.ByNumber(mi.orderedCoderFields[i].num) fi := fields.ByNumber(mi.orderedCoderFields[i].num)
fj := fields.ByNumber(mi.orderedCoderFields[j].num) fj := fields.ByNumber(mi.orderedCoderFields[j].num)
return fieldsort.Less(fi, fj) return order.LegacyFieldOrder(fi, fj)
}) })
} }
@ -157,3 +190,28 @@ func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
mi.methods.Merge = mi.merge mi.methods.Merge = mi.merge
} }
} }
// getUnknownBytes returns a *[]byte for the unknown fields.
// It is the caller's responsibility to check whether the pointer is nil.
// This function is specially designed to be inlineable.
func (mi *MessageInfo) getUnknownBytes(p pointer) *[]byte {
if mi.unknownPtrKind {
return *p.Apply(mi.unknownOffset).BytesPtr()
} else {
return p.Apply(mi.unknownOffset).Bytes()
}
}
// mutableUnknownBytes returns a *[]byte for the unknown fields.
// The returned pointer is guaranteed to not be nil.
func (mi *MessageInfo) mutableUnknownBytes(p pointer) *[]byte {
if mi.unknownPtrKind {
bp := p.Apply(mi.unknownOffset).BytesPtr()
if *bp == nil {
*bp = new([]byte)
}
return *bp
} else {
return p.Apply(mi.unknownOffset).Bytes()
}
}

View File

@ -29,8 +29,9 @@ func sizeMessageSet(mi *MessageInfo, p pointer, opts marshalOptions) (size int)
size += xi.funcs.size(x.Value(), protowire.SizeTag(messageset.FieldMessage), opts) size += xi.funcs.size(x.Value(), protowire.SizeTag(messageset.FieldMessage), opts)
} }
unknown := *p.Apply(mi.unknownOffset).Bytes() if u := mi.getUnknownBytes(p); u != nil {
size += messageset.SizeUnknown(unknown) size += messageset.SizeUnknown(*u)
}
return size return size
} }
@ -69,10 +70,12 @@ func marshalMessageSet(mi *MessageInfo, b []byte, p pointer, opts marshalOptions
} }
} }
unknown := *p.Apply(mi.unknownOffset).Bytes() if u := mi.getUnknownBytes(p); u != nil {
b, err := messageset.AppendUnknown(b, unknown) var err error
if err != nil { b, err = messageset.AppendUnknown(b, *u)
return b, err if err != nil {
return b, err
}
} }
return b, nil return b, nil
@ -100,13 +103,13 @@ func unmarshalMessageSet(mi *MessageInfo, b []byte, p pointer, opts unmarshalOpt
*ep = make(map[int32]ExtensionField) *ep = make(map[int32]ExtensionField)
} }
ext := *ep ext := *ep
unknown := p.Apply(mi.unknownOffset).Bytes()
initialized := true initialized := true
err = messageset.Unmarshal(b, true, func(num protowire.Number, v []byte) error { err = messageset.Unmarshal(b, true, func(num protowire.Number, v []byte) error {
o, err := mi.unmarshalExtension(v, num, protowire.BytesType, ext, opts) o, err := mi.unmarshalExtension(v, num, protowire.BytesType, ext, opts)
if err == errUnknown { if err == errUnknown {
*unknown = protowire.AppendTag(*unknown, num, protowire.BytesType) u := mi.mutableUnknownBytes(p)
*unknown = append(*unknown, v...) *u = protowire.AppendTag(*u, num, protowire.BytesType)
*u = append(*u, v...)
return nil return nil
} }
if !o.initialized { if !o.initialized {

View File

@ -30,7 +30,7 @@ func consumeEnum(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, _
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
p.v.Elem().SetInt(int64(v)) p.v.Elem().SetInt(int64(v))
out.n = n out.n = n
@ -130,12 +130,12 @@ func consumeEnumSlice(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInf
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
b, n := protowire.ConsumeBytes(b) b, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
for len(b) > 0 { for len(b) > 0 {
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
rv := reflect.New(s.Type().Elem()).Elem() rv := reflect.New(s.Type().Elem()).Elem()
rv.SetInt(int64(v)) rv.SetInt(int64(v))
@ -150,7 +150,7 @@ func consumeEnumSlice(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInf
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
rv := reflect.New(s.Type().Elem()).Elem() rv := reflect.New(s.Type().Elem()).Elem()
rv.SetInt(int64(v)) rv.SetInt(int64(v))

View File

@ -423,6 +423,13 @@ func (c *messageConverter) PBValueOf(v reflect.Value) pref.Value {
if v.Type() != c.goType { if v.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
} }
if c.isNonPointer() {
if v.CanAddr() {
v = v.Addr() // T => *T
} else {
v = reflect.Zero(reflect.PtrTo(v.Type()))
}
}
if m, ok := v.Interface().(pref.ProtoMessage); ok { if m, ok := v.Interface().(pref.ProtoMessage); ok {
return pref.ValueOfMessage(m.ProtoReflect()) return pref.ValueOfMessage(m.ProtoReflect())
} }
@ -437,6 +444,16 @@ func (c *messageConverter) GoValueOf(v pref.Value) reflect.Value {
} else { } else {
rv = reflect.ValueOf(m.Interface()) rv = reflect.ValueOf(m.Interface())
} }
if c.isNonPointer() {
if rv.Type() != reflect.PtrTo(c.goType) {
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), reflect.PtrTo(c.goType)))
}
if !rv.IsNil() {
rv = rv.Elem() // *T => T
} else {
rv = reflect.Zero(rv.Type().Elem())
}
}
if rv.Type() != c.goType { if rv.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), c.goType)) panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), c.goType))
} }
@ -451,6 +468,9 @@ func (c *messageConverter) IsValidPB(v pref.Value) bool {
} else { } else {
rv = reflect.ValueOf(m.Interface()) rv = reflect.ValueOf(m.Interface())
} }
if c.isNonPointer() {
return rv.Type() == reflect.PtrTo(c.goType)
}
return rv.Type() == c.goType return rv.Type() == c.goType
} }
@ -459,9 +479,18 @@ func (c *messageConverter) IsValidGo(v reflect.Value) bool {
} }
func (c *messageConverter) New() pref.Value { func (c *messageConverter) New() pref.Value {
if c.isNonPointer() {
return c.PBValueOf(reflect.New(c.goType).Elem())
}
return c.PBValueOf(reflect.New(c.goType.Elem())) return c.PBValueOf(reflect.New(c.goType.Elem()))
} }
func (c *messageConverter) Zero() pref.Value { func (c *messageConverter) Zero() pref.Value {
return c.PBValueOf(reflect.Zero(c.goType)) return c.PBValueOf(reflect.Zero(c.goType))
} }
// isNonPointer reports whether the type is a non-pointer type.
// This never occurs for generated message types.
func (c *messageConverter) isNonPointer() bool {
return c.goType.Kind() != reflect.Ptr
}

View File

@ -17,6 +17,8 @@ import (
piface "google.golang.org/protobuf/runtime/protoiface" piface "google.golang.org/protobuf/runtime/protoiface"
) )
var errDecode = errors.New("cannot parse invalid wire-format data")
type unmarshalOptions struct { type unmarshalOptions struct {
flags protoiface.UnmarshalInputFlags flags protoiface.UnmarshalInputFlags
resolver interface { resolver interface {
@ -100,13 +102,13 @@ func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag protowire.
var n int var n int
tag, n = protowire.ConsumeVarint(b) tag, n = protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
b = b[n:] b = b[n:]
} }
var num protowire.Number var num protowire.Number
if n := tag >> 3; n < uint64(protowire.MinValidNumber) || n > uint64(protowire.MaxValidNumber) { if n := tag >> 3; n < uint64(protowire.MinValidNumber) || n > uint64(protowire.MaxValidNumber) {
return out, errors.New("invalid field number") return out, errDecode
} else { } else {
num = protowire.Number(n) num = protowire.Number(n)
} }
@ -114,7 +116,7 @@ func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag protowire.
if wtyp == protowire.EndGroupType { if wtyp == protowire.EndGroupType {
if num != groupTag { if num != groupTag {
return out, errors.New("mismatching end group marker") return out, errDecode
} }
groupTag = 0 groupTag = 0
break break
@ -170,10 +172,10 @@ func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag protowire.
} }
n = protowire.ConsumeFieldValue(num, wtyp, b) n = protowire.ConsumeFieldValue(num, wtyp, b)
if n < 0 { if n < 0 {
return out, protowire.ParseError(n) return out, errDecode
} }
if !opts.DiscardUnknown() && mi.unknownOffset.IsValid() { if !opts.DiscardUnknown() && mi.unknownOffset.IsValid() {
u := p.Apply(mi.unknownOffset).Bytes() u := mi.mutableUnknownBytes(p)
*u = protowire.AppendTag(*u, num, wtyp) *u = protowire.AppendTag(*u, num, wtyp)
*u = append(*u, b[:n]...) *u = append(*u, b[:n]...)
} }
@ -181,7 +183,7 @@ func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag protowire.
b = b[n:] b = b[n:]
} }
if groupTag != 0 { if groupTag != 0 {
return out, errors.New("missing end group marker") return out, errDecode
} }
if mi.numRequiredFields > 0 && bits.OnesCount64(requiredMask) != int(mi.numRequiredFields) { if mi.numRequiredFields > 0 && bits.OnesCount64(requiredMask) != int(mi.numRequiredFields) {
initialized = false initialized = false
@ -221,7 +223,7 @@ func (mi *MessageInfo) unmarshalExtension(b []byte, num protowire.Number, wtyp p
return out, nil return out, nil
} }
case ValidationInvalid: case ValidationInvalid:
return out, errors.New("invalid wire format") return out, errDecode
case ValidationUnknown: case ValidationUnknown:
} }
} }

View File

@ -79,8 +79,9 @@ func (mi *MessageInfo) sizePointerSlow(p pointer, opts marshalOptions) (size int
size += f.funcs.size(fptr, f, opts) size += f.funcs.size(fptr, f, opts)
} }
if mi.unknownOffset.IsValid() { if mi.unknownOffset.IsValid() {
u := *p.Apply(mi.unknownOffset).Bytes() if u := mi.getUnknownBytes(p); u != nil {
size += len(u) size += len(*u)
}
} }
if mi.sizecacheOffset.IsValid() { if mi.sizecacheOffset.IsValid() {
if size > math.MaxInt32 { if size > math.MaxInt32 {
@ -141,8 +142,9 @@ func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOpt
} }
} }
if mi.unknownOffset.IsValid() && !mi.isMessageSet { if mi.unknownOffset.IsValid() && !mi.isMessageSet {
u := *p.Apply(mi.unknownOffset).Bytes() if u := mi.getUnknownBytes(p); u != nil {
b = append(b, u...) b = append(b, (*u)...)
}
} }
return b, nil return b, nil
} }

View File

@ -30,7 +30,7 @@ func (Export) LegacyMessageTypeOf(m piface.MessageV1, name pref.FullName) pref.M
if mv := (Export{}).protoMessageV2Of(m); mv != nil { if mv := (Export{}).protoMessageV2Of(m); mv != nil {
return mv.ProtoReflect().Type() return mv.ProtoReflect().Type()
} }
return legacyLoadMessageInfo(reflect.TypeOf(m), name) return legacyLoadMessageType(reflect.TypeOf(m), name)
} }
// UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input. // UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input.

View File

@ -154,7 +154,8 @@ func (x placeholderExtension) Number() pref.FieldNumber { retu
func (x placeholderExtension) Cardinality() pref.Cardinality { return 0 } func (x placeholderExtension) Cardinality() pref.Cardinality { return 0 }
func (x placeholderExtension) Kind() pref.Kind { return 0 } func (x placeholderExtension) Kind() pref.Kind { return 0 }
func (x placeholderExtension) HasJSONName() bool { return false } func (x placeholderExtension) HasJSONName() bool { return false }
func (x placeholderExtension) JSONName() string { return "" } func (x placeholderExtension) JSONName() string { return "[" + string(x.name) + "]" }
func (x placeholderExtension) TextName() string { return "[" + string(x.name) + "]" }
func (x placeholderExtension) HasPresence() bool { return false } func (x placeholderExtension) HasPresence() bool { return false }
func (x placeholderExtension) HasOptionalKeyword() bool { return false } func (x placeholderExtension) HasOptionalKeyword() bool { return false }
func (x placeholderExtension) IsExtension() bool { return true } func (x placeholderExtension) IsExtension() bool { return true }

View File

@ -24,14 +24,24 @@ import (
// legacyWrapMessage wraps v as a protoreflect.Message, // legacyWrapMessage wraps v as a protoreflect.Message,
// where v must be a *struct kind and not implement the v2 API already. // where v must be a *struct kind and not implement the v2 API already.
func legacyWrapMessage(v reflect.Value) pref.Message { func legacyWrapMessage(v reflect.Value) pref.Message {
typ := v.Type() t := v.Type()
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct { if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
return aberrantMessage{v: v} return aberrantMessage{v: v}
} }
mt := legacyLoadMessageInfo(typ, "") mt := legacyLoadMessageInfo(t, "")
return mt.MessageOf(v.Interface()) return mt.MessageOf(v.Interface())
} }
// legacyLoadMessageType dynamically loads a protoreflect.Type for t,
// where t must be not implement the v2 API already.
// The provided name is used if it cannot be determined from the message.
func legacyLoadMessageType(t reflect.Type, name pref.FullName) protoreflect.MessageType {
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
return aberrantMessageType{t}
}
return legacyLoadMessageInfo(t, name)
}
var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
// legacyLoadMessageInfo dynamically loads a *MessageInfo for t, // legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
@ -49,8 +59,9 @@ func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
GoReflectType: t, GoReflectType: t,
} }
var hasMarshal, hasUnmarshal bool
v := reflect.Zero(t).Interface() v := reflect.Zero(t).Interface()
if _, ok := v.(legacyMarshaler); ok { if _, hasMarshal = v.(legacyMarshaler); hasMarshal {
mi.methods.Marshal = legacyMarshal mi.methods.Marshal = legacyMarshal
// We have no way to tell whether the type's Marshal method // We have no way to tell whether the type's Marshal method
@ -59,10 +70,10 @@ func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
// calling Marshal methods when present. // calling Marshal methods when present.
mi.methods.Flags |= piface.SupportMarshalDeterministic mi.methods.Flags |= piface.SupportMarshalDeterministic
} }
if _, ok := v.(legacyUnmarshaler); ok { if _, hasUnmarshal = v.(legacyUnmarshaler); hasUnmarshal {
mi.methods.Unmarshal = legacyUnmarshal mi.methods.Unmarshal = legacyUnmarshal
} }
if _, ok := v.(legacyMerger); ok { if _, hasMerge := v.(legacyMerger); hasMerge || (hasMarshal && hasUnmarshal) {
mi.methods.Merge = legacyMerge mi.methods.Merge = legacyMerge
} }
@ -75,7 +86,7 @@ func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type, // LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
// which must be a *struct kind and not implement the v2 API already. // which should be a *struct kind and must not implement the v2 API already.
// //
// This is exported for testing purposes. // This is exported for testing purposes.
func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor { func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
@ -114,17 +125,19 @@ func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescr
// If the Go type has no fields, then this might be a proto3 empty message // If the Go type has no fields, then this might be a proto3 empty message
// from before the size cache was added. If there are any fields, check to // from before the size cache was added. If there are any fields, check to
// see that at least one of them looks like something we generated. // see that at least one of them looks like something we generated.
if nfield := t.Elem().NumField(); nfield > 0 { if t.Elem().Kind() == reflect.Struct {
hasProtoField := false if nfield := t.Elem().NumField(); nfield > 0 {
for i := 0; i < nfield; i++ { hasProtoField := false
f := t.Elem().Field(i) for i := 0; i < nfield; i++ {
if f.Tag.Get("protobuf") != "" || f.Tag.Get("protobuf_oneof") != "" || strings.HasPrefix(f.Name, "XXX_") { f := t.Elem().Field(i)
hasProtoField = true if f.Tag.Get("protobuf") != "" || f.Tag.Get("protobuf_oneof") != "" || strings.HasPrefix(f.Name, "XXX_") {
break hasProtoField = true
break
}
}
if !hasProtoField {
return aberrantLoadMessageDesc(t, name)
} }
}
if !hasProtoField {
return aberrantLoadMessageDesc(t, name)
} }
} }
@ -370,7 +383,7 @@ type legacyMerger interface {
Merge(protoiface.MessageV1) Merge(protoiface.MessageV1)
} }
var legacyProtoMethods = &piface.Methods{ var aberrantProtoMethods = &piface.Methods{
Marshal: legacyMarshal, Marshal: legacyMarshal,
Unmarshal: legacyUnmarshal, Unmarshal: legacyUnmarshal,
Merge: legacyMerge, Merge: legacyMerge,
@ -401,18 +414,40 @@ func legacyUnmarshal(in piface.UnmarshalInput) (piface.UnmarshalOutput, error) {
v := in.Message.(unwrapper).protoUnwrap() v := in.Message.(unwrapper).protoUnwrap()
unmarshaler, ok := v.(legacyUnmarshaler) unmarshaler, ok := v.(legacyUnmarshaler)
if !ok { if !ok {
return piface.UnmarshalOutput{}, errors.New("%T does not implement Marshal", v) return piface.UnmarshalOutput{}, errors.New("%T does not implement Unmarshal", v)
} }
return piface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf) return piface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf)
} }
func legacyMerge(in piface.MergeInput) piface.MergeOutput { func legacyMerge(in piface.MergeInput) piface.MergeOutput {
// Check whether this supports the legacy merger.
dstv := in.Destination.(unwrapper).protoUnwrap() dstv := in.Destination.(unwrapper).protoUnwrap()
merger, ok := dstv.(legacyMerger) merger, ok := dstv.(legacyMerger)
if ok {
merger.Merge(Export{}.ProtoMessageV1Of(in.Source))
return piface.MergeOutput{Flags: piface.MergeComplete}
}
// If legacy merger is unavailable, implement merge in terms of
// a marshal and unmarshal operation.
srcv := in.Source.(unwrapper).protoUnwrap()
marshaler, ok := srcv.(legacyMarshaler)
if !ok { if !ok {
return piface.MergeOutput{} return piface.MergeOutput{}
} }
merger.Merge(Export{}.ProtoMessageV1Of(in.Source)) dstv = in.Destination.(unwrapper).protoUnwrap()
unmarshaler, ok := dstv.(legacyUnmarshaler)
if !ok {
return piface.MergeOutput{}
}
b, err := marshaler.Marshal()
if err != nil {
return piface.MergeOutput{}
}
err = unmarshaler.Unmarshal(b)
if err != nil {
return piface.MergeOutput{}
}
return piface.MergeOutput{Flags: piface.MergeComplete} return piface.MergeOutput{Flags: piface.MergeComplete}
} }
@ -422,6 +457,9 @@ type aberrantMessageType struct {
} }
func (mt aberrantMessageType) New() pref.Message { func (mt aberrantMessageType) New() pref.Message {
if mt.t.Kind() == reflect.Ptr {
return aberrantMessage{reflect.New(mt.t.Elem())}
}
return aberrantMessage{reflect.Zero(mt.t)} return aberrantMessage{reflect.Zero(mt.t)}
} }
func (mt aberrantMessageType) Zero() pref.Message { func (mt aberrantMessageType) Zero() pref.Message {
@ -443,6 +481,17 @@ type aberrantMessage struct {
v reflect.Value v reflect.Value
} }
// Reset implements the v1 proto.Message.Reset method.
func (m aberrantMessage) Reset() {
if mr, ok := m.v.Interface().(interface{ Reset() }); ok {
mr.Reset()
return
}
if m.v.Kind() == reflect.Ptr && !m.v.IsNil() {
m.v.Elem().Set(reflect.Zero(m.v.Type().Elem()))
}
}
func (m aberrantMessage) ProtoReflect() pref.Message { func (m aberrantMessage) ProtoReflect() pref.Message {
return m return m
} }
@ -454,33 +503,40 @@ func (m aberrantMessage) Type() pref.MessageType {
return aberrantMessageType{m.v.Type()} return aberrantMessageType{m.v.Type()}
} }
func (m aberrantMessage) New() pref.Message { func (m aberrantMessage) New() pref.Message {
if m.v.Type().Kind() == reflect.Ptr {
return aberrantMessage{reflect.New(m.v.Type().Elem())}
}
return aberrantMessage{reflect.Zero(m.v.Type())} return aberrantMessage{reflect.Zero(m.v.Type())}
} }
func (m aberrantMessage) Interface() pref.ProtoMessage { func (m aberrantMessage) Interface() pref.ProtoMessage {
return m return m
} }
func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) { func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
return
} }
func (m aberrantMessage) Has(pref.FieldDescriptor) bool { func (m aberrantMessage) Has(pref.FieldDescriptor) bool {
panic("invalid field descriptor") return false
} }
func (m aberrantMessage) Clear(pref.FieldDescriptor) { func (m aberrantMessage) Clear(pref.FieldDescriptor) {
panic("invalid field descriptor") panic("invalid Message.Clear on " + string(m.Descriptor().FullName()))
} }
func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value { func (m aberrantMessage) Get(fd pref.FieldDescriptor) pref.Value {
panic("invalid field descriptor") if fd.Default().IsValid() {
return fd.Default()
}
panic("invalid Message.Get on " + string(m.Descriptor().FullName()))
} }
func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) { func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) {
panic("invalid field descriptor") panic("invalid Message.Set on " + string(m.Descriptor().FullName()))
} }
func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value { func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value {
panic("invalid field descriptor") panic("invalid Message.Mutable on " + string(m.Descriptor().FullName()))
} }
func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value { func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value {
panic("invalid field descriptor") panic("invalid Message.NewField on " + string(m.Descriptor().FullName()))
} }
func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor { func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
panic("invalid oneof descriptor") panic("invalid Message.WhichOneof descriptor on " + string(m.Descriptor().FullName()))
} }
func (m aberrantMessage) GetUnknown() pref.RawFields { func (m aberrantMessage) GetUnknown() pref.RawFields {
return nil return nil
@ -489,13 +545,13 @@ func (m aberrantMessage) SetUnknown(pref.RawFields) {
// SetUnknown discards its input on messages which don't support unknown field storage. // SetUnknown discards its input on messages which don't support unknown field storage.
} }
func (m aberrantMessage) IsValid() bool { func (m aberrantMessage) IsValid() bool {
// An invalid message is a read-only, empty message. Since we don't know anything if m.v.Kind() == reflect.Ptr {
// about the alleged contents of this message, we can't say with confidence that return !m.v.IsNil()
// it is invalid in this sense. Therefore, report it as valid. }
return true return false
} }
func (m aberrantMessage) ProtoMethods() *piface.Methods { func (m aberrantMessage) ProtoMethods() *piface.Methods {
return legacyProtoMethods return aberrantProtoMethods
} }
func (m aberrantMessage) protoUnwrap() interface{} { func (m aberrantMessage) protoUnwrap() interface{} {
return m.v.Interface() return m.v.Interface()

View File

@ -77,9 +77,9 @@ func (mi *MessageInfo) mergePointer(dst, src pointer, opts mergeOptions) {
} }
} }
if mi.unknownOffset.IsValid() { if mi.unknownOffset.IsValid() {
du := dst.Apply(mi.unknownOffset).Bytes() su := mi.getUnknownBytes(src)
su := src.Apply(mi.unknownOffset).Bytes() if su != nil && len(*su) > 0 {
if len(*su) > 0 { du := mi.mutableUnknownBytes(dst)
*du = append(*du, *su...) *du = append(*du, *su...)
} }
} }

View File

@ -12,9 +12,10 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"google.golang.org/protobuf/internal/genname" "google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry"
) )
// MessageInfo provides protobuf related functionality for a given Go type // MessageInfo provides protobuf related functionality for a given Go type
@ -109,22 +110,29 @@ func (mi *MessageInfo) getPointer(m pref.Message) (p pointer, ok bool) {
type ( type (
SizeCache = int32 SizeCache = int32
WeakFields = map[int32]protoreflect.ProtoMessage WeakFields = map[int32]protoreflect.ProtoMessage
UnknownFields = []byte UnknownFields = unknownFieldsA // TODO: switch to unknownFieldsB
unknownFieldsA = []byte
unknownFieldsB = *[]byte
ExtensionFields = map[int32]ExtensionField ExtensionFields = map[int32]ExtensionField
) )
var ( var (
sizecacheType = reflect.TypeOf(SizeCache(0)) sizecacheType = reflect.TypeOf(SizeCache(0))
weakFieldsType = reflect.TypeOf(WeakFields(nil)) weakFieldsType = reflect.TypeOf(WeakFields(nil))
unknownFieldsType = reflect.TypeOf(UnknownFields(nil)) unknownFieldsAType = reflect.TypeOf(unknownFieldsA(nil))
unknownFieldsBType = reflect.TypeOf(unknownFieldsB(nil))
extensionFieldsType = reflect.TypeOf(ExtensionFields(nil)) extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
) )
type structInfo struct { type structInfo struct {
sizecacheOffset offset sizecacheOffset offset
sizecacheType reflect.Type
weakOffset offset weakOffset offset
weakType reflect.Type
unknownOffset offset unknownOffset offset
unknownType reflect.Type
extensionOffset offset extensionOffset offset
extensionType reflect.Type
fieldsByNumber map[pref.FieldNumber]reflect.StructField fieldsByNumber map[pref.FieldNumber]reflect.StructField
oneofsByName map[pref.Name]reflect.StructField oneofsByName map[pref.Name]reflect.StructField
@ -148,21 +156,25 @@ func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
fieldLoop: fieldLoop:
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
switch f := t.Field(i); f.Name { switch f := t.Field(i); f.Name {
case genname.SizeCache, genname.SizeCacheA: case genid.SizeCache_goname, genid.SizeCacheA_goname:
if f.Type == sizecacheType { if f.Type == sizecacheType {
si.sizecacheOffset = offsetOf(f, mi.Exporter) si.sizecacheOffset = offsetOf(f, mi.Exporter)
si.sizecacheType = f.Type
} }
case genname.WeakFields, genname.WeakFieldsA: case genid.WeakFields_goname, genid.WeakFieldsA_goname:
if f.Type == weakFieldsType { if f.Type == weakFieldsType {
si.weakOffset = offsetOf(f, mi.Exporter) si.weakOffset = offsetOf(f, mi.Exporter)
si.weakType = f.Type
} }
case genname.UnknownFields, genname.UnknownFieldsA: case genid.UnknownFields_goname, genid.UnknownFieldsA_goname:
if f.Type == unknownFieldsType { if f.Type == unknownFieldsAType || f.Type == unknownFieldsBType {
si.unknownOffset = offsetOf(f, mi.Exporter) si.unknownOffset = offsetOf(f, mi.Exporter)
si.unknownType = f.Type
} }
case genname.ExtensionFields, genname.ExtensionFieldsA, genname.ExtensionFieldsB: case genid.ExtensionFields_goname, genid.ExtensionFieldsA_goname, genid.ExtensionFieldsB_goname:
if f.Type == extensionFieldsType { if f.Type == extensionFieldsType {
si.extensionOffset = offsetOf(f, mi.Exporter) si.extensionOffset = offsetOf(f, mi.Exporter)
si.extensionType = f.Type
} }
default: default:
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") { for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
@ -212,4 +224,53 @@ func (mi *MessageInfo) New() protoreflect.Message {
func (mi *MessageInfo) Zero() protoreflect.Message { func (mi *MessageInfo) Zero() protoreflect.Message {
return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface()) return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface())
} }
func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor { return mi.Desc } func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor {
return mi.Desc
}
func (mi *MessageInfo) Enum(i int) protoreflect.EnumType {
mi.init()
fd := mi.Desc.Fields().Get(i)
return Export{}.EnumTypeOf(mi.fieldTypes[fd.Number()])
}
func (mi *MessageInfo) Message(i int) protoreflect.MessageType {
mi.init()
fd := mi.Desc.Fields().Get(i)
switch {
case fd.IsWeak():
mt, _ := preg.GlobalTypes.FindMessageByName(fd.Message().FullName())
return mt
case fd.IsMap():
return mapEntryType{fd.Message(), mi.fieldTypes[fd.Number()]}
default:
return Export{}.MessageTypeOf(mi.fieldTypes[fd.Number()])
}
}
type mapEntryType struct {
desc protoreflect.MessageDescriptor
valType interface{} // zero value of enum or message type
}
func (mt mapEntryType) New() protoreflect.Message {
return nil
}
func (mt mapEntryType) Zero() protoreflect.Message {
return nil
}
func (mt mapEntryType) Descriptor() protoreflect.MessageDescriptor {
return mt.desc
}
func (mt mapEntryType) Enum(i int) protoreflect.EnumType {
fd := mt.desc.Fields().Get(i)
if fd.Enum() == nil {
return nil
}
return Export{}.EnumTypeOf(mt.valType)
}
func (mt mapEntryType) Message(i int) protoreflect.MessageType {
fd := mt.desc.Fields().Get(i)
if fd.Message() == nil {
return nil
}
return Export{}.MessageTypeOf(mt.valType)
}

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/internal/pragma" "google.golang.org/protobuf/internal/pragma"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
) )
@ -16,6 +17,11 @@ type reflectMessageInfo struct {
fields map[pref.FieldNumber]*fieldInfo fields map[pref.FieldNumber]*fieldInfo
oneofs map[pref.Name]*oneofInfo oneofs map[pref.Name]*oneofInfo
// fieldTypes contains the zero value of an enum or message field.
// For lists, it contains the element type.
// For maps, it contains the entry value type.
fieldTypes map[pref.FieldNumber]interface{}
// denseFields is a subset of fields where: // denseFields is a subset of fields where:
// 0 < fieldDesc.Number() < len(denseFields) // 0 < fieldDesc.Number() < len(denseFields)
// It provides faster access to the fieldInfo, but may be incomplete. // It provides faster access to the fieldInfo, but may be incomplete.
@ -36,6 +42,7 @@ func (mi *MessageInfo) makeReflectFuncs(t reflect.Type, si structInfo) {
mi.makeKnownFieldsFunc(si) mi.makeKnownFieldsFunc(si)
mi.makeUnknownFieldsFunc(t, si) mi.makeUnknownFieldsFunc(t, si)
mi.makeExtensionFieldsFunc(t, si) mi.makeExtensionFieldsFunc(t, si)
mi.makeFieldTypes(si)
} }
// makeKnownFieldsFunc generates functions for operations that can be performed // makeKnownFieldsFunc generates functions for operations that can be performed
@ -51,17 +58,23 @@ func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
for i := 0; i < fds.Len(); i++ { for i := 0; i < fds.Len(); i++ {
fd := fds.Get(i) fd := fds.Get(i)
fs := si.fieldsByNumber[fd.Number()] fs := si.fieldsByNumber[fd.Number()]
isOneof := fd.ContainingOneof() != nil && !fd.ContainingOneof().IsSynthetic()
if isOneof {
fs = si.oneofsByName[fd.ContainingOneof().Name()]
}
var fi fieldInfo var fi fieldInfo
switch { switch {
case fd.ContainingOneof() != nil && !fd.ContainingOneof().IsSynthetic(): case fs.Type == nil:
fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], mi.Exporter, si.oneofWrappersByNumber[fd.Number()]) fi = fieldInfoForMissing(fd) // never occurs for officially generated message types
case isOneof:
fi = fieldInfoForOneof(fd, fs, mi.Exporter, si.oneofWrappersByNumber[fd.Number()])
case fd.IsMap(): case fd.IsMap():
fi = fieldInfoForMap(fd, fs, mi.Exporter) fi = fieldInfoForMap(fd, fs, mi.Exporter)
case fd.IsList(): case fd.IsList():
fi = fieldInfoForList(fd, fs, mi.Exporter) fi = fieldInfoForList(fd, fs, mi.Exporter)
case fd.IsWeak(): case fd.IsWeak():
fi = fieldInfoForWeakMessage(fd, si.weakOffset) fi = fieldInfoForWeakMessage(fd, si.weakOffset)
case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind: case fd.Message() != nil:
fi = fieldInfoForMessage(fd, fs, mi.Exporter) fi = fieldInfoForMessage(fd, fs, mi.Exporter)
default: default:
fi = fieldInfoForScalar(fd, fs, mi.Exporter) fi = fieldInfoForScalar(fd, fs, mi.Exporter)
@ -92,27 +105,53 @@ func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
i++ i++
} }
} }
// Introduce instability to iteration order, but keep it deterministic.
if len(mi.rangeInfos) > 1 && detrand.Bool() {
i := detrand.Intn(len(mi.rangeInfos) - 1)
mi.rangeInfos[i], mi.rangeInfos[i+1] = mi.rangeInfos[i+1], mi.rangeInfos[i]
}
} }
func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) { func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
mi.getUnknown = func(pointer) pref.RawFields { return nil } switch {
mi.setUnknown = func(pointer, pref.RawFields) { return } case si.unknownOffset.IsValid() && si.unknownType == unknownFieldsAType:
if si.unknownOffset.IsValid() { // Handle as []byte.
mi.getUnknown = func(p pointer) pref.RawFields { mi.getUnknown = func(p pointer) pref.RawFields {
if p.IsNil() { if p.IsNil() {
return nil return nil
} }
rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType) return *p.Apply(mi.unknownOffset).Bytes()
return pref.RawFields(*rv.Interface().(*[]byte))
} }
mi.setUnknown = func(p pointer, b pref.RawFields) { mi.setUnknown = func(p pointer, b pref.RawFields) {
if p.IsNil() { if p.IsNil() {
panic("invalid SetUnknown on nil Message") panic("invalid SetUnknown on nil Message")
} }
rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType) *p.Apply(mi.unknownOffset).Bytes() = b
*rv.Interface().(*[]byte) = []byte(b)
} }
} else { case si.unknownOffset.IsValid() && si.unknownType == unknownFieldsBType:
// Handle as *[]byte.
mi.getUnknown = func(p pointer) pref.RawFields {
if p.IsNil() {
return nil
}
bp := p.Apply(mi.unknownOffset).BytesPtr()
if *bp == nil {
return nil
}
return **bp
}
mi.setUnknown = func(p pointer, b pref.RawFields) {
if p.IsNil() {
panic("invalid SetUnknown on nil Message")
}
bp := p.Apply(mi.unknownOffset).BytesPtr()
if *bp == nil {
*bp = new([]byte)
}
**bp = b
}
default:
mi.getUnknown = func(pointer) pref.RawFields { mi.getUnknown = func(pointer) pref.RawFields {
return nil return nil
} }
@ -139,6 +178,58 @@ func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
} }
} }
} }
func (mi *MessageInfo) makeFieldTypes(si structInfo) {
md := mi.Desc
fds := md.Fields()
for i := 0; i < fds.Len(); i++ {
var ft reflect.Type
fd := fds.Get(i)
fs := si.fieldsByNumber[fd.Number()]
isOneof := fd.ContainingOneof() != nil && !fd.ContainingOneof().IsSynthetic()
if isOneof {
fs = si.oneofsByName[fd.ContainingOneof().Name()]
}
var isMessage bool
switch {
case fs.Type == nil:
continue // never occurs for officially generated message types
case isOneof:
if fd.Enum() != nil || fd.Message() != nil {
ft = si.oneofWrappersByNumber[fd.Number()].Field(0).Type
}
case fd.IsMap():
if fd.MapValue().Enum() != nil || fd.MapValue().Message() != nil {
ft = fs.Type.Elem()
}
isMessage = fd.MapValue().Message() != nil
case fd.IsList():
if fd.Enum() != nil || fd.Message() != nil {
ft = fs.Type.Elem()
}
isMessage = fd.Message() != nil
case fd.Enum() != nil:
ft = fs.Type
if fd.HasPresence() && ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
case fd.Message() != nil:
ft = fs.Type
if fd.IsWeak() {
ft = nil
}
isMessage = true
}
if isMessage && ft != nil && ft.Kind() != reflect.Ptr {
ft = reflect.PtrTo(ft) // never occurs for officially generated message types
}
if ft != nil {
if mi.fieldTypes == nil {
mi.fieldTypes = make(map[pref.FieldNumber]interface{})
}
mi.fieldTypes[fd.Number()] = reflect.Zero(ft).Interface()
}
}
}
type extensionMap map[int32]ExtensionField type extensionMap map[int32]ExtensionField
@ -306,7 +397,6 @@ var (
// pointer to a named Go struct. If the provided type has a ProtoReflect method, // pointer to a named Go struct. If the provided type has a ProtoReflect method,
// it must be implemented by calling this method. // it must be implemented by calling this method.
func (mi *MessageInfo) MessageOf(m interface{}) pref.Message { func (mi *MessageInfo) MessageOf(m interface{}) pref.Message {
// TODO: Switch the input to be an opaque Pointer.
if reflect.TypeOf(m) != mi.GoReflectType { if reflect.TypeOf(m) != mi.GoReflectType {
panic(fmt.Sprintf("type mismatch: got %T, want %v", m, mi.GoReflectType)) panic(fmt.Sprintf("type mismatch: got %T, want %v", m, mi.GoReflectType))
} }
@ -320,6 +410,17 @@ func (mi *MessageInfo) MessageOf(m interface{}) pref.Message {
func (m *messageReflectWrapper) pointer() pointer { return m.p } func (m *messageReflectWrapper) pointer() pointer { return m.p }
func (m *messageReflectWrapper) messageInfo() *MessageInfo { return m.mi } func (m *messageReflectWrapper) messageInfo() *MessageInfo { return m.mi }
// Reset implements the v1 proto.Message.Reset method.
func (m *messageIfaceWrapper) Reset() {
if mr, ok := m.protoUnwrap().(interface{ Reset() }); ok {
mr.Reset()
return
}
rv := reflect.ValueOf(m.protoUnwrap())
if rv.Kind() == reflect.Ptr && !rv.IsNil() {
rv.Elem().Set(reflect.Zero(rv.Type().Elem()))
}
}
func (m *messageIfaceWrapper) ProtoReflect() pref.Message { func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
return (*messageReflectWrapper)(m) return (*messageReflectWrapper)(m)
} }

View File

@ -28,6 +28,39 @@ type fieldInfo struct {
newField func() pref.Value newField func() pref.Value
} }
func fieldInfoForMissing(fd pref.FieldDescriptor) fieldInfo {
// This never occurs for generated message types.
// It implies that a hand-crafted type has missing Go fields
// for specific protobuf message fields.
return fieldInfo{
fieldDesc: fd,
has: func(p pointer) bool {
return false
},
clear: func(p pointer) {
panic("missing Go struct field for " + string(fd.FullName()))
},
get: func(p pointer) pref.Value {
return fd.Default()
},
set: func(p pointer, v pref.Value) {
panic("missing Go struct field for " + string(fd.FullName()))
},
mutable: func(p pointer) pref.Value {
panic("missing Go struct field for " + string(fd.FullName()))
},
newMessage: func() pref.Message {
panic("missing Go struct field for " + string(fd.FullName()))
},
newField: func() pref.Value {
if v := fd.Default(); v.IsValid() {
return v
}
panic("missing Go struct field for " + string(fd.FullName()))
},
}
}
func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, x exporter, ot reflect.Type) fieldInfo { func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, x exporter, ot reflect.Type) fieldInfo {
ft := fs.Type ft := fs.Type
if ft.Kind() != reflect.Interface { if ft.Kind() != reflect.Interface {
@ -97,7 +130,7 @@ func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, x export
rv.Set(reflect.New(ot)) rv.Set(reflect.New(ot))
} }
rv = rv.Elem().Elem().Field(0) rv = rv.Elem().Elem().Field(0)
if rv.IsNil() { if rv.Kind() == reflect.Ptr && rv.IsNil() {
rv.Set(conv.GoValueOf(pref.ValueOfMessage(conv.New().Message()))) rv.Set(conv.GoValueOf(pref.ValueOfMessage(conv.New().Message())))
} }
return conv.PBValueOf(rv) return conv.PBValueOf(rv)
@ -225,7 +258,10 @@ func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField, x expor
isBytes := ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 isBytes := ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8
if nullable { if nullable {
if ft.Kind() != reflect.Ptr && ft.Kind() != reflect.Slice { if ft.Kind() != reflect.Ptr && ft.Kind() != reflect.Slice {
panic(fmt.Sprintf("field %v has invalid type: got %v, want pointer", fd.FullName(), ft)) // This never occurs for generated message types.
// Despite the protobuf type system specifying presence,
// the Go field type cannot represent it.
nullable = false
} }
if ft.Kind() == reflect.Ptr { if ft.Kind() == reflect.Ptr {
ft = ft.Elem() ft = ft.Elem()
@ -388,6 +424,9 @@ func fieldInfoForMessage(fd pref.FieldDescriptor, fs reflect.StructField, x expo
return false return false
} }
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem() rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if fs.Type.Kind() != reflect.Ptr {
return !isZero(rv)
}
return !rv.IsNil() return !rv.IsNil()
}, },
clear: func(p pointer) { clear: func(p pointer) {
@ -404,13 +443,13 @@ func fieldInfoForMessage(fd pref.FieldDescriptor, fs reflect.StructField, x expo
set: func(p pointer, v pref.Value) { set: func(p pointer, v pref.Value) {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem() rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
rv.Set(conv.GoValueOf(v)) rv.Set(conv.GoValueOf(v))
if rv.IsNil() { if fs.Type.Kind() == reflect.Ptr && rv.IsNil() {
panic(fmt.Sprintf("field %v has invalid nil pointer", fd.FullName())) panic(fmt.Sprintf("field %v has invalid nil pointer", fd.FullName()))
} }
}, },
mutable: func(p pointer) pref.Value { mutable: func(p pointer) pref.Value {
rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem() rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
if rv.IsNil() { if fs.Type.Kind() == reflect.Ptr && rv.IsNil() {
rv.Set(conv.GoValueOf(conv.New())) rv.Set(conv.GoValueOf(conv.New()))
} }
return conv.PBValueOf(rv) return conv.PBValueOf(rv)
@ -464,3 +503,41 @@ func makeOneofInfo(od pref.OneofDescriptor, si structInfo, x exporter) *oneofInf
} }
return oi return oi
} }
// isZero is identical to reflect.Value.IsZero.
// TODO: Remove this when Go1.13 is the minimally supported Go version.
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return math.Float64bits(v.Float()) == 0
case reflect.Complex64, reflect.Complex128:
c := v.Complex()
return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
case reflect.Array:
for i := 0; i < v.Len(); i++ {
if !isZero(v.Index(i)) {
return false
}
}
return true
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return v.IsNil()
case reflect.String:
return v.Len() == 0
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if !isZero(v.Field(i)) {
return false
}
}
return true
default:
panic(&reflect.ValueError{"reflect.Value.IsZero", v.Kind()})
}
}

View File

@ -121,6 +121,7 @@ func (p pointer) String() *string { return p.v.Interface().(*string) }
func (p pointer) StringPtr() **string { return p.v.Interface().(**string) } func (p pointer) StringPtr() **string { return p.v.Interface().(**string) }
func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) } func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) }
func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) } func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) }
func (p pointer) BytesPtr() **[]byte { return p.v.Interface().(**[]byte) }
func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) } func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) }
func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.v.Interface().(*WeakFields)) } func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.v.Interface().(*WeakFields)) }
func (p pointer) Extensions() *map[int32]ExtensionField { func (p pointer) Extensions() *map[int32]ExtensionField {

View File

@ -109,6 +109,7 @@ func (p pointer) String() *string { return (*string)(p.p)
func (p pointer) StringPtr() **string { return (**string)(p.p) } func (p pointer) StringPtr() **string { return (**string)(p.p) }
func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) } func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) }
func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) } func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) }
func (p pointer) BytesPtr() **[]byte { return (**[]byte)(p.p) }
func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) } func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) }
func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.p) } func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.p) }
func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) } func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) }

View File

@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/strs" "google.golang.org/protobuf/internal/strs"
pref "google.golang.org/protobuf/reflect/protoreflect" pref "google.golang.org/protobuf/reflect/protoreflect"
preg "google.golang.org/protobuf/reflect/protoregistry" preg "google.golang.org/protobuf/reflect/protoregistry"
@ -282,9 +283,9 @@ State:
switch { switch {
case st.typ == validationTypeMap: case st.typ == validationTypeMap:
switch num { switch num {
case 1: case genid.MapEntry_Key_field_number:
vi.typ = st.keyType vi.typ = st.keyType
case 2: case genid.MapEntry_Value_field_number:
vi.typ = st.valType vi.typ = st.valType
vi.mi = st.mi vi.mi = st.mi
vi.requiredBit = 1 vi.requiredBit = 1

View File

@ -1,43 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package mapsort provides sorted access to maps.
package mapsort
import (
"sort"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Range iterates over every map entry in sorted key order,
// calling f for each key and value encountered.
func Range(mapv protoreflect.Map, keyKind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
var keys []protoreflect.MapKey
mapv.Range(func(key protoreflect.MapKey, _ protoreflect.Value) bool {
keys = append(keys, key)
return true
})
sort.Slice(keys, func(i, j int) bool {
switch keyKind {
case protoreflect.BoolKind:
return !keys[i].Bool() && keys[j].Bool()
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind,
protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return keys[i].Int() < keys[j].Int()
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind,
protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return keys[i].Uint() < keys[j].Uint()
case protoreflect.StringKind:
return keys[i].String() < keys[j].String()
default:
panic("invalid kind: " + keyKind.String())
}
})
for _, key := range keys {
if !f(key, mapv.Get(key)) {
break
}
}
}

View File

@ -0,0 +1,89 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package order
import (
pref "google.golang.org/protobuf/reflect/protoreflect"
)
// FieldOrder specifies the ordering to visit message fields.
// It is a function that reports whether x is ordered before y.
type FieldOrder func(x, y pref.FieldDescriptor) bool
var (
// AnyFieldOrder specifies no specific field ordering.
AnyFieldOrder FieldOrder = nil
// LegacyFieldOrder sorts fields in the same ordering as emitted by
// wire serialization in the github.com/golang/protobuf implementation.
LegacyFieldOrder FieldOrder = func(x, y pref.FieldDescriptor) bool {
ox, oy := x.ContainingOneof(), y.ContainingOneof()
inOneof := func(od pref.OneofDescriptor) bool {
return od != nil && !od.IsSynthetic()
}
// Extension fields sort before non-extension fields.
if x.IsExtension() != y.IsExtension() {
return x.IsExtension() && !y.IsExtension()
}
// Fields not within a oneof sort before those within a oneof.
if inOneof(ox) != inOneof(oy) {
return !inOneof(ox) && inOneof(oy)
}
// Fields in disjoint oneof sets are sorted by declaration index.
if ox != nil && oy != nil && ox != oy {
return ox.Index() < oy.Index()
}
// Fields sorted by field number.
return x.Number() < y.Number()
}
// NumberFieldOrder sorts fields by their field number.
NumberFieldOrder FieldOrder = func(x, y pref.FieldDescriptor) bool {
return x.Number() < y.Number()
}
// IndexNameFieldOrder sorts non-extension fields before extension fields.
// Non-extensions are sorted according to their declaration index.
// Extensions are sorted according to their full name.
IndexNameFieldOrder FieldOrder = func(x, y pref.FieldDescriptor) bool {
// Non-extension fields sort before extension fields.
if x.IsExtension() != y.IsExtension() {
return !x.IsExtension() && y.IsExtension()
}
// Extensions sorted by fullname.
if x.IsExtension() && y.IsExtension() {
return x.FullName() < y.FullName()
}
// Non-extensions sorted by declaration index.
return x.Index() < y.Index()
}
)
// KeyOrder specifies the ordering to visit map entries.
// It is a function that reports whether x is ordered before y.
type KeyOrder func(x, y pref.MapKey) bool
var (
// AnyKeyOrder specifies no specific key ordering.
AnyKeyOrder KeyOrder = nil
// GenericKeyOrder sorts false before true, numeric keys in ascending order,
// and strings in lexicographical ordering according to UTF-8 codepoints.
GenericKeyOrder KeyOrder = func(x, y pref.MapKey) bool {
switch x.Interface().(type) {
case bool:
return !x.Bool() && y.Bool()
case int32, int64:
return x.Int() < y.Int()
case uint32, uint64:
return x.Uint() < y.Uint()
case string:
return x.String() < y.String()
default:
panic("invalid map key type")
}
}
)

View File

@ -0,0 +1,115 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package order provides ordered access to messages and maps.
package order
import (
"sort"
"sync"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
type messageField struct {
fd pref.FieldDescriptor
v pref.Value
}
var messageFieldPool = sync.Pool{
New: func() interface{} { return new([]messageField) },
}
type (
// FieldRnger is an interface for visiting all fields in a message.
// The protoreflect.Message type implements this interface.
FieldRanger interface{ Range(VisitField) }
// VisitField is called everytime a message field is visited.
VisitField = func(pref.FieldDescriptor, pref.Value) bool
)
// RangeFields iterates over the fields of fs according to the specified order.
func RangeFields(fs FieldRanger, less FieldOrder, fn VisitField) {
if less == nil {
fs.Range(fn)
return
}
// Obtain a pre-allocated scratch buffer.
p := messageFieldPool.Get().(*[]messageField)
fields := (*p)[:0]
defer func() {
if cap(fields) < 1024 {
*p = fields
messageFieldPool.Put(p)
}
}()
// Collect all fields in the message and sort them.
fs.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
fields = append(fields, messageField{fd, v})
return true
})
sort.Slice(fields, func(i, j int) bool {
return less(fields[i].fd, fields[j].fd)
})
// Visit the fields in the specified ordering.
for _, f := range fields {
if !fn(f.fd, f.v) {
return
}
}
}
type mapEntry struct {
k pref.MapKey
v pref.Value
}
var mapEntryPool = sync.Pool{
New: func() interface{} { return new([]mapEntry) },
}
type (
// EntryRanger is an interface for visiting all fields in a message.
// The protoreflect.Map type implements this interface.
EntryRanger interface{ Range(VisitEntry) }
// VisitEntry is called everytime a map entry is visited.
VisitEntry = func(pref.MapKey, pref.Value) bool
)
// RangeEntries iterates over the entries of es according to the specified order.
func RangeEntries(es EntryRanger, less KeyOrder, fn VisitEntry) {
if less == nil {
es.Range(fn)
return
}
// Obtain a pre-allocated scratch buffer.
p := mapEntryPool.Get().(*[]mapEntry)
entries := (*p)[:0]
defer func() {
if cap(entries) < 1024 {
*p = entries
mapEntryPool.Put(p)
}
}()
// Collect all entries in the map and sort them.
es.Range(func(k pref.MapKey, v pref.Value) bool {
entries = append(entries, mapEntry{k, v})
return true
})
sort.Slice(entries, func(i, j int) bool {
return less(entries[i].k, entries[j].k)
})
// Visit the entries in the specified ordering.
for _, e := range entries {
if !fn(e.k, e.v) {
return
}
}
}

View File

@ -52,7 +52,7 @@ import (
// 10. Send out the CL for review and submit it. // 10. Send out the CL for review and submit it.
const ( const (
Major = 1 Major = 1
Minor = 23 Minor = 26
Patch = 0 Patch = 0
PreRelease = "" PreRelease = ""
) )

View File

@ -9,6 +9,7 @@ import (
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/pragma" "google.golang.org/protobuf/internal/pragma"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/reflect/protoregistry"
@ -44,12 +45,14 @@ type UnmarshalOptions struct {
} }
// Unmarshal parses the wire-format message in b and places the result in m. // Unmarshal parses the wire-format message in b and places the result in m.
// The provided message must be mutable (e.g., a non-nil pointer to a message).
func Unmarshal(b []byte, m Message) error { func Unmarshal(b []byte, m Message) error {
_, err := UnmarshalOptions{}.unmarshal(b, m.ProtoReflect()) _, err := UnmarshalOptions{}.unmarshal(b, m.ProtoReflect())
return err return err
} }
// Unmarshal parses the wire-format message in b and places the result in m. // Unmarshal parses the wire-format message in b and places the result in m.
// The provided message must be mutable (e.g., a non-nil pointer to a message).
func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error { func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
_, err := o.unmarshal(b, m.ProtoReflect()) _, err := o.unmarshal(b, m.ProtoReflect())
return err return err
@ -63,12 +66,15 @@ func (o UnmarshalOptions) UnmarshalState(in protoiface.UnmarshalInput) (protoifa
return o.unmarshal(in.Buf, in.Message) return o.unmarshal(in.Buf, in.Message)
} }
// unmarshal is a centralized function that all unmarshal operations go through.
// For profiling purposes, avoid changing the name of this function or
// introducing other code paths for unmarshal that do not go through this.
func (o UnmarshalOptions) unmarshal(b []byte, m protoreflect.Message) (out protoiface.UnmarshalOutput, err error) { func (o UnmarshalOptions) unmarshal(b []byte, m protoreflect.Message) (out protoiface.UnmarshalOutput, err error) {
if o.Resolver == nil { if o.Resolver == nil {
o.Resolver = protoregistry.GlobalTypes o.Resolver = protoregistry.GlobalTypes
} }
if !o.Merge { if !o.Merge {
Reset(m.Interface()) // TODO Reset(m.Interface())
} }
allowPartial := o.AllowPartial allowPartial := o.AllowPartial
o.Merge = true o.Merge = true
@ -105,17 +111,17 @@ func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) err
func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error { func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
md := m.Descriptor() md := m.Descriptor()
if messageset.IsMessageSet(md) { if messageset.IsMessageSet(md) {
return unmarshalMessageSet(b, m, o) return o.unmarshalMessageSet(b, m)
} }
fields := md.Fields() fields := md.Fields()
for len(b) > 0 { for len(b) > 0 {
// Parse the tag (field number and wire type). // Parse the tag (field number and wire type).
num, wtyp, tagLen := protowire.ConsumeTag(b) num, wtyp, tagLen := protowire.ConsumeTag(b)
if tagLen < 0 { if tagLen < 0 {
return protowire.ParseError(tagLen) return errDecode
} }
if num > protowire.MaxValidNumber { if num > protowire.MaxValidNumber {
return errors.New("invalid field number") return errDecode
} }
// Find the field descriptor for this field number. // Find the field descriptor for this field number.
@ -155,7 +161,7 @@ func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message)
} }
valLen = protowire.ConsumeFieldValue(num, wtyp, b[tagLen:]) valLen = protowire.ConsumeFieldValue(num, wtyp, b[tagLen:])
if valLen < 0 { if valLen < 0 {
return protowire.ParseError(valLen) return errDecode
} }
if !o.DiscardUnknown { if !o.DiscardUnknown {
m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...)) m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
@ -190,7 +196,7 @@ func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv proto
} }
b, n = protowire.ConsumeBytes(b) b, n = protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
var ( var (
keyField = fd.MapKey() keyField = fd.MapKey()
@ -209,21 +215,21 @@ func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv proto
for len(b) > 0 { for len(b) > 0 {
num, wtyp, n := protowire.ConsumeTag(b) num, wtyp, n := protowire.ConsumeTag(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
if num > protowire.MaxValidNumber { if num > protowire.MaxValidNumber {
return 0, errors.New("invalid field number") return 0, errDecode
} }
b = b[n:] b = b[n:]
err = errUnknown err = errUnknown
switch num { switch num {
case 1: case genid.MapEntry_Key_field_number:
key, n, err = o.unmarshalScalar(b, wtyp, keyField) key, n, err = o.unmarshalScalar(b, wtyp, keyField)
if err != nil { if err != nil {
break break
} }
haveKey = true haveKey = true
case 2: case genid.MapEntry_Value_field_number:
var v protoreflect.Value var v protoreflect.Value
v, n, err = o.unmarshalScalar(b, wtyp, valField) v, n, err = o.unmarshalScalar(b, wtyp, valField)
if err != nil { if err != nil {
@ -242,7 +248,7 @@ func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv proto
if err == errUnknown { if err == errUnknown {
n = protowire.ConsumeFieldValue(num, wtyp, b) n = protowire.ConsumeFieldValue(num, wtyp, b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
} else if err != nil { } else if err != nil {
return 0, err return 0, err
@ -268,3 +274,5 @@ func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv proto
// to the unknown field set of a message. It is never returned from an exported // to the unknown field set of a message. It is never returned from an exported
// function. // function.
var errUnknown = errors.New("BUG: internal error (unknown)") var errUnknown = errors.New("BUG: internal error (unknown)")
var errDecode = errors.New("cannot parse invalid wire-format data")

View File

@ -27,7 +27,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfBool(protowire.DecodeBool(v)), n, nil return protoreflect.ValueOfBool(protowire.DecodeBool(v)), n, nil
case protoreflect.EnumKind: case protoreflect.EnumKind:
@ -36,7 +36,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)), n, nil return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)), n, nil
case protoreflect.Int32Kind: case protoreflect.Int32Kind:
@ -45,7 +45,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfInt32(int32(v)), n, nil return protoreflect.ValueOfInt32(int32(v)), n, nil
case protoreflect.Sint32Kind: case protoreflect.Sint32Kind:
@ -54,7 +54,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))), n, nil return protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))), n, nil
case protoreflect.Uint32Kind: case protoreflect.Uint32Kind:
@ -63,7 +63,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfUint32(uint32(v)), n, nil return protoreflect.ValueOfUint32(uint32(v)), n, nil
case protoreflect.Int64Kind: case protoreflect.Int64Kind:
@ -72,7 +72,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfInt64(int64(v)), n, nil return protoreflect.ValueOfInt64(int64(v)), n, nil
case protoreflect.Sint64Kind: case protoreflect.Sint64Kind:
@ -81,7 +81,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)), n, nil return protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)), n, nil
case protoreflect.Uint64Kind: case protoreflect.Uint64Kind:
@ -90,7 +90,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfUint64(v), n, nil return protoreflect.ValueOfUint64(v), n, nil
case protoreflect.Sfixed32Kind: case protoreflect.Sfixed32Kind:
@ -99,7 +99,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeFixed32(b) v, n := protowire.ConsumeFixed32(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfInt32(int32(v)), n, nil return protoreflect.ValueOfInt32(int32(v)), n, nil
case protoreflect.Fixed32Kind: case protoreflect.Fixed32Kind:
@ -108,7 +108,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeFixed32(b) v, n := protowire.ConsumeFixed32(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfUint32(uint32(v)), n, nil return protoreflect.ValueOfUint32(uint32(v)), n, nil
case protoreflect.FloatKind: case protoreflect.FloatKind:
@ -117,7 +117,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeFixed32(b) v, n := protowire.ConsumeFixed32(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))), n, nil return protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))), n, nil
case protoreflect.Sfixed64Kind: case protoreflect.Sfixed64Kind:
@ -126,7 +126,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeFixed64(b) v, n := protowire.ConsumeFixed64(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfInt64(int64(v)), n, nil return protoreflect.ValueOfInt64(int64(v)), n, nil
case protoreflect.Fixed64Kind: case protoreflect.Fixed64Kind:
@ -135,7 +135,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeFixed64(b) v, n := protowire.ConsumeFixed64(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfUint64(v), n, nil return protoreflect.ValueOfUint64(v), n, nil
case protoreflect.DoubleKind: case protoreflect.DoubleKind:
@ -144,7 +144,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeFixed64(b) v, n := protowire.ConsumeFixed64(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfFloat64(math.Float64frombits(v)), n, nil return protoreflect.ValueOfFloat64(math.Float64frombits(v)), n, nil
case protoreflect.StringKind: case protoreflect.StringKind:
@ -153,7 +153,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
if strs.EnforceUTF8(fd) && !utf8.Valid(v) { if strs.EnforceUTF8(fd) && !utf8.Valid(v) {
return protoreflect.Value{}, 0, errors.InvalidUTF8(string(fd.FullName())) return protoreflect.Value{}, 0, errors.InvalidUTF8(string(fd.FullName()))
@ -165,7 +165,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfBytes(append(emptyBuf[:], v...)), n, nil return protoreflect.ValueOfBytes(append(emptyBuf[:], v...)), n, nil
case protoreflect.MessageKind: case protoreflect.MessageKind:
@ -174,7 +174,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfBytes(v), n, nil return protoreflect.ValueOfBytes(v), n, nil
case protoreflect.GroupKind: case protoreflect.GroupKind:
@ -183,7 +183,7 @@ func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd prot
} }
v, n := protowire.ConsumeGroup(fd.Number(), b) v, n := protowire.ConsumeGroup(fd.Number(), b)
if n < 0 { if n < 0 {
return val, 0, protowire.ParseError(n) return val, 0, errDecode
} }
return protoreflect.ValueOfBytes(v), n, nil return protoreflect.ValueOfBytes(v), n, nil
default: default:
@ -197,12 +197,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v))) list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v)))
@ -214,7 +214,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v))) list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v)))
return n, nil return n, nil
@ -222,12 +222,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v))) list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)))
@ -239,7 +239,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v))) list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)))
return n, nil return n, nil
@ -247,12 +247,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfInt32(int32(v))) list.Append(protoreflect.ValueOfInt32(int32(v)))
@ -264,7 +264,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfInt32(int32(v))) list.Append(protoreflect.ValueOfInt32(int32(v)))
return n, nil return n, nil
@ -272,12 +272,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32)))) list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))))
@ -289,7 +289,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32)))) list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))))
return n, nil return n, nil
@ -297,12 +297,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfUint32(uint32(v))) list.Append(protoreflect.ValueOfUint32(uint32(v)))
@ -314,7 +314,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfUint32(uint32(v))) list.Append(protoreflect.ValueOfUint32(uint32(v)))
return n, nil return n, nil
@ -322,12 +322,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfInt64(int64(v))) list.Append(protoreflect.ValueOfInt64(int64(v)))
@ -339,7 +339,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfInt64(int64(v))) list.Append(protoreflect.ValueOfInt64(int64(v)))
return n, nil return n, nil
@ -347,12 +347,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v))) list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)))
@ -364,7 +364,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v))) list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)))
return n, nil return n, nil
@ -372,12 +372,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeVarint(buf) v, n := protowire.ConsumeVarint(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfUint64(v)) list.Append(protoreflect.ValueOfUint64(v))
@ -389,7 +389,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeVarint(b) v, n := protowire.ConsumeVarint(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfUint64(v)) list.Append(protoreflect.ValueOfUint64(v))
return n, nil return n, nil
@ -397,12 +397,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeFixed32(buf) v, n := protowire.ConsumeFixed32(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfInt32(int32(v))) list.Append(protoreflect.ValueOfInt32(int32(v)))
@ -414,7 +414,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeFixed32(b) v, n := protowire.ConsumeFixed32(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfInt32(int32(v))) list.Append(protoreflect.ValueOfInt32(int32(v)))
return n, nil return n, nil
@ -422,12 +422,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeFixed32(buf) v, n := protowire.ConsumeFixed32(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfUint32(uint32(v))) list.Append(protoreflect.ValueOfUint32(uint32(v)))
@ -439,7 +439,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeFixed32(b) v, n := protowire.ConsumeFixed32(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfUint32(uint32(v))) list.Append(protoreflect.ValueOfUint32(uint32(v)))
return n, nil return n, nil
@ -447,12 +447,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeFixed32(buf) v, n := protowire.ConsumeFixed32(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v)))) list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))))
@ -464,7 +464,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeFixed32(b) v, n := protowire.ConsumeFixed32(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v)))) list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))))
return n, nil return n, nil
@ -472,12 +472,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeFixed64(buf) v, n := protowire.ConsumeFixed64(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfInt64(int64(v))) list.Append(protoreflect.ValueOfInt64(int64(v)))
@ -489,7 +489,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeFixed64(b) v, n := protowire.ConsumeFixed64(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfInt64(int64(v))) list.Append(protoreflect.ValueOfInt64(int64(v)))
return n, nil return n, nil
@ -497,12 +497,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeFixed64(buf) v, n := protowire.ConsumeFixed64(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfUint64(v)) list.Append(protoreflect.ValueOfUint64(v))
@ -514,7 +514,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeFixed64(b) v, n := protowire.ConsumeFixed64(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfUint64(v)) list.Append(protoreflect.ValueOfUint64(v))
return n, nil return n, nil
@ -522,12 +522,12 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
if wtyp == protowire.BytesType { if wtyp == protowire.BytesType {
buf, n := protowire.ConsumeBytes(b) buf, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
for len(buf) > 0 { for len(buf) > 0 {
v, n := protowire.ConsumeFixed64(buf) v, n := protowire.ConsumeFixed64(buf)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
buf = buf[n:] buf = buf[n:]
list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v))) list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v)))
@ -539,7 +539,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeFixed64(b) v, n := protowire.ConsumeFixed64(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v))) list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v)))
return n, nil return n, nil
@ -549,7 +549,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
if strs.EnforceUTF8(fd) && !utf8.Valid(v) { if strs.EnforceUTF8(fd) && !utf8.Valid(v) {
return 0, errors.InvalidUTF8(string(fd.FullName())) return 0, errors.InvalidUTF8(string(fd.FullName()))
@ -562,7 +562,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
list.Append(protoreflect.ValueOfBytes(append(emptyBuf[:], v...))) list.Append(protoreflect.ValueOfBytes(append(emptyBuf[:], v...)))
return n, nil return n, nil
@ -572,7 +572,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeBytes(b) v, n := protowire.ConsumeBytes(b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
m := list.NewElement() m := list.NewElement()
if err := o.unmarshalMessage(v, m.Message()); err != nil { if err := o.unmarshalMessage(v, m.Message()); err != nil {
@ -586,7 +586,7 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list prot
} }
v, n := protowire.ConsumeGroup(fd.Number(), b) v, n := protowire.ConsumeGroup(fd.Number(), b)
if n < 0 { if n < 0 {
return 0, protowire.ParseError(n) return 0, errDecode
} }
m := list.NewElement() m := list.NewElement()
if err := o.unmarshalMessage(v, m.Message()); err != nil { if err := o.unmarshalMessage(v, m.Message()); err != nil {

View File

@ -5,12 +5,9 @@
package proto package proto
import ( import (
"sort"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/fieldsort" "google.golang.org/protobuf/internal/order"
"google.golang.org/protobuf/internal/mapsort"
"google.golang.org/protobuf/internal/pragma" "google.golang.org/protobuf/internal/pragma"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoiface" "google.golang.org/protobuf/runtime/protoiface"
@ -134,6 +131,9 @@ func (o MarshalOptions) MarshalState(in protoiface.MarshalInput) (protoiface.Mar
return o.marshal(in.Buf, in.Message) return o.marshal(in.Buf, in.Message)
} }
// marshal is a centralized function that all marshal operations go through.
// For profiling purposes, avoid changing the name of this function or
// introducing other code paths for marshal that do not go through this.
func (o MarshalOptions) marshal(b []byte, m protoreflect.Message) (out protoiface.MarshalOutput, err error) { func (o MarshalOptions) marshal(b []byte, m protoreflect.Message) (out protoiface.MarshalOutput, err error) {
allowPartial := o.AllowPartial allowPartial := o.AllowPartial
o.AllowPartial = true o.AllowPartial = true
@ -206,16 +206,17 @@ func growcap(oldcap, wantcap int) (newcap int) {
func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) { func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
if messageset.IsMessageSet(m.Descriptor()) { if messageset.IsMessageSet(m.Descriptor()) {
return marshalMessageSet(b, m, o) return o.marshalMessageSet(b, m)
}
fieldOrder := order.AnyFieldOrder
if o.Deterministic {
// TODO: This should use a more natural ordering like NumberFieldOrder,
// but doing so breaks golden tests that make invalid assumption about
// output stability of this implementation.
fieldOrder = order.LegacyFieldOrder
} }
// There are many choices for what order we visit fields in. The default one here
// is chosen for reasonable efficiency and simplicity given the protoreflect API.
// It is not deterministic, since Message.Range does not return fields in any
// defined order.
//
// When using deterministic serialization, we sort the known fields.
var err error var err error
o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
b, err = o.marshalField(b, fd, v) b, err = o.marshalField(b, fd, v)
return err == nil return err == nil
}) })
@ -226,27 +227,6 @@ func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]
return b, nil return b, nil
} }
// rangeFields visits fields in a defined order when deterministic serialization is enabled.
func (o MarshalOptions) rangeFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
if !o.Deterministic {
m.Range(f)
return
}
var fds []protoreflect.FieldDescriptor
m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
fds = append(fds, fd)
return true
})
sort.Slice(fds, func(a, b int) bool {
return fieldsort.Less(fds[a], fds[b])
})
for _, fd := range fds {
if !f(fd, m.Get(fd)) {
break
}
}
}
func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) { func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
switch { switch {
case fd.IsList(): case fd.IsList():
@ -289,8 +269,12 @@ func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, l
func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) { func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
keyf := fd.MapKey() keyf := fd.MapKey()
valf := fd.MapValue() valf := fd.MapValue()
keyOrder := order.AnyKeyOrder
if o.Deterministic {
keyOrder = order.GenericKeyOrder
}
var err error var err error
o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool { order.RangeEntries(mapv, keyOrder, func(key protoreflect.MapKey, value protoreflect.Value) bool {
b = protowire.AppendTag(b, fd.Number(), protowire.BytesType) b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
var pos int var pos int
b, pos = appendSpeculativeLength(b) b, pos = appendSpeculativeLength(b)
@ -309,14 +293,6 @@ func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, ma
return b, err return b, err
} }
func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
if !o.Deterministic {
mapv.Range(f)
return
}
mapsort.Range(mapv, kind, f)
}
// When encoding length-prefixed fields, we speculatively set aside some number of bytes // When encoding length-prefixed fields, we speculatively set aside some number of bytes
// for the length, encode the data, and then encode the length (shifting the data if necessary // for the length, encode the data, and then encode the length (shifting the data if necessary
// to make room). // to make room).

View File

@ -111,18 +111,31 @@ func equalList(fd pref.FieldDescriptor, x, y pref.List) bool {
// equalValue compares two singular values. // equalValue compares two singular values.
func equalValue(fd pref.FieldDescriptor, x, y pref.Value) bool { func equalValue(fd pref.FieldDescriptor, x, y pref.Value) bool {
switch { switch fd.Kind() {
case fd.Message() != nil: case pref.BoolKind:
return equalMessage(x.Message(), y.Message()) return x.Bool() == y.Bool()
case fd.Kind() == pref.BytesKind: case pref.EnumKind:
return bytes.Equal(x.Bytes(), y.Bytes()) return x.Enum() == y.Enum()
case fd.Kind() == pref.FloatKind, fd.Kind() == pref.DoubleKind: case pref.Int32Kind, pref.Sint32Kind,
pref.Int64Kind, pref.Sint64Kind,
pref.Sfixed32Kind, pref.Sfixed64Kind:
return x.Int() == y.Int()
case pref.Uint32Kind, pref.Uint64Kind,
pref.Fixed32Kind, pref.Fixed64Kind:
return x.Uint() == y.Uint()
case pref.FloatKind, pref.DoubleKind:
fx := x.Float() fx := x.Float()
fy := y.Float() fy := y.Float()
if math.IsNaN(fx) || math.IsNaN(fy) { if math.IsNaN(fx) || math.IsNaN(fy) {
return math.IsNaN(fx) && math.IsNaN(fy) return math.IsNaN(fx) && math.IsNaN(fy)
} }
return fx == fy return fx == fy
case pref.StringKind:
return x.String() == y.String()
case pref.BytesKind:
return bytes.Equal(x.Bytes(), y.Bytes())
case pref.MessageKind, pref.GroupKind:
return equalMessage(x.Message(), y.Message())
default: default:
return x.Interface() == y.Interface() return x.Interface() == y.Interface()
} }

View File

@ -9,28 +9,33 @@ import (
"google.golang.org/protobuf/internal/encoding/messageset" "google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/flags" "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/order"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry" "google.golang.org/protobuf/reflect/protoregistry"
) )
func sizeMessageSet(m protoreflect.Message) (size int) { func (o MarshalOptions) sizeMessageSet(m protoreflect.Message) (size int) {
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
size += messageset.SizeField(fd.Number()) size += messageset.SizeField(fd.Number())
size += protowire.SizeTag(messageset.FieldMessage) size += protowire.SizeTag(messageset.FieldMessage)
size += protowire.SizeBytes(sizeMessage(v.Message())) size += protowire.SizeBytes(o.size(v.Message()))
return true return true
}) })
size += messageset.SizeUnknown(m.GetUnknown()) size += messageset.SizeUnknown(m.GetUnknown())
return size return size
} }
func marshalMessageSet(b []byte, m protoreflect.Message, o MarshalOptions) ([]byte, error) { func (o MarshalOptions) marshalMessageSet(b []byte, m protoreflect.Message) ([]byte, error) {
if !flags.ProtoLegacy { if !flags.ProtoLegacy {
return b, errors.New("no support for message_set_wire_format") return b, errors.New("no support for message_set_wire_format")
} }
fieldOrder := order.AnyFieldOrder
if o.Deterministic {
fieldOrder = order.NumberFieldOrder
}
var err error var err error
o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
b, err = marshalMessageSetField(b, fd, v, o) b, err = o.marshalMessageSetField(b, fd, v)
return err == nil return err == nil
}) })
if err != nil { if err != nil {
@ -39,7 +44,7 @@ func marshalMessageSet(b []byte, m protoreflect.Message, o MarshalOptions) ([]by
return messageset.AppendUnknown(b, m.GetUnknown()) return messageset.AppendUnknown(b, m.GetUnknown())
} }
func marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value, o MarshalOptions) ([]byte, error) { func (o MarshalOptions) marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
b = messageset.AppendFieldStart(b, fd.Number()) b = messageset.AppendFieldStart(b, fd.Number())
b = protowire.AppendTag(b, messageset.FieldMessage, protowire.BytesType) b = protowire.AppendTag(b, messageset.FieldMessage, protowire.BytesType)
b = protowire.AppendVarint(b, uint64(o.Size(value.Message().Interface()))) b = protowire.AppendVarint(b, uint64(o.Size(value.Message().Interface())))
@ -51,12 +56,12 @@ func marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value pro
return b, nil return b, nil
} }
func unmarshalMessageSet(b []byte, m protoreflect.Message, o UnmarshalOptions) error { func (o UnmarshalOptions) unmarshalMessageSet(b []byte, m protoreflect.Message) error {
if !flags.ProtoLegacy { if !flags.ProtoLegacy {
return errors.New("no support for message_set_wire_format") return errors.New("no support for message_set_wire_format")
} }
return messageset.Unmarshal(b, false, func(num protowire.Number, v []byte) error { return messageset.Unmarshal(b, false, func(num protowire.Number, v []byte) error {
err := unmarshalMessageSetField(m, num, v, o) err := o.unmarshalMessageSetField(m, num, v)
if err == errUnknown { if err == errUnknown {
unknown := m.GetUnknown() unknown := m.GetUnknown()
unknown = protowire.AppendTag(unknown, num, protowire.BytesType) unknown = protowire.AppendTag(unknown, num, protowire.BytesType)
@ -68,7 +73,7 @@ func unmarshalMessageSet(b []byte, m protoreflect.Message, o UnmarshalOptions) e
}) })
} }
func unmarshalMessageSetField(m protoreflect.Message, num protowire.Number, v []byte, o UnmarshalOptions) error { func (o UnmarshalOptions) unmarshalMessageSetField(m protoreflect.Message, num protowire.Number, v []byte) error {
md := m.Descriptor() md := m.Descriptor()
if !md.ExtensionRanges().Has(num) { if !md.ExtensionRanges().Has(num) {
return errUnknown return errUnknown

View File

@ -32,3 +32,12 @@ var Error error
func init() { func init() {
Error = errors.Error Error = errors.Error
} }
// MessageName returns the full name of m.
// If m is nil, it returns an empty string.
func MessageName(m Message) protoreflect.FullName {
if m == nil {
return ""
}
return m.ProtoReflect().Descriptor().FullName()
}

View File

@ -23,10 +23,13 @@ func (o MarshalOptions) Size(m Message) int {
return 0 return 0
} }
return sizeMessage(m.ProtoReflect()) return o.size(m.ProtoReflect())
} }
func sizeMessage(m protoreflect.Message) (size int) { // size is a centralized function that all size operations go through.
// For profiling purposes, avoid changing the name of this function or
// introducing other code paths for size that do not go through this.
func (o MarshalOptions) size(m protoreflect.Message) (size int) {
methods := protoMethods(m) methods := protoMethods(m)
if methods != nil && methods.Size != nil { if methods != nil && methods.Size != nil {
out := methods.Size(protoiface.SizeInput{ out := methods.Size(protoiface.SizeInput{
@ -42,52 +45,52 @@ func sizeMessage(m protoreflect.Message) (size int) {
}) })
return len(out.Buf) return len(out.Buf)
} }
return sizeMessageSlow(m) return o.sizeMessageSlow(m)
} }
func sizeMessageSlow(m protoreflect.Message) (size int) { func (o MarshalOptions) sizeMessageSlow(m protoreflect.Message) (size int) {
if messageset.IsMessageSet(m.Descriptor()) { if messageset.IsMessageSet(m.Descriptor()) {
return sizeMessageSet(m) return o.sizeMessageSet(m)
} }
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
size += sizeField(fd, v) size += o.sizeField(fd, v)
return true return true
}) })
size += len(m.GetUnknown()) size += len(m.GetUnknown())
return size return size
} }
func sizeField(fd protoreflect.FieldDescriptor, value protoreflect.Value) (size int) { func (o MarshalOptions) sizeField(fd protoreflect.FieldDescriptor, value protoreflect.Value) (size int) {
num := fd.Number() num := fd.Number()
switch { switch {
case fd.IsList(): case fd.IsList():
return sizeList(num, fd, value.List()) return o.sizeList(num, fd, value.List())
case fd.IsMap(): case fd.IsMap():
return sizeMap(num, fd, value.Map()) return o.sizeMap(num, fd, value.Map())
default: default:
return protowire.SizeTag(num) + sizeSingular(num, fd.Kind(), value) return protowire.SizeTag(num) + o.sizeSingular(num, fd.Kind(), value)
} }
} }
func sizeList(num protowire.Number, fd protoreflect.FieldDescriptor, list protoreflect.List) (size int) { func (o MarshalOptions) sizeList(num protowire.Number, fd protoreflect.FieldDescriptor, list protoreflect.List) (size int) {
if fd.IsPacked() && list.Len() > 0 { if fd.IsPacked() && list.Len() > 0 {
content := 0 content := 0
for i, llen := 0, list.Len(); i < llen; i++ { for i, llen := 0, list.Len(); i < llen; i++ {
content += sizeSingular(num, fd.Kind(), list.Get(i)) content += o.sizeSingular(num, fd.Kind(), list.Get(i))
} }
return protowire.SizeTag(num) + protowire.SizeBytes(content) return protowire.SizeTag(num) + protowire.SizeBytes(content)
} }
for i, llen := 0, list.Len(); i < llen; i++ { for i, llen := 0, list.Len(); i < llen; i++ {
size += protowire.SizeTag(num) + sizeSingular(num, fd.Kind(), list.Get(i)) size += protowire.SizeTag(num) + o.sizeSingular(num, fd.Kind(), list.Get(i))
} }
return size return size
} }
func sizeMap(num protowire.Number, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) (size int) { func (o MarshalOptions) sizeMap(num protowire.Number, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) (size int) {
mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool { mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {
size += protowire.SizeTag(num) size += protowire.SizeTag(num)
size += protowire.SizeBytes(sizeField(fd.MapKey(), key.Value()) + sizeField(fd.MapValue(), value)) size += protowire.SizeBytes(o.sizeField(fd.MapKey(), key.Value()) + o.sizeField(fd.MapValue(), value))
return true return true
}) })
return size return size

View File

@ -11,7 +11,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
) )
func sizeSingular(num protowire.Number, kind protoreflect.Kind, v protoreflect.Value) int { func (o MarshalOptions) sizeSingular(num protowire.Number, kind protoreflect.Kind, v protoreflect.Value) int {
switch kind { switch kind {
case protoreflect.BoolKind: case protoreflect.BoolKind:
return protowire.SizeVarint(protowire.EncodeBool(v.Bool())) return protowire.SizeVarint(protowire.EncodeBool(v.Bool()))
@ -46,9 +46,9 @@ func sizeSingular(num protowire.Number, kind protoreflect.Kind, v protoreflect.V
case protoreflect.BytesKind: case protoreflect.BytesKind:
return protowire.SizeBytes(len(v.Bytes())) return protowire.SizeBytes(len(v.Bytes()))
case protoreflect.MessageKind: case protoreflect.MessageKind:
return protowire.SizeBytes(sizeMessage(v.Message())) return protowire.SizeBytes(o.size(v.Message()))
case protoreflect.GroupKind: case protoreflect.GroupKind:
return protowire.SizeGroup(num, sizeMessage(v.Message())) return protowire.SizeGroup(num, o.size(v.Message()))
default: default:
return 0 return 0
} }

View File

@ -0,0 +1,276 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package protodesc provides functionality for converting
// FileDescriptorProto messages to/from protoreflect.FileDescriptor values.
//
// The google.protobuf.FileDescriptorProto is a protobuf message that describes
// the type information for a .proto file in a form that is easily serializable.
// The protoreflect.FileDescriptor is a more structured representation of
// the FileDescriptorProto message where references and remote dependencies
// can be directly followed.
package protodesc
import (
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/internal/pragma"
"google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
)
// Resolver is the resolver used by NewFile to resolve dependencies.
// The enums and messages provided must belong to some parent file,
// which is also registered.
//
// It is implemented by protoregistry.Files.
type Resolver interface {
FindFileByPath(string) (protoreflect.FileDescriptor, error)
FindDescriptorByName(protoreflect.FullName) (protoreflect.Descriptor, error)
}
// FileOptions configures the construction of file descriptors.
type FileOptions struct {
pragma.NoUnkeyedLiterals
// AllowUnresolvable configures New to permissively allow unresolvable
// file, enum, or message dependencies. Unresolved dependencies are replaced
// by placeholder equivalents.
//
// The following dependencies may be left unresolved:
// • Resolving an imported file.
// • Resolving the type for a message field or extension field.
// If the kind of the field is unknown, then a placeholder is used for both
// the Enum and Message accessors on the protoreflect.FieldDescriptor.
// • Resolving an enum value set as the default for an optional enum field.
// If unresolvable, the protoreflect.FieldDescriptor.Default is set to the
// first value in the associated enum (or zero if the also enum dependency
// is also unresolvable). The protoreflect.FieldDescriptor.DefaultEnumValue
// is populated with a placeholder.
// • Resolving the extended message type for an extension field.
// • Resolving the input or output message type for a service method.
//
// If the unresolved dependency uses a relative name,
// then the placeholder will contain an invalid FullName with a "*." prefix,
// indicating that the starting prefix of the full name is unknown.
AllowUnresolvable bool
}
// NewFile creates a new protoreflect.FileDescriptor from the provided
// file descriptor message. See FileOptions.New for more information.
func NewFile(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
return FileOptions{}.New(fd, r)
}
// NewFiles creates a new protoregistry.Files from the provided
// FileDescriptorSet message. See FileOptions.NewFiles for more information.
func NewFiles(fd *descriptorpb.FileDescriptorSet) (*protoregistry.Files, error) {
return FileOptions{}.NewFiles(fd)
}
// New creates a new protoreflect.FileDescriptor from the provided
// file descriptor message. The file must represent a valid proto file according
// to protobuf semantics. The returned descriptor is a deep copy of the input.
//
// Any imported files, enum types, or message types referenced in the file are
// resolved using the provided registry. When looking up an import file path,
// the path must be unique. The newly created file descriptor is not registered
// back into the provided file registry.
func (o FileOptions) New(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
if r == nil {
r = (*protoregistry.Files)(nil) // empty resolver
}
// Handle the file descriptor content.
f := &filedesc.File{L2: &filedesc.FileL2{}}
switch fd.GetSyntax() {
case "proto2", "":
f.L1.Syntax = protoreflect.Proto2
case "proto3":
f.L1.Syntax = protoreflect.Proto3
default:
return nil, errors.New("invalid syntax: %q", fd.GetSyntax())
}
f.L1.Path = fd.GetName()
if f.L1.Path == "" {
return nil, errors.New("file path must be populated")
}
f.L1.Package = protoreflect.FullName(fd.GetPackage())
if !f.L1.Package.IsValid() && f.L1.Package != "" {
return nil, errors.New("invalid package: %q", f.L1.Package)
}
if opts := fd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.FileOptions)
f.L2.Options = func() protoreflect.ProtoMessage { return opts }
}
f.L2.Imports = make(filedesc.FileImports, len(fd.GetDependency()))
for _, i := range fd.GetPublicDependency() {
if !(0 <= i && int(i) < len(f.L2.Imports)) || f.L2.Imports[i].IsPublic {
return nil, errors.New("invalid or duplicate public import index: %d", i)
}
f.L2.Imports[i].IsPublic = true
}
for _, i := range fd.GetWeakDependency() {
if !(0 <= i && int(i) < len(f.L2.Imports)) || f.L2.Imports[i].IsWeak {
return nil, errors.New("invalid or duplicate weak import index: %d", i)
}
f.L2.Imports[i].IsWeak = true
}
imps := importSet{f.Path(): true}
for i, path := range fd.GetDependency() {
imp := &f.L2.Imports[i]
f, err := r.FindFileByPath(path)
if err == protoregistry.NotFound && (o.AllowUnresolvable || imp.IsWeak) {
f = filedesc.PlaceholderFile(path)
} else if err != nil {
return nil, errors.New("could not resolve import %q: %v", path, err)
}
imp.FileDescriptor = f
if imps[imp.Path()] {
return nil, errors.New("already imported %q", path)
}
imps[imp.Path()] = true
}
for i := range fd.GetDependency() {
imp := &f.L2.Imports[i]
imps.importPublic(imp.Imports())
}
// Handle source locations.
f.L2.Locations.File = f
for _, loc := range fd.GetSourceCodeInfo().GetLocation() {
var l protoreflect.SourceLocation
// TODO: Validate that the path points to an actual declaration?
l.Path = protoreflect.SourcePath(loc.GetPath())
s := loc.GetSpan()
switch len(s) {
case 3:
l.StartLine, l.StartColumn, l.EndLine, l.EndColumn = int(s[0]), int(s[1]), int(s[0]), int(s[2])
case 4:
l.StartLine, l.StartColumn, l.EndLine, l.EndColumn = int(s[0]), int(s[1]), int(s[2]), int(s[3])
default:
return nil, errors.New("invalid span: %v", s)
}
// TODO: Validate that the span information is sensible?
// See https://github.com/protocolbuffers/protobuf/issues/6378.
if false && (l.EndLine < l.StartLine || l.StartLine < 0 || l.StartColumn < 0 || l.EndColumn < 0 ||
(l.StartLine == l.EndLine && l.EndColumn <= l.StartColumn)) {
return nil, errors.New("invalid span: %v", s)
}
l.LeadingDetachedComments = loc.GetLeadingDetachedComments()
l.LeadingComments = loc.GetLeadingComments()
l.TrailingComments = loc.GetTrailingComments()
f.L2.Locations.List = append(f.L2.Locations.List, l)
}
// Step 1: Allocate and derive the names for all declarations.
// This copies all fields from the descriptor proto except:
// google.protobuf.FieldDescriptorProto.type_name
// google.protobuf.FieldDescriptorProto.default_value
// google.protobuf.FieldDescriptorProto.oneof_index
// google.protobuf.FieldDescriptorProto.extendee
// google.protobuf.MethodDescriptorProto.input
// google.protobuf.MethodDescriptorProto.output
var err error
sb := new(strs.Builder)
r1 := make(descsByName)
if f.L1.Enums.List, err = r1.initEnumDeclarations(fd.GetEnumType(), f, sb); err != nil {
return nil, err
}
if f.L1.Messages.List, err = r1.initMessagesDeclarations(fd.GetMessageType(), f, sb); err != nil {
return nil, err
}
if f.L1.Extensions.List, err = r1.initExtensionDeclarations(fd.GetExtension(), f, sb); err != nil {
return nil, err
}
if f.L1.Services.List, err = r1.initServiceDeclarations(fd.GetService(), f, sb); err != nil {
return nil, err
}
// Step 2: Resolve every dependency reference not handled by step 1.
r2 := &resolver{local: r1, remote: r, imports: imps, allowUnresolvable: o.AllowUnresolvable}
if err := r2.resolveMessageDependencies(f.L1.Messages.List, fd.GetMessageType()); err != nil {
return nil, err
}
if err := r2.resolveExtensionDependencies(f.L1.Extensions.List, fd.GetExtension()); err != nil {
return nil, err
}
if err := r2.resolveServiceDependencies(f.L1.Services.List, fd.GetService()); err != nil {
return nil, err
}
// Step 3: Validate every enum, message, and extension declaration.
if err := validateEnumDeclarations(f.L1.Enums.List, fd.GetEnumType()); err != nil {
return nil, err
}
if err := validateMessageDeclarations(f.L1.Messages.List, fd.GetMessageType()); err != nil {
return nil, err
}
if err := validateExtensionDeclarations(f.L1.Extensions.List, fd.GetExtension()); err != nil {
return nil, err
}
return f, nil
}
type importSet map[string]bool
func (is importSet) importPublic(imps protoreflect.FileImports) {
for i := 0; i < imps.Len(); i++ {
if imp := imps.Get(i); imp.IsPublic {
is[imp.Path()] = true
is.importPublic(imp.Imports())
}
}
}
// NewFiles creates a new protoregistry.Files from the provided
// FileDescriptorSet message. The descriptor set must include only
// valid files according to protobuf semantics. The returned descriptors
// are a deep copy of the input.
func (o FileOptions) NewFiles(fds *descriptorpb.FileDescriptorSet) (*protoregistry.Files, error) {
files := make(map[string]*descriptorpb.FileDescriptorProto)
for _, fd := range fds.File {
if _, ok := files[fd.GetName()]; ok {
return nil, errors.New("file appears multiple times: %q", fd.GetName())
}
files[fd.GetName()] = fd
}
r := &protoregistry.Files{}
for _, fd := range files {
if err := o.addFileDeps(r, fd, files); err != nil {
return nil, err
}
}
return r, nil
}
func (o FileOptions) addFileDeps(r *protoregistry.Files, fd *descriptorpb.FileDescriptorProto, files map[string]*descriptorpb.FileDescriptorProto) error {
// Set the entry to nil while descending into a file's dependencies to detect cycles.
files[fd.GetName()] = nil
for _, dep := range fd.Dependency {
depfd, ok := files[dep]
if depfd == nil {
if ok {
return errors.New("import cycle in file: %q", dep)
}
continue
}
if err := o.addFileDeps(r, depfd, files); err != nil {
return err
}
}
// Delete the entry once dependencies are processed.
delete(files, fd.GetName())
f, err := o.New(fd, r)
if err != nil {
return err
}
return r.RegisterFile(f)
}

View File

@ -0,0 +1,248 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protodesc
import (
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
type descsByName map[protoreflect.FullName]protoreflect.Descriptor
func (r descsByName) initEnumDeclarations(eds []*descriptorpb.EnumDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (es []filedesc.Enum, err error) {
es = make([]filedesc.Enum, len(eds)) // allocate up-front to ensure stable pointers
for i, ed := range eds {
e := &es[i]
e.L2 = new(filedesc.EnumL2)
if e.L0, err = r.makeBase(e, parent, ed.GetName(), i, sb); err != nil {
return nil, err
}
if opts := ed.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.EnumOptions)
e.L2.Options = func() protoreflect.ProtoMessage { return opts }
}
for _, s := range ed.GetReservedName() {
e.L2.ReservedNames.List = append(e.L2.ReservedNames.List, protoreflect.Name(s))
}
for _, rr := range ed.GetReservedRange() {
e.L2.ReservedRanges.List = append(e.L2.ReservedRanges.List, [2]protoreflect.EnumNumber{
protoreflect.EnumNumber(rr.GetStart()),
protoreflect.EnumNumber(rr.GetEnd()),
})
}
if e.L2.Values.List, err = r.initEnumValuesFromDescriptorProto(ed.GetValue(), e, sb); err != nil {
return nil, err
}
}
return es, nil
}
func (r descsByName) initEnumValuesFromDescriptorProto(vds []*descriptorpb.EnumValueDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (vs []filedesc.EnumValue, err error) {
vs = make([]filedesc.EnumValue, len(vds)) // allocate up-front to ensure stable pointers
for i, vd := range vds {
v := &vs[i]
if v.L0, err = r.makeBase(v, parent, vd.GetName(), i, sb); err != nil {
return nil, err
}
if opts := vd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.EnumValueOptions)
v.L1.Options = func() protoreflect.ProtoMessage { return opts }
}
v.L1.Number = protoreflect.EnumNumber(vd.GetNumber())
}
return vs, nil
}
func (r descsByName) initMessagesDeclarations(mds []*descriptorpb.DescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Message, err error) {
ms = make([]filedesc.Message, len(mds)) // allocate up-front to ensure stable pointers
for i, md := range mds {
m := &ms[i]
m.L2 = new(filedesc.MessageL2)
if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
return nil, err
}
if opts := md.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.MessageOptions)
m.L2.Options = func() protoreflect.ProtoMessage { return opts }
m.L1.IsMapEntry = opts.GetMapEntry()
m.L1.IsMessageSet = opts.GetMessageSetWireFormat()
}
for _, s := range md.GetReservedName() {
m.L2.ReservedNames.List = append(m.L2.ReservedNames.List, protoreflect.Name(s))
}
for _, rr := range md.GetReservedRange() {
m.L2.ReservedRanges.List = append(m.L2.ReservedRanges.List, [2]protoreflect.FieldNumber{
protoreflect.FieldNumber(rr.GetStart()),
protoreflect.FieldNumber(rr.GetEnd()),
})
}
for _, xr := range md.GetExtensionRange() {
m.L2.ExtensionRanges.List = append(m.L2.ExtensionRanges.List, [2]protoreflect.FieldNumber{
protoreflect.FieldNumber(xr.GetStart()),
protoreflect.FieldNumber(xr.GetEnd()),
})
var optsFunc func() protoreflect.ProtoMessage
if opts := xr.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.ExtensionRangeOptions)
optsFunc = func() protoreflect.ProtoMessage { return opts }
}
m.L2.ExtensionRangeOptions = append(m.L2.ExtensionRangeOptions, optsFunc)
}
if m.L2.Fields.List, err = r.initFieldsFromDescriptorProto(md.GetField(), m, sb); err != nil {
return nil, err
}
if m.L2.Oneofs.List, err = r.initOneofsFromDescriptorProto(md.GetOneofDecl(), m, sb); err != nil {
return nil, err
}
if m.L1.Enums.List, err = r.initEnumDeclarations(md.GetEnumType(), m, sb); err != nil {
return nil, err
}
if m.L1.Messages.List, err = r.initMessagesDeclarations(md.GetNestedType(), m, sb); err != nil {
return nil, err
}
if m.L1.Extensions.List, err = r.initExtensionDeclarations(md.GetExtension(), m, sb); err != nil {
return nil, err
}
}
return ms, nil
}
func (r descsByName) initFieldsFromDescriptorProto(fds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (fs []filedesc.Field, err error) {
fs = make([]filedesc.Field, len(fds)) // allocate up-front to ensure stable pointers
for i, fd := range fds {
f := &fs[i]
if f.L0, err = r.makeBase(f, parent, fd.GetName(), i, sb); err != nil {
return nil, err
}
f.L1.IsProto3Optional = fd.GetProto3Optional()
if opts := fd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
f.L1.Options = func() protoreflect.ProtoMessage { return opts }
f.L1.IsWeak = opts.GetWeak()
f.L1.HasPacked = opts.Packed != nil
f.L1.IsPacked = opts.GetPacked()
}
f.L1.Number = protoreflect.FieldNumber(fd.GetNumber())
f.L1.Cardinality = protoreflect.Cardinality(fd.GetLabel())
if fd.Type != nil {
f.L1.Kind = protoreflect.Kind(fd.GetType())
}
if fd.JsonName != nil {
f.L1.StringName.InitJSON(fd.GetJsonName())
}
}
return fs, nil
}
func (r descsByName) initOneofsFromDescriptorProto(ods []*descriptorpb.OneofDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (os []filedesc.Oneof, err error) {
os = make([]filedesc.Oneof, len(ods)) // allocate up-front to ensure stable pointers
for i, od := range ods {
o := &os[i]
if o.L0, err = r.makeBase(o, parent, od.GetName(), i, sb); err != nil {
return nil, err
}
if opts := od.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.OneofOptions)
o.L1.Options = func() protoreflect.ProtoMessage { return opts }
}
}
return os, nil
}
func (r descsByName) initExtensionDeclarations(xds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (xs []filedesc.Extension, err error) {
xs = make([]filedesc.Extension, len(xds)) // allocate up-front to ensure stable pointers
for i, xd := range xds {
x := &xs[i]
x.L2 = new(filedesc.ExtensionL2)
if x.L0, err = r.makeBase(x, parent, xd.GetName(), i, sb); err != nil {
return nil, err
}
if opts := xd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
x.L2.Options = func() protoreflect.ProtoMessage { return opts }
x.L2.IsPacked = opts.GetPacked()
}
x.L1.Number = protoreflect.FieldNumber(xd.GetNumber())
x.L1.Cardinality = protoreflect.Cardinality(xd.GetLabel())
if xd.Type != nil {
x.L1.Kind = protoreflect.Kind(xd.GetType())
}
if xd.JsonName != nil {
x.L2.StringName.InitJSON(xd.GetJsonName())
}
}
return xs, nil
}
func (r descsByName) initServiceDeclarations(sds []*descriptorpb.ServiceDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ss []filedesc.Service, err error) {
ss = make([]filedesc.Service, len(sds)) // allocate up-front to ensure stable pointers
for i, sd := range sds {
s := &ss[i]
s.L2 = new(filedesc.ServiceL2)
if s.L0, err = r.makeBase(s, parent, sd.GetName(), i, sb); err != nil {
return nil, err
}
if opts := sd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.ServiceOptions)
s.L2.Options = func() protoreflect.ProtoMessage { return opts }
}
if s.L2.Methods.List, err = r.initMethodsFromDescriptorProto(sd.GetMethod(), s, sb); err != nil {
return nil, err
}
}
return ss, nil
}
func (r descsByName) initMethodsFromDescriptorProto(mds []*descriptorpb.MethodDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Method, err error) {
ms = make([]filedesc.Method, len(mds)) // allocate up-front to ensure stable pointers
for i, md := range mds {
m := &ms[i]
if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
return nil, err
}
if opts := md.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.MethodOptions)
m.L1.Options = func() protoreflect.ProtoMessage { return opts }
}
m.L1.IsStreamingClient = md.GetClientStreaming()
m.L1.IsStreamingServer = md.GetServerStreaming()
}
return ms, nil
}
func (r descsByName) makeBase(child, parent protoreflect.Descriptor, name string, idx int, sb *strs.Builder) (filedesc.BaseL0, error) {
if !protoreflect.Name(name).IsValid() {
return filedesc.BaseL0{}, errors.New("descriptor %q has an invalid nested name: %q", parent.FullName(), name)
}
// Derive the full name of the child.
// Note that enum values are a sibling to the enum parent in the namespace.
var fullName protoreflect.FullName
if _, ok := parent.(protoreflect.EnumDescriptor); ok {
fullName = sb.AppendFullName(parent.FullName().Parent(), protoreflect.Name(name))
} else {
fullName = sb.AppendFullName(parent.FullName(), protoreflect.Name(name))
}
if _, ok := r[fullName]; ok {
return filedesc.BaseL0{}, errors.New("descriptor %q already declared", fullName)
}
r[fullName] = child
// TODO: Verify that the full name does not already exist in the resolver?
// This is not as critical since most usages of NewFile will register
// the created file back into the registry, which will perform this check.
return filedesc.BaseL0{
FullName: fullName,
ParentFile: parent.ParentFile().(*filedesc.File),
Parent: parent,
Index: idx,
}, nil
}

View File

@ -0,0 +1,286 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protodesc
import (
"google.golang.org/protobuf/internal/encoding/defval"
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
)
// resolver is a wrapper around a local registry of declarations within the file
// and the remote resolver. The remote resolver is restricted to only return
// descriptors that have been imported.
type resolver struct {
local descsByName
remote Resolver
imports importSet
allowUnresolvable bool
}
func (r *resolver) resolveMessageDependencies(ms []filedesc.Message, mds []*descriptorpb.DescriptorProto) (err error) {
for i, md := range mds {
m := &ms[i]
for j, fd := range md.GetField() {
f := &m.L2.Fields.List[j]
if f.L1.Cardinality == protoreflect.Required {
m.L2.RequiredNumbers.List = append(m.L2.RequiredNumbers.List, f.L1.Number)
}
if fd.OneofIndex != nil {
k := int(fd.GetOneofIndex())
if !(0 <= k && k < len(md.GetOneofDecl())) {
return errors.New("message field %q has an invalid oneof index: %d", f.FullName(), k)
}
o := &m.L2.Oneofs.List[k]
f.L1.ContainingOneof = o
o.L1.Fields.List = append(o.L1.Fields.List, f)
}
if f.L1.Kind, f.L1.Enum, f.L1.Message, err = r.findTarget(f.Kind(), f.Parent().FullName(), partialName(fd.GetTypeName()), f.IsWeak()); err != nil {
return errors.New("message field %q cannot resolve type: %v", f.FullName(), err)
}
if fd.DefaultValue != nil {
v, ev, err := unmarshalDefault(fd.GetDefaultValue(), f, r.allowUnresolvable)
if err != nil {
return errors.New("message field %q has invalid default: %v", f.FullName(), err)
}
f.L1.Default = filedesc.DefaultValue(v, ev)
}
}
if err := r.resolveMessageDependencies(m.L1.Messages.List, md.GetNestedType()); err != nil {
return err
}
if err := r.resolveExtensionDependencies(m.L1.Extensions.List, md.GetExtension()); err != nil {
return err
}
}
return nil
}
func (r *resolver) resolveExtensionDependencies(xs []filedesc.Extension, xds []*descriptorpb.FieldDescriptorProto) (err error) {
for i, xd := range xds {
x := &xs[i]
if x.L1.Extendee, err = r.findMessageDescriptor(x.Parent().FullName(), partialName(xd.GetExtendee()), false); err != nil {
return errors.New("extension field %q cannot resolve extendee: %v", x.FullName(), err)
}
if x.L1.Kind, x.L2.Enum, x.L2.Message, err = r.findTarget(x.Kind(), x.Parent().FullName(), partialName(xd.GetTypeName()), false); err != nil {
return errors.New("extension field %q cannot resolve type: %v", x.FullName(), err)
}
if xd.DefaultValue != nil {
v, ev, err := unmarshalDefault(xd.GetDefaultValue(), x, r.allowUnresolvable)
if err != nil {
return errors.New("extension field %q has invalid default: %v", x.FullName(), err)
}
x.L2.Default = filedesc.DefaultValue(v, ev)
}
}
return nil
}
func (r *resolver) resolveServiceDependencies(ss []filedesc.Service, sds []*descriptorpb.ServiceDescriptorProto) (err error) {
for i, sd := range sds {
s := &ss[i]
for j, md := range sd.GetMethod() {
m := &s.L2.Methods.List[j]
m.L1.Input, err = r.findMessageDescriptor(m.Parent().FullName(), partialName(md.GetInputType()), false)
if err != nil {
return errors.New("service method %q cannot resolve input: %v", m.FullName(), err)
}
m.L1.Output, err = r.findMessageDescriptor(s.FullName(), partialName(md.GetOutputType()), false)
if err != nil {
return errors.New("service method %q cannot resolve output: %v", m.FullName(), err)
}
}
}
return nil
}
// findTarget finds an enum or message descriptor if k is an enum, message,
// group, or unknown. If unknown, and the name could be resolved, the kind
// returned kind is set based on the type of the resolved descriptor.
func (r *resolver) findTarget(k protoreflect.Kind, scope protoreflect.FullName, ref partialName, isWeak bool) (protoreflect.Kind, protoreflect.EnumDescriptor, protoreflect.MessageDescriptor, error) {
switch k {
case protoreflect.EnumKind:
ed, err := r.findEnumDescriptor(scope, ref, isWeak)
if err != nil {
return 0, nil, nil, err
}
return k, ed, nil, nil
case protoreflect.MessageKind, protoreflect.GroupKind:
md, err := r.findMessageDescriptor(scope, ref, isWeak)
if err != nil {
return 0, nil, nil, err
}
return k, nil, md, nil
case 0:
// Handle unspecified kinds (possible with parsers that operate
// on a per-file basis without knowledge of dependencies).
d, err := r.findDescriptor(scope, ref)
if err == protoregistry.NotFound && (r.allowUnresolvable || isWeak) {
return k, filedesc.PlaceholderEnum(ref.FullName()), filedesc.PlaceholderMessage(ref.FullName()), nil
} else if err == protoregistry.NotFound {
return 0, nil, nil, errors.New("%q not found", ref.FullName())
} else if err != nil {
return 0, nil, nil, err
}
switch d := d.(type) {
case protoreflect.EnumDescriptor:
return protoreflect.EnumKind, d, nil, nil
case protoreflect.MessageDescriptor:
return protoreflect.MessageKind, nil, d, nil
default:
return 0, nil, nil, errors.New("unknown kind")
}
default:
if ref != "" {
return 0, nil, nil, errors.New("target name cannot be specified for %v", k)
}
if !k.IsValid() {
return 0, nil, nil, errors.New("invalid kind: %d", k)
}
return k, nil, nil, nil
}
}
// findDescriptor finds the descriptor by name,
// which may be a relative name within some scope.
//
// Suppose the scope was "fizz.buzz" and the reference was "Foo.Bar",
// then the following full names are searched:
// * fizz.buzz.Foo.Bar
// * fizz.Foo.Bar
// * Foo.Bar
func (r *resolver) findDescriptor(scope protoreflect.FullName, ref partialName) (protoreflect.Descriptor, error) {
if !ref.IsValid() {
return nil, errors.New("invalid name reference: %q", ref)
}
if ref.IsFull() {
scope, ref = "", ref[1:]
}
var foundButNotImported protoreflect.Descriptor
for {
// Derive the full name to search.
s := protoreflect.FullName(ref)
if scope != "" {
s = scope + "." + s
}
// Check the current file for the descriptor.
if d, ok := r.local[s]; ok {
return d, nil
}
// Check the remote registry for the descriptor.
d, err := r.remote.FindDescriptorByName(s)
if err == nil {
// Only allow descriptors covered by one of the imports.
if r.imports[d.ParentFile().Path()] {
return d, nil
}
foundButNotImported = d
} else if err != protoregistry.NotFound {
return nil, errors.Wrap(err, "%q", s)
}
// Continue on at a higher level of scoping.
if scope == "" {
if d := foundButNotImported; d != nil {
return nil, errors.New("resolved %q, but %q is not imported", d.FullName(), d.ParentFile().Path())
}
return nil, protoregistry.NotFound
}
scope = scope.Parent()
}
}
func (r *resolver) findEnumDescriptor(scope protoreflect.FullName, ref partialName, isWeak bool) (protoreflect.EnumDescriptor, error) {
d, err := r.findDescriptor(scope, ref)
if err == protoregistry.NotFound && (r.allowUnresolvable || isWeak) {
return filedesc.PlaceholderEnum(ref.FullName()), nil
} else if err == protoregistry.NotFound {
return nil, errors.New("%q not found", ref.FullName())
} else if err != nil {
return nil, err
}
ed, ok := d.(protoreflect.EnumDescriptor)
if !ok {
return nil, errors.New("resolved %q, but it is not an enum", d.FullName())
}
return ed, nil
}
func (r *resolver) findMessageDescriptor(scope protoreflect.FullName, ref partialName, isWeak bool) (protoreflect.MessageDescriptor, error) {
d, err := r.findDescriptor(scope, ref)
if err == protoregistry.NotFound && (r.allowUnresolvable || isWeak) {
return filedesc.PlaceholderMessage(ref.FullName()), nil
} else if err == protoregistry.NotFound {
return nil, errors.New("%q not found", ref.FullName())
} else if err != nil {
return nil, err
}
md, ok := d.(protoreflect.MessageDescriptor)
if !ok {
return nil, errors.New("resolved %q, but it is not an message", d.FullName())
}
return md, nil
}
// partialName is the partial name. A leading dot means that the name is full,
// otherwise the name is relative to some current scope.
// See google.protobuf.FieldDescriptorProto.type_name.
type partialName string
func (s partialName) IsFull() bool {
return len(s) > 0 && s[0] == '.'
}
func (s partialName) IsValid() bool {
if s.IsFull() {
return protoreflect.FullName(s[1:]).IsValid()
}
return protoreflect.FullName(s).IsValid()
}
const unknownPrefix = "*."
// FullName converts the partial name to a full name on a best-effort basis.
// If relative, it creates an invalid full name, using a "*." prefix
// to indicate that the start of the full name is unknown.
func (s partialName) FullName() protoreflect.FullName {
if s.IsFull() {
return protoreflect.FullName(s[1:])
}
return protoreflect.FullName(unknownPrefix + s)
}
func unmarshalDefault(s string, fd protoreflect.FieldDescriptor, allowUnresolvable bool) (protoreflect.Value, protoreflect.EnumValueDescriptor, error) {
var evs protoreflect.EnumValueDescriptors
if fd.Enum() != nil {
evs = fd.Enum().Values()
}
v, ev, err := defval.Unmarshal(s, fd.Kind(), evs, defval.Descriptor)
if err != nil && allowUnresolvable && evs != nil && protoreflect.Name(s).IsValid() {
v = protoreflect.ValueOfEnum(0)
if evs.Len() > 0 {
v = protoreflect.ValueOfEnum(evs.Get(0).Number())
}
ev = filedesc.PlaceholderEnumValue(fd.Enum().FullName().Parent().Append(protoreflect.Name(s)))
} else if err != nil {
return v, ev, err
}
if fd.Syntax() == protoreflect.Proto3 {
return v, ev, errors.New("cannot be specified under proto3 semantics")
}
if fd.Kind() == protoreflect.MessageKind || fd.Kind() == protoreflect.GroupKind || fd.Cardinality() == protoreflect.Repeated {
return v, ev, errors.New("cannot be specified on composite types")
}
return v, ev, nil
}

View File

@ -0,0 +1,374 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protodesc
import (
"strings"
"unicode"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
func validateEnumDeclarations(es []filedesc.Enum, eds []*descriptorpb.EnumDescriptorProto) error {
for i, ed := range eds {
e := &es[i]
if err := e.L2.ReservedNames.CheckValid(); err != nil {
return errors.New("enum %q reserved names has %v", e.FullName(), err)
}
if err := e.L2.ReservedRanges.CheckValid(); err != nil {
return errors.New("enum %q reserved ranges has %v", e.FullName(), err)
}
if len(ed.GetValue()) == 0 {
return errors.New("enum %q must contain at least one value declaration", e.FullName())
}
allowAlias := ed.GetOptions().GetAllowAlias()
foundAlias := false
for i := 0; i < e.Values().Len(); i++ {
v1 := e.Values().Get(i)
if v2 := e.Values().ByNumber(v1.Number()); v1 != v2 {
foundAlias = true
if !allowAlias {
return errors.New("enum %q has conflicting non-aliased values on number %d: %q with %q", e.FullName(), v1.Number(), v1.Name(), v2.Name())
}
}
}
if allowAlias && !foundAlias {
return errors.New("enum %q allows aliases, but none were found", e.FullName())
}
if e.Syntax() == protoreflect.Proto3 {
if v := e.Values().Get(0); v.Number() != 0 {
return errors.New("enum %q using proto3 semantics must have zero number for the first value", v.FullName())
}
// Verify that value names in proto3 do not conflict if the
// case-insensitive prefix is removed.
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:4991-5055
names := map[string]protoreflect.EnumValueDescriptor{}
prefix := strings.Replace(strings.ToLower(string(e.Name())), "_", "", -1)
for i := 0; i < e.Values().Len(); i++ {
v1 := e.Values().Get(i)
s := strs.EnumValueName(strs.TrimEnumPrefix(string(v1.Name()), prefix))
if v2, ok := names[s]; ok && v1.Number() != v2.Number() {
return errors.New("enum %q using proto3 semantics has conflict: %q with %q", e.FullName(), v1.Name(), v2.Name())
}
names[s] = v1
}
}
for j, vd := range ed.GetValue() {
v := &e.L2.Values.List[j]
if vd.Number == nil {
return errors.New("enum value %q must have a specified number", v.FullName())
}
if e.L2.ReservedNames.Has(v.Name()) {
return errors.New("enum value %q must not use reserved name", v.FullName())
}
if e.L2.ReservedRanges.Has(v.Number()) {
return errors.New("enum value %q must not use reserved number %d", v.FullName(), v.Number())
}
}
}
return nil
}
func validateMessageDeclarations(ms []filedesc.Message, mds []*descriptorpb.DescriptorProto) error {
for i, md := range mds {
m := &ms[i]
// Handle the message descriptor itself.
isMessageSet := md.GetOptions().GetMessageSetWireFormat()
if err := m.L2.ReservedNames.CheckValid(); err != nil {
return errors.New("message %q reserved names has %v", m.FullName(), err)
}
if err := m.L2.ReservedRanges.CheckValid(isMessageSet); err != nil {
return errors.New("message %q reserved ranges has %v", m.FullName(), err)
}
if err := m.L2.ExtensionRanges.CheckValid(isMessageSet); err != nil {
return errors.New("message %q extension ranges has %v", m.FullName(), err)
}
if err := (*filedesc.FieldRanges).CheckOverlap(&m.L2.ReservedRanges, &m.L2.ExtensionRanges); err != nil {
return errors.New("message %q reserved and extension ranges has %v", m.FullName(), err)
}
for i := 0; i < m.Fields().Len(); i++ {
f1 := m.Fields().Get(i)
if f2 := m.Fields().ByNumber(f1.Number()); f1 != f2 {
return errors.New("message %q has conflicting fields: %q with %q", m.FullName(), f1.Name(), f2.Name())
}
}
if isMessageSet && !flags.ProtoLegacy {
return errors.New("message %q is a MessageSet, which is a legacy proto1 feature that is no longer supported", m.FullName())
}
if isMessageSet && (m.Syntax() != protoreflect.Proto2 || m.Fields().Len() > 0 || m.ExtensionRanges().Len() == 0) {
return errors.New("message %q is an invalid proto1 MessageSet", m.FullName())
}
if m.Syntax() == protoreflect.Proto3 {
if m.ExtensionRanges().Len() > 0 {
return errors.New("message %q using proto3 semantics cannot have extension ranges", m.FullName())
}
// Verify that field names in proto3 do not conflict if lowercased
// with all underscores removed.
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:5830-5847
names := map[string]protoreflect.FieldDescriptor{}
for i := 0; i < m.Fields().Len(); i++ {
f1 := m.Fields().Get(i)
s := strings.Replace(strings.ToLower(string(f1.Name())), "_", "", -1)
if f2, ok := names[s]; ok {
return errors.New("message %q using proto3 semantics has conflict: %q with %q", m.FullName(), f1.Name(), f2.Name())
}
names[s] = f1
}
}
for j, fd := range md.GetField() {
f := &m.L2.Fields.List[j]
if m.L2.ReservedNames.Has(f.Name()) {
return errors.New("message field %q must not use reserved name", f.FullName())
}
if !f.Number().IsValid() {
return errors.New("message field %q has an invalid number: %d", f.FullName(), f.Number())
}
if !f.Cardinality().IsValid() {
return errors.New("message field %q has an invalid cardinality: %d", f.FullName(), f.Cardinality())
}
if m.L2.ReservedRanges.Has(f.Number()) {
return errors.New("message field %q must not use reserved number %d", f.FullName(), f.Number())
}
if m.L2.ExtensionRanges.Has(f.Number()) {
return errors.New("message field %q with number %d in extension range", f.FullName(), f.Number())
}
if fd.Extendee != nil {
return errors.New("message field %q may not have extendee: %q", f.FullName(), fd.GetExtendee())
}
if f.L1.IsProto3Optional {
if f.Syntax() != protoreflect.Proto3 {
return errors.New("message field %q under proto3 optional semantics must be specified in the proto3 syntax", f.FullName())
}
if f.Cardinality() != protoreflect.Optional {
return errors.New("message field %q under proto3 optional semantics must have optional cardinality", f.FullName())
}
if f.ContainingOneof() != nil && f.ContainingOneof().Fields().Len() != 1 {
return errors.New("message field %q under proto3 optional semantics must be within a single element oneof", f.FullName())
}
}
if f.IsWeak() && !flags.ProtoLegacy {
return errors.New("message field %q is a weak field, which is a legacy proto1 feature that is no longer supported", f.FullName())
}
if f.IsWeak() && (f.Syntax() != protoreflect.Proto2 || !isOptionalMessage(f) || f.ContainingOneof() != nil) {
return errors.New("message field %q may only be weak for an optional message", f.FullName())
}
if f.IsPacked() && !isPackable(f) {
return errors.New("message field %q is not packable", f.FullName())
}
if err := checkValidGroup(f); err != nil {
return errors.New("message field %q is an invalid group: %v", f.FullName(), err)
}
if err := checkValidMap(f); err != nil {
return errors.New("message field %q is an invalid map: %v", f.FullName(), err)
}
if f.Syntax() == protoreflect.Proto3 {
if f.Cardinality() == protoreflect.Required {
return errors.New("message field %q using proto3 semantics cannot be required", f.FullName())
}
if f.Enum() != nil && !f.Enum().IsPlaceholder() && f.Enum().Syntax() != protoreflect.Proto3 {
return errors.New("message field %q using proto3 semantics may only depend on a proto3 enum", f.FullName())
}
}
}
seenSynthetic := false // synthetic oneofs for proto3 optional must come after real oneofs
for j := range md.GetOneofDecl() {
o := &m.L2.Oneofs.List[j]
if o.Fields().Len() == 0 {
return errors.New("message oneof %q must contain at least one field declaration", o.FullName())
}
if n := o.Fields().Len(); n-1 != (o.Fields().Get(n-1).Index() - o.Fields().Get(0).Index()) {
return errors.New("message oneof %q must have consecutively declared fields", o.FullName())
}
if o.IsSynthetic() {
seenSynthetic = true
continue
}
if !o.IsSynthetic() && seenSynthetic {
return errors.New("message oneof %q must be declared before synthetic oneofs", o.FullName())
}
for i := 0; i < o.Fields().Len(); i++ {
f := o.Fields().Get(i)
if f.Cardinality() != protoreflect.Optional {
return errors.New("message field %q belongs in a oneof and must be optional", f.FullName())
}
if f.IsWeak() {
return errors.New("message field %q belongs in a oneof and must not be a weak reference", f.FullName())
}
}
}
if err := validateEnumDeclarations(m.L1.Enums.List, md.GetEnumType()); err != nil {
return err
}
if err := validateMessageDeclarations(m.L1.Messages.List, md.GetNestedType()); err != nil {
return err
}
if err := validateExtensionDeclarations(m.L1.Extensions.List, md.GetExtension()); err != nil {
return err
}
}
return nil
}
func validateExtensionDeclarations(xs []filedesc.Extension, xds []*descriptorpb.FieldDescriptorProto) error {
for i, xd := range xds {
x := &xs[i]
// NOTE: Avoid using the IsValid method since extensions to MessageSet
// may have a field number higher than normal. This check only verifies
// that the number is not negative or reserved. We check again later
// if we know that the extendee is definitely not a MessageSet.
if n := x.Number(); n < 0 || (protowire.FirstReservedNumber <= n && n <= protowire.LastReservedNumber) {
return errors.New("extension field %q has an invalid number: %d", x.FullName(), x.Number())
}
if !x.Cardinality().IsValid() || x.Cardinality() == protoreflect.Required {
return errors.New("extension field %q has an invalid cardinality: %d", x.FullName(), x.Cardinality())
}
if xd.JsonName != nil {
// A bug in older versions of protoc would always populate the
// "json_name" option for extensions when it is meaningless.
// When it did so, it would always use the camel-cased field name.
if xd.GetJsonName() != strs.JSONCamelCase(string(x.Name())) {
return errors.New("extension field %q may not have an explicitly set JSON name: %q", x.FullName(), xd.GetJsonName())
}
}
if xd.OneofIndex != nil {
return errors.New("extension field %q may not be part of a oneof", x.FullName())
}
if md := x.ContainingMessage(); !md.IsPlaceholder() {
if !md.ExtensionRanges().Has(x.Number()) {
return errors.New("extension field %q extends %q with non-extension field number: %d", x.FullName(), md.FullName(), x.Number())
}
isMessageSet := md.Options().(*descriptorpb.MessageOptions).GetMessageSetWireFormat()
if isMessageSet && !isOptionalMessage(x) {
return errors.New("extension field %q extends MessageSet and must be an optional message", x.FullName())
}
if !isMessageSet && !x.Number().IsValid() {
return errors.New("extension field %q has an invalid number: %d", x.FullName(), x.Number())
}
}
if xd.GetOptions().GetWeak() {
return errors.New("extension field %q cannot be a weak reference", x.FullName())
}
if x.IsPacked() && !isPackable(x) {
return errors.New("extension field %q is not packable", x.FullName())
}
if err := checkValidGroup(x); err != nil {
return errors.New("extension field %q is an invalid group: %v", x.FullName(), err)
}
if md := x.Message(); md != nil && md.IsMapEntry() {
return errors.New("extension field %q cannot be a map entry", x.FullName())
}
if x.Syntax() == protoreflect.Proto3 {
switch x.ContainingMessage().FullName() {
case (*descriptorpb.FileOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.EnumOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.EnumValueOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.MessageOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.FieldOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.OneofOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.ExtensionRangeOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.ServiceOptions)(nil).ProtoReflect().Descriptor().FullName():
case (*descriptorpb.MethodOptions)(nil).ProtoReflect().Descriptor().FullName():
default:
return errors.New("extension field %q cannot be declared in proto3 unless extended descriptor options", x.FullName())
}
}
}
return nil
}
// isOptionalMessage reports whether this is an optional message.
// If the kind is unknown, it is assumed to be a message.
func isOptionalMessage(fd protoreflect.FieldDescriptor) bool {
return (fd.Kind() == 0 || fd.Kind() == protoreflect.MessageKind) && fd.Cardinality() == protoreflect.Optional
}
// isPackable checks whether the pack option can be specified.
func isPackable(fd protoreflect.FieldDescriptor) bool {
switch fd.Kind() {
case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind:
return false
}
return fd.IsList()
}
// checkValidGroup reports whether fd is a valid group according to the same
// rules that protoc imposes.
func checkValidGroup(fd protoreflect.FieldDescriptor) error {
md := fd.Message()
switch {
case fd.Kind() != protoreflect.GroupKind:
return nil
case fd.Syntax() != protoreflect.Proto2:
return errors.New("invalid under proto2 semantics")
case md == nil || md.IsPlaceholder():
return errors.New("message must be resolvable")
case fd.FullName().Parent() != md.FullName().Parent():
return errors.New("message and field must be declared in the same scope")
case !unicode.IsUpper(rune(md.Name()[0])):
return errors.New("message name must start with an uppercase")
case fd.Name() != protoreflect.Name(strings.ToLower(string(md.Name()))):
return errors.New("field name must be lowercased form of the message name")
}
return nil
}
// checkValidMap checks whether the field is a valid map according to the same
// rules that protoc imposes.
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:6045-6115
func checkValidMap(fd protoreflect.FieldDescriptor) error {
md := fd.Message()
switch {
case md == nil || !md.IsMapEntry():
return nil
case fd.FullName().Parent() != md.FullName().Parent():
return errors.New("message and field must be declared in the same scope")
case md.Name() != protoreflect.Name(strs.MapEntryName(string(fd.Name()))):
return errors.New("incorrect implicit map entry name")
case fd.Cardinality() != protoreflect.Repeated:
return errors.New("field must be repeated")
case md.Fields().Len() != 2:
return errors.New("message must have exactly two fields")
case md.ExtensionRanges().Len() > 0:
return errors.New("message must not have any extension ranges")
case md.Enums().Len()+md.Messages().Len()+md.Extensions().Len() > 0:
return errors.New("message must not have any nested declarations")
}
kf := md.Fields().Get(0)
vf := md.Fields().Get(1)
switch {
case kf.Name() != genid.MapEntry_Key_field_name || kf.Number() != genid.MapEntry_Key_field_number || kf.Cardinality() != protoreflect.Optional || kf.ContainingOneof() != nil || kf.HasDefault():
return errors.New("invalid key field")
case vf.Name() != genid.MapEntry_Value_field_name || vf.Number() != genid.MapEntry_Value_field_number || vf.Cardinality() != protoreflect.Optional || vf.ContainingOneof() != nil || vf.HasDefault():
return errors.New("invalid value field")
}
switch kf.Kind() {
case protoreflect.BoolKind: // bool
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: // int32
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: // int64
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: // uint32
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: // uint64
case protoreflect.StringKind: // string
default:
return errors.New("invalid key kind: %v", kf.Kind())
}
if e := vf.Enum(); e != nil && e.Values().Len() > 0 && e.Values().Get(0).Number() != 0 {
return errors.New("map enum value must have zero number for the first value")
}
return nil
}

View File

@ -0,0 +1,252 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protodesc
import (
"fmt"
"strings"
"google.golang.org/protobuf/internal/encoding/defval"
"google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
// ToFileDescriptorProto copies a protoreflect.FileDescriptor into a
// google.protobuf.FileDescriptorProto message.
func ToFileDescriptorProto(file protoreflect.FileDescriptor) *descriptorpb.FileDescriptorProto {
p := &descriptorpb.FileDescriptorProto{
Name: proto.String(file.Path()),
Options: proto.Clone(file.Options()).(*descriptorpb.FileOptions),
}
if file.Package() != "" {
p.Package = proto.String(string(file.Package()))
}
for i, imports := 0, file.Imports(); i < imports.Len(); i++ {
imp := imports.Get(i)
p.Dependency = append(p.Dependency, imp.Path())
if imp.IsPublic {
p.PublicDependency = append(p.PublicDependency, int32(i))
}
if imp.IsWeak {
p.WeakDependency = append(p.WeakDependency, int32(i))
}
}
for i, locs := 0, file.SourceLocations(); i < locs.Len(); i++ {
loc := locs.Get(i)
l := &descriptorpb.SourceCodeInfo_Location{}
l.Path = append(l.Path, loc.Path...)
if loc.StartLine == loc.EndLine {
l.Span = []int32{int32(loc.StartLine), int32(loc.StartColumn), int32(loc.EndColumn)}
} else {
l.Span = []int32{int32(loc.StartLine), int32(loc.StartColumn), int32(loc.EndLine), int32(loc.EndColumn)}
}
l.LeadingDetachedComments = append([]string(nil), loc.LeadingDetachedComments...)
if loc.LeadingComments != "" {
l.LeadingComments = proto.String(loc.LeadingComments)
}
if loc.TrailingComments != "" {
l.TrailingComments = proto.String(loc.TrailingComments)
}
if p.SourceCodeInfo == nil {
p.SourceCodeInfo = &descriptorpb.SourceCodeInfo{}
}
p.SourceCodeInfo.Location = append(p.SourceCodeInfo.Location, l)
}
for i, messages := 0, file.Messages(); i < messages.Len(); i++ {
p.MessageType = append(p.MessageType, ToDescriptorProto(messages.Get(i)))
}
for i, enums := 0, file.Enums(); i < enums.Len(); i++ {
p.EnumType = append(p.EnumType, ToEnumDescriptorProto(enums.Get(i)))
}
for i, services := 0, file.Services(); i < services.Len(); i++ {
p.Service = append(p.Service, ToServiceDescriptorProto(services.Get(i)))
}
for i, exts := 0, file.Extensions(); i < exts.Len(); i++ {
p.Extension = append(p.Extension, ToFieldDescriptorProto(exts.Get(i)))
}
if syntax := file.Syntax(); syntax != protoreflect.Proto2 {
p.Syntax = proto.String(file.Syntax().String())
}
return p
}
// ToDescriptorProto copies a protoreflect.MessageDescriptor into a
// google.protobuf.DescriptorProto message.
func ToDescriptorProto(message protoreflect.MessageDescriptor) *descriptorpb.DescriptorProto {
p := &descriptorpb.DescriptorProto{
Name: proto.String(string(message.Name())),
Options: proto.Clone(message.Options()).(*descriptorpb.MessageOptions),
}
for i, fields := 0, message.Fields(); i < fields.Len(); i++ {
p.Field = append(p.Field, ToFieldDescriptorProto(fields.Get(i)))
}
for i, exts := 0, message.Extensions(); i < exts.Len(); i++ {
p.Extension = append(p.Extension, ToFieldDescriptorProto(exts.Get(i)))
}
for i, messages := 0, message.Messages(); i < messages.Len(); i++ {
p.NestedType = append(p.NestedType, ToDescriptorProto(messages.Get(i)))
}
for i, enums := 0, message.Enums(); i < enums.Len(); i++ {
p.EnumType = append(p.EnumType, ToEnumDescriptorProto(enums.Get(i)))
}
for i, xranges := 0, message.ExtensionRanges(); i < xranges.Len(); i++ {
xrange := xranges.Get(i)
p.ExtensionRange = append(p.ExtensionRange, &descriptorpb.DescriptorProto_ExtensionRange{
Start: proto.Int32(int32(xrange[0])),
End: proto.Int32(int32(xrange[1])),
Options: proto.Clone(message.ExtensionRangeOptions(i)).(*descriptorpb.ExtensionRangeOptions),
})
}
for i, oneofs := 0, message.Oneofs(); i < oneofs.Len(); i++ {
p.OneofDecl = append(p.OneofDecl, ToOneofDescriptorProto(oneofs.Get(i)))
}
for i, ranges := 0, message.ReservedRanges(); i < ranges.Len(); i++ {
rrange := ranges.Get(i)
p.ReservedRange = append(p.ReservedRange, &descriptorpb.DescriptorProto_ReservedRange{
Start: proto.Int32(int32(rrange[0])),
End: proto.Int32(int32(rrange[1])),
})
}
for i, names := 0, message.ReservedNames(); i < names.Len(); i++ {
p.ReservedName = append(p.ReservedName, string(names.Get(i)))
}
return p
}
// ToFieldDescriptorProto copies a protoreflect.FieldDescriptor into a
// google.protobuf.FieldDescriptorProto message.
func ToFieldDescriptorProto(field protoreflect.FieldDescriptor) *descriptorpb.FieldDescriptorProto {
p := &descriptorpb.FieldDescriptorProto{
Name: proto.String(string(field.Name())),
Number: proto.Int32(int32(field.Number())),
Label: descriptorpb.FieldDescriptorProto_Label(field.Cardinality()).Enum(),
Options: proto.Clone(field.Options()).(*descriptorpb.FieldOptions),
}
if field.IsExtension() {
p.Extendee = fullNameOf(field.ContainingMessage())
}
if field.Kind().IsValid() {
p.Type = descriptorpb.FieldDescriptorProto_Type(field.Kind()).Enum()
}
if field.Enum() != nil {
p.TypeName = fullNameOf(field.Enum())
}
if field.Message() != nil {
p.TypeName = fullNameOf(field.Message())
}
if field.HasJSONName() {
// A bug in older versions of protoc would always populate the
// "json_name" option for extensions when it is meaningless.
// When it did so, it would always use the camel-cased field name.
if field.IsExtension() {
p.JsonName = proto.String(strs.JSONCamelCase(string(field.Name())))
} else {
p.JsonName = proto.String(field.JSONName())
}
}
if field.Syntax() == protoreflect.Proto3 && field.HasOptionalKeyword() {
p.Proto3Optional = proto.Bool(true)
}
if field.HasDefault() {
def, err := defval.Marshal(field.Default(), field.DefaultEnumValue(), field.Kind(), defval.Descriptor)
if err != nil && field.DefaultEnumValue() != nil {
def = string(field.DefaultEnumValue().Name()) // occurs for unresolved enum values
} else if err != nil {
panic(fmt.Sprintf("%v: %v", field.FullName(), err))
}
p.DefaultValue = proto.String(def)
}
if oneof := field.ContainingOneof(); oneof != nil {
p.OneofIndex = proto.Int32(int32(oneof.Index()))
}
return p
}
// ToOneofDescriptorProto copies a protoreflect.OneofDescriptor into a
// google.protobuf.OneofDescriptorProto message.
func ToOneofDescriptorProto(oneof protoreflect.OneofDescriptor) *descriptorpb.OneofDescriptorProto {
return &descriptorpb.OneofDescriptorProto{
Name: proto.String(string(oneof.Name())),
Options: proto.Clone(oneof.Options()).(*descriptorpb.OneofOptions),
}
}
// ToEnumDescriptorProto copies a protoreflect.EnumDescriptor into a
// google.protobuf.EnumDescriptorProto message.
func ToEnumDescriptorProto(enum protoreflect.EnumDescriptor) *descriptorpb.EnumDescriptorProto {
p := &descriptorpb.EnumDescriptorProto{
Name: proto.String(string(enum.Name())),
Options: proto.Clone(enum.Options()).(*descriptorpb.EnumOptions),
}
for i, values := 0, enum.Values(); i < values.Len(); i++ {
p.Value = append(p.Value, ToEnumValueDescriptorProto(values.Get(i)))
}
for i, ranges := 0, enum.ReservedRanges(); i < ranges.Len(); i++ {
rrange := ranges.Get(i)
p.ReservedRange = append(p.ReservedRange, &descriptorpb.EnumDescriptorProto_EnumReservedRange{
Start: proto.Int32(int32(rrange[0])),
End: proto.Int32(int32(rrange[1])),
})
}
for i, names := 0, enum.ReservedNames(); i < names.Len(); i++ {
p.ReservedName = append(p.ReservedName, string(names.Get(i)))
}
return p
}
// ToEnumValueDescriptorProto copies a protoreflect.EnumValueDescriptor into a
// google.protobuf.EnumValueDescriptorProto message.
func ToEnumValueDescriptorProto(value protoreflect.EnumValueDescriptor) *descriptorpb.EnumValueDescriptorProto {
return &descriptorpb.EnumValueDescriptorProto{
Name: proto.String(string(value.Name())),
Number: proto.Int32(int32(value.Number())),
Options: proto.Clone(value.Options()).(*descriptorpb.EnumValueOptions),
}
}
// ToServiceDescriptorProto copies a protoreflect.ServiceDescriptor into a
// google.protobuf.ServiceDescriptorProto message.
func ToServiceDescriptorProto(service protoreflect.ServiceDescriptor) *descriptorpb.ServiceDescriptorProto {
p := &descriptorpb.ServiceDescriptorProto{
Name: proto.String(string(service.Name())),
Options: proto.Clone(service.Options()).(*descriptorpb.ServiceOptions),
}
for i, methods := 0, service.Methods(); i < methods.Len(); i++ {
p.Method = append(p.Method, ToMethodDescriptorProto(methods.Get(i)))
}
return p
}
// ToMethodDescriptorProto copies a protoreflect.MethodDescriptor into a
// google.protobuf.MethodDescriptorProto message.
func ToMethodDescriptorProto(method protoreflect.MethodDescriptor) *descriptorpb.MethodDescriptorProto {
p := &descriptorpb.MethodDescriptorProto{
Name: proto.String(string(method.Name())),
InputType: fullNameOf(method.Input()),
OutputType: fullNameOf(method.Output()),
Options: proto.Clone(method.Options()).(*descriptorpb.MethodOptions),
}
if method.IsStreamingClient() {
p.ClientStreaming = proto.Bool(true)
}
if method.IsStreamingServer() {
p.ServerStreaming = proto.Bool(true)
}
return p
}
func fullNameOf(d protoreflect.Descriptor) *string {
if d == nil {
return nil
}
if strings.HasPrefix(string(d.FullName()), unknownPrefix) {
return proto.String(string(d.FullName()[len(unknownPrefix):]))
}
return proto.String("." + string(d.FullName()))
}

View File

@ -128,7 +128,6 @@ package protoreflect
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/encoding/protowire"
@ -408,19 +407,14 @@ type EnumRanges interface {
doNotImplement doNotImplement
} }
var (
regexName = regexp.MustCompile(`^[_a-zA-Z][_a-zA-Z0-9]*$`)
regexFullName = regexp.MustCompile(`^[_a-zA-Z][_a-zA-Z0-9]*(\.[_a-zA-Z][_a-zA-Z0-9]*)*$`)
)
// Name is the short name for a proto declaration. This is not the name // Name is the short name for a proto declaration. This is not the name
// as used in Go source code, which might not be identical to the proto name. // as used in Go source code, which might not be identical to the proto name.
type Name string // e.g., "Kind" type Name string // e.g., "Kind"
// IsValid reports whether n is a syntactically valid name. // IsValid reports whether s is a syntactically valid name.
// An empty name is invalid. // An empty name is invalid.
func (n Name) IsValid() bool { func (s Name) IsValid() bool {
return regexName.MatchString(string(n)) return consumeIdent(string(s)) == len(s)
} }
// Names represent a list of names. // Names represent a list of names.
@ -443,10 +437,42 @@ type Names interface {
// This should not have any leading or trailing dots. // This should not have any leading or trailing dots.
type FullName string // e.g., "google.protobuf.Field.Kind" type FullName string // e.g., "google.protobuf.Field.Kind"
// IsValid reports whether n is a syntactically valid full name. // IsValid reports whether s is a syntactically valid full name.
// An empty full name is invalid. // An empty full name is invalid.
func (n FullName) IsValid() bool { func (s FullName) IsValid() bool {
return regexFullName.MatchString(string(n)) i := consumeIdent(string(s))
if i < 0 {
return false
}
for len(s) > i {
if s[i] != '.' {
return false
}
i++
n := consumeIdent(string(s[i:]))
if n < 0 {
return false
}
i += n
}
return true
}
func consumeIdent(s string) (i int) {
if len(s) == 0 || !isLetter(s[i]) {
return -1
}
i++
for len(s) > i && isLetterDigit(s[i]) {
i++
}
return i
}
func isLetter(c byte) bool {
return c == '_' || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
}
func isLetterDigit(c byte) bool {
return isLetter(c) || ('0' <= c && c <= '9')
} }
// Name returns the short name, which is the last identifier segment. // Name returns the short name, which is the last identifier segment.

View File

@ -4,6 +4,10 @@
package protoreflect package protoreflect
import (
"strconv"
)
// SourceLocations is a list of source locations. // SourceLocations is a list of source locations.
type SourceLocations interface { type SourceLocations interface {
// Len reports the number of source locations in the proto file. // Len reports the number of source locations in the proto file.
@ -11,9 +15,20 @@ type SourceLocations interface {
// Get returns the ith SourceLocation. It panics if out of bounds. // Get returns the ith SourceLocation. It panics if out of bounds.
Get(int) SourceLocation Get(int) SourceLocation
doNotImplement // ByPath returns the SourceLocation for the given path,
// returning the first location if multiple exist for the same path.
// If multiple locations exist for the same path,
// then SourceLocation.Next index can be used to identify the
// index of the next SourceLocation.
// If no location exists for this path, it returns the zero value.
ByPath(path SourcePath) SourceLocation
// TODO: Add ByPath and ByDescriptor helper methods. // ByDescriptor returns the SourceLocation for the given descriptor,
// returning the first location if multiple exist for the same path.
// If no location exists for this descriptor, it returns the zero value.
ByDescriptor(desc Descriptor) SourceLocation
doNotImplement
} }
// SourceLocation describes a source location and // SourceLocation describes a source location and
@ -39,6 +54,10 @@ type SourceLocation struct {
LeadingComments string LeadingComments string
// TrailingComments is the trailing attached comment for the declaration. // TrailingComments is the trailing attached comment for the declaration.
TrailingComments string TrailingComments string
// Next is an index into SourceLocations for the next source location that
// has the same Path. It is zero if there is no next location.
Next int
} }
// SourcePath identifies part of a file descriptor for a source location. // SourcePath identifies part of a file descriptor for a source location.
@ -48,5 +67,62 @@ type SourceLocation struct {
// See google.protobuf.SourceCodeInfo.Location.path. // See google.protobuf.SourceCodeInfo.Location.path.
type SourcePath []int32 type SourcePath []int32
// TODO: Add SourcePath.String method to pretty-print the path. For example: // Equal reports whether p1 equals p2.
// ".message_type[6].nested_type[15].field[3]" func (p1 SourcePath) Equal(p2 SourcePath) bool {
if len(p1) != len(p2) {
return false
}
for i := range p1 {
if p1[i] != p2[i] {
return false
}
}
return true
}
// String formats the path in a humanly readable manner.
// The output is guaranteed to be deterministic,
// making it suitable for use as a key into a Go map.
// It is not guaranteed to be stable as the exact output could change
// in a future version of this module.
//
// Example output:
// .message_type[6].nested_type[15].field[3]
func (p SourcePath) String() string {
b := p.appendFileDescriptorProto(nil)
for _, i := range p {
b = append(b, '.')
b = strconv.AppendInt(b, int64(i), 10)
}
return string(b)
}
type appendFunc func(*SourcePath, []byte) []byte
func (p *SourcePath) appendSingularField(b []byte, name string, f appendFunc) []byte {
if len(*p) == 0 {
return b
}
b = append(b, '.')
b = append(b, name...)
*p = (*p)[1:]
if f != nil {
b = f(p, b)
}
return b
}
func (p *SourcePath) appendRepeatedField(b []byte, name string, f appendFunc) []byte {
b = p.appendSingularField(b, name, nil)
if len(*p) == 0 || (*p)[0] < 0 {
return b
}
b = append(b, '[')
b = strconv.AppendUint(b, uint64((*p)[0]), 10)
b = append(b, ']')
*p = (*p)[1:]
if f != nil {
b = f(p, b)
}
return b
}

View File

@ -0,0 +1,461 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by generate-protos. DO NOT EDIT.
package protoreflect
func (p *SourcePath) appendFileDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendSingularField(b, "package", nil)
case 3:
b = p.appendRepeatedField(b, "dependency", nil)
case 10:
b = p.appendRepeatedField(b, "public_dependency", nil)
case 11:
b = p.appendRepeatedField(b, "weak_dependency", nil)
case 4:
b = p.appendRepeatedField(b, "message_type", (*SourcePath).appendDescriptorProto)
case 5:
b = p.appendRepeatedField(b, "enum_type", (*SourcePath).appendEnumDescriptorProto)
case 6:
b = p.appendRepeatedField(b, "service", (*SourcePath).appendServiceDescriptorProto)
case 7:
b = p.appendRepeatedField(b, "extension", (*SourcePath).appendFieldDescriptorProto)
case 8:
b = p.appendSingularField(b, "options", (*SourcePath).appendFileOptions)
case 9:
b = p.appendSingularField(b, "source_code_info", (*SourcePath).appendSourceCodeInfo)
case 12:
b = p.appendSingularField(b, "syntax", nil)
}
return b
}
func (p *SourcePath) appendDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendRepeatedField(b, "field", (*SourcePath).appendFieldDescriptorProto)
case 6:
b = p.appendRepeatedField(b, "extension", (*SourcePath).appendFieldDescriptorProto)
case 3:
b = p.appendRepeatedField(b, "nested_type", (*SourcePath).appendDescriptorProto)
case 4:
b = p.appendRepeatedField(b, "enum_type", (*SourcePath).appendEnumDescriptorProto)
case 5:
b = p.appendRepeatedField(b, "extension_range", (*SourcePath).appendDescriptorProto_ExtensionRange)
case 8:
b = p.appendRepeatedField(b, "oneof_decl", (*SourcePath).appendOneofDescriptorProto)
case 7:
b = p.appendSingularField(b, "options", (*SourcePath).appendMessageOptions)
case 9:
b = p.appendRepeatedField(b, "reserved_range", (*SourcePath).appendDescriptorProto_ReservedRange)
case 10:
b = p.appendRepeatedField(b, "reserved_name", nil)
}
return b
}
func (p *SourcePath) appendEnumDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendRepeatedField(b, "value", (*SourcePath).appendEnumValueDescriptorProto)
case 3:
b = p.appendSingularField(b, "options", (*SourcePath).appendEnumOptions)
case 4:
b = p.appendRepeatedField(b, "reserved_range", (*SourcePath).appendEnumDescriptorProto_EnumReservedRange)
case 5:
b = p.appendRepeatedField(b, "reserved_name", nil)
}
return b
}
func (p *SourcePath) appendServiceDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendRepeatedField(b, "method", (*SourcePath).appendMethodDescriptorProto)
case 3:
b = p.appendSingularField(b, "options", (*SourcePath).appendServiceOptions)
}
return b
}
func (p *SourcePath) appendFieldDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 3:
b = p.appendSingularField(b, "number", nil)
case 4:
b = p.appendSingularField(b, "label", nil)
case 5:
b = p.appendSingularField(b, "type", nil)
case 6:
b = p.appendSingularField(b, "type_name", nil)
case 2:
b = p.appendSingularField(b, "extendee", nil)
case 7:
b = p.appendSingularField(b, "default_value", nil)
case 9:
b = p.appendSingularField(b, "oneof_index", nil)
case 10:
b = p.appendSingularField(b, "json_name", nil)
case 8:
b = p.appendSingularField(b, "options", (*SourcePath).appendFieldOptions)
case 17:
b = p.appendSingularField(b, "proto3_optional", nil)
}
return b
}
func (p *SourcePath) appendFileOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "java_package", nil)
case 8:
b = p.appendSingularField(b, "java_outer_classname", nil)
case 10:
b = p.appendSingularField(b, "java_multiple_files", nil)
case 20:
b = p.appendSingularField(b, "java_generate_equals_and_hash", nil)
case 27:
b = p.appendSingularField(b, "java_string_check_utf8", nil)
case 9:
b = p.appendSingularField(b, "optimize_for", nil)
case 11:
b = p.appendSingularField(b, "go_package", nil)
case 16:
b = p.appendSingularField(b, "cc_generic_services", nil)
case 17:
b = p.appendSingularField(b, "java_generic_services", nil)
case 18:
b = p.appendSingularField(b, "py_generic_services", nil)
case 42:
b = p.appendSingularField(b, "php_generic_services", nil)
case 23:
b = p.appendSingularField(b, "deprecated", nil)
case 31:
b = p.appendSingularField(b, "cc_enable_arenas", nil)
case 36:
b = p.appendSingularField(b, "objc_class_prefix", nil)
case 37:
b = p.appendSingularField(b, "csharp_namespace", nil)
case 39:
b = p.appendSingularField(b, "swift_prefix", nil)
case 40:
b = p.appendSingularField(b, "php_class_prefix", nil)
case 41:
b = p.appendSingularField(b, "php_namespace", nil)
case 44:
b = p.appendSingularField(b, "php_metadata_namespace", nil)
case 45:
b = p.appendSingularField(b, "ruby_package", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendSourceCodeInfo(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendRepeatedField(b, "location", (*SourcePath).appendSourceCodeInfo_Location)
}
return b
}
func (p *SourcePath) appendDescriptorProto_ExtensionRange(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "start", nil)
case 2:
b = p.appendSingularField(b, "end", nil)
case 3:
b = p.appendSingularField(b, "options", (*SourcePath).appendExtensionRangeOptions)
}
return b
}
func (p *SourcePath) appendOneofDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendSingularField(b, "options", (*SourcePath).appendOneofOptions)
}
return b
}
func (p *SourcePath) appendMessageOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "message_set_wire_format", nil)
case 2:
b = p.appendSingularField(b, "no_standard_descriptor_accessor", nil)
case 3:
b = p.appendSingularField(b, "deprecated", nil)
case 7:
b = p.appendSingularField(b, "map_entry", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendDescriptorProto_ReservedRange(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "start", nil)
case 2:
b = p.appendSingularField(b, "end", nil)
}
return b
}
func (p *SourcePath) appendEnumValueDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendSingularField(b, "number", nil)
case 3:
b = p.appendSingularField(b, "options", (*SourcePath).appendEnumValueOptions)
}
return b
}
func (p *SourcePath) appendEnumOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 2:
b = p.appendSingularField(b, "allow_alias", nil)
case 3:
b = p.appendSingularField(b, "deprecated", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendEnumDescriptorProto_EnumReservedRange(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "start", nil)
case 2:
b = p.appendSingularField(b, "end", nil)
}
return b
}
func (p *SourcePath) appendMethodDescriptorProto(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name", nil)
case 2:
b = p.appendSingularField(b, "input_type", nil)
case 3:
b = p.appendSingularField(b, "output_type", nil)
case 4:
b = p.appendSingularField(b, "options", (*SourcePath).appendMethodOptions)
case 5:
b = p.appendSingularField(b, "client_streaming", nil)
case 6:
b = p.appendSingularField(b, "server_streaming", nil)
}
return b
}
func (p *SourcePath) appendServiceOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 33:
b = p.appendSingularField(b, "deprecated", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendFieldOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "ctype", nil)
case 2:
b = p.appendSingularField(b, "packed", nil)
case 6:
b = p.appendSingularField(b, "jstype", nil)
case 5:
b = p.appendSingularField(b, "lazy", nil)
case 3:
b = p.appendSingularField(b, "deprecated", nil)
case 10:
b = p.appendSingularField(b, "weak", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendUninterpretedOption(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 2:
b = p.appendRepeatedField(b, "name", (*SourcePath).appendUninterpretedOption_NamePart)
case 3:
b = p.appendSingularField(b, "identifier_value", nil)
case 4:
b = p.appendSingularField(b, "positive_int_value", nil)
case 5:
b = p.appendSingularField(b, "negative_int_value", nil)
case 6:
b = p.appendSingularField(b, "double_value", nil)
case 7:
b = p.appendSingularField(b, "string_value", nil)
case 8:
b = p.appendSingularField(b, "aggregate_value", nil)
}
return b
}
func (p *SourcePath) appendSourceCodeInfo_Location(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendRepeatedField(b, "path", nil)
case 2:
b = p.appendRepeatedField(b, "span", nil)
case 3:
b = p.appendSingularField(b, "leading_comments", nil)
case 4:
b = p.appendSingularField(b, "trailing_comments", nil)
case 6:
b = p.appendRepeatedField(b, "leading_detached_comments", nil)
}
return b
}
func (p *SourcePath) appendExtensionRangeOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendOneofOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendEnumValueOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "deprecated", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendMethodOptions(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 33:
b = p.appendSingularField(b, "deprecated", nil)
case 34:
b = p.appendSingularField(b, "idempotency_level", nil)
case 999:
b = p.appendRepeatedField(b, "uninterpreted_option", (*SourcePath).appendUninterpretedOption)
}
return b
}
func (p *SourcePath) appendUninterpretedOption_NamePart(b []byte) []byte {
if len(*p) == 0 {
return b
}
switch (*p)[0] {
case 1:
b = p.appendSingularField(b, "name_part", nil)
case 2:
b = p.appendSingularField(b, "is_extension", nil)
}
return b
}

View File

@ -232,11 +232,15 @@ type MessageDescriptor interface {
type isMessageDescriptor interface{ ProtoType(MessageDescriptor) } type isMessageDescriptor interface{ ProtoType(MessageDescriptor) }
// MessageType encapsulates a MessageDescriptor with a concrete Go implementation. // MessageType encapsulates a MessageDescriptor with a concrete Go implementation.
// It is recommended that implementations of this interface also implement the
// MessageFieldTypes interface.
type MessageType interface { type MessageType interface {
// New returns a newly allocated empty message. // New returns a newly allocated empty message.
// It may return nil for synthetic messages representing a map entry.
New() Message New() Message
// Zero returns an empty, read-only message. // Zero returns an empty, read-only message.
// It may return nil for synthetic messages representing a map entry.
Zero() Message Zero() Message
// Descriptor returns the message descriptor. // Descriptor returns the message descriptor.
@ -245,6 +249,26 @@ type MessageType interface {
Descriptor() MessageDescriptor Descriptor() MessageDescriptor
} }
// MessageFieldTypes extends a MessageType by providing type information
// regarding enums and messages referenced by the message fields.
type MessageFieldTypes interface {
MessageType
// Enum returns the EnumType for the ith field in Descriptor.Fields.
// It returns nil if the ith field is not an enum kind.
// It panics if out of bounds.
//
// Invariant: mt.Enum(i).Descriptor() == mt.Descriptor().Fields(i).Enum()
Enum(i int) EnumType
// Message returns the MessageType for the ith field in Descriptor.Fields.
// It returns nil if the ith field is not a message or group kind.
// It panics if out of bounds.
//
// Invariant: mt.Message(i).Descriptor() == mt.Descriptor().Fields(i).Message()
Message(i int) MessageType
}
// MessageDescriptors is a list of message declarations. // MessageDescriptors is a list of message declarations.
type MessageDescriptors interface { type MessageDescriptors interface {
// Len reports the number of messages. // Len reports the number of messages.
@ -279,8 +303,15 @@ type FieldDescriptor interface {
// JSONName reports the name used for JSON serialization. // JSONName reports the name used for JSON serialization.
// It is usually the camel-cased form of the field name. // It is usually the camel-cased form of the field name.
// Extension fields are represented by the full name surrounded by brackets.
JSONName() string JSONName() string
// TextName reports the name used for text serialization.
// It is usually the name of the field, except that groups use the name
// of the inlined message, and extension fields are represented by the
// full name surrounded by brackets.
TextName() string
// HasPresence reports whether the field distinguishes between unpopulated // HasPresence reports whether the field distinguishes between unpopulated
// and default values. // and default values.
HasPresence() bool HasPresence() bool
@ -371,6 +402,9 @@ type FieldDescriptors interface {
// ByJSONName returns the FieldDescriptor for a field with s as the JSON name. // ByJSONName returns the FieldDescriptor for a field with s as the JSON name.
// It returns nil if not found. // It returns nil if not found.
ByJSONName(s string) FieldDescriptor ByJSONName(s string) FieldDescriptor
// ByTextName returns the FieldDescriptor for a field with s as the text name.
// It returns nil if not found.
ByTextName(s string) FieldDescriptor
// ByNumber returns the FieldDescriptor for a field numbered n. // ByNumber returns the FieldDescriptor for a field numbered n.
// It returns nil if not found. // It returns nil if not found.
ByNumber(n FieldNumber) FieldDescriptor ByNumber(n FieldNumber) FieldDescriptor

View File

@ -85,6 +85,8 @@ func ValueOf(v interface{}) Value {
return ValueOfEnum(v) return ValueOfEnum(v)
case Message, List, Map: case Message, List, Map:
return valueOfIface(v) return valueOfIface(v)
case ProtoMessage:
panic(fmt.Sprintf("invalid proto.Message(%T) type, expected a protoreflect.Message type", v))
default: default:
panic(fmt.Sprintf("invalid type: %T", v)) panic(fmt.Sprintf("invalid type: %T", v))
} }

View File

@ -17,24 +17,49 @@ package protoregistry
import ( import (
"fmt" "fmt"
"log" "os"
"strings" "strings"
"sync" "sync"
"google.golang.org/protobuf/internal/encoding/messageset"
"google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
) )
// conflictPolicy configures the policy for handling registration conflicts.
//
// It can be over-written at compile time with a linker-initialized variable:
// go build -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
//
// It can be over-written at program execution with an environment variable:
// GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn ./main
//
// Neither of the above are covered by the compatibility promise and
// may be removed in a future release of this module.
var conflictPolicy = "panic" // "panic" | "warn" | "ignore"
// ignoreConflict reports whether to ignore a registration conflict // ignoreConflict reports whether to ignore a registration conflict
// given the descriptor being registered and the error. // given the descriptor being registered and the error.
// It is a variable so that the behavior is easily overridden in another file. // It is a variable so that the behavior is easily overridden in another file.
var ignoreConflict = func(d protoreflect.Descriptor, err error) bool { var ignoreConflict = func(d protoreflect.Descriptor, err error) bool {
log.Printf(""+ const env = "GOLANG_PROTOBUF_REGISTRATION_CONFLICT"
"WARNING: %v\n"+ const faq = "https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict"
"A future release will panic on registration conflicts. See:\n"+ policy := conflictPolicy
"https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict\n"+ if v := os.Getenv(env); v != "" {
"\n", err) policy = v
return true }
switch policy {
case "panic":
panic(fmt.Sprintf("%v\nSee %v\n", err, faq))
case "warn":
fmt.Fprintf(os.Stderr, "WARNING: %v\nSee %v\n\n", err, faq)
return true
case "ignore":
return true
default:
panic("invalid " + env + " value: " + os.Getenv(env))
}
} }
var globalMutex sync.RWMutex var globalMutex sync.RWMutex
@ -96,6 +121,7 @@ func (r *Files) RegisterFile(file protoreflect.FileDescriptor) error {
} }
path := file.Path() path := file.Path()
if prev := r.filesByPath[path]; prev != nil { if prev := r.filesByPath[path]; prev != nil {
r.checkGenProtoConflict(path)
err := errors.New("file %q is already registered", file.Path()) err := errors.New("file %q is already registered", file.Path())
err = amendErrorWithCaller(err, prev, file) err = amendErrorWithCaller(err, prev, file)
if r == GlobalFiles && ignoreConflict(file, err) { if r == GlobalFiles && ignoreConflict(file, err) {
@ -146,6 +172,47 @@ func (r *Files) RegisterFile(file protoreflect.FileDescriptor) error {
return nil return nil
} }
// Several well-known types were hosted in the google.golang.org/genproto module
// but were later moved to this module. To avoid a weak dependency on the
// genproto module (and its relatively large set of transitive dependencies),
// we rely on a registration conflict to determine whether the genproto version
// is too old (i.e., does not contain aliases to the new type declarations).
func (r *Files) checkGenProtoConflict(path string) {
if r != GlobalFiles {
return
}
var prevPath string
const prevModule = "google.golang.org/genproto"
const prevVersion = "cb27e3aa (May 26th, 2020)"
switch path {
case "google/protobuf/field_mask.proto":
prevPath = prevModule + "/protobuf/field_mask"
case "google/protobuf/api.proto":
prevPath = prevModule + "/protobuf/api"
case "google/protobuf/type.proto":
prevPath = prevModule + "/protobuf/ptype"
case "google/protobuf/source_context.proto":
prevPath = prevModule + "/protobuf/source_context"
default:
return
}
pkgName := strings.TrimSuffix(strings.TrimPrefix(path, "google/protobuf/"), ".proto")
pkgName = strings.Replace(pkgName, "_", "", -1) + "pb" // e.g., "field_mask" => "fieldmaskpb"
currPath := "google.golang.org/protobuf/types/known/" + pkgName
panic(fmt.Sprintf(""+
"duplicate registration of %q\n"+
"\n"+
"The generated definition for this file has moved:\n"+
"\tfrom: %q\n"+
"\tto: %q\n"+
"A dependency on the %q module must\n"+
"be at version %v or higher.\n"+
"\n"+
"Upgrade the dependency by running:\n"+
"\tgo get -u %v\n",
path, prevPath, currPath, prevModule, prevVersion, prevPath))
}
// FindDescriptorByName looks up a descriptor by the full name. // FindDescriptorByName looks up a descriptor by the full name.
// //
// This returns (nil, NotFound) if not found. // This returns (nil, NotFound) if not found.
@ -528,13 +595,25 @@ func (r *Types) FindEnumByName(enum protoreflect.FullName) (protoreflect.EnumTyp
return nil, NotFound return nil, NotFound
} }
// FindMessageByName looks up a message by its full name. // FindMessageByName looks up a message by its full name,
// E.g., "google.protobuf.Any" // e.g. "google.protobuf.Any".
// //
// This return (nil, NotFound) if not found. // This returns (nil, NotFound) if not found.
func (r *Types) FindMessageByName(message protoreflect.FullName) (protoreflect.MessageType, error) { func (r *Types) FindMessageByName(message protoreflect.FullName) (protoreflect.MessageType, error) {
// The full name by itself is a valid URL. if r == nil {
return r.FindMessageByURL(string(message)) return nil, NotFound
}
if r == GlobalTypes {
globalMutex.RLock()
defer globalMutex.RUnlock()
}
if v := r.typesByName[message]; v != nil {
if mt, _ := v.(protoreflect.MessageType); mt != nil {
return mt, nil
}
return nil, errors.New("found wrong type: got %v, want message", typeName(v))
}
return nil, NotFound
} }
// FindMessageByURL looks up a message by a URL identifier. // FindMessageByURL looks up a message by a URL identifier.
@ -542,6 +621,8 @@ func (r *Types) FindMessageByName(message protoreflect.FullName) (protoreflect.M
// //
// This returns (nil, NotFound) if not found. // This returns (nil, NotFound) if not found.
func (r *Types) FindMessageByURL(url string) (protoreflect.MessageType, error) { func (r *Types) FindMessageByURL(url string) (protoreflect.MessageType, error) {
// This function is similar to FindMessageByName but
// truncates anything before and including '/' in the URL.
if r == nil { if r == nil {
return nil, NotFound return nil, NotFound
} }
@ -581,6 +662,26 @@ func (r *Types) FindExtensionByName(field protoreflect.FullName) (protoreflect.E
if xt, _ := v.(protoreflect.ExtensionType); xt != nil { if xt, _ := v.(protoreflect.ExtensionType); xt != nil {
return xt, nil return xt, nil
} }
// MessageSet extensions are special in that the name of the extension
// is the name of the message type used to extend the MessageSet.
// This naming scheme is used by text and JSON serialization.
//
// This feature is protected by the ProtoLegacy flag since MessageSets
// are a proto1 feature that is long deprecated.
if flags.ProtoLegacy {
if _, ok := v.(protoreflect.MessageType); ok {
field := field.Append(messageset.ExtensionName)
if v := r.typesByName[field]; v != nil {
if xt, _ := v.(protoreflect.ExtensionType); xt != nil {
if messageset.IsMessageSetExtension(xt.TypeDescriptor()) {
return xt, nil
}
}
}
}
}
return nil, errors.New("found wrong type: got %v, want extension", typeName(v)) return nil, errors.New("found wrong type: got %v, want extension", typeName(v))
} }
return nil, NotFound return nil, NotFound

File diff suppressed because it is too large Load Diff

View File

@ -31,12 +31,100 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// source: google/protobuf/any.proto // source: google/protobuf/any.proto
// Package anypb contains generated types for google/protobuf/any.proto.
//
// The Any message is a dynamic representation of any other message value.
// It is functionally a tuple of the full name of the remote message type and
// the serialized bytes of the remote message value.
//
//
// Constructing an Any
//
// An Any message containing another message value is constructed using New:
//
// any, err := anypb.New(m)
// if err != nil {
// ... // handle error
// }
// ... // make use of any
//
//
// Unmarshaling an Any
//
// With a populated Any message, the underlying message can be serialized into
// a remote concrete message value in a few ways.
//
// If the exact concrete type is known, then a new (or pre-existing) instance
// of that message can be passed to the UnmarshalTo method:
//
// m := new(foopb.MyMessage)
// if err := any.UnmarshalTo(m); err != nil {
// ... // handle error
// }
// ... // make use of m
//
// If the exact concrete type is not known, then the UnmarshalNew method can be
// used to unmarshal the contents into a new instance of the remote message type:
//
// m, err := any.UnmarshalNew()
// if err != nil {
// ... // handle error
// }
// ... // make use of m
//
// UnmarshalNew uses the global type registry to resolve the message type and
// construct a new instance of that message to unmarshal into. In order for a
// message type to appear in the global registry, the Go type representing that
// protobuf message type must be linked into the Go binary. For messages
// generated by protoc-gen-go, this is achieved through an import of the
// generated Go package representing a .proto file.
//
// A common pattern with UnmarshalNew is to use a type switch with the resulting
// proto.Message value:
//
// switch m := m.(type) {
// case *foopb.MyMessage:
// ... // make use of m as a *foopb.MyMessage
// case *barpb.OtherMessage:
// ... // make use of m as a *barpb.OtherMessage
// case *bazpb.SomeMessage:
// ... // make use of m as a *bazpb.SomeMessage
// }
//
// This pattern ensures that the generated packages containing the message types
// listed in the case clauses are linked into the Go binary and therefore also
// registered in the global registry.
//
//
// Type checking an Any
//
// In order to type check whether an Any message represents some other message,
// then use the MessageIs method:
//
// if any.MessageIs((*foopb.MyMessage)(nil)) {
// ... // make use of any, knowing that it contains a foopb.MyMessage
// }
//
// The MessageIs method can also be used with an allocated instance of the target
// message type if the intention is to unmarshal into it if the type matches:
//
// m := new(foopb.MyMessage)
// if any.MessageIs(m) {
// if err := any.UnmarshalTo(m); err != nil {
// ... // handle error
// }
// ... // make use of m
// }
//
package anypb package anypb
import ( import (
proto "google.golang.org/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoregistry "google.golang.org/protobuf/reflect/protoregistry"
protoimpl "google.golang.org/protobuf/runtime/protoimpl" protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect" reflect "reflect"
strings "strings"
sync "sync" sync "sync"
) )
@ -78,10 +166,13 @@ import (
// Example 4: Pack and unpack a message in Go // Example 4: Pack and unpack a message in Go
// //
// foo := &pb.Foo{...} // foo := &pb.Foo{...}
// any, err := ptypes.MarshalAny(foo) // any, err := anypb.New(foo)
// if err != nil {
// ...
// }
// ... // ...
// foo := &pb.Foo{} // foo := &pb.Foo{}
// if err := ptypes.UnmarshalAny(any, foo); err != nil { // if err := any.UnmarshalTo(foo); err != nil {
// ... // ...
// } // }
// //
@ -158,6 +249,125 @@ type Any struct {
Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
} }
// New marshals src into a new Any instance.
func New(src proto.Message) (*Any, error) {
dst := new(Any)
if err := dst.MarshalFrom(src); err != nil {
return nil, err
}
return dst, nil
}
// MarshalFrom marshals src into dst as the underlying message
// using the provided marshal options.
//
// If no options are specified, call dst.MarshalFrom instead.
func MarshalFrom(dst *Any, src proto.Message, opts proto.MarshalOptions) error {
const urlPrefix = "type.googleapis.com/"
if src == nil {
return protoimpl.X.NewError("invalid nil source message")
}
b, err := opts.Marshal(src)
if err != nil {
return err
}
dst.TypeUrl = urlPrefix + string(src.ProtoReflect().Descriptor().FullName())
dst.Value = b
return nil
}
// UnmarshalTo unmarshals the underlying message from src into dst
// using the provided unmarshal options.
// It reports an error if dst is not of the right message type.
//
// If no options are specified, call src.UnmarshalTo instead.
func UnmarshalTo(src *Any, dst proto.Message, opts proto.UnmarshalOptions) error {
if src == nil {
return protoimpl.X.NewError("invalid nil source message")
}
if !src.MessageIs(dst) {
got := dst.ProtoReflect().Descriptor().FullName()
want := src.MessageName()
return protoimpl.X.NewError("mismatched message type: got %q, want %q", got, want)
}
return opts.Unmarshal(src.GetValue(), dst)
}
// UnmarshalNew unmarshals the underlying message from src into dst,
// which is newly created message using a type resolved from the type URL.
// The message type is resolved according to opt.Resolver,
// which should implement protoregistry.MessageTypeResolver.
// It reports an error if the underlying message type could not be resolved.
//
// If no options are specified, call src.UnmarshalNew instead.
func UnmarshalNew(src *Any, opts proto.UnmarshalOptions) (dst proto.Message, err error) {
if src.GetTypeUrl() == "" {
return nil, protoimpl.X.NewError("invalid empty type URL")
}
if opts.Resolver == nil {
opts.Resolver = protoregistry.GlobalTypes
}
r, ok := opts.Resolver.(protoregistry.MessageTypeResolver)
if !ok {
return nil, protoregistry.NotFound
}
mt, err := r.FindMessageByURL(src.GetTypeUrl())
if err != nil {
if err == protoregistry.NotFound {
return nil, err
}
return nil, protoimpl.X.NewError("could not resolve %q: %v", src.GetTypeUrl(), err)
}
dst = mt.New().Interface()
return dst, opts.Unmarshal(src.GetValue(), dst)
}
// MessageIs reports whether the underlying message is of the same type as m.
func (x *Any) MessageIs(m proto.Message) bool {
if m == nil {
return false
}
url := x.GetTypeUrl()
name := string(m.ProtoReflect().Descriptor().FullName())
if !strings.HasSuffix(url, name) {
return false
}
return len(url) == len(name) || url[len(url)-len(name)-1] == '/'
}
// MessageName reports the full name of the underlying message,
// returning an empty string if invalid.
func (x *Any) MessageName() protoreflect.FullName {
url := x.GetTypeUrl()
name := protoreflect.FullName(url)
if i := strings.LastIndexByte(url, '/'); i >= 0 {
name = name[i+len("/"):]
}
if !name.IsValid() {
return ""
}
return name
}
// MarshalFrom marshals m into x as the underlying message.
func (x *Any) MarshalFrom(m proto.Message) error {
return MarshalFrom(x, m, proto.MarshalOptions{})
}
// UnmarshalTo unmarshals the contents of the underlying message of x into m.
// It resets m before performing the unmarshal operation.
// It reports an error if m is not of the right message type.
func (x *Any) UnmarshalTo(m proto.Message) error {
return UnmarshalTo(x, m, proto.UnmarshalOptions{})
}
// UnmarshalNew unmarshals the contents of the underlying message of x into
// a newly allocated message of the specified type.
// It reports an error if the underlying message type could not be resolved.
func (x *Any) UnmarshalNew() (proto.Message, error) {
return UnmarshalNew(x, proto.UnmarshalOptions{})
}
func (x *Any) Reset() { func (x *Any) Reset() {
*x = Any{} *x = Any{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
@ -213,14 +423,15 @@ var file_google_protobuf_any_proto_rawDesc = []byte{
0x41, 0x6e, 0x79, 0x12, 0x19, 0x0a, 0x08, 0x74, 0x79, 0x70, 0x65, 0x5f, 0x75, 0x72, 0x6c, 0x18, 0x41, 0x6e, 0x79, 0x12, 0x19, 0x0a, 0x08, 0x74, 0x79, 0x70, 0x65, 0x5f, 0x75, 0x72, 0x6c, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x74, 0x79, 0x70, 0x65, 0x55, 0x72, 0x6c, 0x12, 0x14, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x74, 0x79, 0x70, 0x65, 0x55, 0x72, 0x6c, 0x12, 0x14,
0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76,
0x61, 0x6c, 0x75, 0x65, 0x42, 0x6f, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x76, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x08, 0x41, 0x6e, 0x79, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x08, 0x41, 0x6e, 0x79,
0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x75, 0x66, 0x2f, 0x70, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x61, 0x6e, 0x79, 0xa2, 0x02, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x2f,
0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x61, 0x6e, 0x79, 0x70, 0x62, 0xa2, 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65, 0x6c, 0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65,
0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x6c, 0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -31,13 +31,58 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// source: google/protobuf/duration.proto // source: google/protobuf/duration.proto
// Package durationpb contains generated types for google/protobuf/duration.proto.
//
// The Duration message represents a signed span of time.
//
//
// Conversion to a Go Duration
//
// The AsDuration method can be used to convert a Duration message to a
// standard Go time.Duration value:
//
// d := dur.AsDuration()
// ... // make use of d as a time.Duration
//
// Converting to a time.Duration is a common operation so that the extensive
// set of time-based operations provided by the time package can be leveraged.
// See https://golang.org/pkg/time for more information.
//
// The AsDuration method performs the conversion on a best-effort basis.
// Durations with denormal values (e.g., nanoseconds beyond -99999999 and
// +99999999, inclusive; or seconds and nanoseconds with opposite signs)
// are normalized during the conversion to a time.Duration. To manually check for
// invalid Duration per the documented limitations in duration.proto,
// additionally call the CheckValid method:
//
// if err := dur.CheckValid(); err != nil {
// ... // handle error
// }
//
// Note that the documented limitations in duration.proto does not protect a
// Duration from overflowing the representable range of a time.Duration in Go.
// The AsDuration method uses saturation arithmetic such that an overflow clamps
// the resulting value to the closest representable value (e.g., math.MaxInt64
// for positive overflow and math.MinInt64 for negative overflow).
//
//
// Conversion from a Go Duration
//
// The durationpb.New function can be used to construct a Duration message
// from a standard Go time.Duration value:
//
// dur := durationpb.New(d)
// ... // make use of d as a *durationpb.Duration
//
package durationpb package durationpb
import ( import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl" protoimpl "google.golang.org/protobuf/runtime/protoimpl"
math "math"
reflect "reflect" reflect "reflect"
sync "sync" sync "sync"
time "time"
) )
// A Duration represents a signed, fixed-length span of time represented // A Duration represents a signed, fixed-length span of time represented
@ -118,6 +163,91 @@ type Duration struct {
Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"` Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
} }
// New constructs a new Duration from the provided time.Duration.
func New(d time.Duration) *Duration {
nanos := d.Nanoseconds()
secs := nanos / 1e9
nanos -= secs * 1e9
return &Duration{Seconds: int64(secs), Nanos: int32(nanos)}
}
// AsDuration converts x to a time.Duration,
// returning the closest duration value in the event of overflow.
func (x *Duration) AsDuration() time.Duration {
secs := x.GetSeconds()
nanos := x.GetNanos()
d := time.Duration(secs) * time.Second
overflow := d/time.Second != time.Duration(secs)
d += time.Duration(nanos) * time.Nanosecond
overflow = overflow || (secs < 0 && nanos < 0 && d > 0)
overflow = overflow || (secs > 0 && nanos > 0 && d < 0)
if overflow {
switch {
case secs < 0:
return time.Duration(math.MinInt64)
case secs > 0:
return time.Duration(math.MaxInt64)
}
}
return d
}
// IsValid reports whether the duration is valid.
// It is equivalent to CheckValid == nil.
func (x *Duration) IsValid() bool {
return x.check() == 0
}
// CheckValid returns an error if the duration is invalid.
// In particular, it checks whether the value is within the range of
// -10000 years to +10000 years inclusive.
// An error is reported for a nil Duration.
func (x *Duration) CheckValid() error {
switch x.check() {
case invalidNil:
return protoimpl.X.NewError("invalid nil Duration")
case invalidUnderflow:
return protoimpl.X.NewError("duration (%v) exceeds -10000 years", x)
case invalidOverflow:
return protoimpl.X.NewError("duration (%v) exceeds +10000 years", x)
case invalidNanosRange:
return protoimpl.X.NewError("duration (%v) has out-of-range nanos", x)
case invalidNanosSign:
return protoimpl.X.NewError("duration (%v) has seconds and nanos with different signs", x)
default:
return nil
}
}
const (
_ = iota
invalidNil
invalidUnderflow
invalidOverflow
invalidNanosRange
invalidNanosSign
)
func (x *Duration) check() uint {
const absDuration = 315576000000 // 10000yr * 365.25day/yr * 24hr/day * 60min/hr * 60sec/min
secs := x.GetSeconds()
nanos := x.GetNanos()
switch {
case x == nil:
return invalidNil
case secs < -absDuration:
return invalidUnderflow
case secs > +absDuration:
return invalidOverflow
case nanos <= -1e9 || nanos >= +1e9:
return invalidNanosRange
case (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0):
return invalidNanosSign
default:
return 0
}
}
func (x *Duration) Reset() { func (x *Duration) Reset() {
*x = Duration{} *x = Duration{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
@ -173,16 +303,16 @@ var file_google_protobuf_duration_proto_rawDesc = []byte{
0x66, 0x22, 0x3a, 0x0a, 0x08, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x18, 0x0a, 0x66, 0x22, 0x3a, 0x0a, 0x08, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x18, 0x0a,
0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07,
0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73,
0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x42, 0x7c, 0x0a, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x42, 0x83, 0x01,
0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
0x6f, 0x62, 0x75, 0x66, 0x42, 0x0d, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x50, 0x72, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x0d, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x50,
0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x31, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x67,
0x6d, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x66, 0x2f, 0x70, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x75, 0x66, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x2f, 0x64,
0x6e, 0xf8, 0x01, 0x01, 0xa2, 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f, 0x6f, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x70, 0x62, 0xf8, 0x01, 0x01, 0xa2, 0x02, 0x03, 0x47,
0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65, 0x6c, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74,
0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65, 0x6c, 0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x54, 0x79,
0x74, 0x6f, 0x33, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -31,6 +31,48 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// source: google/protobuf/timestamp.proto // source: google/protobuf/timestamp.proto
// Package timestamppb contains generated types for google/protobuf/timestamp.proto.
//
// The Timestamp message represents a timestamp,
// an instant in time since the Unix epoch (January 1st, 1970).
//
//
// Conversion to a Go Time
//
// The AsTime method can be used to convert a Timestamp message to a
// standard Go time.Time value in UTC:
//
// t := ts.AsTime()
// ... // make use of t as a time.Time
//
// Converting to a time.Time is a common operation so that the extensive
// set of time-based operations provided by the time package can be leveraged.
// See https://golang.org/pkg/time for more information.
//
// The AsTime method performs the conversion on a best-effort basis. Timestamps
// with denormal values (e.g., nanoseconds beyond 0 and 99999999, inclusive)
// are normalized during the conversion to a time.Time. To manually check for
// invalid Timestamps per the documented limitations in timestamp.proto,
// additionally call the CheckValid method:
//
// if err := ts.CheckValid(); err != nil {
// ... // handle error
// }
//
//
// Conversion from a Go Time
//
// The timestamppb.New function can be used to construct a Timestamp message
// from a standard Go time.Time value:
//
// ts := timestamppb.New(t)
// ... // make use of ts as a *timestamppb.Timestamp
//
// In order to construct a Timestamp representing the current time, use Now:
//
// ts := timestamppb.Now()
// ... // make use of ts as a *timestamppb.Timestamp
//
package timestamppb package timestamppb
import ( import (
@ -38,6 +80,7 @@ import (
protoimpl "google.golang.org/protobuf/runtime/protoimpl" protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect" reflect "reflect"
sync "sync" sync "sync"
time "time"
) )
// A Timestamp represents a point in time independent of any time zone or local // A Timestamp represents a point in time independent of any time zone or local
@ -91,7 +134,16 @@ import (
// .setNanos((int) ((millis % 1000) * 1000000)).build(); // .setNanos((int) ((millis % 1000) * 1000000)).build();
// //
// //
// Example 5: Compute Timestamp from current time in Python. // Example 5: Compute Timestamp from Java `Instant.now()`.
//
// Instant now = Instant.now();
//
// Timestamp timestamp =
// Timestamp.newBuilder().setSeconds(now.getEpochSecond())
// .setNanos(now.getNano()).build();
//
//
// Example 6: Compute Timestamp from current time in Python.
// //
// timestamp = Timestamp() // timestamp = Timestamp()
// timestamp.GetCurrentTime() // timestamp.GetCurrentTime()
@ -140,6 +192,73 @@ type Timestamp struct {
Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"` Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
} }
// Now constructs a new Timestamp from the current time.
func Now() *Timestamp {
return New(time.Now())
}
// New constructs a new Timestamp from the provided time.Time.
func New(t time.Time) *Timestamp {
return &Timestamp{Seconds: int64(t.Unix()), Nanos: int32(t.Nanosecond())}
}
// AsTime converts x to a time.Time.
func (x *Timestamp) AsTime() time.Time {
return time.Unix(int64(x.GetSeconds()), int64(x.GetNanos())).UTC()
}
// IsValid reports whether the timestamp is valid.
// It is equivalent to CheckValid == nil.
func (x *Timestamp) IsValid() bool {
return x.check() == 0
}
// CheckValid returns an error if the timestamp is invalid.
// In particular, it checks whether the value represents a date that is
// in the range of 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.
// An error is reported for a nil Timestamp.
func (x *Timestamp) CheckValid() error {
switch x.check() {
case invalidNil:
return protoimpl.X.NewError("invalid nil Timestamp")
case invalidUnderflow:
return protoimpl.X.NewError("timestamp (%v) before 0001-01-01", x)
case invalidOverflow:
return protoimpl.X.NewError("timestamp (%v) after 9999-12-31", x)
case invalidNanos:
return protoimpl.X.NewError("timestamp (%v) has out-of-range nanos", x)
default:
return nil
}
}
const (
_ = iota
invalidNil
invalidUnderflow
invalidOverflow
invalidNanos
)
func (x *Timestamp) check() uint {
const minTimestamp = -62135596800 // Seconds between 1970-01-01T00:00:00Z and 0001-01-01T00:00:00Z, inclusive
const maxTimestamp = +253402300799 // Seconds between 1970-01-01T00:00:00Z and 9999-12-31T23:59:59Z, inclusive
secs := x.GetSeconds()
nanos := x.GetNanos()
switch {
case x == nil:
return invalidNil
case secs < minTimestamp:
return invalidUnderflow
case secs > maxTimestamp:
return invalidOverflow
case nanos < 0 || nanos >= 1e9:
return invalidNanos
default:
return 0
}
}
func (x *Timestamp) Reset() { func (x *Timestamp) Reset() {
*x = Timestamp{} *x = Timestamp{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
@ -196,15 +315,15 @@ var file_google_protobuf_timestamp_proto_rawDesc = []byte{
0x18, 0x0a, 0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x18, 0x0a, 0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03,
0x52, 0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x61, 0x6e, 0x52, 0x07, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x61, 0x6e,
0x6f, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x42, 0x6f, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x42,
0x7e, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x85, 0x01, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x0e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x0e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61,
0x70, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x6d, 0x70, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x32, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x65, 0x2e, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x70, 0x72, 0x6f,
0x6f, 0x62, 0x75, 0x66, 0x2f, 0x70, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x6b, 0x6e, 0x6f, 0x77,
0x73, 0x74, 0x61, 0x6d, 0x70, 0xf8, 0x01, 0x01, 0xa2, 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x6e, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x70, 0x62, 0xf8, 0x01, 0x01,
0x1e, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0xa2, 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
0x2e, 0x57, 0x65, 0x6c, 0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65, 0x6c, 0x6c, 0x4b, 0x6e, 0x6f,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

Some files were not shown because too many files have changed in this diff Show More