mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
internal/proto: ensure dynamic message key incremental
This commit is contained in:
parent
d9cc29c678
commit
bdb083558b
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user