-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxredis_get.go
226 lines (207 loc) · 5.84 KB
/
xredis_get.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package redis
import (
"context"
"errors"
"reflect"
"time"
)
func (c *Client) GetValue(ctx context.Context, key string, value interface{}, opts ...Option) (err error) {
options := c.options
for _, opt := range opts {
opt(&options)
}
valValue := reflect.ValueOf(value)
if valValue.Kind() != reflect.Ptr {
return errors.New("value CanSet returns false")
}
valValue = valValue.Elem()
switch valValue.Kind() {
case reflect.Struct:
if _, ok := valValue.Interface().(time.Time); ok {
return c.getSingleValue(ctx, key, valValue, options)
}
return c.getStructValue(ctx, key, valValue, options)
case reflect.Array:
return c.getArrayValue(ctx, key, valValue, options)
case reflect.Slice:
return c.getSliceValue(ctx, key, valValue, options)
case reflect.Map:
switch val := value.(type) {
case *map[string]string, *map[string]interface{}:
return c.getMapValue(ctx, key, val, options)
}
return errors.New("value map is not map[string]string, map[string]interface{}")
default:
return c.getSingleValue(ctx, key, valValue, options)
}
}
func (c *Client) GetSingleValue(ctx context.Context, key string, value interface{}, opts ...Option) (err error) {
options := c.options
for _, opt := range opts {
opt(&options)
}
valValue := reflect.ValueOf(value)
if valValue.Kind() == reflect.Ptr {
valValue = valValue.Elem()
}
return c.getSingleValue(ctx, key, valValue, options)
}
func (c *Client) GetSliceValue(ctx context.Context, key string, value interface{}, opts ...Option) (err error) {
options := c.options
for _, opt := range opts {
opt(&options)
}
valValue := reflect.ValueOf(value)
if valValue.Kind() == reflect.Ptr {
valValue = valValue.Elem()
}
switch valValue.Kind() {
case reflect.Array:
return c.getArrayValue(ctx, key, valValue, options)
case reflect.Slice:
return c.getSliceValue(ctx, key, valValue, options)
default:
return errors.New("value is not array or slice")
}
}
func (c *Client) GetStructValue(ctx context.Context, key string, value interface{}, opts ...Option) (err error) {
options := c.options
for _, opt := range opts {
opt(&options)
}
valValue := reflect.ValueOf(value)
if valValue.Kind() != reflect.Ptr {
return errors.New("value CanSet returns false")
}
valValue = valValue.Elem()
switch valValue.Kind() {
case reflect.Struct:
return c.getStructValue(ctx, key, valValue, options)
default:
return errors.New("value is not struct")
}
}
func (c *Client) GetMapValue(ctx context.Context, key string, value interface{}, opts ...Option) (err error) {
options := c.options
for _, opt := range opts {
opt(&options)
}
switch val := value.(type) {
case map[string]string, map[string]interface{}, *map[string]string, *map[string]interface{}:
return c.getMapValue(ctx, key, val, options)
default:
return errors.New("value map is not map[string]string, map[string]interface{}")
}
}
func (c *Client) getSingleValue(ctx context.Context, key string, valValue reflect.Value, options Options) (err error) {
str, err := c.Get(ctx, key).Result()
if err != nil {
return err
}
return setValueByString(valValue, str)
}
func (c *Client) getSliceValue(ctx context.Context, key string, valValue reflect.Value, options Options) (err error) {
if options.Stop == 0 {
options.Stop = int64(valValue.Len() - 1)
}
strings, err := c.LRange(ctx, key, options.Start, options.Stop).Result()
if err != nil {
return err
}
return setSlice(strings, valValue)
}
func (c *Client) getArrayValue(ctx context.Context, key string, valValue reflect.Value, options Options) (err error) {
if options.Stop == 0 {
options.Stop = int64(valValue.Len() - 1)
}
strings, err := c.LRange(ctx, key, options.Start, options.Stop).Result()
if err != nil {
return err
}
return setArray(strings, valValue)
}
func (c *Client) getStructValue(ctx context.Context, key string, valValue reflect.Value, options Options) (err error) {
fieldKeyIdxMap := make(map[string]int)
numField := valValue.NumField()
for i := 0; i < numField; i++ {
key := getStructKey(valValue.Type(), i, options.Tag)
if key == "" {
continue
}
fieldKeyIdxMap[key] = i
}
fieldKeys := make([]string, 0, len(fieldKeyIdxMap))
for k := range fieldKeyIdxMap {
fieldKeys = append(fieldKeys, k)
}
fieldVals, err := c.HMGet(ctx, key, fieldKeys...).Result()
if err != nil {
return err
}
if len(fieldKeys) != len(fieldVals) {
return errors.New("HMGet should have the same number of keys and vals")
}
for i := range fieldVals {
key := fieldKeys[i]
idx := fieldKeyIdxMap[key]
field := valValue.Field(idx)
valStr, ok := fieldVals[i].(string)
if !ok {
continue
}
err := setValueByString(field, valStr)
if err != nil {
return err
}
}
return nil
}
func (c *Client) getMapValueSring(ctx context.Context, key string, val map[string]string, options Options) (err error) {
stringStringMap, err := c.HGetAll(ctx, key).Result()
if err != nil {
return err
}
for k, v := range stringStringMap {
val[k] = v
}
return nil
}
func (c *Client) getMapValue(ctx context.Context, key string, val interface{}, options Options) (err error) {
switch val := val.(type) {
case map[string]string:
stringStringMap, err := c.HGetAll(ctx, key).Result()
if err != nil {
return err
}
for k, v := range stringStringMap {
val[k] = v
}
case map[string]interface{}:
stringStringMap, err := c.HGetAll(ctx, key).Result()
if err != nil {
return err
}
for k, v := range stringStringMap {
val[k] = v
}
case *map[string]string:
stringStringMap, err := c.HGetAll(ctx, key).Result()
if err != nil {
return err
}
for k, v := range stringStringMap {
(*(val))[k] = v
}
case *map[string]interface{}:
stringStringMap, err := c.HGetAll(ctx, key).Result()
if err != nil {
return err
}
for k, v := range stringStringMap {
(*(val))[k] = v
}
default:
return errors.New("value map is not map[string]string, map[string]interface{}")
}
return nil
}