From b0b4769f73e51d663f2ed892dd0c44150d4f7d4c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 15 Jul 2024 11:16:29 +0300 Subject: [PATCH] [#91] protogen: Support unpacked repeated uint64 fields Signed-off-by: Evgenii Stratonikov --- util/proto/marshal_test.go | 59 ++++++++++++++++++++++++++++++++++++- util/proto/test/test.pb.go | Bin 13851 -> 14249 bytes util/proto/test/test.proto | 1 + util/protogen/main.go | 38 ++++++++++++++++++++---- 4 files changed, 92 insertions(+), 6 deletions(-) diff --git a/util/proto/marshal_test.go b/util/proto/marshal_test.go index b06cd4f..73129c2 100644 --- a/util/proto/marshal_test.go +++ b/util/proto/marshal_test.go @@ -1,12 +1,14 @@ package proto_test import ( + "encoding/binary" "math" "testing" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test" "github.com/stretchr/testify/require" + "google.golang.org/protobuf/encoding/protowire" goproto "google.golang.org/protobuf/proto" ) @@ -33,6 +35,8 @@ type stableRepPrimitives struct { FieldD []uint32 FieldE []int64 FieldF []uint64 + + FieldFu []uint64 } const ( @@ -187,6 +191,20 @@ func (s *stableRepPrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte } i += proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF) + fieldNum = 7 + if wrongField { + fieldNum++ + } + for j := range s.FieldFu { + { + prefix := protowire.EncodeTag( + protowire.Number(fieldNum), + protowire.VarintType) + i += binary.PutUvarint(buf[i:], uint64(prefix)) + i += binary.PutUvarint(buf[i:], s.FieldFu[j]) + } + } + return buf, nil } @@ -198,7 +216,12 @@ func (s *stableRepPrimitives) stableSize() int { f5, _ := proto.RepeatedInt64Size(5, s.FieldE) f6, _ := proto.RepeatedUInt64Size(6, s.FieldF) - return f1 + f2 + f3 + f4 + f5 + f6 + var f7 int + for i := range s.FieldFu { + f7 += protowire.SizeGroup(protowire.Number(7), protowire.SizeVarint(s.FieldFu[i])) + } + + return f1 + f2 + f3 + f4 + f5 + f6 + f7 } func TestBytesMarshal(t *testing.T) { @@ -406,6 +429,22 @@ func TestRepeatedUInt64Marshal(t *testing.T) { }) } +func TestRepeatedUInt64MarshalUnpacked(t *testing.T) { + t.Run("not empty", func(t *testing.T) { + data := []uint64{0, 1, 2, 3, 4, 5} + testRepeatedUInt64MarshalUnpacked(t, data, false) + testRepeatedUInt64MarshalUnpacked(t, data, true) + }) + + t.Run("empty", func(t *testing.T) { + testRepeatedUInt64MarshalUnpacked(t, []uint64{}, false) + }) + + t.Run("nil", func(t *testing.T) { + testRepeatedUInt64MarshalUnpacked(t, nil, false) + }) +} + func TestFixed64Marshal(t *testing.T) { t.Run("zero", func(t *testing.T) { testFixed64Marshal(t, 0, false) @@ -740,6 +779,24 @@ func testRepeatedUInt64Marshal(t *testing.T, n []uint64, wrongField bool) { } } +func testRepeatedUInt64MarshalUnpacked(t *testing.T, n []uint64, wrongField bool) { + var ( + custom = stableRepPrimitives{FieldFu: n} + transport = test.RepPrimitives{FieldFu: n} + ) + + result := testRepMarshal(t, custom, &transport, wrongField) + + if !wrongField { + require.Len(t, result.FieldFu, len(n)) + if len(n) > 0 { + require.Equal(t, n, result.FieldFu) + } + } else { + require.Len(t, result.FieldFu, 0) + } +} + func testFixed64Marshal(t *testing.T, n uint64, wrongField bool) { var ( custom = stablePrimitives{FieldI: n} diff --git a/util/proto/test/test.pb.go b/util/proto/test/test.pb.go index 2fa2d4ec13f2187e68e531a9b2cc4ccd33983f6d..e8ec7198066833ac9827a3f09bce137dbf1a1f31 100644 GIT binary patch delta 292 zcmbQ8voe1|Hk+xQk-47H#zHoc$z>ublWzjCjc5+1LUe3tW?qT0(d37m!jlh+25Os*A6nf#BNjmfPP$dpkq*C|RZ(8){8O|?zSOwCD&Pb<~QD$dV? z@PLXAa)>gel}>Kpk(`_-&A<7NSRd$PlGZ5R{6fA847B`=~K}%8&q|gMc%K)O+04xb6 pO^qhkDQU=>AnAoDH8cb%GED?pGnrRAa&m(#8xt4j<~;4STmXZ9R7L;* delta 119 zcmZ3PKRahaHk*l_k+Gi1#zHoc$rU2WlkWhrgJ|~THKN(<(Xp9%C1xg*1Gz*d8;b=@ z{vakjdA^v(