From 4cb495bb3065a6dc597fb7bf13ddd64f46ce00cb Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 11 Sep 2024 18:02:03 +0800 Subject: [PATCH] Add new messagemagic (#5) * feat: support messagemagic * test: fix testdata --- go.mod | 1 + go.sum | 2 + messagemagic/messagemagic.go | 32 +++++ messagemagic/messagemagic_test.go | 174 +++++++++++++++++++++++ testdata/gen/dummy/v1/message.pb.go | 195 +++++++++++++++++++++++--- testdata/proto/dummy/v1/message.proto | 14 ++ 6 files changed, 398 insertions(+), 20 deletions(-) create mode 100644 messagemagic/messagemagic.go create mode 100644 messagemagic/messagemagic_test.go diff --git a/go.mod b/go.mod index fad0d90..8fe4d29 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/gochore/protomagic go 1.23 require ( + github.com/gochore/pt v1.3.0 github.com/stretchr/testify v1.9.0 google.golang.org/protobuf v1.34.2 ) diff --git a/go.sum b/go.sum index 4cda4a4..24da598 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gochore/pt v1.3.0 h1:E/cLFk84WyNDCwFvgGZezLo656KEJNMUAznFcb5NYP0= +github.com/gochore/pt v1.3.0/go.mod h1:VgAadJxZUjqIPhLPrfC8BgTWQzi1tVIrQOefxBC64PQ= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/messagemagic/messagemagic.go b/messagemagic/messagemagic.go new file mode 100644 index 0000000..67d32c8 --- /dev/null +++ b/messagemagic/messagemagic.go @@ -0,0 +1,32 @@ +package messagemagic + +import ( + "google.golang.org/protobuf/proto" +) + +// Patch patches the message with the patch. +// It copies all not optional fields from the patch to the message. +// For optional fields, it copies the value only if the patch has the field. +// Otherwise, it keeps the value in the message unchanged. + +func Patch[T proto.Message](msg, patch T) T { + dst := proto.Clone(msg).(T) + + dstRefl, srcRefl := dst.ProtoReflect(), patch.ProtoReflect() + for i := range srcRefl.Descriptor().Fields().Len() { + fd := srcRefl.Descriptor().Fields().Get(i) + if fd.HasOptionalKeyword() { + if srcRefl.Has(fd) { + dstRefl.Set(fd, srcRefl.Get(fd)) + } + } else { + if srcRefl.Has(fd) { + dstRefl.Set(fd, srcRefl.Get(fd)) + } else { + dstRefl.Clear(fd) + } + } + } + + return dst +} diff --git a/messagemagic/messagemagic_test.go b/messagemagic/messagemagic_test.go new file mode 100644 index 0000000..5c24b7a --- /dev/null +++ b/messagemagic/messagemagic_test.go @@ -0,0 +1,174 @@ +package messagemagic + +import ( + "encoding/json" + "testing" + + dummyv1 "github.com/gochore/protomagic/testdata/gen/dummy/v1" + + "github.com/gochore/pt" + "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/proto" +) + +func assertMessageEqual(t *testing.T, expected, actual proto.Message) { + if !assert.True(t, proto.Equal(expected, actual)) { + expectedJson, _ := json.Marshal(expected) + actualJson, _ := json.Marshal(actual) + assert.JSONEq(t, string(expectedJson), string(actualJson)) // to show the diff + } +} + +func TestPatch(t *testing.T) { + t.Run("regular", func(t *testing.T) { + msg := &dummyv1.DummyA{ + Name: "name", + Value: 1, + Values: []string{"a", "b"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO, + ConfigA: &dummyv1.DummyConfigA{Name: "name"}, + OName: pt.P("o_name"), + OValue: pt.P[int32](2), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR), + OConfigA: &dummyv1.DummyConfigA{Name: "name"}, + } + patch := &dummyv1.DummyA{ + Name: "name2", + Value: 2, + Values: []string{"c", "d"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR, + ConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + OName: pt.P("o_name2"), + OValue: pt.P[int32](3), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO), + OConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + } + want := &dummyv1.DummyA{ + Name: "name2", + Value: 2, + Values: []string{"c", "d"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR, + ConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + OName: pt.P("o_name2"), + OValue: pt.P[int32](3), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO), + OConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + } + got := Patch(msg, patch) + assertMessageEqual(t, want, got) + }) + + t.Run("empty patch", func(t *testing.T) { + msg := &dummyv1.DummyA{ + Name: "name", + Value: 1, + Values: []string{"a", "b"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO, + ConfigA: &dummyv1.DummyConfigA{Name: "name"}, + OName: pt.P("o_name"), + OValue: pt.P[int32](2), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR), + OConfigA: &dummyv1.DummyConfigA{Name: "name"}, + } + patch := &dummyv1.DummyA{ + Name: "", + Value: 0, + Values: nil, + TestType: 0, + ConfigA: nil, + OName: nil, + OValue: nil, + OTestType: nil, + OConfigA: nil, + } + want := &dummyv1.DummyA{ + Name: "", + Value: 0, + Values: nil, + TestType: 0, + ConfigA: nil, + OName: pt.P("o_name"), + OValue: pt.P[int32](2), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR), + OConfigA: &dummyv1.DummyConfigA{Name: "name"}, + } + got := Patch(msg, patch) + assertMessageEqual(t, want, got) + }) + + t.Run("empty msg", func(t *testing.T) { + msg := &dummyv1.DummyA{ + Name: "", + Value: 0, + Values: nil, + TestType: 0, + ConfigA: nil, + OName: nil, + OValue: nil, + OTestType: nil, + OConfigA: nil, + } + patch := &dummyv1.DummyA{ + Name: "name2", + Value: 2, + Values: []string{"c", "d"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR, + ConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + OName: pt.P("o_name2"), + OValue: pt.P[int32](3), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO), + OConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + } + want := &dummyv1.DummyA{ + Name: "name2", + Value: 2, + Values: []string{"c", "d"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR, + ConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + OName: pt.P("o_name2"), + OValue: pt.P[int32](3), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO), + OConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + } + got := Patch(msg, patch) + assertMessageEqual(t, want, got) + }) + + t.Run("zero patch", func(t *testing.T) { + msg := &dummyv1.DummyA{ + Name: "name", + Value: 1, + Values: []string{"a", "b"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_FOO, + ConfigA: &dummyv1.DummyConfigA{Name: "name"}, + OName: pt.P("o_name"), + OValue: pt.P[int32](2), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR), + OConfigA: &dummyv1.DummyConfigA{Name: "name"}, + } + patch := &dummyv1.DummyA{ + Name: "name2", + Value: 2, + Values: []string{"c", "d"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR, + ConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + OName: pt.P(""), + OValue: pt.P[int32](0), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_UNSPECIFIED), + OConfigA: &dummyv1.DummyConfigA{}, + } + want := &dummyv1.DummyA{ + Name: "name2", + Value: 2, + Values: []string{"c", "d"}, + TestType: dummyv1.TestEnumType_TEST_ENUM_TYPE_BAR, + ConfigA: &dummyv1.DummyConfigA{Name: "name2"}, + OName: pt.P(""), + OValue: pt.P[int32](0), + OTestType: pt.P(dummyv1.TestEnumType_TEST_ENUM_TYPE_UNSPECIFIED), + OConfigA: &dummyv1.DummyConfigA{}, + } + got := Patch(msg, patch) + assertMessageEqual(t, want, got) + }) +} diff --git a/testdata/gen/dummy/v1/message.pb.go b/testdata/gen/dummy/v1/message.pb.go index 1ab31f2..86b87fe 100644 --- a/testdata/gen/dummy/v1/message.pb.go +++ b/testdata/gen/dummy/v1/message.pb.go @@ -225,6 +225,117 @@ func (x *DummyConfigB) GetValue() int64 { return 0 } +type DummyA struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Value int32 `protobuf:"varint,2,opt,name=value,proto3" json:"value,omitempty"` + Values []string `protobuf:"bytes,3,rep,name=values,proto3" json:"values,omitempty"` + TestType TestEnumType `protobuf:"varint,4,opt,name=test_type,json=testType,proto3,enum=dummy.v1.TestEnumType" json:"test_type,omitempty"` + ConfigA *DummyConfigA `protobuf:"bytes,11,opt,name=config_a,json=configA,proto3" json:"config_a,omitempty"` + OName *string `protobuf:"bytes,21,opt,name=o_name,json=oName,proto3,oneof" json:"o_name,omitempty"` + OValue *int32 `protobuf:"varint,22,opt,name=o_value,json=oValue,proto3,oneof" json:"o_value,omitempty"` + OTestType *TestEnumType `protobuf:"varint,23,opt,name=o_test_type,json=oTestType,proto3,enum=dummy.v1.TestEnumType,oneof" json:"o_test_type,omitempty"` + OConfigA *DummyConfigA `protobuf:"bytes,24,opt,name=o_config_a,json=oConfigA,proto3,oneof" json:"o_config_a,omitempty"` +} + +func (x *DummyA) Reset() { + *x = DummyA{} + if protoimpl.UnsafeEnabled { + mi := &file_dummy_v1_message_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DummyA) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DummyA) ProtoMessage() {} + +func (x *DummyA) ProtoReflect() protoreflect.Message { + mi := &file_dummy_v1_message_proto_msgTypes[3] + 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 DummyA.ProtoReflect.Descriptor instead. +func (*DummyA) Descriptor() ([]byte, []int) { + return file_dummy_v1_message_proto_rawDescGZIP(), []int{3} +} + +func (x *DummyA) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *DummyA) GetValue() int32 { + if x != nil { + return x.Value + } + return 0 +} + +func (x *DummyA) GetValues() []string { + if x != nil { + return x.Values + } + return nil +} + +func (x *DummyA) GetTestType() TestEnumType { + if x != nil { + return x.TestType + } + return TestEnumType_TEST_ENUM_TYPE_UNSPECIFIED +} + +func (x *DummyA) GetConfigA() *DummyConfigA { + if x != nil { + return x.ConfigA + } + return nil +} + +func (x *DummyA) GetOName() string { + if x != nil && x.OName != nil { + return *x.OName + } + return "" +} + +func (x *DummyA) GetOValue() int32 { + if x != nil && x.OValue != nil { + return *x.OValue + } + return 0 +} + +func (x *DummyA) GetOTestType() TestEnumType { + if x != nil && x.OTestType != nil { + return *x.OTestType + } + return TestEnumType_TEST_ENUM_TYPE_UNSPECIFIED +} + +func (x *DummyA) GetOConfigA() *DummyConfigA { + if x != nil { + return x.OConfigA + } + return nil +} + var File_dummy_v1_message_proto protoreflect.FileDescriptor var file_dummy_v1_message_proto_rawDesc = []byte{ @@ -256,17 +367,43 @@ var file_dummy_v1_message_proto_rawDesc = []byte{ 0x0c, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x42, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, - 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x9a, 0x01, 0x0a, 0x0c, 0x63, 0x6f, 0x6d, 0x2e, - 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x2e, 0x76, 0x31, 0x42, 0x0c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, - 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x63, 0x68, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x6d, 0x61, 0x67, 0x69, 0x63, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, - 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x2f, 0x76, 0x31, 0x3b, 0x64, 0x75, - 0x6d, 0x6d, 0x79, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x44, 0x58, 0x58, 0xaa, 0x02, 0x08, 0x44, 0x75, - 0x6d, 0x6d, 0x79, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x5c, 0x56, - 0x31, 0xe2, 0x02, 0x14, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, - 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x09, 0x44, 0x75, 0x6d, 0x6d, 0x79, - 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x9a, 0x03, 0x0a, 0x06, 0x44, 0x75, 0x6d, 0x6d, + 0x79, 0x41, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a, 0x06, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x73, 0x12, 0x33, 0x0a, 0x09, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x79, 0x70, + 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x16, 0x2e, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x2e, + 0x76, 0x31, 0x2e, 0x54, 0x65, 0x73, 0x74, 0x45, 0x6e, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x52, + 0x08, 0x74, 0x65, 0x73, 0x74, 0x54, 0x79, 0x70, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x63, 0x6f, 0x6e, + 0x66, 0x69, 0x67, 0x5f, 0x61, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x64, 0x75, + 0x6d, 0x6d, 0x79, 0x2e, 0x76, 0x31, 0x2e, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x43, 0x6f, 0x6e, 0x66, + 0x69, 0x67, 0x41, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x41, 0x12, 0x1a, 0x0a, 0x06, + 0x6f, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x15, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x05, + 0x6f, 0x4e, 0x61, 0x6d, 0x65, 0x88, 0x01, 0x01, 0x12, 0x1c, 0x0a, 0x07, 0x6f, 0x5f, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x18, 0x16, 0x20, 0x01, 0x28, 0x05, 0x48, 0x01, 0x52, 0x06, 0x6f, 0x56, 0x61, + 0x6c, 0x75, 0x65, 0x88, 0x01, 0x01, 0x12, 0x3b, 0x0a, 0x0b, 0x6f, 0x5f, 0x74, 0x65, 0x73, 0x74, + 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x17, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x16, 0x2e, 0x64, 0x75, + 0x6d, 0x6d, 0x79, 0x2e, 0x76, 0x31, 0x2e, 0x54, 0x65, 0x73, 0x74, 0x45, 0x6e, 0x75, 0x6d, 0x54, + 0x79, 0x70, 0x65, 0x48, 0x02, 0x52, 0x09, 0x6f, 0x54, 0x65, 0x73, 0x74, 0x54, 0x79, 0x70, 0x65, + 0x88, 0x01, 0x01, 0x12, 0x39, 0x0a, 0x0a, 0x6f, 0x5f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x5f, + 0x61, 0x18, 0x18, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x2e, + 0x76, 0x31, 0x2e, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x41, 0x48, + 0x03, 0x52, 0x08, 0x6f, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x41, 0x88, 0x01, 0x01, 0x42, 0x09, + 0x0a, 0x07, 0x5f, 0x6f, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x6f, 0x5f, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x0e, 0x0a, 0x0c, 0x5f, 0x6f, 0x5f, 0x74, 0x65, 0x73, 0x74, + 0x5f, 0x74, 0x79, 0x70, 0x65, 0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x6f, 0x5f, 0x63, 0x6f, 0x6e, 0x66, + 0x69, 0x67, 0x5f, 0x61, 0x42, 0x9a, 0x01, 0x0a, 0x0c, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x75, 0x6d, + 0x6d, 0x79, 0x2e, 0x76, 0x31, 0x42, 0x0c, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x50, 0x72, + 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x67, 0x6f, 0x63, 0x68, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x6d, + 0x61, 0x67, 0x69, 0x63, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x67, 0x65, + 0x6e, 0x2f, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x2f, 0x76, 0x31, 0x3b, 0x64, 0x75, 0x6d, 0x6d, 0x79, + 0x76, 0x31, 0xa2, 0x02, 0x03, 0x44, 0x58, 0x58, 0xaa, 0x02, 0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, + 0x2e, 0x56, 0x31, 0xca, 0x02, 0x08, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x5c, 0x56, 0x31, 0xe2, 0x02, + 0x14, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, + 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x09, 0x44, 0x75, 0x6d, 0x6d, 0x79, 0x3a, 0x3a, 0x56, + 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -281,23 +418,28 @@ func file_dummy_v1_message_proto_rawDescGZIP() []byte { return file_dummy_v1_message_proto_rawDescData } -var file_dummy_v1_message_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_dummy_v1_message_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_dummy_v1_message_proto_goTypes = []any{ (*Dummy)(nil), // 0: dummy.v1.Dummy (*DummyConfigA)(nil), // 1: dummy.v1.DummyConfigA (*DummyConfigB)(nil), // 2: dummy.v1.DummyConfigB - (TestEnumType)(0), // 3: dummy.v1.TestEnumType + (*DummyA)(nil), // 3: dummy.v1.DummyA + (TestEnumType)(0), // 4: dummy.v1.TestEnumType } var file_dummy_v1_message_proto_depIdxs = []int32{ - 3, // 0: dummy.v1.Dummy.test_type:type_name -> dummy.v1.TestEnumType + 4, // 0: dummy.v1.Dummy.test_type:type_name -> dummy.v1.TestEnumType 1, // 1: dummy.v1.Dummy.config_a:type_name -> dummy.v1.DummyConfigA 2, // 2: dummy.v1.Dummy.config_b:type_name -> dummy.v1.DummyConfigB 2, // 3: dummy.v1.Dummy.config_b2:type_name -> dummy.v1.DummyConfigB - 4, // [4:4] is the sub-list for method output_type - 4, // [4:4] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name + 4, // 4: dummy.v1.DummyA.test_type:type_name -> dummy.v1.TestEnumType + 1, // 5: dummy.v1.DummyA.config_a:type_name -> dummy.v1.DummyConfigA + 4, // 6: dummy.v1.DummyA.o_test_type:type_name -> dummy.v1.TestEnumType + 1, // 7: dummy.v1.DummyA.o_config_a:type_name -> dummy.v1.DummyConfigA + 8, // [8:8] is the sub-list for method output_type + 8, // [8:8] is the sub-list for method input_type + 8, // [8:8] is the sub-list for extension type_name + 8, // [8:8] is the sub-list for extension extendee + 0, // [0:8] is the sub-list for field type_name } func init() { file_dummy_v1_message_proto_init() } @@ -343,14 +485,27 @@ func file_dummy_v1_message_proto_init() { return nil } } + file_dummy_v1_message_proto_msgTypes[3].Exporter = func(v any, i int) any { + switch v := v.(*DummyA); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } + file_dummy_v1_message_proto_msgTypes[3].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_dummy_v1_message_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 4, NumExtensions: 0, NumServices: 0, }, diff --git a/testdata/proto/dummy/v1/message.proto b/testdata/proto/dummy/v1/message.proto index 8c008f3..3b38654 100644 --- a/testdata/proto/dummy/v1/message.proto +++ b/testdata/proto/dummy/v1/message.proto @@ -24,3 +24,17 @@ message DummyConfigB { string name = 1; int64 value = 2; } + +message DummyA { + string name = 1; + int32 value = 2; + repeated string values = 3; + TestEnumType test_type = 4; + + DummyConfigA config_a = 11; + + optional string o_name = 21; + optional int32 o_value = 22; + optional TestEnumType o_test_type = 23; + optional DummyConfigA o_config_a = 24; +}