1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

internal/proto: re-use map[uint64]any

use slice will cause some panic.
This commit is contained in:
wdvxdr 2023-02-11 18:01:19 +08:00
parent 733944693c
commit 841fef387a
2 changed files with 21 additions and 6 deletions

View File

@ -175,7 +175,7 @@ func (g *GuildInfo) removeChannel(id uint64) {
} }
func (s *GuildService) GetUserProfile(tinyId uint64) (*GuildUserProfile, error) { func (s *GuildService) GetUserProfile(tinyId uint64) (*GuildUserProfile, error) {
flags := make(proto.DynamicMessage, 101) flags := proto.DynamicMessage{}
for i := 3; i <= 29; i++ { for i := 3; i <= 29; i++ {
flags[uint64(i)] = 1 flags[uint64(i)] = 1
} }
@ -275,7 +275,7 @@ func (s *GuildService) FetchGuildMemberListWithRole(guildId, channelId uint64, s
// FetchGuildMemberProfileInfo 获取单个频道成员资料 // FetchGuildMemberProfileInfo 获取单个频道成员资料
func (s *GuildService) FetchGuildMemberProfileInfo(guildId, tinyId uint64) (*GuildUserProfile, error) { func (s *GuildService) FetchGuildMemberProfileInfo(guildId, tinyId uint64) (*GuildUserProfile, error) {
seq := s.c.nextSeq() seq := s.c.nextSeq()
flags := make(proto.DynamicMessage, 101) flags := proto.DynamicMessage{}
for i := 3; i <= 29; i++ { for i := 3; i <= 29; i++ {
flags[uint64(i)] = 1 flags[uint64(i)] = 1
} }

View File

@ -3,9 +3,10 @@ package proto
import ( import (
"encoding/binary" "encoding/binary"
"math" "math"
"sort"
) )
type DynamicMessage []any type DynamicMessage map[uint64]any
// zigzag encoding types // zigzag encoding types
type ( type (
@ -20,10 +21,24 @@ type encoder struct {
func (msg DynamicMessage) Encode() []byte { func (msg DynamicMessage) Encode() []byte {
en := encoder{} en := encoder{}
// sort all items
type pair struct {
key uint64
value any
}
var all []pair
for k, v := range msg {
all = append(all, pair{key: k, value: v})
}
sort.Slice(all, func(i, j int) bool {
return all[i].key < all[j].key
})
//nolint:staticcheck //nolint:staticcheck
for id, value := range msg { for _, item := range all {
key := uint64(id << 3) key := item.key << 3
switch v := value.(type) { switch v := item.value.(type) {
case bool: case bool:
en.uvarint(key | 0) en.uvarint(key | 0)
vi := uint64(0) vi := uint64(0)