internal: generate extension numbers, fix editions parsing

Before this commit, message-level Go editions features were not parsed
correctly.

Change-Id: I94ead5428fadae5fd70ed991fa405ce9388e9660
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/603015
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Stapelberg <stapelberg@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
This commit is contained in:
Michael Stapelberg 2024-08-05 16:41:25 +02:00 committed by Gopher Robot
parent 2a82025330
commit 75479a36c9
3 changed files with 21 additions and 1 deletions

View File

@ -311,6 +311,7 @@ func generateIdentifiers(gen *protogen.Plugin, file *protogen.File) {
var processEnums func([]*protogen.Enum)
var processMessages func([]*protogen.Message)
var processExtensions func([]*protogen.Extension)
const protoreflectPackage = protogen.GoImportPath("google.golang.org/protobuf/reflect/protoreflect")
processEnums = func(enums []*protogen.Enum) {
for _, enum := range enums {
@ -377,10 +378,24 @@ func generateIdentifiers(gen *protogen.Plugin, file *protogen.File) {
processEnums(message.Enums)
processMessages(message.Messages)
processExtensions(message.Extensions)
}
}
processExtensions = func(extensions []*protogen.Extension) {
if len(extensions) == 0 {
return
}
g.P("// Extension numbers")
g.P("const (")
for _, ext := range extensions {
g.P(ext.Extendee.GoIdent.GoName, "_", ext.GoName, "_ext_number ", protoreflectPackage.Ident("FieldNumber"), " = ", ext.Desc.Number())
}
g.P(")")
}
processEnums(file.Enums)
processMessages(file.Messages)
processExtensions(file.Extensions)
}
// generateSourceContextStringer generates the implementation for the

View File

@ -68,7 +68,7 @@ func unmarshalFeatureSet(b []byte, parent EditionFeatures) EditionFeatures {
v, m := protowire.ConsumeBytes(b)
b = b[m:]
switch num {
case genid.GoFeatures_LegacyUnmarshalJsonEnum_field_number:
case genid.FeatureSet_Go_ext_number:
parent = unmarshalGoFeature(v, parent)
}
}

View File

@ -29,3 +29,8 @@ const (
const (
GoFeatures_LegacyUnmarshalJsonEnum_field_number protoreflect.FieldNumber = 1
)
// Extension numbers
const (
FeatureSet_Go_ext_number protoreflect.FieldNumber = 1002
)