-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmder.go
83 lines (74 loc) · 2.19 KB
/
cmder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package redis
import (
"reflect"
"github.com/bytedance/sonic"
"github.com/farseer-go/fs/parse"
"github.com/farseer-go/fs/types"
"github.com/go-redis/redis/v8"
)
type PipelineCmder struct {
cmder []redis.Cmder
}
func (receiver PipelineCmder) Fill(sliceOrList any) {
sliceOrListVal := reflect.ValueOf(sliceOrList).Elem()
// 切片类型
if sliceType, isSlice := types.IsSlice(sliceOrListVal); isSlice {
itemType := sliceType.Elem()
value := reflect.MakeSlice(sliceType, 0, 0)
for _, cmder := range receiver.cmder {
switch cmderType := cmder.(type) {
case *redis.StringCmd:
item := reflect.New(itemType).Interface()
if jsonVal := cmderType.Val(); jsonVal != "" {
_ = sonic.Unmarshal([]byte(jsonVal), item)
value = reflect.Append(value, reflect.ValueOf(item).Elem())
}
case *redis.StringStringMapCmd:
jsonVal := cmderType.Val()
for _, v := range jsonVal {
value = reflect.Append(value, reflect.ValueOf(parse.ConvertValue(v, itemType)))
}
}
}
sliceOrListVal.Set(value)
return
}
// List类型
if listType, isList := types.IsList(sliceOrListVal); isList {
itemType := types.GetListItemType(listType)
value := types.ListNew(listType)
for _, cmder := range receiver.cmder {
switch cmderType := cmder.(type) {
case *redis.StringCmd:
item := reflect.New(itemType).Interface()
if jsonVal := cmderType.Val(); jsonVal != "" {
_ = sonic.Unmarshal([]byte(jsonVal), item)
types.ListAdd(value, item)
}
case *redis.StringStringMapCmd:
jsonVal := cmderType.Val()
for _, v := range jsonVal {
types.ListAdd(value, parse.ConvertValue(v, itemType))
}
}
}
sliceOrListVal.Set(value.Elem())
return
}
// map类型
if mapType, isList := types.IsMap(sliceOrListVal); isList {
// make.... var m map[k]v = make(map[k]v)
sliceOrListVal.Set(reflect.MakeMap(mapType))
for _, cmder := range receiver.cmder {
switch cmderType := cmder.(type) {
case *redis.StringStringMapCmd:
jsonVal := cmderType.Val()
for k, v := range jsonVal {
sliceOrListVal.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
}
}
return
}
panic("sliceOrList入参必须为切片、collections.List类型、map类型")
}