diff --git a/cmd/protoc-gen-go/annotation_test.go b/cmd/protoc-gen-go/annotation_test.go index 7463215b..8f6adb47 100644 --- a/cmd/protoc-gen-go/annotation_test.go +++ b/cmd/protoc-gen-go/annotation_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/v2/internal/descfield" "github.com/golang/protobuf/v2/internal/scalar" descriptorpb "github.com/golang/protobuf/v2/types/descriptor" @@ -35,19 +36,19 @@ func TestAnnotations(t *testing.T) { path []int32 }{{ "type ", "AnnotationsTestEnum", " int32", - []int32{5 /* enum_type */, 0}, + []int32{descfield.FileDescriptorProto_EnumType, 0}, }, { "\t", "AnnotationsTestEnum_ANNOTATIONS_TEST_ENUM_VALUE", " AnnotationsTestEnum = 0", - []int32{5 /* enum_type */, 0, 2 /* value */, 0}, + []int32{descfield.FileDescriptorProto_EnumType, 0, descfield.EnumDescriptorProto_Value, 0}, }, { "type ", "AnnotationsTestMessage", " struct {", - []int32{4 /* message_type */, 0}, + []int32{descfield.FileDescriptorProto_MessageType, 0}, }, { "\t", "AnnotationsTestField", " ", - []int32{4 /* message_type */, 0, 2 /* field */, 0}, + []int32{descfield.FileDescriptorProto_MessageType, 0, descfield.DescriptorProto_Field, 0}, }, { "func (m *AnnotationsTestMessage) ", "GetAnnotationsTestField", "() string {", - []int32{4 /* message_type */, 0, 2 /* field */, 0}, + []int32{descfield.FileDescriptorProto_MessageType, 0, descfield.DescriptorProto_Field, 0}, }} { s := want.prefix + want.text + want.suffix pos := bytes.Index(sourceFile, []byte(s)) diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index 4748f677..c1244678 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -18,6 +18,7 @@ import ( "unicode/utf8" "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/v2/internal/descfield" "github.com/golang/protobuf/v2/internal/encoding/tag" "github.com/golang/protobuf/v2/protogen" "github.com/golang/protobuf/v2/reflect/protoreflect" @@ -118,10 +119,9 @@ func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated g.P("// source: ", f.Desc.Path()) } g.P() - const filePackageField = 2 // FileDescriptorProto.package g.PrintLeadingComments(protogen.Location{ SourceFile: f.Proto.GetName(), - Path: []int32{filePackageField}, + Path: []int32{descfield.FileDescriptorProto_Package}, }) g.P() g.P("package ", f.GoPackageName) diff --git a/internal/cmd/generate-protos/main.go b/internal/cmd/generate-protos/main.go index 86983467..cf7c688d 100644 --- a/internal/cmd/generate-protos/main.go +++ b/internal/cmd/generate-protos/main.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "os" "os/exec" + "path" "path/filepath" "regexp" "strings" @@ -19,6 +20,7 @@ import ( gengogrpc "github.com/golang/protobuf/v2/cmd/protoc-gen-go-grpc/internal_gengogrpc" gengo "github.com/golang/protobuf/v2/cmd/protoc-gen-go/internal_gengo" "github.com/golang/protobuf/v2/protogen" + "github.com/golang/protobuf/v2/reflect/protoreflect" ) func init() { @@ -31,6 +33,7 @@ func init() { if file.Generate { gengo.GenerateFile(gen, file) gengogrpc.GenerateFile(gen, file) + generateDescriptorFields(gen, file) } } return nil @@ -43,6 +46,15 @@ var ( run bool protoRoot string repoRoot string + + generatedPreamble = []string{ + "// 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.", + "", + } ) func main() { @@ -152,6 +164,44 @@ func protoc(args ...string) { check(err) } +// generateDescriptorFields generates an internal package for descriptor.proto. +func generateDescriptorFields(gen *protogen.Plugin, file *protogen.File) { + if file.Desc.Path() != "google/protobuf/descriptor.proto" { + return + } + + const importPath = "github.com/golang/protobuf/v2/internal/descfield" + g := gen.NewGeneratedFile(importPath+"/field_gen.go", importPath) + for _, s := range generatedPreamble { + g.P(s) + } + g.P("// Package descfield contains constants for field numbers in descriptor.proto.") + g.P("package ", path.Base(importPath)) + g.P("") + + var processMessages func([]*protogen.Message) + processMessages = func(messages []*protogen.Message) { + for _, message := range messages { + g.P("// Field numbers for ", message.Desc.FullName(), ".") + g.P("const (") + for _, field := range message.Fields { + fd := field.Desc + typeName := fd.Kind().String() + switch fd.Kind() { + case protoreflect.EnumKind: + typeName = string(fd.EnumType().FullName()) + case protoreflect.MessageKind, protoreflect.GroupKind: + typeName = string(fd.MessageType().FullName()) + } + g.P(message.GoIdent.GoName, "_", field.GoName, "=", fd.Number(), "// ", fd.Cardinality(), " ", typeName) + } + g.P(")") + processMessages(message.Messages) + } + } + processMessages(file.Messages) +} + func syncOutput(dstDir, srcDir string) { filepath.Walk(srcDir, func(srcPath string, _ os.FileInfo, _ error) error { if !strings.HasSuffix(srcPath, ".go") && !strings.HasSuffix(srcPath, ".meta") { diff --git a/internal/descfield/field_gen.go b/internal/descfield/field_gen.go new file mode 100644 index 00000000..0bc9f591 --- /dev/null +++ b/internal/descfield/field_gen.go @@ -0,0 +1,240 @@ +// 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 descfield contains constants for field numbers in descriptor.proto. +package descfield + +// 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 +) + +// 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 +) diff --git a/internal/fileinit/desc_init.go b/internal/fileinit/desc_init.go index 4955f4d9..bd706fc0 100644 --- a/internal/fileinit/desc_init.go +++ b/internal/fileinit/desc_init.go @@ -5,6 +5,7 @@ package fileinit import ( + descfield "github.com/golang/protobuf/v2/internal/descfield" wire "github.com/golang/protobuf/v2/internal/encoding/wire" pref "github.com/golang/protobuf/v2/reflect/protoreflect" ) @@ -88,36 +89,36 @@ func (fd *fileDesc) unmarshalSeed(b []byte) { v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case fileDesc_Name: + case descfield.FileDescriptorProto_Name: fd.path = nb.MakeString(v) - case fileDesc_Package: + case descfield.FileDescriptorProto_Package: fd.protoPackage = pref.FullName(nb.MakeString(v)) - case fileDesc_Enums: - if prevField != fileDesc_Enums { + case descfield.FileDescriptorProto_EnumType: + if prevField != descfield.FileDescriptorProto_EnumType { if numEnums > 0 { panic("non-contiguous repeated field") } posEnums = len(b0) - len(b) - n - m } numEnums++ - case fileDesc_Messages: - if prevField != fileDesc_Messages { + case descfield.FileDescriptorProto_MessageType: + if prevField != descfield.FileDescriptorProto_MessageType { if numMessages > 0 { panic("non-contiguous repeated field") } posMessages = len(b0) - len(b) - n - m } numMessages++ - case fileDesc_Extensions: - if prevField != fileDesc_Extensions { + case descfield.FileDescriptorProto_Extension: + if prevField != descfield.FileDescriptorProto_Extension { if numExtensions > 0 { panic("non-contiguous repeated field") } posExtensions = len(b0) - len(b) - n - m } numExtensions++ - case fileDesc_Services: - if prevField != fileDesc_Services { + case descfield.FileDescriptorProto_Service: + if prevField != descfield.FileDescriptorProto_Service { if numServices > 0 { panic("non-contiguous repeated field") } @@ -199,7 +200,7 @@ func (ed *enumDesc) unmarshalSeed(b []byte, nb *nameBuilder, pf *fileDesc, pd pr v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case enumDesc_Name: + case descfield.EnumDescriptorProto_Name: ed.fullName = nb.AppendFullName(pd.FullName(), v) } default: @@ -226,26 +227,26 @@ func (md *messageDesc) unmarshalSeed(b []byte, nb *nameBuilder, pf *fileDesc, pd v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case messageDesc_Name: + case descfield.DescriptorProto_Name: md.fullName = nb.AppendFullName(pd.FullName(), v) - case messageDesc_Enums: - if prevField != messageDesc_Enums { + case descfield.DescriptorProto_EnumType: + if prevField != descfield.DescriptorProto_EnumType { if numEnums > 0 { panic("non-contiguous repeated field") } posEnums = len(b0) - len(b) - n - m } numEnums++ - case messageDesc_Messages: - if prevField != messageDesc_Messages { + case descfield.DescriptorProto_NestedType: + if prevField != descfield.DescriptorProto_NestedType { if numMessages > 0 { panic("non-contiguous repeated field") } posMessages = len(b0) - len(b) - n - m } numMessages++ - case messageDesc_Extensions: - if prevField != messageDesc_Extensions { + case descfield.DescriptorProto_Extension: + if prevField != descfield.DescriptorProto_Extension { if numExtensions > 0 { panic("non-contiguous repeated field") } @@ -315,14 +316,14 @@ func (xd *extensionDesc) unmarshalSeed(b []byte, nb *nameBuilder, pf *fileDesc, v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case fieldDesc_Number: + case descfield.FieldDescriptorProto_Number: xd.number = pref.FieldNumber(v) } case wire.BytesType: v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case fieldDesc_Name: + case descfield.FieldDescriptorProto_Name: xd.fullName = nb.AppendFullName(pd.FullName(), v) } default: @@ -345,7 +346,7 @@ func (sd *serviceDesc) unmarshalSeed(b []byte, nb *nameBuilder, pf *fileDesc, pd v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case serviceDesc_Name: + case descfield.ServiceDescriptorProto_Name: sd.fullName = nb.AppendFullName(pd.FullName(), v) } default: diff --git a/internal/fileinit/desc_lazy.go b/internal/fileinit/desc_lazy.go index fc2235c6..2f7d3caa 100644 --- a/internal/fileinit/desc_lazy.go +++ b/internal/fileinit/desc_lazy.go @@ -9,6 +9,7 @@ import ( "fmt" "reflect" + descfield "github.com/golang/protobuf/v2/internal/descfield" defval "github.com/golang/protobuf/v2/internal/encoding/defval" wire "github.com/golang/protobuf/v2/internal/encoding/wire" pimpl "github.com/golang/protobuf/v2/internal/impl" @@ -344,16 +345,16 @@ func (fd *fileDesc) unmarshalFull(b []byte) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case fileDesc_PublicImports: + case descfield.FileDescriptorProto_PublicDependency: fd.lazy.imports[v].IsPublic = true - case fileDesc_WeakImports: + case descfield.FileDescriptorProto_WeakDependency: fd.lazy.imports[v].IsWeak = true } case wire.BytesType: v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case fileDesc_Syntax: + case descfield.FileDescriptorProto_Syntax: hasSyntax = true switch string(v) { case "proto2": @@ -363,23 +364,23 @@ func (fd *fileDesc) unmarshalFull(b []byte) { default: panic("invalid syntax") } - case fileDesc_Imports: + case descfield.FileDescriptorProto_Dependency: fd.lazy.imports = append(fd.lazy.imports, pref.FileImport{ FileDescriptor: ptype.PlaceholderFile(nb.MakeString(v), ""), }) - case fileDesc_Enums: + case descfield.FileDescriptorProto_EnumType: fd.enums.list[enumIdx].unmarshalFull(v, nb) enumIdx++ - case fileDesc_Messages: + case descfield.FileDescriptorProto_MessageType: fd.messages.list[messageIdx].unmarshalFull(v, nb) messageIdx++ - case fileDesc_Extensions: + case descfield.FileDescriptorProto_Extension: fd.extensions.list[extensionIdx].unmarshalFull(v, nb) extensionIdx++ - case fileDesc_Services: + case descfield.FileDescriptorProto_Service: fd.services.list[serviceIdx].unmarshalFull(v, nb) serviceIdx++ - case fileDesc_Options: + case descfield.FileDescriptorProto_Options: fd.lazy.options = append(fd.lazy.options, v...) } default: @@ -405,13 +406,13 @@ func (ed *enumDesc) unmarshalFull(b []byte, nb *nameBuilder) { v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case enumDesc_Values: + case descfield.EnumDescriptorProto_Value: rawValues = append(rawValues, v) - case enumDesc_ReservedNames: + case descfield.EnumDescriptorProto_ReservedName: ed.lazy.resvNames.list = append(ed.lazy.resvNames.list, pref.Name(nb.MakeString(v))) - case enumDesc_ReservedRanges: + case descfield.EnumDescriptorProto_ReservedRange: ed.lazy.resvRanges.list = append(ed.lazy.resvRanges.list, unmarshalEnumReservedRange(v)) - case enumDesc_Options: + case descfield.EnumDescriptorProto_Options: ed.lazy.options = append(ed.lazy.options, v...) } default: @@ -439,9 +440,9 @@ func unmarshalEnumReservedRange(b []byte) (r [2]pref.EnumNumber) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case enumReservedRange_Start: + case descfield.EnumDescriptorProto_EnumReservedRange_Start: r[0] = pref.EnumNumber(v) - case enumReservedRange_End: + case descfield.EnumDescriptorProto_EnumReservedRange_End: r[1] = pref.EnumNumber(v) } default: @@ -465,16 +466,16 @@ func (vd *enumValueDesc) unmarshalFull(b []byte, nb *nameBuilder, pf *fileDesc, v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case enumValueDesc_Number: + case descfield.EnumValueDescriptorProto_Number: vd.number = pref.EnumNumber(v) } case wire.BytesType: v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case enumValueDesc_Name: + case descfield.EnumValueDescriptorProto_Name: vd.fullName = nb.AppendFullName(pd.FullName(), v) - case enumValueDesc_Options: + case descfield.EnumValueDescriptorProto_Options: vd.options = append(vd.options, v...) } default: @@ -498,28 +499,28 @@ func (md *messageDesc) unmarshalFull(b []byte, nb *nameBuilder) { v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case messageDesc_Fields: + case descfield.DescriptorProto_Field: rawFields = append(rawFields, v) - case messageDesc_Oneofs: + case descfield.DescriptorProto_OneofDecl: rawOneofs = append(rawOneofs, v) - case messageDesc_ReservedNames: + case descfield.DescriptorProto_ReservedName: md.lazy.resvNames.list = append(md.lazy.resvNames.list, pref.Name(nb.MakeString(v))) - case messageDesc_ReservedRanges: + case descfield.DescriptorProto_ReservedRange: md.lazy.resvRanges.list = append(md.lazy.resvRanges.list, unmarshalMessageReservedRange(v)) - case messageDesc_ExtensionRanges: + case descfield.DescriptorProto_ExtensionRange: r, opts := unmarshalMessageExtensionRange(v) md.lazy.extRanges.list = append(md.lazy.extRanges.list, r) md.lazy.extRangeOptions = append(md.lazy.extRangeOptions, opts) - case messageDesc_Enums: + case descfield.DescriptorProto_EnumType: md.enums.list[enumIdx].unmarshalFull(v, nb) enumIdx++ - case messageDesc_Messages: + case descfield.DescriptorProto_NestedType: md.messages.list[messageIdx].unmarshalFull(v, nb) messageIdx++ - case messageDesc_Extensions: + case descfield.DescriptorProto_Extension: md.extensions.list[extensionIdx].unmarshalFull(v, nb) extensionIdx++ - case messageDesc_Options: + case descfield.DescriptorProto_Options: md.unmarshalOptions(v) } default: @@ -557,7 +558,7 @@ func (md *messageDesc) unmarshalOptions(b []byte) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case messageOptions_IsMapEntry: + case descfield.MessageOptions_MapEntry: md.lazy.isMapEntry = wire.DecodeBool(v) } default: @@ -576,9 +577,9 @@ func unmarshalMessageReservedRange(b []byte) (r [2]pref.FieldNumber) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case messageReservedRange_Start: + case descfield.DescriptorProto_ReservedRange_Start: r[0] = pref.FieldNumber(v) - case messageReservedRange_End: + case descfield.DescriptorProto_ReservedRange_End: r[1] = pref.FieldNumber(v) } default: @@ -598,16 +599,16 @@ func unmarshalMessageExtensionRange(b []byte) (r [2]pref.FieldNumber, opts []byt v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case messageExtensionRange_Start: + case descfield.DescriptorProto_ExtensionRange_Start: r[0] = pref.FieldNumber(v) - case messageExtensionRange_End: + case descfield.DescriptorProto_ExtensionRange_End: r[1] = pref.FieldNumber(v) } case wire.BytesType: v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case messageExtensionRange_Options: + case descfield.DescriptorProto_ExtensionRange_Options: opts = append(opts, v...) } default: @@ -633,13 +634,13 @@ func (fd *fieldDesc) unmarshalFull(b []byte, nb *nameBuilder, pf *fileDesc, pd p v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case fieldDesc_Number: + case descfield.FieldDescriptorProto_Number: fd.number = pref.FieldNumber(v) - case fieldDesc_Cardinality: + case descfield.FieldDescriptorProto_Label: fd.cardinality = pref.Cardinality(v) - case fieldDesc_Kind: + case descfield.FieldDescriptorProto_Type: fd.kind = pref.Kind(v) - case fieldDesc_OneofIndex: + case descfield.FieldDescriptorProto_OneofIndex: // In messageDesc.UnmarshalFull, we allocate slices for both // the field and oneof descriptors before unmarshaling either // of them. This ensures pointers to slice elements are stable. @@ -654,17 +655,17 @@ func (fd *fieldDesc) unmarshalFull(b []byte, nb *nameBuilder, pf *fileDesc, pd p v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case fieldDesc_Name: + case descfield.FieldDescriptorProto_Name: fd.fullName = nb.AppendFullName(pd.FullName(), v) - case fieldDesc_JSONName: + case descfield.FieldDescriptorProto_JsonName: fd.hasJSONName = true fd.jsonName = nb.MakeString(v) - case fieldDesc_Default: + case descfield.FieldDescriptorProto_DefaultValue: fd.defVal.has = true rawDefVal = v - case fieldDesc_TypeName: + case descfield.FieldDescriptorProto_TypeName: rawTypeName = v - case fieldDesc_Options: + case descfield.FieldDescriptorProto_Options: fd.unmarshalOptions(v) } default: @@ -703,10 +704,10 @@ func (fd *fieldDesc) unmarshalOptions(b []byte) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case fieldOptions_IsPacked: + case descfield.FieldOptions_Packed: fd.hasPacked = true fd.isPacked = wire.DecodeBool(v) - case fieldOptions_IsWeak: + case descfield.FieldOptions_Weak: fd.isWeak = wire.DecodeBool(v) } default: @@ -729,9 +730,9 @@ func (od *oneofDesc) unmarshalFull(b []byte, nb *nameBuilder, pf *fileDesc, pd p v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case oneofDesc_Name: + case descfield.OneofDescriptorProto_Name: od.fullName = nb.AppendFullName(pd.FullName(), v) - case oneofDesc_Options: + case descfield.OneofDescriptorProto_Options: od.options = append(od.options, v...) } default: @@ -754,22 +755,22 @@ func (xd *extensionDesc) unmarshalFull(b []byte, nb *nameBuilder) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case fieldDesc_Cardinality: + case descfield.FieldDescriptorProto_Label: xd.lazy.cardinality = pref.Cardinality(v) - case fieldDesc_Kind: + case descfield.FieldDescriptorProto_Type: xd.lazy.kind = pref.Kind(v) } case wire.BytesType: v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case fieldDesc_JSONName: + case descfield.FieldDescriptorProto_JsonName: xd.lazy.hasJSONName = true xd.lazy.jsonName = nb.MakeString(v) - case fieldDesc_Default: + case descfield.FieldDescriptorProto_DefaultValue: xd.lazy.defVal.has = true rawDefVal = v - case fieldDesc_Options: + case descfield.FieldDescriptorProto_Options: xd.unmarshalOptions(v) } default: @@ -799,7 +800,7 @@ func (xd *extensionDesc) unmarshalOptions(b []byte) { v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case fieldOptions_IsPacked: + case descfield.FieldOptions_Packed: xd.lazy.isPacked = wire.DecodeBool(v) } default: @@ -820,9 +821,9 @@ func (sd *serviceDesc) unmarshalFull(b []byte, nb *nameBuilder) { v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case serviceDesc_Methods: + case descfield.ServiceDescriptorProto_Method: rawMethods = append(rawMethods, v) - case serviceDesc_Options: + case descfield.ServiceDescriptorProto_Options: sd.lazy.options = append(sd.lazy.options, v...) } default: @@ -854,18 +855,18 @@ func (md *methodDesc) unmarshalFull(b []byte, nb *nameBuilder, pf *fileDesc, pd v, m := wire.ConsumeVarint(b) b = b[m:] switch num { - case methodDesc_IsStreamingClient: + case descfield.MethodDescriptorProto_ClientStreaming: md.isStreamingClient = wire.DecodeBool(v) - case methodDesc_IsStreamingServer: + case descfield.MethodDescriptorProto_ServerStreaming: md.isStreamingServer = wire.DecodeBool(v) } case wire.BytesType: v, m := wire.ConsumeBytes(b) b = b[m:] switch num { - case methodDesc_Name: + case descfield.MethodDescriptorProto_Name: md.fullName = nb.AppendFullName(pd.FullName(), v) - case methodDesc_Options: + case descfield.MethodDescriptorProto_Options: md.options = append(md.options, v...) } default: diff --git a/internal/fileinit/desc_wire.go b/internal/fileinit/desc_wire.go deleted file mode 100644 index b8b16843..00000000 --- a/internal/fileinit/desc_wire.go +++ /dev/null @@ -1,94 +0,0 @@ -// 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 fileinit - -// Constants for field numbers of messages declared in descriptor.proto. -const ( - // FileDescriptorProto field numbers - fileDesc_Syntax = 12 // optional string - fileDesc_Name = 1 // optional string - fileDesc_Package = 2 // optional string - fileDesc_Imports = 3 // repeated string - fileDesc_PublicImports = 10 // repeated int32 - fileDesc_WeakImports = 11 // repeated int32 - fileDesc_Enums = 5 // repeated EnumDescriptorProto - fileDesc_Messages = 4 // repeated DescriptorProto - fileDesc_Extensions = 7 // repeated FieldDescriptorProto - fileDesc_Services = 6 // repeated ServiceDescriptorProto - fileDesc_Options = 8 // optional FileOptions - - // EnumDescriptorProto field numbers - enumDesc_Name = 1 // optional string - enumDesc_Values = 2 // repeated EnumValueDescriptorProto - enumDesc_ReservedNames = 5 // repeated string - enumDesc_ReservedRanges = 4 // repeated EnumReservedRange - enumDesc_Options = 3 // optional EnumOptions - - // EnumReservedRange field numbers - enumReservedRange_Start = 1 // optional int32 - enumReservedRange_End = 2 // optional int32 - - // EnumValueDescriptorProto field numbers - enumValueDesc_Name = 1 // optional string - enumValueDesc_Number = 2 // optional int32 - enumValueDesc_Options = 3 // optional EnumValueOptions - - // DescriptorProto field numbers - messageDesc_Name = 1 // optional string - messageDesc_Fields = 2 // repeated FieldDescriptorProto - messageDesc_Oneofs = 8 // repeated OneofDescriptorProto - messageDesc_ReservedNames = 10 // repeated string - messageDesc_ReservedRanges = 9 // repeated ReservedRange - messageDesc_ExtensionRanges = 5 // repeated ExtensionRange - messageDesc_Enums = 4 // repeated EnumDescriptorProto - messageDesc_Messages = 3 // repeated DescriptorProto - messageDesc_Extensions = 6 // repeated FieldDescriptorProto - messageDesc_Options = 7 // optional MessageOptions - - // ReservedRange field numbers - messageReservedRange_Start = 1 // optional int32 - messageReservedRange_End = 2 // optional int32 - - // ExtensionRange field numbers - messageExtensionRange_Start = 1 // optional int32 - messageExtensionRange_End = 2 // optional int32 - messageExtensionRange_Options = 3 // optional ExtensionRangeOptions - - // MessageOptions field numbers - messageOptions_IsMapEntry = 7 // optional bool - - // FieldDescriptorProto field numbers - fieldDesc_Name = 1 // optional string - fieldDesc_Number = 3 // optional int32 - fieldDesc_Cardinality = 4 // optional Label - fieldDesc_Kind = 5 // optional Type - fieldDesc_JSONName = 10 // optional string - fieldDesc_Default = 7 // optional string - fieldDesc_OneofIndex = 9 // optional int32 - fieldDesc_TypeName = 6 // optional string - fieldDesc_ExtendedType = 2 // optional string - fieldDesc_Options = 8 // optional FieldOptions - - // FieldOptions field numbers - fieldOptions_IsPacked = 2 // optional bool - fieldOptions_IsWeak = 10 // optional bool - - // OneofDescriptorProto field numbers - oneofDesc_Name = 1 // optional string - oneofDesc_Options = 2 // optional OneofOptions - - // ServiceDescriptorProto field numbers - serviceDesc_Name = 1 // optional string - serviceDesc_Methods = 2 // repeated MethodDescriptorProto - serviceDesc_Options = 3 // optional ServiceOptions - - // MethodDescriptorProto field numbers - methodDesc_Name = 1 // optional string - methodDesc_InputType = 2 // optional string - methodDesc_OutputType = 3 // optional string - methodDesc_IsStreamingClient = 5 // optional bool - methodDesc_IsStreamingServer = 6 // optional bool - methodDesc_Options = 4 // optional MethodOptions -) diff --git a/protogen/protogen.go b/protogen/protogen.go index f9456907..a61baa0d 100644 --- a/protogen/protogen.go +++ b/protogen/protogen.go @@ -29,6 +29,7 @@ import ( "strings" "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/v2/internal/descfield" "github.com/golang/protobuf/v2/internal/scalar" "github.com/golang/protobuf/v2/reflect/protodesc" "github.com/golang/protobuf/v2/reflect/protoreflect" @@ -519,9 +520,9 @@ type Message struct { func newMessage(gen *Plugin, f *File, parent *Message, desc protoreflect.MessageDescriptor) *Message { var loc Location if parent != nil { - loc = parent.Location.appendPath(messageMessageField, int32(desc.Index())) + loc = parent.Location.appendPath(descfield.DescriptorProto_NestedType, int32(desc.Index())) } else { - loc = f.location(fileMessageField, int32(desc.Index())) + loc = f.location(descfield.FileDescriptorProto_MessageType, int32(desc.Index())) } message := &Message{ Desc: desc, @@ -637,11 +638,11 @@ func newField(gen *Plugin, f *File, message *Message, desc protoreflect.FieldDes var loc Location switch { case desc.ExtendedType() != nil && message == nil: - loc = f.location(fileExtensionField, int32(desc.Index())) + loc = f.location(descfield.FileDescriptorProto_Extension, int32(desc.Index())) case desc.ExtendedType() != nil && message != nil: - loc = message.Location.appendPath(messageExtensionField, int32(desc.Index())) + loc = message.Location.appendPath(descfield.DescriptorProto_Extension, int32(desc.Index())) default: - loc = message.Location.appendPath(messageFieldField, int32(desc.Index())) + loc = message.Location.appendPath(descfield.DescriptorProto_Field, int32(desc.Index())) } field := &Field{ Desc: desc, @@ -702,7 +703,7 @@ func newOneof(gen *Plugin, f *File, message *Message, desc protoreflect.OneofDes Desc: desc, ParentMessage: message, GoName: camelCase(string(desc.Name())), - Location: message.Location.appendPath(messageOneofField, int32(desc.Index())), + Location: message.Location.appendPath(descfield.DescriptorProto_OneofDecl, int32(desc.Index())), } } @@ -724,9 +725,9 @@ type Enum struct { func newEnum(gen *Plugin, f *File, parent *Message, desc protoreflect.EnumDescriptor) *Enum { var loc Location if parent != nil { - loc = parent.Location.appendPath(messageEnumField, int32(desc.Index())) + loc = parent.Location.appendPath(descfield.DescriptorProto_EnumType, int32(desc.Index())) } else { - loc = f.location(fileEnumField, int32(desc.Index())) + loc = f.location(descfield.FileDescriptorProto_EnumType, int32(desc.Index())) } enum := &Enum{ Desc: desc, @@ -761,7 +762,7 @@ func newEnumValue(gen *Plugin, f *File, message *Message, enum *Enum, desc proto return &EnumValue{ Desc: desc, GoIdent: f.GoImportPath.Ident(name), - Location: enum.Location.appendPath(enumValueField, int32(desc.Index())), + Location: enum.Location.appendPath(descfield.EnumDescriptorProto_Value, int32(desc.Index())), } } @@ -778,7 +779,7 @@ func newService(gen *Plugin, f *File, desc protoreflect.ServiceDescriptor) *Serv service := &Service{ Desc: desc, GoName: camelCase(string(desc.Name())), - Location: f.location(fileServiceField, int32(desc.Index())), + Location: f.location(descfield.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))) @@ -802,7 +803,7 @@ func newMethod(gen *Plugin, f *File, service *Service, desc protoreflect.MethodD Desc: desc, GoName: camelCase(string(desc.Name())), ParentService: service, - Location: service.Location.appendPath(serviceMethodField, int32(desc.Index())), + Location: service.Location.appendPath(descfield.ServiceDescriptorProto_Method, int32(desc.Index())), } return method } @@ -1081,34 +1082,6 @@ const ( pathTypeSourceRelative ) -// The SourceCodeInfo message describes the location of elements of a parsed -// .proto file by way of a "path", which is a sequence of integers that -// describe the route from a FileDescriptorProto to the relevant submessage. -// The path alternates between a field number of a repeated field, and an index -// into that repeated field. The constants below define the field numbers that -// are used. -// -// See descriptor.proto for more information about this. -const ( - // field numbers in FileDescriptorProto - filePackageField = 2 // package - fileMessageField = 4 // message_type - fileEnumField = 5 // enum_type - fileServiceField = 6 // service - fileExtensionField = 7 // extension - // field numbers in DescriptorProto - messageFieldField = 2 // field - messageMessageField = 3 // nested_type - messageEnumField = 4 // enum_type - messageExtensionField = 6 // extension - messageOneofField = 8 // oneof_decl - // field numbers in EnumDescriptorProto - enumValueField = 2 // value - // field numbers in ServiceDescriptorProto - serviceMethodField = 2 // method - serviceStreamField = 4 // stream -) - // A Location is a location in a .proto source file. // // See the google.protobuf.SourceCodeInfo documentation in descriptor.proto