From 8f8f4e2008bf5fc635f963656510f3e2b9077263 Mon Sep 17 00:00:00 2001 From: Andrea Mangione <35958935+MangioneAndrea@users.noreply.github.com> Date: Sun, 16 Jan 2022 22:25:24 +0100 Subject: [PATCH 1/5] implement new file setting ignore_internal_struct_field_tags --- gogoproto/gogo.pb.go | 10 ++++++++++ gogoproto/gogo.proto | 3 +++ gogoproto/helper.go | 12 ++++++++++++ protoc-gen-gogo/generator/generator.go | 19 ++++++++++++++----- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/gogoproto/gogo.pb.go b/gogoproto/gogo.pb.go index 1e91766aee..e13abae188 100644 --- a/gogoproto/gogo.pb.go +++ b/gogoproto/gogo.pb.go @@ -363,6 +363,15 @@ var E_GoprotoUnkeyedAll = &proto.ExtensionDesc{ Filename: "gogo.proto", } +var E_IgnoreInternalStructFieldTags = &proto.ExtensionDesc{ + ExtendedType: (*descriptor.FileOptions)(nil), + ExtensionType: ([]string)(nil), + Field: 64036, + Name: "gogoproto.ignore_internal_struct_field_tags", + Tag: "bytes,63036,rep,name=ignore_internal_struct_field_tags", + Filename: "gogoproto/gogo.proto", +} + var E_GoprotoGetters = &proto.ExtensionDesc{ ExtendedType: (*descriptor.MessageOptions)(nil), ExtensionType: (*bool)(nil), @@ -597,6 +606,7 @@ var E_GoprotoUnkeyed = &proto.ExtensionDesc{ Filename: "gogo.proto", } + var E_Nullable = &proto.ExtensionDesc{ ExtendedType: (*descriptor.FieldOptions)(nil), ExtensionType: (*bool)(nil), diff --git a/gogoproto/gogo.proto b/gogoproto/gogo.proto index b80c85653f..9d7921b0b7 100644 --- a/gogoproto/gogo.proto +++ b/gogoproto/gogo.proto @@ -87,6 +87,8 @@ extend google.protobuf.FileOptions { optional bool goproto_sizecache_all = 63034; optional bool goproto_unkeyed_all = 63035; + + repeated string ignore_internal_struct_field_tags = 63036; } extend google.protobuf.MessageOptions { @@ -124,6 +126,7 @@ extend google.protobuf.MessageOptions { optional bool goproto_sizecache = 64034; optional bool goproto_unkeyed = 64035; + } extend google.protobuf.FieldOptions { diff --git a/gogoproto/helper.go b/gogoproto/helper.go index 390d4e4be6..33051ac3ce 100644 --- a/gogoproto/helper.go +++ b/gogoproto/helper.go @@ -287,6 +287,18 @@ func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string { } return nil } +func GetIgnoreInternalStructFieldTags(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) []*string { + if file == nil { + return nil + } + if file.Options != nil { + v, err := proto.GetExtension(file.Options, E_IgnoreInternalStructFieldTags) + if err == nil && v.([]*string) != nil { + return (v.([]*string)) + } + } + return nil +} type EnableFunc func(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool diff --git a/protoc-gen-gogo/generator/generator.go b/protoc-gen-gogo/generator/generator.go index 879939ea48..4db996cc09 100644 --- a/protoc-gen-gogo/generator/generator.go +++ b/protoc-gen-gogo/generator/generator.go @@ -2494,8 +2494,17 @@ func (g *Generator) generateGet(mc *msgCtx, protoField *descriptor.FieldDescript // generateInternalStructFields just adds the XXX_ fields to the message struct. func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []topLevelField) { + ignoreInternalStructFieldTags := gogoproto.GetIgnoreInternalStructFieldTags(g.file.FileDescriptorProto, mc.message.DescriptorProto) + + resolvedIgnoredTags := "" + if len(ignoreInternalStructFieldTags) > 0 { + for _, el := range ignoreInternalStructFieldTags { + resolvedIgnoredTags = "," + (*el) + "\"-\"" + } + } + 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:\"-\"`" + resolvedIgnoredTags) // prevent unkeyed struct literals } if len(mc.message.ExtensionRange) > 0 { if gogoproto.HasExtensionsMap(g.file.FileDescriptorProto, mc.message.DescriptorProto) { @@ -2503,16 +2512,16 @@ func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []to 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:\"-\"`"+resolvedIgnoredTags) } 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:\"-\"`" + resolvedIgnoredTags) } } if gogoproto.HasUnrecognized(g.file.FileDescriptorProto, mc.message.DescriptorProto) { - g.P("XXX_unrecognized\t[]byte `json:\"-\"`") + g.P("XXX_unrecognized\t[]byte `json:\"-\"`" + resolvedIgnoredTags) } if gogoproto.HasSizecache(g.file.FileDescriptorProto, mc.message.DescriptorProto) { - g.P("XXX_sizecache\tint32 `json:\"-\"`") + g.P("XXX_sizecache\tint32 `json:\"-\"`" + resolvedIgnoredTags) } } From 4ba0647dc28115d7153815de4858af058ba8e8bc Mon Sep 17 00:00:00 2001 From: Andrea Mangione <35958935+MangioneAndrea@users.noreply.github.com> Date: Sun, 16 Jan 2022 22:27:53 +0100 Subject: [PATCH 2/5] trigger workflow --- gogoproto/gogo.pb.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gogoproto/gogo.pb.go b/gogoproto/gogo.pb.go index e13abae188..e697ba85b5 100644 --- a/gogoproto/gogo.pb.go +++ b/gogoproto/gogo.pb.go @@ -606,7 +606,6 @@ var E_GoprotoUnkeyed = &proto.ExtensionDesc{ Filename: "gogo.proto", } - var E_Nullable = &proto.ExtensionDesc{ ExtendedType: (*descriptor.FieldOptions)(nil), ExtensionType: (*bool)(nil), From b98d56b3819d7b8a5bf00d4208ed9e4e1a3c1c45 Mon Sep 17 00:00:00 2001 From: Andrea Mangione <35958935+MangioneAndrea@users.noreply.github.com> Date: Sun, 16 Jan 2022 22:39:31 +0100 Subject: [PATCH 3/5] string array to string --- gogoproto/gogo.pb.go | 2 +- gogoproto/gogo.proto | 2 +- gogoproto/helper.go | 6 +++--- protoc-gen-gogo/generator/generator.go | 20 +++++++++----------- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/gogoproto/gogo.pb.go b/gogoproto/gogo.pb.go index e697ba85b5..62eebb8052 100644 --- a/gogoproto/gogo.pb.go +++ b/gogoproto/gogo.pb.go @@ -365,7 +365,7 @@ var E_GoprotoUnkeyedAll = &proto.ExtensionDesc{ var E_IgnoreInternalStructFieldTags = &proto.ExtensionDesc{ ExtendedType: (*descriptor.FileOptions)(nil), - ExtensionType: ([]string)(nil), + ExtensionType: (*string)(nil), Field: 64036, Name: "gogoproto.ignore_internal_struct_field_tags", Tag: "bytes,63036,rep,name=ignore_internal_struct_field_tags", diff --git a/gogoproto/gogo.proto b/gogoproto/gogo.proto index 9d7921b0b7..772971df42 100644 --- a/gogoproto/gogo.proto +++ b/gogoproto/gogo.proto @@ -88,7 +88,7 @@ extend google.protobuf.FileOptions { optional bool goproto_sizecache_all = 63034; optional bool goproto_unkeyed_all = 63035; - repeated string ignore_internal_struct_field_tags = 63036; + optional string ignore_internal_struct_field_tags = 63036; } extend google.protobuf.MessageOptions { diff --git a/gogoproto/helper.go b/gogoproto/helper.go index 33051ac3ce..b09a52f692 100644 --- a/gogoproto/helper.go +++ b/gogoproto/helper.go @@ -287,14 +287,14 @@ func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string { } return nil } -func GetIgnoreInternalStructFieldTags(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) []*string { +func GetInternalStructFieldTags(file *google_protobuf.FileDescriptorProto) *string { if file == nil { return nil } if file.Options != nil { v, err := proto.GetExtension(file.Options, E_IgnoreInternalStructFieldTags) - if err == nil && v.([]*string) != nil { - return (v.([]*string)) + if err == nil && v.(*string) != nil { + return (v.(*string)) } } return nil diff --git a/protoc-gen-gogo/generator/generator.go b/protoc-gen-gogo/generator/generator.go index 4db996cc09..d0eb2d00d4 100644 --- a/protoc-gen-gogo/generator/generator.go +++ b/protoc-gen-gogo/generator/generator.go @@ -2494,17 +2494,15 @@ func (g *Generator) generateGet(mc *msgCtx, protoField *descriptor.FieldDescript // generateInternalStructFields just adds the XXX_ fields to the message struct. func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []topLevelField) { - ignoreInternalStructFieldTags := gogoproto.GetIgnoreInternalStructFieldTags(g.file.FileDescriptorProto, mc.message.DescriptorProto) + internalStructFieldTags := gogoproto.GetInternalStructFieldTags(g.file.FileDescriptorProto) - resolvedIgnoredTags := "" - if len(ignoreInternalStructFieldTags) > 0 { - for _, el := range ignoreInternalStructFieldTags { - resolvedIgnoredTags = "," + (*el) + "\"-\"" - } + resolvedInternaltags := "" + if internalStructFieldTags != nil { + resolvedInternaltags = ", " + *internalStructFieldTags } if gogoproto.HasUnkeyed(g.file.FileDescriptorProto, mc.message.DescriptorProto) { - g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\"`" + resolvedIgnoredTags) // 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) { @@ -2512,16 +2510,16 @@ func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []to if opts := mc.message.Options; opts != nil && opts.GetMessageSetWireFormat() { messageset = "protobuf_messageset:\"1\" " } - g.P(g.Pkg["proto"], ".XXX_InternalExtensions `", messageset, "json:\"-\"`"+resolvedIgnoredTags) + g.P(g.Pkg["proto"], ".XXX_InternalExtensions `", messageset, "json:\"-\"`"+resolvedInternaltags) } else { - g.P("XXX_extensions\t\t[]byte `protobuf:\"bytes,0,opt\" json:\"-\"`" + resolvedIgnoredTags) + 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:\"-\"`" + resolvedIgnoredTags) + g.P("XXX_unrecognized\t[]byte `json:\"-\"`" + resolvedInternaltags) } if gogoproto.HasSizecache(g.file.FileDescriptorProto, mc.message.DescriptorProto) { - g.P("XXX_sizecache\tint32 `json:\"-\"`" + resolvedIgnoredTags) + g.P("XXX_sizecache\tint32 `json:\"-\"`" + resolvedInternaltags) } } From a508b47f2fde73681c024e91e78c0c58d208d817 Mon Sep 17 00:00:00 2001 From: Andrea Mangione <35958935+MangioneAndrea@users.noreply.github.com> Date: Sun, 16 Jan 2022 22:51:27 +0100 Subject: [PATCH 4/5] rename prop --- gogoproto/gogo.pb.go | 6 +++--- gogoproto/gogo.proto | 2 +- gogoproto/helper.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gogoproto/gogo.pb.go b/gogoproto/gogo.pb.go index 62eebb8052..fafc0435fe 100644 --- a/gogoproto/gogo.pb.go +++ b/gogoproto/gogo.pb.go @@ -363,12 +363,12 @@ var E_GoprotoUnkeyedAll = &proto.ExtensionDesc{ Filename: "gogo.proto", } -var E_IgnoreInternalStructFieldTags = &proto.ExtensionDesc{ +var E_InternalStructFieldTags = &proto.ExtensionDesc{ ExtendedType: (*descriptor.FileOptions)(nil), ExtensionType: (*string)(nil), Field: 64036, - Name: "gogoproto.ignore_internal_struct_field_tags", - Tag: "bytes,63036,rep,name=ignore_internal_struct_field_tags", + Name: "gogoproto.internal_struct_field_tags", + Tag: "bytes,63036,rep,name=internal_struct_field_tags", Filename: "gogoproto/gogo.proto", } diff --git a/gogoproto/gogo.proto b/gogoproto/gogo.proto index 772971df42..cbdfaaa9a4 100644 --- a/gogoproto/gogo.proto +++ b/gogoproto/gogo.proto @@ -88,7 +88,7 @@ extend google.protobuf.FileOptions { optional bool goproto_sizecache_all = 63034; optional bool goproto_unkeyed_all = 63035; - optional string ignore_internal_struct_field_tags = 63036; + optional string internal_struct_field_tags = 63036; } extend google.protobuf.MessageOptions { diff --git a/gogoproto/helper.go b/gogoproto/helper.go index b09a52f692..6b73f00bb9 100644 --- a/gogoproto/helper.go +++ b/gogoproto/helper.go @@ -292,7 +292,7 @@ func GetInternalStructFieldTags(file *google_protobuf.FileDescriptorProto) *stri return nil } if file.Options != nil { - v, err := proto.GetExtension(file.Options, E_IgnoreInternalStructFieldTags) + v, err := proto.GetExtension(file.Options, E_InternalStructFieldTags) if err == nil && v.(*string) != nil { return (v.(*string)) } From d3fa6ff0c0ba55668d2f03c46e0700264e231fbb Mon Sep 17 00:00:00 2001 From: Andrea Mangione <35958935+MangioneAndrea@users.noreply.github.com> Date: Sun, 16 Jan 2022 23:09:26 +0100 Subject: [PATCH 5/5] add internal_struct_field_tags test --- gogoproto/gogo.pb.go | 2 +- test/tags/tags.proto | 1 + test/tags/tags_test.go | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gogoproto/gogo.pb.go b/gogoproto/gogo.pb.go index fafc0435fe..cf22ea778a 100644 --- a/gogoproto/gogo.pb.go +++ b/gogoproto/gogo.pb.go @@ -369,7 +369,7 @@ var E_InternalStructFieldTags = &proto.ExtensionDesc{ Field: 64036, Name: "gogoproto.internal_struct_field_tags", Tag: "bytes,63036,rep,name=internal_struct_field_tags", - Filename: "gogoproto/gogo.proto", + Filename: "gogo.proto", } var E_GoprotoGetters = &proto.ExtensionDesc{ diff --git a/test/tags/tags.proto b/test/tags/tags.proto index 3f82b2382b..3677f2b1fb 100644 --- a/test/tags/tags.proto +++ b/test/tags/tags.proto @@ -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) = ""]; diff --git a/test/tags/tags_test.go b/test/tags/tags_test.go index ca472a4ef7..9b3ca48b3d 100644 --- a/test/tags/tags_test.go +++ b/test/tags/tags_test.go @@ -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, "-") + } + } +}