From 8e813ed1d3828edcbe718b8178d192a2c55eb465 Mon Sep 17 00:00:00 2001 From: Adphi Date: Sat, 26 Jun 2021 19:10:18 +0200 Subject: [PATCH] add embedded message field support --- .gitignore | 2 + patch/go.proto | 4 + patch/gopb/go.pb.go | 112 +++++++++++++------------ patch/patcher.go | 53 ++++++++---- tests/message/message_renames.pb.go | 121 ++++++++++++++++++++++------ tests/message/message_renames.proto | 4 + tests/message/message_test.go | 17 +++- 7 files changed, 223 insertions(+), 90 deletions(-) diff --git a/.gitignore b/.gitignore index 239ae78..747e3bb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ *.out *.so *.test +debug +.idea diff --git a/patch/go.proto b/patch/go.proto index 7141c07..eaadb1e 100644 --- a/patch/go.proto +++ b/patch/go.proto @@ -16,6 +16,10 @@ message Options { // For an enum value, this renames the generated Go const. optional string name = 1; + // The embedded option changes the field as embedded in the generated Go struct. + // Only message types are allowed to be embedded, oneof fields are not embeddable. + optional bool embedded = 2; + // The getter option renames the generated getter method (default: Get) // so a custom getter can be implemented in its place. optional string getter = 10; // TODO: implement this diff --git a/patch/gopb/go.pb.go b/patch/gopb/go.pb.go index e1991ea..dacd7bb 100644 --- a/patch/gopb/go.pb.go +++ b/patch/gopb/go.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.26.0 -// protoc v3.15.6 +// protoc v3.17.1 // source: patch/go.proto package gopb @@ -34,6 +34,9 @@ type Options struct { // For an enum, this renames the generated Go type. // For an enum value, this renames the generated Go const. Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` + // The embedded option changes the field as embedded in the generated Go struct. + // Only message types are allowed to be embedded, oneof fields are not embeddable. + Embedded *bool `protobuf:"varint,2,opt,name=embedded" json:"embedded,omitempty"` // The getter option renames the generated getter method (default: Get) // so a custom getter can be implemented in its place. Getter *string `protobuf:"bytes,10,opt,name=getter" json:"getter,omitempty"` // TODO: implement this @@ -88,6 +91,13 @@ func (x *Options) GetName() string { return "" } +func (x *Options) GetEmbedded() bool { + if x != nil && x.Embedded != nil { + return *x.Embedded + } + return false +} + func (x *Options) GetGetter() string { if x != nil && x.Getter != nil { return *x.Getter @@ -317,56 +327,58 @@ var file_patch_go_proto_rawDesc = []byte{ 0x0a, 0x0e, 0x70, 0x61, 0x74, 0x63, 0x68, 0x2f, 0x67, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x67, 0x6f, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x8a, 0x01, 0x0a, 0x07, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa6, 0x01, 0x0a, 0x07, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x67, 0x65, 0x74, 0x74, 0x65, 0x72, - 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x67, 0x65, 0x74, 0x74, 0x65, 0x72, 0x12, 0x12, - 0x0a, 0x04, 0x74, 0x61, 0x67, 0x73, 0x18, 0x14, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, - 0x67, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x18, 0x1e, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x12, 0x23, - 0x0a, 0x0d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, - 0x1f, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x4e, - 0x61, 0x6d, 0x65, 0x22, 0xc3, 0x01, 0x0a, 0x0b, 0x4c, 0x69, 0x6e, 0x74, 0x4f, 0x70, 0x74, 0x69, - 0x6f, 0x6e, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x6c, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x03, 0x61, 0x6c, 0x6c, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x73, 0x12, 0x16, 0x0a, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x08, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x6e, 0x75, - 0x6d, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x12, - 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, - 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x65, 0x78, 0x74, 0x65, 0x6e, - 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x65, 0x78, 0x74, - 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x69, 0x6e, 0x69, 0x74, 0x69, - 0x61, 0x6c, 0x69, 0x73, 0x6d, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x69, 0x6e, - 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69, 0x73, 0x6d, 0x73, 0x3a, 0x47, 0x0a, 0x07, 0x6d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x12, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x67, - 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x3a, 0x41, 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x1d, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, - 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, - 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, - 0x66, 0x69, 0x65, 0x6c, 0x64, 0x3a, 0x41, 0x0a, 0x05, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x12, 0x1d, - 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, - 0x2e, 0x4f, 0x6e, 0x65, 0x6f, 0x66, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, - 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, - 0x73, 0x52, 0x05, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x3a, 0x3e, 0x0a, 0x04, 0x65, 0x6e, 0x75, 0x6d, - 0x12, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, - 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, - 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, - 0x6e, 0x73, 0x52, 0x04, 0x65, 0x6e, 0x75, 0x6d, 0x3a, 0x45, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x12, 0x21, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x4f, 0x70, 0x74, - 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, - 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, - 0x42, 0x0a, 0x04, 0x6c, 0x69, 0x6e, 0x74, 0x12, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4f, 0x70, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x67, - 0x6f, 0x2e, 0x4c, 0x69, 0x6e, 0x74, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x04, 0x6c, - 0x69, 0x6e, 0x74, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x61, 0x6c, 0x74, 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x70, 0x61, 0x74, 0x63, - 0x68, 0x2f, 0x70, 0x61, 0x74, 0x63, 0x68, 0x2f, 0x67, 0x6f, 0x70, 0x62, + 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x65, 0x6d, 0x62, 0x65, 0x64, 0x64, + 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x65, 0x6d, 0x62, 0x65, 0x64, 0x64, + 0x65, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x67, 0x65, 0x74, 0x74, 0x65, 0x72, 0x18, 0x0a, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x06, 0x67, 0x65, 0x74, 0x74, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x61, + 0x67, 0x73, 0x18, 0x14, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x61, 0x67, 0x73, 0x12, 0x1a, + 0x0a, 0x08, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x18, 0x1e, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x08, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x12, 0x23, 0x0a, 0x0d, 0x73, 0x74, + 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x1f, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0c, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x65, 0x72, 0x4e, 0x61, 0x6d, 0x65, 0x22, + 0xc3, 0x01, 0x0a, 0x0b, 0x4c, 0x69, 0x6e, 0x74, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, + 0x10, 0x0a, 0x03, 0x61, 0x6c, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x03, 0x61, 0x6c, + 0x6c, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x08, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x12, 0x16, 0x0a, + 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x66, + 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x76, + 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, + 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, + 0x6f, 0x6e, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69, 0x73, + 0x6d, 0x73, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0b, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x61, + 0x6c, 0x69, 0x73, 0x6d, 0x73, 0x3a, 0x47, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x12, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x3a, 0x41, + 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, + 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, + 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x66, 0x69, 0x65, 0x6c, + 0x64, 0x3a, 0x41, 0x0a, 0x05, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4f, 0x6e, 0x65, + 0x6f, 0x66, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x6f, + 0x6e, 0x65, 0x6f, 0x66, 0x3a, 0x3e, 0x0a, 0x04, 0x65, 0x6e, 0x75, 0x6d, 0x12, 0x1c, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, + 0x6e, 0x75, 0x6d, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x04, + 0x65, 0x6e, 0x75, 0x6d, 0x3a, 0x45, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x21, 0x2e, + 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x67, 0x6f, 0x2e, 0x4f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x42, 0x0a, 0x04, 0x6c, + 0x69, 0x6e, 0x74, 0x12, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x18, 0xd9, 0x36, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x67, 0x6f, 0x2e, 0x4c, 0x69, + 0x6e, 0x74, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x04, 0x6c, 0x69, 0x6e, 0x74, 0x42, + 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x6c, + 0x74, 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x70, 0x61, 0x74, 0x63, 0x68, 0x2f, 0x70, 0x61, + 0x74, 0x63, 0x68, 0x2f, 0x67, 0x6f, 0x70, 0x62, } var ( diff --git a/patch/patcher.go b/patch/patcher.go index 0dc007d..eac767a 100644 --- a/patch/patcher.go +++ b/patch/patcher.go @@ -47,6 +47,8 @@ type Patcher struct { objectRenames map[types.Object]string tags map[protogen.GoIdent]string fieldTags map[types.Object]string + embedded map[protogen.GoIdent]string + fieldEmbeds map[types.Object]string } // NewPatcher returns an initialized Patcher for gen. @@ -63,6 +65,8 @@ func NewPatcher(gen *protogen.Plugin) (*Patcher, error) { objectRenames: make(map[types.Object]string), tags: make(map[protogen.GoIdent]string), fieldTags: make(map[types.Object]string), + embedded: make(map[protogen.GoIdent]string), + fieldEmbeds: make(map[types.Object]string), } return p, p.scan() } @@ -243,7 +247,7 @@ func (p *Patcher) scanOneof(o *protogen.Oneof) { newName = lint.Name(newName, lints.InitialismsMap()) } if newName != "" { - p.RenameField(ident.WithChild(m.GoIdent, o.GoName), newName) // Oneof + p.RenameField(ident.WithChild(m.GoIdent, o.GoName), newName, false) // Oneof p.RenameMethod(ident.WithChild(m.GoIdent, "Get"+o.GoName), "Get"+newName) // Getter ifName := ident.WithPrefix(o.GoIdent, "is") newIfName := "is" + p.nameFor(m.GoIdent) + "_" + newName @@ -276,14 +280,24 @@ func (p *Patcher) scanField(f *protogen.Field) { } newName = lint.Name(newName, lints.InitialismsMap()) } + // check embed for message types + embedded := f.Message != nil && f.Oneof == nil && opts.GetEmbedded() + if embedded { + // use the embedded field message type's go name or rename option if defined + if mOpts := messageOptions(f.Message); mOpts.GetName() != "" { + newName = mOpts.GetName() + } else { + newName = f.Message.GoIdent.GoName + } + } if newName != "" { if o != nil { - p.RenameType(f.GoIdent, p.nameFor(m.GoIdent)+"_"+newName) // Oneof wrapper struct - p.RenameField(ident.WithChild(f.GoIdent, f.GoName), newName) // Oneof wrapper field + p.RenameType(f.GoIdent, p.nameFor(m.GoIdent)+"_"+newName) // Oneof wrapper struct + p.RenameField(ident.WithChild(f.GoIdent, f.GoName), newName, false) // Oneof wrapper field (not embeddable) ifName := ident.WithPrefix(o.GoIdent, "is") p.RenameMethod(ident.WithChild(f.GoIdent, ifName.GoName), p.nameFor(ifName)) // Oneof interface method } else { - p.RenameField(ident.WithChild(m.GoIdent, f.GoName), newName) // Field + p.RenameField(ident.WithChild(m.GoIdent, f.GoName), newName, embedded) // Field } p.RenameMethod(ident.WithChild(m.GoIdent, "Get"+f.GoName), "Get"+newName) // Getter } @@ -344,9 +358,12 @@ func (p *Patcher) RenameValue(id protogen.GoIdent, newName string) { // The id argument specifies a GoName from GoImportPath, e.g.: "github.com/org/repo/example".FooMessage.BarField // newName should be the unqualified name (after the dot). // The value of id.GoName should be the original generated identifier name, not a renamed identifier. -func (p *Patcher) RenameField(id protogen.GoIdent, newName string) { +func (p *Patcher) RenameField(id protogen.GoIdent, newName string, embedded bool) { p.renames[id] = newName p.fieldRenames[id] = newName + if embedded { + p.embedded[id] = newName + } log.Printf("Rename field:\t%s.%s → %s", id.GoImportPath, id.GoName, newName) } @@ -510,6 +527,9 @@ func (p *Patcher) checkGoFiles() error { continue } p.objectRenames[obj] = name + if _, ok := p.embedded[id]; ok { + p.fieldEmbeds[obj] = name + } } // Map struct tags. @@ -648,7 +668,7 @@ func (p *Patcher) serializeGoFiles(res *pluginpb.CodeGeneratorResponse) error { func (p *Patcher) patchGoFiles() error { log.Printf("\nDefs") for id, obj := range p.info.Defs { - p.patchIdent(id, obj) + p.patchIdent(id, obj, true) p.patchTags(id, obj) // if id.IsExported() { // f := p.fset.File(id.NamePos) @@ -658,27 +678,32 @@ func (p *Patcher) patchGoFiles() error { log.Printf("\nUses\n") for id, obj := range p.info.Uses { - p.patchIdent(id, obj) + p.patchIdent(id, obj, false) } log.Printf("\nUnresolved\n") for _, f := range p.filesByName { for _, id := range f.Unresolved { - p.patchIdent(id, nil) + p.patchIdent(id, nil, false) } } return nil } -func (p *Patcher) patchIdent(id *ast.Ident, obj types.Object) { +func (p *Patcher) patchIdent(id *ast.Ident, obj types.Object, isDecl bool) { name := p.objectRenames[obj] - if name != "" { - p.patchComments(id, name) - id.Name = name - log.Printf("Renamed %s:\t%s → %s", typeString(obj), id.Name, name) - } else { + if name == "" { // log.Printf("Unresolved:\t%v", id) + return + } + p.patchComments(id, name) + if _, ok := p.fieldEmbeds[obj]; ok && isDecl { + log.Printf("Renamed %s:\t%s → %s (embedded)", typeString(obj), id.Name, name) + id.Name = "" + } else { + log.Printf("Renamed %s:\t%s → %s", typeString(obj), id.Name, name) + id.Name = name } } diff --git a/tests/message/message_renames.pb.go b/tests/message/message_renames.pb.go index 535922f..ca228a1 100644 --- a/tests/message/message_renames.pb.go +++ b/tests/message/message_renames.pb.go @@ -1,17 +1,19 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.26.0 -// protoc v3.15.6 +// protoc v3.17.1 // source: tests/message/message_renames.proto package message import ( - _ "github.com/alta/protopatch/patch/gopb" - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + + _ "github.com/alta/protopatch/patch/gopb" ) const ( @@ -280,6 +282,53 @@ func (x *MessageWithRenamedField) GetID() int32 { return 0 } +type MessageWithEmbeddedField struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + *RenamedOuterMessage `protobuf:"bytes,5,opt,name=embedded_renamed_message,json=embeddedRenamedMessage,proto3" json:"embedded_renamed_message,omitempty"` +} + +func (x *MessageWithEmbeddedField) Reset() { + *x = MessageWithEmbeddedField{} + if protoimpl.UnsafeEnabled { + mi := &file_tests_message_message_renames_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *MessageWithEmbeddedField) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*MessageWithEmbeddedField) ProtoMessage() {} + +func (x *MessageWithEmbeddedField) ProtoReflect() protoreflect.Message { + mi := &file_tests_message_message_renames_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use MessageWithEmbeddedField.ProtoReflect.Descriptor instead. +func (*MessageWithEmbeddedField) Descriptor() ([]byte, []int) { + return file_tests_message_message_renames_proto_rawDescGZIP(), []int{5} +} + +func (x *MessageWithEmbeddedField) GetRenamedOuterMessage() *RenamedOuterMessage { + if x != nil { + return x.RenamedOuterMessage + } + return nil +} + type RenamedOuterMessage_InnerMessage struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -289,7 +338,7 @@ type RenamedOuterMessage_InnerMessage struct { func (x *RenamedOuterMessage_InnerMessage) Reset() { *x = RenamedOuterMessage_InnerMessage{} if protoimpl.UnsafeEnabled { - mi := &file_tests_message_message_renames_proto_msgTypes[5] + mi := &file_tests_message_message_renames_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -302,7 +351,7 @@ func (x *RenamedOuterMessage_InnerMessage) String() string { func (*RenamedOuterMessage_InnerMessage) ProtoMessage() {} func (x *RenamedOuterMessage_InnerMessage) ProtoReflect() protoreflect.Message { - mi := &file_tests_message_message_renames_proto_msgTypes[5] + mi := &file_tests_message_message_renames_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -327,7 +376,7 @@ type RenamedInnerMessage struct { func (x *RenamedInnerMessage) Reset() { *x = RenamedInnerMessage{} if protoimpl.UnsafeEnabled { - mi := &file_tests_message_message_renames_proto_msgTypes[6] + mi := &file_tests_message_message_renames_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -340,7 +389,7 @@ func (x *RenamedInnerMessage) String() string { func (*RenamedInnerMessage) ProtoMessage() {} func (x *RenamedInnerMessage) ProtoReflect() protoreflect.Message { - mi := &file_tests_message_message_renames_proto_msgTypes[6] + mi := &file_tests_message_message_renames_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -393,11 +442,19 @@ var file_tests_message_message_renames_proto_rawDesc = []byte{ 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x33, 0x0a, 0x17, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x57, 0x69, 0x74, 0x68, 0x52, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x64, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x18, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, - 0x42, 0x08, 0xca, 0xb5, 0x03, 0x04, 0x0a, 0x02, 0x49, 0x44, 0x52, 0x02, 0x69, 0x64, 0x42, 0x2a, - 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x6c, 0x74, - 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x70, 0x61, 0x74, 0x63, 0x68, 0x2f, 0x74, 0x65, 0x73, - 0x74, 0x73, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x42, 0x08, 0xca, 0xb5, 0x03, 0x04, 0x0a, 0x02, 0x49, 0x44, 0x52, 0x02, 0x69, 0x64, 0x22, 0x81, + 0x01, 0x0a, 0x18, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x57, 0x69, 0x74, 0x68, 0x45, 0x6d, + 0x62, 0x65, 0x64, 0x64, 0x65, 0x64, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x65, 0x0a, 0x18, 0x65, + 0x6d, 0x62, 0x65, 0x64, 0x64, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x64, 0x5f, + 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x23, 0x2e, + 0x74, 0x65, 0x73, 0x74, 0x73, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x4f, 0x72, + 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x42, 0x06, 0xca, 0xb5, 0x03, 0x02, 0x10, 0x01, 0x52, 0x16, 0x65, 0x6d, 0x62, 0x65, + 0x64, 0x64, 0x65, 0x64, 0x52, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x61, 0x6c, 0x74, 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x70, 0x61, 0x74, 0x63, 0x68, + 0x2f, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -412,24 +469,26 @@ func file_tests_message_message_renames_proto_rawDescGZIP() []byte { return file_tests_message_message_renames_proto_rawDescData } -var file_tests_message_message_renames_proto_msgTypes = make([]protoimpl.MessageInfo, 7) +var file_tests_message_message_renames_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_tests_message_message_renames_proto_goTypes = []interface{}{ (*Frank)(nil), // 0: tests.message.Francis (*RenamedOneofMessage)(nil), // 1: tests.message.OriginalOneofMessage (*RenamedOuterMessage)(nil), // 2: tests.message.OriginalOuterMessage (*OuterMessageWithRenamedInnerMessage)(nil), // 3: tests.message.OuterMessageWithRenamedInnerMessage (*MessageWithRenamedField)(nil), // 4: tests.message.MessageWithRenamedField - (*RenamedOuterMessage_InnerMessage)(nil), // 5: tests.message.OriginalOuterMessage.InnerMessage - (*RenamedInnerMessage)(nil), // 6: tests.message.OuterMessageWithRenamedInnerMessage.InnerMessage + (*MessageWithEmbeddedField)(nil), // 5: tests.message.MessageWithEmbeddedField + (*RenamedOuterMessage_InnerMessage)(nil), // 6: tests.message.OriginalOuterMessage.InnerMessage + (*RenamedInnerMessage)(nil), // 7: tests.message.OuterMessageWithRenamedInnerMessage.InnerMessage } var file_tests_message_message_renames_proto_depIdxs = []int32{ - 5, // 0: tests.message.OriginalOuterMessage.inner:type_name -> tests.message.OriginalOuterMessage.InnerMessage - 6, // 1: tests.message.OuterMessageWithRenamedInnerMessage.inner:type_name -> tests.message.OuterMessageWithRenamedInnerMessage.InnerMessage - 2, // [2:2] is the sub-list for method output_type - 2, // [2:2] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 6, // 0: tests.message.OriginalOuterMessage.inner:type_name -> tests.message.OriginalOuterMessage.InnerMessage + 7, // 1: tests.message.OuterMessageWithRenamedInnerMessage.inner:type_name -> tests.message.OuterMessageWithRenamedInnerMessage.InnerMessage + 2, // 2: tests.message.MessageWithEmbeddedField.embedded_renamed_message:type_name -> tests.message.OriginalOuterMessage + 3, // [3:3] is the sub-list for method output_type + 3, // [3:3] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_tests_message_message_renames_proto_init() } @@ -499,7 +558,7 @@ func file_tests_message_message_renames_proto_init() { } } file_tests_message_message_renames_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*RenamedOuterMessage_InnerMessage); i { + switch v := v.(*MessageWithEmbeddedField); i { case 0: return &v.state case 1: @@ -511,6 +570,18 @@ func file_tests_message_message_renames_proto_init() { } } file_tests_message_message_renames_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RenamedOuterMessage_InnerMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_tests_message_message_renames_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*RenamedInnerMessage); i { case 0: return &v.state @@ -533,7 +604,7 @@ func file_tests_message_message_renames_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_tests_message_message_renames_proto_rawDesc, NumEnums: 0, - NumMessages: 7, + NumMessages: 8, NumExtensions: 0, NumServices: 0, }, diff --git a/tests/message/message_renames.proto b/tests/message/message_renames.proto index a0ed29f..4b57819 100644 --- a/tests/message/message_renames.proto +++ b/tests/message/message_renames.proto @@ -34,3 +34,7 @@ message OuterMessageWithRenamedInnerMessage { message MessageWithRenamedField { int32 id = 1 [(go.field).name = 'ID']; } + +message MessageWithEmbeddedField { + OriginalOuterMessage embedded_renamed_message = 5 [(go.field).embedded = true]; +} diff --git a/tests/message/message_test.go b/tests/message/message_test.go index 6cf3cee..0d2b042 100644 --- a/tests/message/message_test.go +++ b/tests/message/message_test.go @@ -3,8 +3,9 @@ package message import ( "testing" - "github.com/alta/protopatch/tests" "google.golang.org/protobuf/proto" + + "github.com/alta/protopatch/tests" ) func TestBasicMessage(t *testing.T) { @@ -60,6 +61,20 @@ func TestMessageWithRenamedField(t *testing.T) { var _ int32 = m.GetID() } +func TestMessageWithEmbeddedField(t *testing.T) { + m := &MessageWithEmbeddedField{ + RenamedOuterMessage: &RenamedOuterMessage{ + Inner: &RenamedOuterMessage_InnerMessage{ + + }, + }, + } + tests.ValidateMessage(t, m) + if &m.Inner != &m.RenamedOuterMessage.Inner { + t.Error("RenamedOuterMessage message is not embedded") + } +} + func TestMessageWithStructTags(t *testing.T) { m := &MessageWithTags{} tests.ValidateTag(t, m, "Value", "test", "value")