diff --git a/util/proto/marshal.go b/util/proto/marshal.go index 66824b3..a602b78 100644 --- a/util/proto/marshal.go +++ b/util/proto/marshal.go @@ -8,6 +8,7 @@ package proto import ( "encoding/binary" + "math" "math/bits" "reflect" ) @@ -360,3 +361,26 @@ func Fixed64Size(fNum int, v uint64) int { return VarUIntSize(uint64(prefix)) + 8 } + +func Float64Marshal(field int, buf []byte, v float64) (int, error) { + if v == 0 { + return 0, nil + } + + prefix := field<<3 | 1 + + i := binary.PutUvarint(buf, uint64(prefix)) + binary.LittleEndian.PutUint64(buf[i:], math.Float64bits(v)) + + return i + 8, nil +} + +func Float64Size(fNum int, v float64) int { + if v == 0 { + return 0 + } + + prefix := fNum<<3 | 1 + + return VarUIntSize(uint64(prefix)) + 8 +} diff --git a/util/proto/marshal_test.go b/util/proto/marshal_test.go index 6844309..34b5a44 100644 --- a/util/proto/marshal_test.go +++ b/util/proto/marshal_test.go @@ -23,6 +23,7 @@ type stablePrimitives struct { FieldG uint64 FieldH SomeEnum FieldI uint64 // fixed64 + FieldJ float64 } type stableRepPrimitives struct { @@ -133,6 +134,16 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e } i += offset + fieldNum = 206 + if wrongField { + fieldNum++ + } + offset, err = proto.Float64Marshal(fieldNum, buf, s.FieldJ) + if err != nil { + return nil, errors.Wrap(err, "can't marshal field J") + } + i += offset + fieldNum = 300 if wrongField { fieldNum++ @@ -155,6 +166,7 @@ func (s *stablePrimitives) stableSize() int { proto.Int64Size(203, s.FieldF) + proto.UInt64Size(204, s.FieldG) + proto.Fixed64Size(205, s.FieldI) + + proto.Float64Size(206, s.FieldJ) + proto.EnumSize(300, int32(s.FieldH)) } @@ -453,6 +465,19 @@ func TestFixed64Marshal(t *testing.T) { }) } +func TestFloat64Marshal(t *testing.T) { + t.Run("zero", func(t *testing.T) { + testFloat64Marshal(t, 0, false) + }) + + t.Run("non zero", func(t *testing.T) { + f := math.Float64frombits(12345677890) + + testFloat64Marshal(t, f, false) + testFloat64Marshal(t, f, true) + }) +} + func testMarshal(t *testing.T, c stablePrimitives, tr test.Primitives, wrongField bool) *test.Primitives { var ( wire []byte @@ -589,6 +614,21 @@ func testUInt64Marshal(t *testing.T, n uint64, wrongField bool) { } } +func testFloat64Marshal(t *testing.T, n float64, wrongField bool) { + var ( + custom = stablePrimitives{FieldJ: n} + transport = test.Primitives{FieldJ: n} + ) + + result := testMarshal(t, custom, transport, wrongField) + + if !wrongField { + require.Equal(t, n, result.FieldJ) + } else { + require.EqualValues(t, 0, result.FieldJ) + } +} + func testEnumMarshal(t *testing.T, e SomeEnum, wrongField bool) { var ( custom = stablePrimitives{FieldH: e} diff --git a/util/proto/test/test.pb.go b/util/proto/test/test.pb.go index a0808f8..b194e1f 100644 --- a/util/proto/test/test.pb.go +++ b/util/proto/test/test.pb.go @@ -87,6 +87,7 @@ type Primitives struct { FieldF int64 `protobuf:"varint,203,opt,name=field_f,json=fieldF,proto3" json:"field_f,omitempty"` FieldG uint64 `protobuf:"varint,204,opt,name=field_g,json=fieldG,proto3" json:"field_g,omitempty"` FieldI uint64 `protobuf:"fixed64,205,opt,name=field_i,json=fieldI,proto3" json:"field_i,omitempty"` + FieldJ float64 `protobuf:"fixed64,206,opt,name=field_j,json=fieldJ,proto3" json:"field_j,omitempty"` FieldH Primitives_SomeEnum `protobuf:"varint,300,opt,name=field_h,json=fieldH,proto3,enum=test.Primitives_SomeEnum" json:"field_h,omitempty"` } @@ -178,6 +179,13 @@ func (x *Primitives) GetFieldI() uint64 { return 0 } +func (x *Primitives) GetFieldJ() float64 { + if x != nil { + return x.FieldJ + } + return 0 +} + func (x *Primitives) GetFieldH() Primitives_SomeEnum { if x != nil { return x.FieldH @@ -277,7 +285,7 @@ var File_util_proto_test_test_proto protoreflect.FileDescriptor var file_util_proto_test_test_proto_rawDesc = []byte{ 0x0a, 0x1a, 0x75, 0x74, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x74, 0x65, - 0x73, 0x74, 0x22, 0xcd, 0x02, 0x0a, 0x0a, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, + 0x73, 0x74, 0x22, 0xe7, 0x02, 0x0a, 0x0a, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x73, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x41, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x62, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x66, 0x69, 0x65, @@ -291,26 +299,27 @@ var file_util_proto_test_test_proto_rawDesc = []byte{ 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x67, 0x18, 0xcc, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x47, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x69, 0x18, 0xcd, 0x01, 0x20, 0x01, 0x28, 0x06, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x49, 0x12, - 0x33, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x68, 0x18, 0xac, 0x02, 0x20, 0x01, 0x28, - 0x0e, 0x32, 0x19, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, - 0x76, 0x65, 0x73, 0x2e, 0x53, 0x6f, 0x6d, 0x65, 0x45, 0x6e, 0x75, 0x6d, 0x52, 0x06, 0x66, 0x69, - 0x65, 0x6c, 0x64, 0x48, 0x22, 0x3c, 0x0a, 0x08, 0x53, 0x6f, 0x6d, 0x65, 0x45, 0x6e, 0x75, 0x6d, - 0x12, 0x0b, 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, 0x22, 0xa5, 0x01, 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, 0x62, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, - 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, - 0x63, 0x18, 0x03, 0x20, 0x03, 0x28, 0x05, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x43, 0x12, - 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x64, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0d, - 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x44, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, - 0x64, 0x5f, 0x65, 0x18, 0x05, 0x20, 0x03, 0x28, 0x03, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, - 0x45, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x66, 0x18, 0x06, 0x20, 0x03, - 0x28, 0x04, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x46, 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, + 0x18, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x6a, 0x18, 0xce, 0x01, 0x20, 0x01, 0x28, + 0x01, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x4a, 0x12, 0x33, 0x0a, 0x07, 0x66, 0x69, 0x65, + 0x6c, 0x64, 0x5f, 0x68, 0x18, 0xac, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x19, 0x2e, 0x74, 0x65, + 0x73, 0x74, 0x2e, 0x50, 0x72, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x76, 0x65, 0x73, 0x2e, 0x53, 0x6f, + 0x6d, 0x65, 0x45, 0x6e, 0x75, 0x6d, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x48, 0x22, 0x3c, + 0x0a, 0x08, 0x53, 0x6f, 0x6d, 0x65, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x0b, 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, 0x22, 0xa5, 0x01, 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, 0x62, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, + 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x63, 0x18, 0x03, 0x20, 0x03, 0x28, + 0x05, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x43, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, + 0x6c, 0x64, 0x5f, 0x64, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0d, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, + 0x64, 0x44, 0x12, 0x17, 0x0a, 0x07, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x65, 0x18, 0x05, 0x20, + 0x03, 0x28, 0x03, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x45, 0x12, 0x17, 0x0a, 0x07, 0x66, + 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x66, 0x18, 0x06, 0x20, 0x03, 0x28, 0x04, 0x52, 0x06, 0x66, 0x69, + 0x65, 0x6c, 0x64, 0x46, 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 ( diff --git a/util/proto/test/test.proto b/util/proto/test/test.proto index e9c2758..c568a31 100644 --- a/util/proto/test/test.proto +++ b/util/proto/test/test.proto @@ -13,6 +13,7 @@ message Primitives { int64 field_f = 203; uint64 field_g = 204; fixed64 field_i = 205; + double field_j = 206; enum SomeEnum { UNKNOWN = 0;