From d8ddbd5d49cd8fd80cf5f8cc1d719bf9e9ba22c9 Mon Sep 17 00:00:00 2001 From: Dmitriy Matrenichev Date: Thu, 8 Sep 2022 17:35:11 +0300 Subject: [PATCH] chore: add more tests This commit adds tests for empty messages and for messages containing enums. Signed-off-by: Dmitriy Matrenichev --- messages/messages.pb.go | 418 +++++++++++++++++++++++++++++++------- messages/messages.proto | 20 ++ messages/messages_test.go | 194 ++++++++++-------- 3 files changed, 469 insertions(+), 163 deletions(-) diff --git a/messages/messages.pb.go b/messages/messages.pb.go index 3c585b8..86b3b42 100644 --- a/messages/messages.pb.go +++ b/messages/messages.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.28.0 +// protoc-gen-go v1.28.1 // protoc v3.18.1 // source: messages.proto @@ -12,6 +12,7 @@ import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" ) const ( @@ -21,6 +22,52 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +type Enum int32 + +const ( + Enum_ENUM1 Enum = 0 + Enum_ENUM2 Enum = 1 +) + +// Enum value maps for Enum. +var ( + Enum_name = map[int32]string{ + 0: "ENUM1", + 1: "ENUM2", + } + Enum_value = map[string]int32{ + "ENUM1": 0, + "ENUM2": 1, + } +) + +func (x Enum) Enum() *Enum { + p := new(Enum) + *p = x + return p +} + +func (x Enum) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Enum) Descriptor() protoreflect.EnumDescriptor { + return file_messages_proto_enumTypes[0].Descriptor() +} + +func (Enum) Type() protoreflect.EnumType { + return &file_messages_proto_enumTypes[0] +} + +func (x Enum) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Enum.Descriptor instead. +func (Enum) EnumDescriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{0} +} + type BasicMessage struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -289,69 +336,239 @@ func (x *MessageComplexFields) GetPrimitiveMap() map[string]int64 { return nil } +type MessageWithEmptpy struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + BasicMessage *BasicMessage `protobuf:"bytes,1,opt,name=basic_message,json=basicMessage,proto3" json:"basic_message,omitempty"` + EmptyMessage *emptypb.Empty `protobuf:"bytes,2,opt,name=empty_message,json=emptyMessage,proto3" json:"empty_message,omitempty"` +} + +func (x *MessageWithEmptpy) Reset() { + *x = MessageWithEmptpy{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *MessageWithEmptpy) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*MessageWithEmptpy) ProtoMessage() {} + +func (x *MessageWithEmptpy) ProtoReflect() protoreflect.Message { + mi := &file_messages_proto_msgTypes[4] + 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 MessageWithEmptpy.ProtoReflect.Descriptor instead. +func (*MessageWithEmptpy) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{4} +} + +func (x *MessageWithEmptpy) GetBasicMessage() *BasicMessage { + if x != nil { + return x.BasicMessage + } + return nil +} + +func (x *MessageWithEmptpy) GetEmptyMessage() *emptypb.Empty { + if x != nil { + return x.EmptyMessage + } + return nil +} + +type EmptyMessageRep struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + EmptyMessage []*emptypb.Empty `protobuf:"bytes,1,rep,name=empty_message,json=emptyMessage,proto3" json:"empty_message,omitempty"` +} + +func (x *EmptyMessageRep) Reset() { + *x = EmptyMessageRep{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *EmptyMessageRep) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EmptyMessageRep) ProtoMessage() {} + +func (x *EmptyMessageRep) ProtoReflect() protoreflect.Message { + mi := &file_messages_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 EmptyMessageRep.ProtoReflect.Descriptor instead. +func (*EmptyMessageRep) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{5} +} + +func (x *EmptyMessageRep) GetEmptyMessage() []*emptypb.Empty { + if x != nil { + return x.EmptyMessage + } + return nil +} + +type EnumMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + EnumField Enum `protobuf:"varint,1,opt,name=enum_field,json=enumField,proto3,enum=specs.Enum" json:"enum_field,omitempty"` +} + +func (x *EnumMessage) Reset() { + *x = EnumMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *EnumMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EnumMessage) ProtoMessage() {} + +func (x *EnumMessage) ProtoReflect() protoreflect.Message { + mi := &file_messages_proto_msgTypes[6] + 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 EnumMessage.ProtoReflect.Descriptor instead. +func (*EnumMessage) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{6} +} + +func (x *EnumMessage) GetEnumField() Enum { + if x != nil { + return x.EnumField + } + return Enum_ENUM1 +} + var File_messages_proto protoreflect.FileDescriptor var file_messages_proto_rawDesc = []byte{ 0x0a, 0x0e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x12, 0x05, 0x73, 0x70, 0x65, 0x63, 0x73, 0x22, 0x97, 0x01, 0x0a, 0x0c, 0x42, 0x61, 0x73, 0x69, - 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x74, 0x36, - 0x34, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x12, 0x17, - 0x0a, 0x07, 0x75, 0x5f, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, + 0x12, 0x05, 0x73, 0x70, 0x65, 0x63, 0x73, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x97, 0x01, 0x0a, 0x0c, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x12, 0x17, 0x0a, 0x07, 0x75, + 0x5f, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x75, 0x49, + 0x6e, 0x74, 0x36, 0x34, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x69, 0x78, 0x65, 0x64, 0x36, 0x34, 0x18, + 0x05, 0x20, 0x01, 0x28, 0x06, 0x52, 0x07, 0x66, 0x69, 0x78, 0x65, 0x64, 0x36, 0x34, 0x12, 0x1f, + 0x0a, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x06, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, + 0x1d, 0x0a, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x07, 0x20, + 0x01, 0x28, 0x0c, 0x52, 0x09, 0x73, 0x6f, 0x6d, 0x65, 0x42, 0x79, 0x74, 0x65, 0x73, 0x22, 0xa0, + 0x01, 0x0a, 0x15, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x70, 0x65, 0x61, 0x74, + 0x65, 0x64, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x74, 0x36, + 0x34, 0x18, 0x01, 0x20, 0x03, 0x28, 0x03, 0x52, 0x05, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x12, 0x17, + 0x0a, 0x07, 0x75, 0x5f, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x18, 0x03, 0x20, 0x03, 0x28, 0x04, 0x52, 0x06, 0x75, 0x49, 0x6e, 0x74, 0x36, 0x34, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x69, 0x78, 0x65, 0x64, - 0x36, 0x34, 0x18, 0x05, 0x20, 0x01, 0x28, 0x06, 0x52, 0x07, 0x66, 0x69, 0x78, 0x65, 0x64, 0x36, + 0x36, 0x34, 0x18, 0x05, 0x20, 0x03, 0x28, 0x06, 0x52, 0x07, 0x66, 0x69, 0x78, 0x65, 0x64, 0x36, 0x34, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, - 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x53, 0x74, 0x72, 0x69, + 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, - 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x73, 0x6f, 0x6d, 0x65, 0x42, 0x79, 0x74, 0x65, - 0x73, 0x22, 0xa0, 0x01, 0x0a, 0x15, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x70, - 0x65, 0x61, 0x74, 0x65, 0x64, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x69, - 0x6e, 0x74, 0x36, 0x34, 0x18, 0x01, 0x20, 0x03, 0x28, 0x03, 0x52, 0x05, 0x69, 0x6e, 0x74, 0x36, - 0x34, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x5f, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x18, 0x03, 0x20, 0x03, - 0x28, 0x04, 0x52, 0x06, 0x75, 0x49, 0x6e, 0x74, 0x36, 0x34, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x69, - 0x78, 0x65, 0x64, 0x36, 0x34, 0x18, 0x05, 0x20, 0x03, 0x28, 0x06, 0x52, 0x07, 0x66, 0x69, 0x78, - 0x65, 0x64, 0x36, 0x34, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x73, 0x74, 0x72, - 0x69, 0x6e, 0x67, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x53, - 0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x1d, 0x0a, 0x0a, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x62, 0x79, - 0x74, 0x65, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x09, 0x73, 0x6f, 0x6d, 0x65, 0x42, - 0x79, 0x74, 0x65, 0x73, 0x22, 0x4b, 0x0a, 0x0f, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x70, 0x12, 0x38, 0x0a, 0x0d, 0x62, 0x61, 0x73, 0x69, 0x63, - 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, - 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, 0x73, - 0x61, 0x67, 0x65, 0x52, 0x0c, 0x62, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x22, 0xe3, 0x03, 0x0a, 0x14, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, 0x6d, - 0x70, 0x6c, 0x65, 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x45, 0x0a, 0x08, 0x6d, 0x61, - 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x73, - 0x70, 0x65, 0x63, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, 0x6d, 0x70, - 0x6c, 0x65, 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x2e, 0x4d, 0x61, 0x70, 0x54, 0x6f, 0x4d, - 0x73, 0x67, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x6d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, - 0x67, 0x12, 0x48, 0x0a, 0x09, 0x6d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x18, 0x02, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2a, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x4d, 0x65, 0x73, - 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, 0x6d, 0x70, 0x6c, 0x65, 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, - 0x73, 0x2e, 0x4d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, - 0x52, 0x09, 0x6d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x12, 0x51, 0x0a, 0x0c, 0x70, - 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x18, 0x03, 0x20, 0x03, 0x28, - 0x0b, 0x32, 0x2d, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x43, 0x6f, 0x6d, 0x70, 0x6c, 0x65, 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x2e, 0x50, - 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, - 0x52, 0x0c, 0x70, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x1a, 0x50, - 0x0a, 0x0d, 0x4d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, + 0x18, 0x07, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x09, 0x73, 0x6f, 0x6d, 0x65, 0x42, 0x79, 0x74, 0x65, + 0x73, 0x22, 0x4b, 0x0a, 0x0f, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x52, 0x65, 0x70, 0x12, 0x38, 0x0a, 0x0d, 0x62, 0x61, 0x73, 0x69, 0x63, 0x5f, 0x6d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x73, 0x70, + 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x52, 0x0c, 0x62, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0xe3, + 0x03, 0x0a, 0x14, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, 0x6d, 0x70, 0x6c, 0x65, + 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x45, 0x0a, 0x08, 0x6d, 0x61, 0x70, 0x54, 0x6f, + 0x4d, 0x73, 0x67, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x73, 0x70, 0x65, 0x63, + 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, 0x6d, 0x70, 0x6c, 0x65, 0x78, + 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x2e, 0x4d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x45, + 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x6d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x12, 0x48, + 0x0a, 0x09, 0x6d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x2a, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x43, 0x6f, 0x6d, 0x70, 0x6c, 0x65, 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x2e, 0x4d, + 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x09, 0x6d, + 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x12, 0x51, 0x0a, 0x0c, 0x70, 0x72, 0x69, 0x6d, + 0x69, 0x74, 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2d, + 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, + 0x6d, 0x70, 0x6c, 0x65, 0x78, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x2e, 0x50, 0x72, 0x69, 0x6d, + 0x69, 0x74, 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x0c, 0x70, + 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x1a, 0x50, 0x0a, 0x0d, 0x4d, + 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, + 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x29, + 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, + 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x1a, 0x54, 0x0a, + 0x0e, 0x4d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, - 0x79, 0x12, 0x29, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x13, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, - 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, - 0x1a, 0x54, 0x0a, 0x0e, 0x4d, 0x61, 0x70, 0x54, 0x6f, 0x4d, 0x73, 0x67, 0x73, 0x45, 0x6e, 0x74, - 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x03, 0x6b, 0x65, 0x79, 0x12, 0x2c, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, - 0x63, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x70, 0x52, 0x05, 0x76, 0x61, 0x6c, - 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x1a, 0x3f, 0x0a, 0x11, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, - 0x69, 0x76, 0x65, 0x4d, 0x61, 0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, - 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, - 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x76, 0x61, - 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, - 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x69, 0x64, 0x65, 0x72, 0x6f, 0x6c, 0x61, 0x62, 0x73, - 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x65, 0x6e, 0x63, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x79, 0x12, 0x2c, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x16, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x65, 0x70, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, + 0x02, 0x38, 0x01, 0x1a, 0x3f, 0x0a, 0x11, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, + 0x4d, 0x61, 0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x3a, 0x02, 0x38, 0x01, 0x22, 0x8a, 0x01, 0x0a, 0x11, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x57, 0x69, 0x74, 0x68, 0x45, 0x6d, 0x70, 0x74, 0x70, 0x79, 0x12, 0x38, 0x0a, 0x0d, 0x62, 0x61, + 0x73, 0x69, 0x63, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x13, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x4d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x0c, 0x62, 0x61, 0x73, 0x69, 0x63, 0x4d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x12, 0x3b, 0x0a, 0x0d, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x5f, 0x6d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, + 0x70, 0x74, 0x79, 0x52, 0x0c, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x22, 0x4e, 0x0a, 0x0f, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x52, 0x65, 0x70, 0x12, 0x3b, 0x0a, 0x0d, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x5f, 0x6d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, + 0x70, 0x74, 0x79, 0x52, 0x0c, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x22, 0x39, 0x0a, 0x0b, 0x45, 0x6e, 0x75, 0x6d, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x12, 0x2a, 0x0a, 0x0a, 0x65, 0x6e, 0x75, 0x6d, 0x5f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0b, 0x2e, 0x73, 0x70, 0x65, 0x63, 0x73, 0x2e, 0x45, 0x6e, 0x75, + 0x6d, 0x52, 0x09, 0x65, 0x6e, 0x75, 0x6d, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x2a, 0x1c, 0x0a, 0x04, + 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x09, 0x0a, 0x05, 0x45, 0x4e, 0x55, 0x4d, 0x31, 0x10, 0x00, 0x12, + 0x09, 0x0a, 0x05, 0x45, 0x4e, 0x55, 0x4d, 0x32, 0x10, 0x01, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x69, 0x64, 0x65, 0x72, 0x6f, 0x6c, + 0x61, 0x62, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x65, 0x6e, 0x63, 0x2f, 0x6d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -366,28 +583,38 @@ func file_messages_proto_rawDescGZIP() []byte { return file_messages_proto_rawDescData } -var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 7) +var file_messages_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 10) var file_messages_proto_goTypes = []interface{}{ - (*BasicMessage)(nil), // 0: specs.BasicMessage - (*MessageRepeatedFields)(nil), // 1: specs.MessageRepeatedFields - (*BasicMessageRep)(nil), // 2: specs.BasicMessageRep - (*MessageComplexFields)(nil), // 3: specs.MessageComplexFields - nil, // 4: specs.MessageComplexFields.MapToMsgEntry - nil, // 5: specs.MessageComplexFields.MapToMsgsEntry - nil, // 6: specs.MessageComplexFields.PrimitiveMapEntry + (Enum)(0), // 0: specs.Enum + (*BasicMessage)(nil), // 1: specs.BasicMessage + (*MessageRepeatedFields)(nil), // 2: specs.MessageRepeatedFields + (*BasicMessageRep)(nil), // 3: specs.BasicMessageRep + (*MessageComplexFields)(nil), // 4: specs.MessageComplexFields + (*MessageWithEmptpy)(nil), // 5: specs.MessageWithEmptpy + (*EmptyMessageRep)(nil), // 6: specs.EmptyMessageRep + (*EnumMessage)(nil), // 7: specs.EnumMessage + nil, // 8: specs.MessageComplexFields.MapToMsgEntry + nil, // 9: specs.MessageComplexFields.MapToMsgsEntry + nil, // 10: specs.MessageComplexFields.PrimitiveMapEntry + (*emptypb.Empty)(nil), // 11: google.protobuf.Empty } var file_messages_proto_depIdxs = []int32{ - 0, // 0: specs.BasicMessageRep.basic_message:type_name -> specs.BasicMessage - 4, // 1: specs.MessageComplexFields.mapToMsg:type_name -> specs.MessageComplexFields.MapToMsgEntry - 5, // 2: specs.MessageComplexFields.mapToMsgs:type_name -> specs.MessageComplexFields.MapToMsgsEntry - 6, // 3: specs.MessageComplexFields.primitiveMap:type_name -> specs.MessageComplexFields.PrimitiveMapEntry - 0, // 4: specs.MessageComplexFields.MapToMsgEntry.value:type_name -> specs.BasicMessage - 2, // 5: specs.MessageComplexFields.MapToMsgsEntry.value:type_name -> specs.BasicMessageRep - 6, // [6:6] is the sub-list for method output_type - 6, // [6:6] is the sub-list for method input_type - 6, // [6:6] is the sub-list for extension type_name - 6, // [6:6] is the sub-list for extension extendee - 0, // [0:6] is the sub-list for field type_name + 1, // 0: specs.BasicMessageRep.basic_message:type_name -> specs.BasicMessage + 8, // 1: specs.MessageComplexFields.mapToMsg:type_name -> specs.MessageComplexFields.MapToMsgEntry + 9, // 2: specs.MessageComplexFields.mapToMsgs:type_name -> specs.MessageComplexFields.MapToMsgsEntry + 10, // 3: specs.MessageComplexFields.primitiveMap:type_name -> specs.MessageComplexFields.PrimitiveMapEntry + 1, // 4: specs.MessageWithEmptpy.basic_message:type_name -> specs.BasicMessage + 11, // 5: specs.MessageWithEmptpy.empty_message:type_name -> google.protobuf.Empty + 11, // 6: specs.EmptyMessageRep.empty_message:type_name -> google.protobuf.Empty + 0, // 7: specs.EnumMessage.enum_field:type_name -> specs.Enum + 1, // 8: specs.MessageComplexFields.MapToMsgEntry.value:type_name -> specs.BasicMessage + 3, // 9: specs.MessageComplexFields.MapToMsgsEntry.value:type_name -> specs.BasicMessageRep + 10, // [10:10] is the sub-list for method output_type + 10, // [10:10] is the sub-list for method input_type + 10, // [10:10] is the sub-list for extension type_name + 10, // [10:10] is the sub-list for extension extendee + 0, // [0:10] is the sub-list for field type_name } func init() { file_messages_proto_init() } @@ -444,19 +671,56 @@ func file_messages_proto_init() { return nil } } + file_messages_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*MessageWithEmptpy); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_messages_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*EmptyMessageRep); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_messages_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*EnumMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_messages_proto_rawDesc, - NumEnums: 0, - NumMessages: 7, + NumEnums: 1, + NumMessages: 10, NumExtensions: 0, NumServices: 0, }, GoTypes: file_messages_proto_goTypes, DependencyIndexes: file_messages_proto_depIdxs, + EnumInfos: file_messages_proto_enumTypes, MessageInfos: file_messages_proto_msgTypes, }.Build() File_messages_proto = out.File diff --git a/messages/messages.proto b/messages/messages.proto index 40b58de..8434560 100644 --- a/messages/messages.proto +++ b/messages/messages.proto @@ -3,6 +3,8 @@ package specs; option go_package = "github.com/siderolabs/protoenc/messages"; +import "google/protobuf/empty.proto"; + message BasicMessage { int64 int64 = 1; uint64 u_int64 = 3; @@ -28,3 +30,21 @@ message MessageComplexFields { map mapToMsgs = 2; map primitiveMap = 3; } + +message MessageWithEmptpy { + BasicMessage basic_message = 1; + google.protobuf.Empty empty_message = 2; +} + +message EmptyMessageRep { + repeated google.protobuf.Empty empty_message = 1; +} + +enum Enum { + ENUM1 = 0; + ENUM2 = 1; +} + +message EnumMessage { + Enum enum_field = 1; +} diff --git a/messages/messages_test.go b/messages/messages_test.go index fc2e9f2..7825012 100644 --- a/messages/messages_test.go +++ b/messages/messages_test.go @@ -5,9 +5,12 @@ package messages_test import ( + "encoding/hex" "testing" + "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/emptypb" "github.com/siderolabs/protoenc" "github.com/siderolabs/protoenc/messages" @@ -15,6 +18,15 @@ import ( // TODO: ensure that binary output is also the same +func runTestPipe[R any, RP msg[R], T any](t *testing.T, original T) { + encoded1 := must(protoenc.Marshal(&original))(t) + decoded := protoUnmarshal[R, RP](t, encoded1) + encoded2 := must(proto.Marshal(decoded))(t) + result := ourUnmarshal[T](t, encoded2) + + shouldBeEqual(t, original, result) +} + //nolint:govet type BasicMessage struct { Int64 int64 `protobuf:"1"` @@ -30,44 +42,25 @@ func TestBasicMessage(t *testing.T) { t.Run("check that the outputs of both messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := BasicMessage{ + runTestPipe[messages.BasicMessage](t, BasicMessage{ Int64: 1, UInt64: 2, Fixed64: protoenc.FixedU64(3), SomeString: "some string", SomeBytes: []byte("some bytes"), - } - - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.BasicMessage](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[BasicMessage](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + }) }) t.Run("check that the outputs of both zero messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := BasicMessage{} - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.BasicMessage](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[BasicMessage](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + runTestPipe[messages.BasicMessage](t, BasicMessage{}) }) t.Run("check that the outputs of both somewhat empty messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := BasicMessage{SomeString: "some string"} - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.BasicMessage](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[BasicMessage](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + runTestPipe[messages.BasicMessage](t, BasicMessage{SomeString: "some string"}) }) } @@ -85,44 +78,27 @@ func TestMessageRepeatedFields(t *testing.T) { t.Run("check that the outputs of both messages are the same", func(t *testing.T) { t.Parallel() - originalMsg := MessageRepeatedFields{ + runTestPipe[messages.MessageRepeatedFields](t, MessageRepeatedFields{ Int64: []int64{1, 2, 3}, UInt64: []uint64{4, 5, 6}, Fixed64: []protoenc.FixedU64{7, 8, 9}, SomeString: []string{"some string", "some string 2"}, SomeBytes: [][]byte{[]byte("some bytes"), []byte("some bytes 2")}, - } - - encoded1 := must(protoenc.Marshal(&originalMsg))(t) - decodedMsg := protoUnmarshal[messages.MessageRepeatedFields](t, encoded1) - encoded2 := must(proto.Marshal(decodedMsg))(t) - resultMsg := ourUnmarshal[MessageRepeatedFields](t, encoded2) - - shouldBeEqual(t, originalMsg, resultMsg) + }) }) t.Run("check that the outputs of both zero messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := MessageRepeatedFields{} - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.MessageRepeatedFields](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[MessageRepeatedFields](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + runTestPipe[messages.MessageRepeatedFields](t, MessageRepeatedFields{}) }) t.Run("check that the outputs of both somewhat empty messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := MessageRepeatedFields{SomeString: []string{"some string"}} - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.MessageRepeatedFields](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[MessageRepeatedFields](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + runTestPipe[messages.MessageRepeatedFields](t, MessageRepeatedFields{ + SomeString: []string{"some string"}, + }) }) } @@ -136,7 +112,7 @@ func TestBasicMessageRep(t *testing.T) { t.Run("check that the outputs of both messages are the same", func(t *testing.T) { t.Parallel() - originalMsg := BasicMessageRep{ + runTestPipe[messages.BasicMessageRep](t, BasicMessageRep{ BasicMessage: []BasicMessage{ { Int64: 1, @@ -153,43 +129,25 @@ func TestBasicMessageRep(t *testing.T) { SomeBytes: []byte("hot bytes"), }, }, - } - encoded1 := must(protoenc.Marshal(&originalMsg))(t) - decodedMsg := protoUnmarshal[messages.BasicMessageRep](t, encoded1) - encoded2 := must(proto.Marshal(decodedMsg))(t) - resultMsg := ourUnmarshal[BasicMessageRep](t, encoded2) - - shouldBeEqual(t, originalMsg, resultMsg) + }) }) t.Run("check that the outputs of both zero messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := BasicMessageRep{} - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.BasicMessageRep](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[BasicMessageRep](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + runTestPipe[messages.BasicMessageRep](t, BasicMessageRep{}) }) t.Run("check that the outputs of both somewhat empty messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := BasicMessageRep{ + runTestPipe[messages.BasicMessageRep](t, BasicMessageRep{ BasicMessage: []BasicMessage{ { Fixed64: protoenc.FixedU64(3), }, }, - } - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.BasicMessageRep](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[BasicMessageRep](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + }) }) } @@ -259,24 +217,14 @@ func TestMessageComplexFields(t *testing.T) { "empty": 0, }, } - encoded1 := must(protoenc.Marshal(&originalMsg))(t) - decodedMsg := protoUnmarshal[messages.MessageComplexFields](t, encoded1) - encoded2 := must(proto.Marshal(decodedMsg))(t) - resultMsg := ourUnmarshal[MessageComplexFields](t, encoded2) - shouldBeEqual(t, originalMsg, resultMsg) + runTestPipe[messages.MessageComplexFields](t, originalMsg) }) t.Run("check that the outputs of both zero messages are the same", func(t *testing.T) { t.Parallel() - ourBasicMessage := MessageComplexFields{} - encoded1 := must(protoenc.Marshal(&ourBasicMessage))(t) - basicMessage := protoUnmarshal[messages.MessageComplexFields](t, encoded1) - encoded2 := must(proto.Marshal(basicMessage))(t) - decodedMessage := ourUnmarshal[MessageComplexFields](t, encoded2) - - shouldBeEqual(t, ourBasicMessage, decodedMessage) + runTestPipe[messages.MessageComplexFields](t, MessageComplexFields{}) }) t.Run("check that the outputs of both somewhat empty messages are the same", func(t *testing.T) { @@ -287,6 +235,9 @@ func TestMessageComplexFields(t *testing.T) { "key": { Int64: 1, }, + "": { + Int64: 30, + }, }, MapToMsgs: map[string]BasicMessageRep{ "key": { @@ -316,11 +267,82 @@ func TestMessageComplexFields(t *testing.T) { "key": 1, }, } - encoded1 := must(protoenc.Marshal(&originalMsg))(t) - decodedMsg := protoUnmarshal[messages.MessageComplexFields](t, encoded1) - encoded2 := must(proto.Marshal(decodedMsg))(t) - resultMsg := ourUnmarshal[MessageComplexFields](t, encoded2) - shouldBeEqual(t, originalMsg, resultMsg) + runTestPipe[messages.MessageComplexFields](t, originalMsg) }) } + +func TestEmptyMessage(t *testing.T) { + t.Parallel() + + t.Run("empty message", func(t *testing.T) { + t.Parallel() + + type emptyMessage struct{} + + runTestPipe[emptypb.Empty](t, emptyMessage{}) + }) + + t.Run("slice of empty messages", func(t *testing.T) { + t.Parallel() + + type emptyMessage struct{} + + type emptyMessageRep struct { + EmptyMessage []emptyMessage `protobuf:"1"` + } + + runTestPipe[messages.EmptyMessageRep](t, emptyMessageRep{ + EmptyMessage: make([]emptyMessage, 10), + }) + }) + + t.Run("test message containing empty message", func(t *testing.T) { + t.Parallel() + + type emptyMessage struct{} + + type messageWithEmptpy struct { //nolint:govet + BasicMessage BasicMessage `protobuf:"1"` + EmptyMessage emptyMessage `protobuf:"2"` + } + + runTestPipe[messages.MessageWithEmptpy](t, messageWithEmptpy{ + BasicMessage: BasicMessage{ + Int64: 1, + UInt64: 2, + Fixed64: protoenc.FixedU64(3), + SomeString: "some string", + SomeBytes: []byte("some bytes"), + }, + EmptyMessage: emptyMessage{}, + }) + }) +} + +func TestEnumMessage(t *testing.T) { + // This test ensures that we can decode a message with an enum field. + // Even tho we use fixed 32-bit values for encoding enums (unlike protobuf) decoding into int8-16s should still work. + t.Parallel() + + type Enum int8 + + type EnumMessage struct { + EnumField Enum `protobuf:"1"` + } + + original := messages.EnumMessage{ + EnumField: messages.Enum_ENUM2, + } + + encoded, err := proto.Marshal(&original) + require.NoError(t, err) + + t.Log("\n", hex.Dump(encoded)) + + decoded := EnumMessage{} + err = protoenc.Unmarshal(encoded, &decoded) + require.NoError(t, err) + + require.EqualValues(t, original.EnumField, decoded.EnumField) +}