-
Notifications
You must be signed in to change notification settings - Fork 1
/
redisArrayReplyReader.go
230 lines (204 loc) · 4.8 KB
/
redisArrayReplyReader.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
227
228
229
230
package goredis
import (
"strconv"
"gopkg.in/guregu/null.v3"
)
// EmptyRedisReplyValue -
var EmptyRedisReplyValue = &RedisReplyValue{value: nil}
// RedisReplyValue -
type RedisReplyValue struct {
value interface{}
}
// NewRedisReplyValue -
func NewRedisReplyValue(value interface{}) *RedisReplyValue {
return &RedisReplyValue{
value: value,
}
}
// Value -
func (v *RedisReplyValue) Value() interface{} {
return v.value
}
// AsInt32 -
func (v *RedisReplyValue) AsInt32(defaultValue int32) (int32, error) {
if v.value != nil {
switch v.value.(type) {
case string:
{
r, err := strconv.ParseFloat(v.value.(string), 32)
if err != nil {
return defaultValue, err
}
return int32(r), nil
}
case int:
return int32(v.value.(int)), nil
case int32:
return v.value.(int32), nil
case int64:
return int32(v.value.(int64)), nil
}
}
return defaultValue, nil
}
// AsInt64 -
func (v *RedisReplyValue) AsInt64(defaultValue int64) (int64, error) {
if v.value != nil {
switch v.value.(type) {
case string:
{
r, err := strconv.ParseFloat(v.value.(string), 64)
if err != nil {
return defaultValue, err
}
return int64(r), nil
}
case int:
return int64(v.value.(int)), nil
case int32:
return int64(v.value.(int32)), nil
case int64:
return v.value.(int64), nil
}
}
return defaultValue, nil
}
// AsFloat64 -
func (v *RedisReplyValue) AsFloat64(defaultValue float64) (float64, error) {
if v.value != nil {
switch v.value.(type) {
case string:
{
r, err := strconv.ParseFloat(v.value.(string), 64)
if err != nil {
return defaultValue, err
}
return r, nil
}
case int:
return float64(v.value.(int)), nil
case int32:
return float64(v.value.(int32)), nil
case int64:
return float64(v.value.(int64)), nil
case float32:
return float64(v.value.(float32)), nil
case float64:
return v.value.(float64), nil
}
}
return defaultValue, nil
}
// AsString -
func (v *RedisReplyValue) AsString() string {
if v.value != nil {
switch v.value.(type) {
case string:
return v.value.(string)
case int:
return strconv.FormatInt(int64(v.value.(int)), 10)
case int32:
return strconv.FormatInt(int64(v.value.(int32)), 10)
case int64:
return strconv.FormatInt(v.value.(int64), 10)
}
}
return ""
}
// IsNil -
func (v *RedisReplyValue) IsNil() bool {
return v.value == nil
}
// ToArrayReplyReader -
func (v *RedisReplyValue) ToArrayReplyReader() *RedisArrayReplyReader {
i, ok := v.value.([]interface{})
if ok {
return NewRedisArrayReplyReader(i)
}
return nil
}
// GetNullableInt -
func GetNullableInt(value *RedisReplyValue) (null.Int, error) {
if value.value == nil {
return null.IntFromPtr(nil), nil
}
var result int64
var err error
if result, err = value.AsInt64(0); err != nil {
return null.IntFromPtr(nil), err
}
return null.IntFrom(result), nil
}
// GetNullableString -
func GetNullableString(value *RedisReplyValue) null.String {
if value.value == nil {
return null.StringFromPtr(nil)
}
return null.StringFrom(value.AsString())
}
// RedisArrayReplyReader -
type RedisArrayReplyReader struct {
redisReply []interface{}
position uint32
}
// NewRedisArrayReplyReader -
func NewRedisArrayReplyReader(redisReply []interface{}) *RedisArrayReplyReader {
return &RedisArrayReplyReader{
redisReply: redisReply,
position: 0,
}
}
// GetLength -
func (r *RedisArrayReplyReader) GetLength() int {
return len(r.redisReply)
}
// HasNext -
func (r *RedisArrayReplyReader) HasNext() bool {
values := r.redisReply
pos := r.position
return pos < uint32(len(values))
}
// ReadArray -
func (r *RedisArrayReplyReader) ReadArray() *RedisArrayReplyReader {
return NewRedisArrayReplyReader(r.ReadValue().value.([]interface{}))
}
// ReadString -
func (r *RedisArrayReplyReader) ReadString() string {
return r.ReadValue().AsString()
}
// ReadInt32 -
func (r *RedisArrayReplyReader) ReadInt32(defaultValue int32) (int32, error) {
return r.ReadValue().AsInt32(defaultValue)
}
// ReadInt64 -
func (r *RedisArrayReplyReader) ReadInt64(defaultValue int64) (int64, error) {
return r.ReadValue().AsInt64(defaultValue)
}
// ReadFloat64 -
func (r *RedisArrayReplyReader) ReadFloat64(defaultValue float64) (float64, error) {
return r.ReadValue().AsFloat64(defaultValue)
}
// SkipValue -
func (r *RedisArrayReplyReader) SkipValue() {
r.ReadValue()
}
// ReadValue -
func (r *RedisArrayReplyReader) ReadValue() *RedisReplyValue {
values := r.redisReply
pos := r.position
r.position++
if pos < uint32(len(values)) {
return &RedisReplyValue{value: values[pos]}
}
return EmptyRedisReplyValue
}
// ForEach -
func (r *RedisArrayReplyReader) ForEach(action func(i int, v *RedisReplyValue) error) error {
for i, v := range r.redisReply {
err := action(i, &RedisReplyValue{value: v})
if err != nil {
return err
}
}
return nil
}