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

Add package name to nullable enums in gostring result. #638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ regenerate:
make -C test/issue620 regenerate
make -C test/protobuffer regenerate
make -C test/issue630 regenerate
make -C test/issue635 regenerate

make gofmt

Expand Down
24 changes: 22 additions & 2 deletions plugin/gostring/gostring.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import (
"strings"

"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
)

Expand Down Expand Up @@ -260,8 +261,8 @@ func (p *gostring) Generate(file *generator.FileDescriptor) {
}
if field.IsEnum() {
if nullable && !repeated && !proto3 {
goTyp, _ := p.GoType(message, field)
p.P(`s = append(s, "`, fieldname, `: " + valueToGoString`, p.localName, `(this.`, fieldname, `,"`, generator.GoTypeToName(goTyp), `"`, `) + ",\n")`)
typ := enumTypeName(field)
p.P(`s = append(s, "`, fieldname, `: " + valueToGoString`, p.localName, `(this.`, fieldname, `,"`, typ, `"`, `) + ",\n")`)
} else {
p.P(`s = append(s, "`, fieldname, `: " + `, fmtPkg.Use(), `.Sprintf("%#v", this.`, fieldname, `) + ",\n")`)
}
Expand Down Expand Up @@ -381,6 +382,25 @@ func (p *gostring) Generate(file *generator.FileDescriptor) {
}
}

func enumTypeName(field *descriptor.FieldDescriptorProto) string {
tn := []byte(field.GetTypeName())
if len(tn) > 1 && tn[0] == '.' {
tn = tn[1:]
}
first := true
for i, v := range tn {
if v != '.' {
continue
}
if first {
first = false
continue
}
tn[i] = '_'
}
return string(tn)
}

func init() {
generator.RegisterPlugin(NewGoString())
}
12 changes: 6 additions & 6 deletions protoc-gen-gogo/descriptor/descriptor_gostring.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions test/combos/both/thetest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions test/combos/marshaler/thetest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions test/combos/unmarshaler/thetest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.