-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconversions.go
189 lines (176 loc) · 3.41 KB
/
conversions.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
package mexpr
import (
"fmt"
"reflect"
"time"
)
func isNumber(v interface{}) bool {
switch v.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case float32, float64:
return true
}
return false
}
func toNumber(ast *Node, v interface{}) (float64, Error) {
switch n := v.(type) {
case float64:
return n, nil
case int:
return float64(n), nil
case int8:
return float64(n), nil
case int16:
return float64(n), nil
case int32:
return float64(n), nil
case int64:
return float64(n), nil
case uint:
return float64(n), nil
case uint8:
return float64(n), nil
case uint16:
return float64(n), nil
case uint32:
return float64(n), nil
case uint64:
return float64(n), nil
case float32:
return float64(n), nil
}
return 0, NewError(ast.Offset, ast.Length, "unable to convert to number: %v", v)
}
func isString(v interface{}) bool {
switch v.(type) {
case string, rune, byte, []byte:
return true
}
return false
}
func toString(v interface{}) string {
switch s := v.(type) {
case string:
return s
case rune:
return string(s)
case byte:
return string(s)
case []byte:
return string(s)
}
return fmt.Sprintf("%v", v)
}
// toTime converts a string value into a time.Time if possible, otherwise
// returns a zero time.
func toTime(v interface{}) time.Time {
vStr := toString(v)
if t, err := time.Parse(time.RFC3339, vStr); err == nil {
return t
}
if t, err := time.Parse("2006-01-02T15:04:05", vStr); err == nil {
return t
}
if t, err := time.Parse("2006-01-02", vStr); err == nil {
return t
}
return time.Time{}
}
func isSlice(v interface{}) bool {
if _, ok := v.([]interface{}); ok {
return true
}
return false
}
func toBool(v interface{}) bool {
switch n := v.(type) {
case bool:
return n
case int:
return n > 0
case int8:
return n > 0
case int16:
return n > 0
case int32:
return n > 0
case int64:
return n > 0
case uint:
return n > 0
case uint8:
return n > 0
case uint16:
return n > 0
case uint32:
return n > 0
case uint64:
return n > 0
case float32:
return n > 0
case float64:
return n > 0
case string:
return len(n) > 0
case []byte:
return len(n) > 0
case []interface{}:
return len(n) > 0
case map[string]interface{}:
return len(n) > 0
case map[any]any:
return len(n) > 0
}
return false
}
// normalize an input for equality checks. All numbers -> float64, []byte to
// string, etc. Since `rune` is an alias for int32, we can't differentiate it
// for comparison with strings.
func normalize(v interface{}) interface{} {
switch n := v.(type) {
case int:
return float64(n)
case int8:
return float64(n)
case int16:
return float64(n)
case int32:
return float64(n)
case int64:
return float64(n)
case uint:
return float64(n)
case uint8:
return float64(n)
case uint16:
return float64(n)
case uint32:
return float64(n)
case uint64:
return float64(n)
case float32:
return float64(n)
case []byte:
return string(n)
}
return v
}
// deepEqual returns whether two values are deeply equal.
func deepEqual(left, right any) bool {
l := normalize(left)
r := normalize(right)
// Optimization for simple types to prevent allocations
switch l.(type) {
case float64:
if f, ok := r.(float64); ok {
return l == f
}
case string:
if s, ok := r.(string); ok {
return l == s
}
}
// Otherwise, just use the built-in deep equality check.
return reflect.DeepEqual(left, right)
}