1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

internal/proto: ensure dynamic message key incremental

This commit is contained in:
wdvxdr 2023-02-08 14:42:14 +08:00
parent d9cc29c678
commit bdb083558b
2 changed files with 15 additions and 2 deletions

View File

@ -5,7 +5,7 @@ import (
"math" "math"
) )
type DynamicMessage map[uint64]any type DynamicMessage []any
// zigzag encoding types // zigzag encoding types
type ( type (
@ -22,7 +22,7 @@ func (msg DynamicMessage) Encode() []byte {
en := encoder{} en := encoder{}
//nolint:staticcheck //nolint:staticcheck
for id, value := range msg { for id, value := range msg {
key := id << 3 key := uint64(id << 3)
switch v := value.(type) { switch v := value.(type) {
case bool: case bool:
en.uvarint(key | 0) en.uvarint(key | 0)

View File

@ -1,6 +1,7 @@
package proto package proto
import ( import (
"bytes"
"math" "math"
"testing" "testing"
) )
@ -44,3 +45,15 @@ func Benchmark_encoder_svarint(b *testing.B) {
benchEncoderSvarint(b, math.MaxInt64) 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)
}
}