internal/fieldnum: generate field numbers for the google.protobuf package
Generate field numbers for the well-known types,
so that encoding/jsonpb can benefit from them as well.
This CL fixes internal/cmd/generate-protos, which was silently failing
because the modulePath was not properly initialized. We fix this by
moving it to the start of the init function.
Change-Id: I87637176f29218cffa512b4baa49f39dae924061
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168497
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/protogen/protogen.go b/protogen/protogen.go
index 1aade1b..9dd48fc 100644
--- a/protogen/protogen.go
+++ b/protogen/protogen.go
@@ -29,7 +29,7 @@
"strings"
"github.com/golang/protobuf/v2/encoding/textpb"
- "github.com/golang/protobuf/v2/internal/descfield"
+ "github.com/golang/protobuf/v2/internal/fieldnum"
"github.com/golang/protobuf/v2/internal/scalar"
"github.com/golang/protobuf/v2/proto"
"github.com/golang/protobuf/v2/reflect/protodesc"
@@ -520,9 +520,9 @@
func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message {
var loc Location
if parent != nil {
- loc = parent.Location.appendPath(descfield.DescriptorProto_NestedType, int32(desc.Index()))
+ loc = parent.Location.appendPath(fieldnum.DescriptorProto_NestedType, int32(desc.Index()))
} else {
- loc = f.location(descfield.FileDescriptorProto_MessageType, int32(desc.Index()))
+ loc = f.location(fieldnum.FileDescriptorProto_MessageType, int32(desc.Index()))
}
message := &Message{
Desc: desc,
@@ -638,11 +638,11 @@
var loc Location
switch {
case desc.ExtendedType() != nil && message == nil:
- loc = f.location(descfield.FileDescriptorProto_Extension, int32(desc.Index()))
+ loc = f.location(fieldnum.FileDescriptorProto_Extension, int32(desc.Index()))
case desc.ExtendedType() != nil && message != nil:
- loc = message.Location.appendPath(descfield.DescriptorProto_Extension, int32(desc.Index()))
+ loc = message.Location.appendPath(fieldnum.DescriptorProto_Extension, int32(desc.Index()))
default:
- loc = message.Location.appendPath(descfield.DescriptorProto_Field, int32(desc.Index()))
+ loc = message.Location.appendPath(fieldnum.DescriptorProto_Field, int32(desc.Index()))
}
field := &Field{
Desc: desc,
@@ -703,7 +703,7 @@
Desc: desc,
ParentMessage: message,
GoName: camelCase(string(desc.Name())),
- Location: message.Location.appendPath(descfield.DescriptorProto_OneofDecl, int32(desc.Index())),
+ Location: message.Location.appendPath(fieldnum.DescriptorProto_OneofDecl, int32(desc.Index())),
}
}
@@ -725,9 +725,9 @@
func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum {
var loc Location
if parent != nil {
- loc = parent.Location.appendPath(descfield.DescriptorProto_EnumType, int32(desc.Index()))
+ loc = parent.Location.appendPath(fieldnum.DescriptorProto_EnumType, int32(desc.Index()))
} else {
- loc = f.location(descfield.FileDescriptorProto_EnumType, int32(desc.Index()))
+ loc = f.location(fieldnum.FileDescriptorProto_EnumType, int32(desc.Index()))
}
enum := &Enum{
Desc: desc,
@@ -762,7 +762,7 @@
return &EnumValue{
Desc: desc,
GoIdent: f.GoImportPath.Ident(name),
- Location: enum.Location.appendPath(descfield.EnumDescriptorProto_Value, int32(desc.Index())),
+ Location: enum.Location.appendPath(fieldnum.EnumDescriptorProto_Value, int32(desc.Index())),
}
}
@@ -779,7 +779,7 @@
service := &Service{
Desc: desc,
GoName: camelCase(string(desc.Name())),
- Location: f.location(descfield.FileDescriptorProto_Service, int32(desc.Index())),
+ Location: f.location(fieldnum.FileDescriptorProto_Service, int32(desc.Index())),
}
for i, mdescs := 0, desc.Methods(); i < mdescs.Len(); i++ {
service.Methods = append(service.Methods, newMethod(gen, f, service, mdescs.Get(i)))
@@ -803,7 +803,7 @@
Desc: desc,
GoName: camelCase(string(desc.Name())),
ParentService: service,
- Location: service.Location.appendPath(descfield.ServiceDescriptorProto_Method, int32(desc.Index())),
+ Location: service.Location.appendPath(fieldnum.ServiceDescriptorProto_Method, int32(desc.Index())),
}
return method
}