-
Notifications
You must be signed in to change notification settings - Fork 22
/
helpers.go
221 lines (167 loc) · 4.5 KB
/
helpers.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
package stalecucumber
import "fmt"
import "math/big"
/*
This type is returned whenever a helper cannot convert the result of
Unpickle into the desired type.
*/
type WrongTypeError struct {
Result interface{}
Request string
}
func (wte WrongTypeError) Error() string {
return fmt.Sprintf("Unpickling returned type %T which cannot be converted to %s", wte.Result, wte.Request)
}
func newWrongTypeError(result interface{}, request interface{}) error {
return WrongTypeError{Result: result, Request: fmt.Sprintf("%T", request)}
}
/*
This helper attempts to convert the return value of Unpickle into a string.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func String(v interface{}, err error) (string, error) {
if err != nil {
return "", err
}
vs, ok := v.(string)
if ok {
return vs, nil
}
return "", newWrongTypeError(v, vs)
}
/*
This helper attempts to convert the return value of Unpickle into a int64.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func Int(v interface{}, err error) (int64, error) {
if err != nil {
return 0, err
}
vi, ok := v.(int64)
if ok {
return vi, nil
}
vbi, ok := v.(*big.Int)
if ok {
if vbi.BitLen() <= 63 {
return vbi.Int64(), nil
}
}
return 0, newWrongTypeError(v, vi)
}
/*
This helper attempts to convert the return value of Unpickle into a bool.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func Bool(v interface{}, err error) (bool, error) {
if err != nil {
return false, err
}
vb, ok := v.(bool)
if ok {
return vb, nil
}
return false, newWrongTypeError(v, vb)
}
/*
This helper attempts to convert the return value of Unpickle into a *big.Int.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func Big(v interface{}, err error) (*big.Int, error) {
if err != nil {
return nil, err
}
vb, ok := v.(*big.Int)
if ok {
return vb, nil
}
return nil, newWrongTypeError(v, vb)
}
/*
This helper attempts to convert the return value of Unpickle into a float64.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func Float(v interface{}, err error) (float64, error) {
if err != nil {
return 0.0, err
}
vf, ok := v.(float64)
if ok {
return vf, nil
}
return 0.0, newWrongTypeError(v, vf)
}
/*
This helper attempts to convert the return value of Unpickle into a []interface{}.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func ListOrTuple(v interface{}, err error) ([]interface{}, error) {
if err != nil {
return nil, err
}
vl, ok := v.([]interface{})
if ok {
return vl, nil
}
return nil, newWrongTypeError(v, vl)
}
/*
This helper attempts to convert the return value of Unpickle into a map[interface{}]interface{}.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func Dict(v interface{}, err error) (map[interface{}]interface{}, error) {
if err != nil {
return nil, err
}
vd, ok := v.(map[interface{}]interface{})
if ok {
return vd, nil
}
return nil, newWrongTypeError(v, vd)
}
/*
This helper attempts to convert the return value of Unpickle into a map[interface{}]bool.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func Set(v interface{}, err error) (map[interface{}]bool, error) {
if err != nil {
return nil, err
}
vd, ok := v.(map[interface{}]bool)
if ok {
return vd, nil
}
return nil, newWrongTypeError(v, vd)
}
/*
This helper attempts to convert the return value of Unpickle into a map[string]interface{}.
If Unpickle returns an error that error is returned immediately.
If the value cannot be converted an error is returned.
*/
func DictString(v interface{}, err error) (map[string]interface{}, error) {
var src map[interface{}]interface{}
src, err = Dict(v, err)
if err != nil {
return nil, err
}
return tryDictToDictString(src)
}
func tryDictToDictString(src map[interface{}]interface{}) (map[string]interface{}, error) {
dst := make(map[string]interface{}, len(src))
for k, v := range src {
kstr, ok := k.(string)
if !ok {
return nil, newWrongTypeError(src, dst)
}
dst[kstr] = v
}
return dst, nil
}