Skip to content

Commit

Permalink
Add goproto_morexxx and goproto_morexxx_all
Browse files Browse the repository at this point in the history
- Allows omitting XXX fields when using moretags
  • Loading branch information
virtuald committed Sep 21, 2018
1 parent e14cafb commit 1f4392f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions gogoproto/gogo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ extend google.protobuf.FileOptions {

optional bool goproto_sizecache_all = 63034;
optional bool goproto_unkeyed_all = 63035;

optional string goproto_morexxx_all = 63036;
}

extend google.protobuf.MessageOptions {
Expand Down Expand Up @@ -124,6 +126,8 @@ extend google.protobuf.MessageOptions {

optional bool goproto_sizecache = 64034;
optional bool goproto_unkeyed = 64035;

optional string goproto_morexxx = 64036;
}

extend google.protobuf.FieldOptions {
Expand Down
4 changes: 4 additions & 0 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string {
return nil
}

func GetMoreXXXTags(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) string {
return proto.GetStringExtension(message.Options, E_GoprotoMorexxx, proto.GetStringExtension(file.Options, E_GoprotoMorexxxAll, ""))
}

type EnableFunc func(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool

func EnabledGoEnumPrefix(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool {
Expand Down
17 changes: 17 additions & 0 deletions proto/extensions_gogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ func GetBoolExtension(pb Message, extension *ExtensionDesc, ifnotset bool) bool
return *(value.(*bool))
}

func GetStringExtension(pb Message, extension *ExtensionDesc, ifnotset string) string {
if reflect.ValueOf(pb).IsNil() {
return ifnotset
}
value, err := GetExtension(pb, extension)
if err != nil {
return ifnotset
}
if value == nil {
return ifnotset
}
if value.(*string) == nil {
return ifnotset
}
return *(value.(*string))
}

func (this *Extension) Equal(that *Extension) bool {
if err := this.Encode(); err != nil {
return false
Expand Down
14 changes: 9 additions & 5 deletions protoc-gen-gogo/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2196,25 +2196,29 @@ func (g *Generator) generateMessage(message *Descriptor) {
g.RecordTypeUse(field.GetTypeName())
}
}
moreXXXTags := gogoproto.GetMoreXXXTags(g.file.FileDescriptorProto, message.DescriptorProto)
if moreXXXTags != "" {
moreXXXTags = " " + moreXXXTags
}
if gogoproto.HasUnkeyed(g.file.FileDescriptorProto, message.DescriptorProto) {
g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\"`") // prevent unkeyed struct literals
g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\"", moreXXXTags, "`") // prevent unkeyed struct literals
}
if len(message.ExtensionRange) > 0 {
if gogoproto.HasExtensionsMap(g.file.FileDescriptorProto, message.DescriptorProto) {
messageset := ""
if opts := message.Options; opts != nil && opts.GetMessageSetWireFormat() {
messageset = "protobuf_messageset:\"1\" "
}
g.P(g.Pkg["proto"], ".XXX_InternalExtensions `", messageset, "json:\"-\"`")
g.P(g.Pkg["proto"], ".XXX_InternalExtensions `", messageset, "json:\"-\"", moreXXXTags, "`")
} else {
g.P("XXX_extensions\t\t[]byte `protobuf:\"bytes,0,opt\" json:\"-\"`")
g.P("XXX_extensions\t\t[]byte `protobuf:\"bytes,0,opt\" json:\"-\"", moreXXXTags, "`")
}
}
if gogoproto.HasUnrecognized(g.file.FileDescriptorProto, message.DescriptorProto) {
g.P("XXX_unrecognized\t[]byte `json:\"-\"`")
g.P("XXX_unrecognized\t[]byte `json:\"-\"", moreXXXTags, "`")
}
if gogoproto.HasSizecache(g.file.FileDescriptorProto, message.DescriptorProto) {
g.P("XXX_sizecache\tint32 `json:\"-\"`")
g.P("XXX_sizecache\tint32 `json:\"-\"", moreXXXTags, "`")
}
g.Out()
g.P("}")
Expand Down
1 change: 1 addition & 0 deletions test/tags/tags.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ package tags;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";

option (gogoproto.populate_all) = true;
option (gogoproto.goproto_morexxx_all) = "xml:\"-\"";

message Outside {
optional Inside Inside = 1 [(gogoproto.embed) = true, (gogoproto.jsontag) = ""];
Expand Down
2 changes: 1 addition & 1 deletion test/tags/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestJson(t *testing.T) {
}

func TestXml(t *testing.T) {
s := "<Outside>Field1Value<!--Field2Value--><XXX_NoUnkeyedLiteral></XXX_NoUnkeyedLiteral><XXX_unrecognized></XXX_unrecognized><XXX_sizecache>0</XXX_sizecache></Outside>"
s := "<Outside>Field1Value<!--Field2Value--></Outside>"
field1 := "Field1Value"
field2 := "Field2Value"
msg1 := &Outside{}
Expand Down

0 comments on commit 1f4392f

Please sign in to comment.