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 file propery to edit internal state tags #746

Open
wants to merge 5 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gogoproto/gogo.pb.go

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

3 changes: 3 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 internal_struct_field_tags = 63036;
}

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

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

}

extend google.protobuf.FieldOptions {
Expand Down
12 changes: 12 additions & 0 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string {
}
return nil
}
func GetInternalStructFieldTags(file *google_protobuf.FileDescriptorProto) *string {
if file == nil {
return nil
}
if file.Options != nil {
v, err := proto.GetExtension(file.Options, E_InternalStructFieldTags)
if err == nil && v.(*string) != nil {
return (v.(*string))
}
}
return nil
}

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

Expand Down
17 changes: 12 additions & 5 deletions protoc-gen-gogo/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2494,25 +2494,32 @@ func (g *Generator) generateGet(mc *msgCtx, protoField *descriptor.FieldDescript

// generateInternalStructFields just adds the XXX_<something> fields to the message struct.
func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []topLevelField) {
internalStructFieldTags := gogoproto.GetInternalStructFieldTags(g.file.FileDescriptorProto)

resolvedInternaltags := ""
if internalStructFieldTags != nil {
resolvedInternaltags = ", " + *internalStructFieldTags
}

if gogoproto.HasUnkeyed(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\"`") // prevent unkeyed struct literals
g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\"`" + resolvedInternaltags) // prevent unkeyed struct literals
}
if len(mc.message.ExtensionRange) > 0 {
if gogoproto.HasExtensionsMap(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
messageset := ""
if opts := mc.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:\"-\"`"+resolvedInternaltags)
} 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:\"-\"`" + resolvedInternaltags)
}
}
if gogoproto.HasUnrecognized(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
g.P("XXX_unrecognized\t[]byte `json:\"-\"`")
g.P("XXX_unrecognized\t[]byte `json:\"-\"`" + resolvedInternaltags)
}
if gogoproto.HasSizecache(g.file.FileDescriptorProto, mc.message.DescriptorProto) {
g.P("XXX_sizecache\tint32 `json:\"-\"`")
g.P("XXX_sizecache\tint32 `json:\"-\"`" + resolvedInternaltags)
}
}

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.internal_struct_field_tags) = "xml:\"-\"";

message Outside {
optional Inside Inside = 1 [(gogoproto.embed) = true, (gogoproto.jsontag) = ""];
Expand Down
14 changes: 14 additions & 0 deletions test/tags/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ func TestTags(t *testing.T) {
}
}
}

func TestHiddenTags(t *testing.T) {
var tests = []string{
"XXX_NoUnkeyedLiteral", "XXX_unrecognized", "XXX_sizecache",
}

for _, tt := range tests {
tv := reflect.ValueOf(Inside{}).Type()
f, _ := tv.FieldByName(tt)
if xmltag := f.Tag.Get("xml"); xmltag != "-" {
t.Fatalf("proto %q type: xml tag %s != %s", tv.Name(), xmltag, "-")
}
}
}