From bdb083558ba32997771875ab430b1db463b9e5e7 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 8 Feb 2023 14:42:14 +0800 Subject: [PATCH] internal/proto: ensure dynamic message key incremental --- internal/proto/dynamic.go | 4 ++-- internal/proto/dynamic_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/proto/dynamic.go b/internal/proto/dynamic.go index 18feefbf..466ee2ad 100644 --- a/internal/proto/dynamic.go +++ b/internal/proto/dynamic.go @@ -5,7 +5,7 @@ import ( "math" ) -type DynamicMessage map[uint64]any +type DynamicMessage []any // zigzag encoding types type ( @@ -22,7 +22,7 @@ func (msg DynamicMessage) Encode() []byte { en := encoder{} //nolint:staticcheck for id, value := range msg { - key := id << 3 + key := uint64(id << 3) switch v := value.(type) { case bool: en.uvarint(key | 0) diff --git a/internal/proto/dynamic_test.go b/internal/proto/dynamic_test.go index 4cea582a..7a192411 100644 --- a/internal/proto/dynamic_test.go +++ b/internal/proto/dynamic_test.go @@ -1,6 +1,7 @@ package proto import ( + "bytes" "math" "testing" ) @@ -44,3 +45,15 @@ func Benchmark_encoder_svarint(b *testing.B) { benchEncoderSvarint(b, math.MaxInt64) }) } + +func TestDynamicMessage_Encode(t *testing.T) { + input := DynamicMessage{ + 1: 2, + 3: 4, + } + got := input.Encode() + expected := []byte{1 << 3, 2, 3 << 3, 4} + if !bytes.Equal(got, expected) { + t.Fatalf("expected %v but got %v", expected, got) + } +}