Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: field name and method conflict #51

Merged
merged 3 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions cmd/protoc-gen-go-pulsar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"flag"
"fmt"
"log"
"strings"

_ "github.com/cosmos/cosmos-proto/features/fastreflection"
_ "github.com/cosmos/cosmos-proto/features/grpc"
_ "github.com/cosmos/cosmos-proto/features/pool"
_ "github.com/cosmos/cosmos-proto/features/protoc"
"github.com/cosmos/cosmos-proto/generator"
"google.golang.org/protobuf/reflect/protoreflect"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
Expand Down Expand Up @@ -44,6 +46,15 @@ func main() {
f.StringVar(&features, "features", "all", "list of features to generate (separated by '+')")

protogen.Options{ParamFunc: f.Set}.Run(func(plugin *protogen.Plugin) error {
processedMessages := make(map[protoreflect.FullName]struct{})
for _, file := range plugin.Files {
if !file.Generate {
continue
}
for _, message := range file.Messages {
rewriteMessageField(message, processedMessages)
}
}
return generateAllFiles(plugin, strings.Split(features, "+"), poolable)
})
}
Expand Down Expand Up @@ -72,3 +83,47 @@ func generateAllFiles(plugin *protogen.Plugin, featureNames []string, poolable O
// plugin.SupportedFeatures = SupportedFeatures
return nil
}

var reservedFieldNames = map[string]struct{}{
"Descriptor": {},
"Type": {},
"New": {},
"Interface": {},
"Range": {},
"Has": {},
"Clear": {},
"Get": {},
"Set": {},
"Mutable": {},
"NewField": {},
"WhichOneof": {},
"GetUnknown": {},
"SetUnknown": {},
"IsValid": {},
"ProtoMethods": {},
}

func rewriteMessageField(message *protogen.Message, processed map[protoreflect.FullName]struct{}) {
// skip already processed messages, useful for recursive messages
if _, done := processed[message.Desc.FullName()]; done {
return
}
// skip map entries
if message.Desc.IsMapEntry() {
return
}

for _, field := range message.Fields {
_, reserved := reservedFieldNames[field.GoName]
if !reserved {
continue
}
log.Printf("Message %s contains the reserved field name %s which conflicts with protoreflect.Message interface implementation.\nThis field will be suffixed with an underscore '_'.\nIf you can change the message field name, please do so.\nIn a future iteration of pulsar we will make a breaking change to this practice in order to be compliant with field naming of the original golang protobuf implementation.", message.Desc.FullName(), field.Desc.FullName())
fdymylja marked this conversation as resolved.
Show resolved Hide resolved
field.GoName = field.GoName + "_"
}
processed[message.Desc.FullName()] = struct{}{}

for _, nestedMessage := range message.Messages {
rewriteMessageField(nestedMessage, processed)
}
}
2 changes: 1 addition & 1 deletion internal/testprotos/test3/test.pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11221,7 +11221,7 @@ func (x *fastReflection_ForeignMessage) ProtoMethods() *protoiface.Methods {
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.27.0
// protoc v3.15.7
// protoc v3.18.1
// source: internal/testprotos/test3/test.proto

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/testprotos/test3/test_import.pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (x *fastReflection_ImportMessage) ProtoMethods() *protoiface.Methods {
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.27.0
// protoc v3.15.7
// protoc v3.18.1
// source: internal/testprotos/test3/test_import.proto

const (
Expand Down
7 changes: 4 additions & 3 deletions testpb/1.proto
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
syntax="proto3";

option go_package = "github.com/cosmos/cosmos-proto/testpb";
import "testpb/2.proto";

import "google/protobuf/descriptor.proto";
option go_package = "github.com/cosmos/cosmos-proto/testpb";

enum Enumeration {
One = 0;
Expand Down Expand Up @@ -34,7 +34,8 @@ message A {
string ONEOF_STRING = 21;
};
repeated Enumeration LIST_ENUM = 22;
google.protobuf.FileDescriptorProto imported = 23;
ImportedMessage imported = 23;
string type = 24;
}

message B {
Expand Down
Loading