From a66cfca9c89f5bda4b249eff238fed4bbd8a6a1c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 7 May 2021 12:16:09 +0200 Subject: [PATCH] PackAny(): support numbers only as float64 --- pkg/icingadb/objectpacker/objectpacker.go | 21 ++++---------- .../objectpacker/objectpacker_test.go | 28 +++++++++---------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/pkg/icingadb/objectpacker/objectpacker.go b/pkg/icingadb/objectpacker/objectpacker.go index 85c0f6fb..073909ff 100644 --- a/pkg/icingadb/objectpacker/objectpacker.go +++ b/pkg/icingadb/objectpacker/objectpacker.go @@ -33,12 +33,12 @@ func packValue(in reflect.Value, out io.Writer) error { _, err := out.Write([]byte{1}) return err } - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return packFloat64(float64(in.Int()), out) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return packFloat64(float64(in.Uint()), out) - case reflect.Float32, reflect.Float64: - return packFloat64(in.Float(), out) + case reflect.Float64: + if _, err := out.Write([]byte{3}); err != nil { + return err + } + + return binary.Write(out, binary.BigEndian, in.Float()) case reflect.Array, reflect.Slice: if typ := in.Type(); typ.Elem().Kind() == reflect.Uint8 { if kind == reflect.Array { @@ -131,15 +131,6 @@ func packValue(in reflect.Value, out io.Writer) error { } } -// packFloat64 deduplicates float packing of multiple locations in packValue. -func packFloat64(in float64, out io.Writer) error { - if _, errWr := out.Write([]byte{3}); errWr != nil { - return errWr - } - - return binary.Write(out, binary.BigEndian, in) -} - // packString deduplicates string packing of multiple locations in packValue. func packString(in []byte, out io.Writer) error { if _, err := out.Write([]byte{4}); err != nil { diff --git a/pkg/icingadb/objectpacker/objectpacker_test.go b/pkg/icingadb/objectpacker/objectpacker_test.go index 3f7f201c..5d281073 100644 --- a/pkg/icingadb/objectpacker/objectpacker_test.go +++ b/pkg/icingadb/objectpacker/objectpacker_test.go @@ -62,20 +62,20 @@ func TestPackAny(t *testing.T) { assertPackAny(t, false, []byte{1}) assertPackAny(t, true, []byte{2}) - assertPackAny(t, -42, []byte{3, 0xc0, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, int8(-42), []byte{3, 0xc0, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, int16(-42), []byte{3, 0xc0, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, int32(-42), []byte{3, 0xc0, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, int64(-42), []byte{3, 0xc0, 0x45, 0, 0, 0, 0, 0, 0}) + assertPackAnyPanic(t, -42, 0) + assertPackAnyPanic(t, int8(-42), 0) + assertPackAnyPanic(t, int16(-42), 0) + assertPackAnyPanic(t, int32(-42), 0) + assertPackAnyPanic(t, int64(-42), 0) - assertPackAny(t, uint(42), []byte{3, 0x40, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, uint8(42), []byte{3, 0x40, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, uint16(42), []byte{3, 0x40, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, uint32(42), []byte{3, 0x40, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, uint64(42), []byte{3, 0x40, 0x45, 0, 0, 0, 0, 0, 0}) - assertPackAny(t, uintptr(42), []byte{3, 0x40, 0x45, 0, 0, 0, 0, 0, 0}) + assertPackAnyPanic(t, uint(42), 0) + assertPackAnyPanic(t, uint8(42), 0) + assertPackAnyPanic(t, uint16(42), 0) + assertPackAnyPanic(t, uint32(42), 0) + assertPackAnyPanic(t, uint64(42), 0) + assertPackAnyPanic(t, uintptr(42), 0) - assertPackAny(t, float32(-42.5), []byte{3, 0xc0, 0x45, 0x40, 0, 0, 0, 0, 0}) + assertPackAnyPanic(t, float32(-42.5), 0) assertPackAny(t, -42.5, []byte{3, 0xc0, 0x45, 0x40, 0, 0, 0, 0, 0}) assertPackAny(t, []struct{}(nil), []byte{5, 0, 0, 0, 0, 0, 0, 0, 0}) @@ -107,7 +107,7 @@ func TestPackAny(t *testing.T) { 4, 0, 0, 0, 0, 0, 0, 0, 0, }) - assertPackAny(t, map[string]uint8{"": 42}, []byte{ + assertPackAny(t, map[string]float64{"": 42}, []byte{ 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0x40, 0x45, 0, 0, 0, 0, 0, 0, @@ -116,7 +116,7 @@ func TestPackAny(t *testing.T) { assertPackAnyPanic(t, map[struct{}]struct{}{{}: {}}, 9) assertPackAny(t, (*int)(nil), []byte{0}) - assertPackAny(t, new(int), []byte{3, 0, 0, 0, 0, 0, 0, 0, 0}) + assertPackAny(t, new(float64), []byte{3, 0, 0, 0, 0, 0, 0, 0, 0}) assertPackAny(t, "", []byte{4, 0, 0, 0, 0, 0, 0, 0, 0}) assertPackAny(t, "a", []byte{4, 0, 0, 0, 0, 0, 0, 0, 1, 'a'})