Skip to content

add no_omitempty option to remote omitempty label from generated struct #683

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

Closed
wants to merge 3 commits into from
Closed
Changes from all 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
11 changes: 9 additions & 2 deletions protoc-gen-go/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Plugin interface {
}

var plugins []Plugin

var noOmitEmpty bool
// RegisterPlugin installs a (second-order) plugin to be run when the Go output is generated.
// It is typically called during initialization.
func RegisterPlugin(p Plugin) {
Expand Down Expand Up @@ -493,6 +493,10 @@ func (g *Generator) CommandLineParameters(parameter string) {
if v == "true" {
g.annotateCode = true
}
case "no_omitempty" :
if v == "true" {
noOmitEmpty = true
}
default:
if len(k) > 0 && k[0] == 'M' {
g.ImportMap[k[1:]] = v
Expand Down Expand Up @@ -2497,7 +2501,10 @@ func (g *Generator) generateMessage(message *Descriptor) {
fieldName, fieldGetterName := ns[0], ns[1]
typename, wiretype := g.GoType(message, field)
jsonName := *field.Name
tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName+",omitempty")
if !noOmitEmpty {
jsonName = jsonName+",omitempty"
}
tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName)

oneof := field.OneofIndex != nil
if oneof && oFields[*field.OneofIndex] == nil {
Expand Down