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

work-in-progress for fixing pulsar 1.0 blockers #109

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DIRECTORIES_TO_BUILD := "./testpb ./internal/testprotos/test3"
DIRECTORIES_TO_BUILD := "./internal/testpb ./internal/testprotos/test3"

pulsar:
docker build -t dev:proto-build -f Dockerfile .
Expand Down
112 changes: 0 additions & 112 deletions anyutil/any.go

This file was deleted.

51 changes: 0 additions & 51 deletions anyutil/any_test.go

This file was deleted.

19 changes: 0 additions & 19 deletions anyutil/doc.go

This file was deleted.

34 changes: 3 additions & 31 deletions cmd/protoc-gen-go-pulsar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package main
import (
"flag"
"fmt"
"log"
"strings"

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

"google.golang.org/protobuf/compiler/protogen"
Expand Down Expand Up @@ -83,25 +82,6 @@ func generateAllFiles(plugin *protogen.Plugin, featureNames []string, poolable O
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 {
Expand All @@ -112,14 +92,6 @@ func rewriteMessageField(message *protogen.Message, processed map[protoreflect.F
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 may 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())
field.GoName = field.GoName + "_"
}
processed[message.Desc.FullName()] = struct{}{}

for _, nestedMessage := range message.Messages {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fastreflection

import (
"github.com/cosmos/cosmos-proto/generator"
"github.com/cosmos/cosmos-proto/internal/generator"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand Down Expand Up @@ -30,7 +30,7 @@ func (g *clearGen) genComments() {

func (g *clearGen) generate() {
g.genComments()
g.P("func (x *", g.typeName, ") Clear(fd ", protoreflectPkg.Ident("FieldDescriptor"), ") {")
g.P("func (x ", g.typeName, ") Clear(fd ", protoreflectPkg.Ident("FieldDescriptor"), ") {")
g.P("switch fd.FullName() {")
for _, field := range g.message.Fields {
g.genField(field)
Expand All @@ -51,17 +51,17 @@ func (g *clearGen) genField(field *protogen.Field) {
return
}

g.P("x.", field.GoName, " = ", zeroValueForField(nil, field))
g.P("x.x.", field.GoName, " = ", zeroValueForField(nil, field))
}

func (g *clearGen) genNullable(field *protogen.Field) {
switch {
case field.Desc.ContainingOneof() != nil:
g.P("x.", field.Oneof.GoName, " = nil")
g.P("x.x.", field.Oneof.GoName, " = nil")
case field.Desc.IsMap(), field.Desc.IsList(), field.Desc.Kind() == protoreflect.BytesKind:
g.P("x.", field.GoName, " = nil")
g.P("x.x.", field.GoName, " = nil")
case field.Desc.Kind() == protoreflect.MessageKind:
g.P(" x.", field.GoName, " = nil")
g.P("x.x.", field.GoName, " = nil")
default:
panic("unknown case")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package fastreflection

import (
"fmt"
"github.com/cosmos/cosmos-proto/generator"
"github.com/cosmos/cosmos-proto/internal/generator"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/cosmos/cosmos-proto/features/fastreflection/copied"
"github.com/cosmos/cosmos-proto/internal/features/fastreflection/copied"
"google.golang.org/protobuf/compiler/protogen"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fastreflection

import (
"github.com/cosmos/cosmos-proto/generator"
"github.com/cosmos/cosmos-proto/internal/generator"
"google.golang.org/protobuf/compiler/protogen"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fastreflection

import (
"github.com/cosmos/cosmos-proto/generator"
"github.com/cosmos/cosmos-proto/internal/generator"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand All @@ -14,7 +14,7 @@ type getGen struct {

func (g *getGen) generate() {
g.genComment()
g.P("func (x *", g.typeName, ") Get(descriptor ", protoreflectPkg.Ident("FieldDescriptor"), ") ", protoreflectPkg.Ident("Value"), " {")
g.P("func (x ", g.typeName, ") Get(descriptor ", protoreflectPkg.Ident("FieldDescriptor"), ") ", protoreflectPkg.Ident("Value"), " {")
g.P("switch descriptor.FullName() {")
// implement the fastReflectionFeature Get function
for _, field := range g.message.Fields {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (g *getGen) genFieldGetter(field *protogen.Field) {
return
}

fieldRef := "x." + field.GoName
fieldRef := "x.x." + field.GoName
g.P("value := ", fieldRef)
switch field.Desc.Kind() {
case protoreflect.BoolKind:
Expand Down Expand Up @@ -83,15 +83,15 @@ func (g *getGen) genFieldGetter(field *protogen.Field) {

func (g *getGen) genOneofGetter(fd *protogen.Field) {
// handle the case in which the oneof field is not set
g.P("if x.", fd.Oneof.GoName, " == nil {")
g.P("if x.x.", fd.Oneof.GoName, " == nil {")
switch fd.Desc.Kind() {
case protoreflect.MessageKind:
g.P("return ", kindToValueConstructor(fd.Desc.Kind()), "((*", g.QualifiedGoIdent(fd.Message.GoIdent), ")(nil).ProtoReflect())")
default:
g.P("return ", kindToValueConstructor(fd.Desc.Kind()), "(", zeroValueForField(g.GeneratedFile, fd), ")")
}
// handle the case in which oneof field is set and it matches our sub-onefield type
g.P("} else if v, ok := x.", fd.Oneof.GoName, ".(*", fd.GoIdent, "); ok {")
g.P("} else if v, ok := x.x.", fd.Oneof.GoName, ".(*", fd.GoIdent, "); ok {")
oneofTypeContainerFieldName := fd.GoName // field containing the oneof value
switch fd.Desc.Kind() {
case protoreflect.MessageKind: // it can be mutable
Expand Down Expand Up @@ -123,21 +123,21 @@ func (g *getGen) genDefaultCase() {
// genMap generates the protoreflect.Message.Get for map types
func (g *getGen) genMap(field *protogen.Field) {
// gen invalid case
g.P("if len(x.", field.GoName, ") == 0 {")
g.P("if len(x.x.", field.GoName, ") == 0 {")
g.P("return ", protoreflectPkg.Ident("ValueOfMap"), "(&", mapTypeName(field), "{})")
g.P("}")
// gen valid case
g.P("mapValue := &", mapTypeName(field), "{m: &x.", field.GoName, "}")
g.P("mapValue := &", mapTypeName(field), "{m: &x.x.", field.GoName, "}")
g.P("return ", protoreflectPkg.Ident("ValueOfMap"), "(mapValue)")
}

func (g *getGen) genList(field *protogen.Field) {
// gen invalid case
g.P("if len(x.", field.GoName, ") == 0 {")
g.P("if len(x.x.", field.GoName, ") == 0 {")
g.P("return ", protoreflectPkg.Ident("ValueOfList"), "(&", listTypeName(field), "{})")
g.P("}")
// gen valid case
g.P("listValue := &", listTypeName(field), "{list: &x.", field.GoName, "}")
g.P("listValue := &", listTypeName(field), "{list: &x.x.", field.GoName, "}")
g.P("return ", protoreflectPkg.Ident("ValueOfList"), "(listValue)")
}

Expand Down
Loading