[#111] proto/test: Add repeated message test

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-08-27 15:02:16 +03:00
parent eba18f6e67
commit a2025376fc
4 changed files with 304 additions and 28 deletions

View file

@ -113,6 +113,10 @@ func repPrimitivesEqual(t *testing.T, a *generated.RepPrimitives, b *test.RepPri
require.Equal(t, a.FieldE, b.FieldE)
require.Equal(t, a.FieldF, b.FieldF)
require.Equal(t, a.FieldFu, b.FieldFu)
require.Equal(t, len(a.GetFieldAux()), len(b.GetFieldAux()))
for i := range a.FieldAux {
require.Equal(t, a.GetFieldAux()[i].GetInnerField(), b.GetFieldAux()[i].GetInnerField())
}
}
func randIntSlice[T protoInt](n int, includeZero bool) []T {
@ -129,6 +133,14 @@ func randIntSlice[T protoInt](n int, includeZero bool) []T {
return r
}
func uint32SliceToAux(s []uint32) []*generated.RepPrimitives_Aux {
r := make([]*generated.RepPrimitives_Aux, len(s))
for i := range s {
r[i] = &generated.RepPrimitives_Aux{s[i]}
}
return r
}
func TestStableMarshalRep(t *testing.T) {
t.Run("empty", func(t *testing.T) {
marshalCases := []struct {
@ -176,6 +188,9 @@ func TestStableMarshalRep(t *testing.T) {
{name: "uint64", input: &generated.RepPrimitives{FieldFu: randIntSlice[uint64](1, true)}},
{name: "uint64", input: &generated.RepPrimitives{FieldFu: randIntSlice[uint64](2, true)}},
{name: "uint64", input: &generated.RepPrimitives{FieldFu: randIntSlice[uint64](2, false)}},
{name: "message", input: &generated.RepPrimitives{FieldAux: uint32SliceToAux(randIntSlice[uint32](1, true))}},
{name: "message", input: &generated.RepPrimitives{FieldAux: uint32SliceToAux(randIntSlice[uint32](2, true))}},
{name: "message", input: &generated.RepPrimitives{FieldAux: uint32SliceToAux(randIntSlice[uint32](2, false))}},
}
for _, tc := range marshalCases {
t.Run(tc.name, func(t *testing.T) {

View file

@ -802,14 +802,144 @@ func (*Primitives_FieldMe) isPrimitives_FieldM() {}
func (*Primitives_FieldAux) isPrimitives_FieldM() {}
type RepPrimitives_Aux struct {
InnerField uint32 `json:"innerField"`
}
var (
_ encoding.ProtoMarshaler = (*RepPrimitives_Aux)(nil)
_ encoding.ProtoUnmarshaler = (*RepPrimitives_Aux)(nil)
_ json.Marshaler = (*RepPrimitives_Aux)(nil)
_ json.Unmarshaler = (*RepPrimitives_Aux)(nil)
)
// StableSize returns the size of x in protobuf format.
//
// Structures with the same field values have the same binary size.
func (x *RepPrimitives_Aux) StableSize() (size int) {
if x == nil {
return 0
}
size += proto.UInt32Size(1, x.InnerField)
return size
}
// MarshalProtobuf implements the encoding.ProtoMarshaler interface.
func (x *RepPrimitives_Aux) MarshalProtobuf(dst []byte) []byte {
m := pool.MarshalerPool.Get()
defer pool.MarshalerPool.Put(m)
x.EmitProtobuf(m.MessageMarshaler())
dst = m.Marshal(dst)
return dst
}
func (x *RepPrimitives_Aux) EmitProtobuf(mm *easyproto.MessageMarshaler) {
if x == nil {
return
}
if x.InnerField != 0 {
mm.AppendUint32(1, x.InnerField)
}
}
// UnmarshalProtobuf implements the encoding.ProtoUnmarshaler interface.
func (x *RepPrimitives_Aux) UnmarshalProtobuf(src []byte) (err error) {
var fc easyproto.FieldContext
for len(src) > 0 {
src, err = fc.NextField(src)
if err != nil {
return fmt.Errorf("cannot read next field in %s", "RepPrimitives_Aux")
}
switch fc.FieldNum {
case 1: // InnerField
data, ok := fc.Uint32()
if !ok {
return fmt.Errorf("cannot unmarshal field %s", "InnerField")
}
x.InnerField = data
}
}
return nil
}
func (x *RepPrimitives_Aux) GetInnerField() uint32 {
if x != nil {
return x.InnerField
}
return 0
}
func (x *RepPrimitives_Aux) SetInnerField(v uint32) {
x.InnerField = v
}
// MarshalJSON implements the json.Marshaler interface.
func (x *RepPrimitives_Aux) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
x.MarshalEasyJSON(&w)
return w.Buffer.BuildBytes(), w.Error
}
func (x *RepPrimitives_Aux) MarshalEasyJSON(out *jwriter.Writer) {
if x == nil {
out.RawString("null")
return
}
out.RawByte('{')
{
const prefix string = ",\"innerField\":"
out.RawString(prefix[1:])
out.Uint32(x.InnerField)
}
out.RawByte('}')
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (x *RepPrimitives_Aux) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
x.UnmarshalEasyJSON(&r)
return r.Error()
}
func (x *RepPrimitives_Aux) UnmarshalEasyJSON(in *jlexer.Lexer) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
in.Consumed()
}
in.Skip()
return
}
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeFieldName(false)
in.WantColon()
if in.IsNull() {
in.Skip()
in.WantComma()
continue
}
switch key {
case "innerField":
{
var f uint32
f = in.Uint32()
x.InnerField = f
}
}
in.WantComma()
}
in.Delim('}')
if isTopLevel {
in.Consumed()
}
}
type RepPrimitives struct {
FieldA [][]byte `json:"fieldA"`
FieldB []string `json:"fieldB"`
FieldC []int32 `json:"fieldC"`
FieldD []uint32 `json:"fieldD"`
FieldE []int64 `json:"fieldE"`
FieldF []uint64 `json:"fieldF"`
FieldFu []uint64 `json:"fieldFu"`
FieldA [][]byte `json:"fieldA"`
FieldB []string `json:"fieldB"`
FieldC []int32 `json:"fieldC"`
FieldD []uint32 `json:"fieldD"`
FieldE []int64 `json:"fieldE"`
FieldF []uint64 `json:"fieldF"`
FieldFu []uint64 `json:"fieldFu"`
FieldAux []*RepPrimitives_Aux `json:"fieldAux"`
}
var (
@ -840,6 +970,9 @@ func (x *RepPrimitives) StableSize() (size int) {
for i := range x.FieldFu {
size += protowire.SizeGroup(protowire.Number(7), protowire.SizeVarint(x.FieldFu[i]))
}
for i := range x.FieldAux {
size += proto.NestedStructureSize(8, x.FieldAux[i])
}
return size
}
@ -877,6 +1010,11 @@ func (x *RepPrimitives) EmitProtobuf(mm *easyproto.MessageMarshaler) {
for j := range x.FieldFu {
mm.AppendUint64(7, x.FieldFu[j])
}
for i := range x.FieldAux {
if x.FieldAux[i] != nil {
x.FieldAux[i].EmitProtobuf(mm.AppendMessage(8))
}
}
}
// UnmarshalProtobuf implements the encoding.ProtoUnmarshaler interface.
@ -930,6 +1068,16 @@ func (x *RepPrimitives) UnmarshalProtobuf(src []byte) (err error) {
return fmt.Errorf("cannot unmarshal field %s", "FieldFu")
}
x.FieldFu = append(x.FieldFu, data)
case 8: // FieldAux
data, ok := fc.MessageData()
if !ok {
return fmt.Errorf("cannot unmarshal field %s", "FieldAux")
}
x.FieldAux = append(x.FieldAux, new(RepPrimitives_Aux))
ff := x.FieldAux[len(x.FieldAux)-1]
if err := ff.UnmarshalProtobuf(data); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
}
}
return nil
@ -997,6 +1145,15 @@ func (x *RepPrimitives) GetFieldFu() []uint64 {
func (x *RepPrimitives) SetFieldFu(v []uint64) {
x.FieldFu = v
}
func (x *RepPrimitives) GetFieldAux() []*RepPrimitives_Aux {
if x != nil {
return x.FieldAux
}
return nil
}
func (x *RepPrimitives) SetFieldAux(v []*RepPrimitives_Aux) {
x.FieldAux = v
}
// MarshalJSON implements the json.Marshaler interface.
func (x *RepPrimitives) MarshalJSON() ([]byte, error) {
@ -1094,6 +1251,18 @@ func (x *RepPrimitives) MarshalEasyJSON(out *jwriter.Writer) {
}
out.RawByte(']')
}
{
const prefix string = ",\"fieldAux\":"
out.RawString(prefix)
out.RawByte('[')
for i := range x.FieldAux {
if i != 0 {
out.RawByte(',')
}
x.FieldAux[i].MarshalEasyJSON(out)
}
out.RawByte(']')
}
out.RawByte('}')
}
@ -1213,6 +1382,20 @@ func (x *RepPrimitives) UnmarshalEasyJSON(in *jlexer.Lexer) {
x.FieldFu = list
in.Delim(']')
}
case "fieldAux":
{
var f *RepPrimitives_Aux
var list []*RepPrimitives_Aux
in.Delim('[')
for !in.IsDelim(']') {
f = new(RepPrimitives_Aux)
f.UnmarshalEasyJSON(in)
list = append(list, f)
in.WantComma()
}
x.FieldAux = list
in.Delim(']')
}
}
in.WantComma()
}

View file

@ -257,13 +257,14 @@ type RepPrimitives struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
FieldA [][]byte `protobuf:"bytes,1,rep,name=field_a,json=fieldA,proto3" json:"field_a,omitempty"`
FieldB []string `protobuf:"bytes,2,rep,name=field_b,json=fieldB,proto3" json:"field_b,omitempty"`
FieldC []int32 `protobuf:"varint,3,rep,packed,name=field_c,json=fieldC,proto3" json:"field_c,omitempty"`
FieldD []uint32 `protobuf:"varint,4,rep,packed,name=field_d,json=fieldD,proto3" json:"field_d,omitempty"`
FieldE []int64 `protobuf:"varint,5,rep,packed,name=field_e,json=fieldE,proto3" json:"field_e,omitempty"`
FieldF []uint64 `protobuf:"varint,6,rep,packed,name=field_f,json=fieldF,proto3" json:"field_f,omitempty"`
FieldFu []uint64 `protobuf:"varint,7,rep,name=field_fu,json=fieldFu,proto3" json:"field_fu,omitempty"`
FieldA [][]byte `protobuf:"bytes,1,rep,name=field_a,json=fieldA,proto3" json:"field_a,omitempty"`
FieldB []string `protobuf:"bytes,2,rep,name=field_b,json=fieldB,proto3" json:"field_b,omitempty"`
FieldC []int32 `protobuf:"varint,3,rep,packed,name=field_c,json=fieldC,proto3" json:"field_c,omitempty"`
FieldD []uint32 `protobuf:"varint,4,rep,packed,name=field_d,json=fieldD,proto3" json:"field_d,omitempty"`
FieldE []int64 `protobuf:"varint,5,rep,packed,name=field_e,json=fieldE,proto3" json:"field_e,omitempty"`
FieldF []uint64 `protobuf:"varint,6,rep,packed,name=field_f,json=fieldF,proto3" json:"field_f,omitempty"`
FieldFu []uint64 `protobuf:"varint,7,rep,name=field_fu,json=fieldFu,proto3" json:"field_fu,omitempty"`
FieldAux []*RepPrimitives_Aux `protobuf:"bytes,8,rep,name=field_aux,json=fieldAux,proto3" json:"field_aux,omitempty"`
}
func (x *RepPrimitives) Reset() {
@ -347,6 +348,13 @@ func (x *RepPrimitives) GetFieldFu() []uint64 {
return nil
}
func (x *RepPrimitives) GetFieldAux() []*RepPrimitives_Aux {
if x != nil {
return x.FieldAux
}
return nil
}
type Primitives_Aux struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@ -394,6 +402,53 @@ func (x *Primitives_Aux) GetInnerField() uint32 {
return 0
}
type RepPrimitives_Aux struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
InnerField uint32 `protobuf:"varint,1,opt,name=inner_field,json=innerField,proto3" json:"inner_field,omitempty"`
}
func (x *RepPrimitives_Aux) Reset() {
*x = RepPrimitives_Aux{}
if protoimpl.UnsafeEnabled {
mi := &file_util_proto_test_test_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *RepPrimitives_Aux) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*RepPrimitives_Aux) ProtoMessage() {}
func (x *RepPrimitives_Aux) ProtoReflect() protoreflect.Message {
mi := &file_util_proto_test_test_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 RepPrimitives_Aux.ProtoReflect.Descriptor instead.
func (*RepPrimitives_Aux) Descriptor() ([]byte, []int) {
return file_util_proto_test_test_proto_rawDescGZIP(), []int{1, 0}
}
func (x *RepPrimitives_Aux) GetInnerField() uint32 {
if x != nil {
return x.InnerField
}
return 0
}
var File_util_proto_test_test_proto protoreflect.FileDescriptor
var file_util_proto_test_test_proto_rawDesc = []byte{
@ -433,7 +488,7 @@ var file_util_proto_test_test_proto_rawDesc = []byte{
0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x50,
0x4f, 0x53, 0x49, 0x54, 0x49, 0x56, 0x45, 0x10, 0x01, 0x12, 0x15, 0x0a, 0x08, 0x4e, 0x45, 0x47,
0x41, 0x54, 0x49, 0x56, 0x45, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
0x42, 0x09, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x6d, 0x22, 0xc4, 0x01, 0x0a, 0x0d,
0x42, 0x09, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x6d, 0x22, 0xa2, 0x02, 0x0a, 0x0d,
0x52, 0x65, 0x70, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x73, 0x12, 0x17, 0x0a,
0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x61, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x06,
0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f,
@ -446,8 +501,14 @@ var file_util_proto_test_test_proto_rawDesc = []byte{
0x65, 0x6c, 0x64, 0x5f, 0x66, 0x18, 0x06, 0x20, 0x03, 0x28, 0x04, 0x52, 0x06, 0x66, 0x69, 0x65,
0x6c, 0x64, 0x46, 0x12, 0x1d, 0x0a, 0x08, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x66, 0x75, 0x18,
0x07, 0x20, 0x03, 0x28, 0x04, 0x42, 0x02, 0x10, 0x00, 0x52, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64,
0x46, 0x75, 0x42, 0x11, 0x5a, 0x0f, 0x75, 0x74, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x2f, 0x74, 0x65, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x46, 0x75, 0x12, 0x34, 0x0a, 0x09, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x61, 0x75, 0x78, 0x18,
0x08, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x52, 0x65, 0x70,
0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x73, 0x2e, 0x41, 0x75, 0x78, 0x52, 0x08,
0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x75, 0x78, 0x1a, 0x26, 0x0a, 0x03, 0x41, 0x75, 0x78, 0x12,
0x1f, 0x0a, 0x0b, 0x69, 0x6e, 0x6e, 0x65, 0x72, 0x5f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x18, 0x01,
0x20, 0x01, 0x28, 0x0d, 0x52, 0x0a, 0x69, 0x6e, 0x6e, 0x65, 0x72, 0x46, 0x69, 0x65, 0x6c, 0x64,
0x42, 0x11, 0x5a, 0x0f, 0x75, 0x74, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74,
0x65, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -463,21 +524,23 @@ func file_util_proto_test_test_proto_rawDescGZIP() []byte {
}
var file_util_proto_test_test_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_util_proto_test_test_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_util_proto_test_test_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_util_proto_test_test_proto_goTypes = []interface{}{
(Primitives_SomeEnum)(0), // 0: test.Primitives.SomeEnum
(*Primitives)(nil), // 1: test.Primitives
(*RepPrimitives)(nil), // 2: test.RepPrimitives
(*Primitives_Aux)(nil), // 3: test.Primitives.Aux
(Primitives_SomeEnum)(0), // 0: test.Primitives.SomeEnum
(*Primitives)(nil), // 1: test.Primitives
(*RepPrimitives)(nil), // 2: test.RepPrimitives
(*Primitives_Aux)(nil), // 3: test.Primitives.Aux
(*RepPrimitives_Aux)(nil), // 4: test.RepPrimitives.Aux
}
var file_util_proto_test_test_proto_depIdxs = []int32{
0, // 0: test.Primitives.field_h:type_name -> test.Primitives.SomeEnum
3, // 1: test.Primitives.field_aux:type_name -> test.Primitives.Aux
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
4, // 2: test.RepPrimitives.field_aux:type_name -> test.RepPrimitives.Aux
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_util_proto_test_test_proto_init() }
@ -522,6 +585,18 @@ func file_util_proto_test_test_proto_init() {
return nil
}
}
file_util_proto_test_test_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RepPrimitives_Aux); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
file_util_proto_test_test_proto_msgTypes[0].OneofWrappers = []interface{}{
(*Primitives_FieldMa)(nil),
@ -534,7 +609,7 @@ func file_util_proto_test_test_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_util_proto_test_test_proto_rawDesc,
NumEnums: 1,
NumMessages: 3,
NumMessages: 4,
NumExtensions: 0,
NumServices: 0,
},

View file

@ -40,4 +40,7 @@ message RepPrimitives {
repeated int64 field_e = 5;
repeated uint64 field_f = 6;
repeated uint64 field_fu = 7 [ packed = false ];
message Aux { uint32 inner_field = 1; }
repeated Aux field_aux = 8;
}