From 75479a36c9095127c600d31ed3c9591366ffa2a4 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 5 Aug 2024 16:41:25 +0200 Subject: [PATCH] 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 Auto-Submit: Michael Stapelberg Reviewed-by: Lasse Folger --- internal/cmd/generate-protos/main.go | 15 +++++++++++++++ internal/filedesc/editions.go | 2 +- internal/genid/go_features_gen.go | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/cmd/generate-protos/main.go b/internal/cmd/generate-protos/main.go index b2d0803c..071dc73f 100644 --- a/internal/cmd/generate-protos/main.go +++ b/internal/cmd/generate-protos/main.go @@ -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 diff --git a/internal/filedesc/editions.go b/internal/filedesc/editions.go index 11f5f356..fd4d0c83 100644 --- a/internal/filedesc/editions.go +++ b/internal/filedesc/editions.go @@ -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) } } diff --git a/internal/genid/go_features_gen.go b/internal/genid/go_features_gen.go index 546388c4..7f67cbb6 100644 --- a/internal/genid/go_features_gen.go +++ b/internal/genid/go_features_gen.go @@ -29,3 +29,8 @@ const ( const ( GoFeatures_LegacyUnmarshalJsonEnum_field_number protoreflect.FieldNumber = 1 ) + +// Extension numbers +const ( + FeatureSet_Go_ext_number protoreflect.FieldNumber = 1002 +)