-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeconv.go
282 lines (259 loc) · 8.18 KB
/
typeconv.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package getconf
import (
"fmt"
"reflect"
"strconv"
"time"
"github.com/spf13/cast"
)
// GetAll returns a map[string]interface{} with all options that have
// a valid value. nil values are ignored
func GetAll() map[string]interface{} { return g2.GetAll() }
func (gc *GetConf) GetAll() map[string]interface{} {
opts := make(map[string]interface{})
for _, x := range g2.options {
if x.value == nil {
continue
}
opts[x.name] = x.value
}
return opts
}
// Get return the value associated to the key even if it is a
// default value or nil
func Get(key string) interface{} { return g2.Get(key) }
func (gc *GetConf) Get(key string) interface{} {
if o, ok := gc.options[key]; ok != false {
return o.value
}
return nil
}
// GetString returns the value associated with the key as a string
// casting it when necessary
func GetString(key string) string { return g2.GetString(key) }
func (gc *GetConf) GetString(key string) string {
return cast.ToString(gc.Get(key))
}
// GetInt returns the value associated with the key as an int
// casting it when necessary
func GetInt(key string) int { return g2.GetInt(key) }
func (gc *GetConf) GetInt(key string) int {
return cast.ToInt(gc.Get(key))
}
// GetInt8 returns the value associated with the key as an int8
// casting it when necessary
func GetInt8(key string) int8 { return g2.GetInt8(key) }
func (gc *GetConf) GetInt8(key string) int8 {
return cast.ToInt8(gc.Get(key))
}
// GetInt16 returns the value associated with the key as an int16
// casting it when necessary
func GetInt16(key string) int16 { return g2.GetInt16(key) }
func (gc *GetConf) GetInt16(key string) int16 {
return cast.ToInt16(gc.Get(key))
}
// GetInt32 returns the value associated with the key as an int32
// casting it when necessary
func GetInt32(key string) int32 { return g2.GetInt32(key) }
func (gc *GetConf) GetInt32(key string) int32 {
return cast.ToInt32(gc.Get(key))
}
// GetInt64 returns the value associated with the key as an int64
// casting it when necessary
func GetInt64(key string) int64 { return g2.GetInt64(key) }
func (gc *GetConf) GetInt64(key string) int64 {
return cast.ToInt64(gc.Get(key))
}
// GetUint returns the value associated with the key as a uint
// casting it when necessary
func GetUint(key string) uint { return g2.GetUInt(key) }
func (gc *GetConf) GetUInt(key string) uint {
return cast.ToUint(gc.Get(key))
}
// GetUint8 returns the value associated with the key as a uint8
// casting it when necessary
func GetUint8(key string) uint8 { return g2.GetUint8(key) }
func (gc *GetConf) GetUint8(key string) uint8 {
return cast.ToUint8(gc.Get(key))
}
// GetUint16 returns the value associated with the key as a uint16
// casting it when necessary
func GetUint16(key string) uint16 { return g2.GetUint16(key) }
func (gc *GetConf) GetUint16(key string) uint16 {
return cast.ToUint16(gc.Get(key))
}
// GetUint32 returns the value associated with the key as a uint32
// casting it when necessary
func GetUint32(key string) uint32 { return g2.GetUint32(key) }
func (gc *GetConf) GetUint32(key string) uint32 {
return cast.ToUint32(gc.Get(key))
}
// GetUint64 returns the value associated with the key as a uint64
// casting it when necessary
func GetUint64(key string) uint64 { return g2.GetUint64(key) }
func (gc *GetConf) GetUint64(key string) uint64 {
return cast.ToUint64(gc.Get(key))
}
// GetFloat32 returns the value associated with the key as a float32
// casting it when necessary
func GetFloat32(key string) float32 { return g2.GetFloat32(key) }
func (gc *GetConf) GetFloat32(key string) float32 {
return cast.ToFloat32(gc.Get(key))
}
// GetFloat64 returns the value associated with the key as a float64
// casting it when necessary
func GetFloat64(key string) float64 { return g2.GetFloat64(key) }
func (gc *GetConf) GetFloat64(key string) float64 {
return cast.ToFloat64(gc.Get(key))
}
// GetBool returns the value associated with the key as a bool
// casting it when necessary
func GetBool(key string) bool { return g2.GetBool(key) }
func (gc *GetConf) GetBool(key string) bool {
return cast.ToBool(gc.Get(key))
}
// GetTime returns the value associated with the key as a time
// structure, casting it when necessary
func GetTime(key string) time.Time { return g2.GetTime(key) }
func (gc *GetConf) GetTime(key string) time.Time {
return cast.ToTime(gc.Get(key))
}
// func GetDuration(key string) time.Duration { return g2.GetDuration(key) }
// func (gc *GetConf) GetDuration(key string) time.Duration {
// return cast.ToDuration(gc.Get(key))
// }
// getTypedValue takes as params the option value and it type and will do
// the conversion to the required typed value.
//
// The value is passed as a string because this function is intended to be
// used when parsing flags or environment variables that are of string type.
//
// The type t is determined when the config struct is parsed when Load() is called.
func getTypedValue(opt string, t reflect.Kind) interface{} {
switch t {
case reflect.Int:
if value, err := strconv.ParseInt(opt, 10, 0); err == nil {
return int(value)
}
return 0
case reflect.Int8:
if value, err := strconv.ParseInt(opt, 10, 8); err == nil {
return int8(value)
}
return 0
case reflect.Int16:
if value, err := strconv.ParseInt(opt, 10, 16); err == nil {
return int16(value)
}
return 0
case reflect.Int32:
if value, err := strconv.ParseInt(opt, 10, 32); err == nil {
return int32(value)
}
return 0
case reflect.Int64:
if value, err := strconv.ParseInt(opt, 10, 64); err == nil {
return int64(value)
}
return 0
case reflect.Uint:
if value, err := strconv.ParseUint(opt, 10, 0); err == nil {
return uint(value)
}
return 0
case reflect.Uint8:
if value, err := strconv.ParseUint(opt, 10, 8); err == nil {
return uint8(value)
}
return 0
case reflect.Uint16:
if value, err := strconv.ParseUint(opt, 10, 16); err == nil {
return uint16(value)
}
return 0
case reflect.Uint32:
if value, err := strconv.ParseUint(opt, 10, 32); err == nil {
return uint32(value)
}
return 0
case reflect.Uint64:
if value, err := strconv.ParseUint(opt, 10, 64); err == nil {
return uint64(value)
}
return 0
case reflect.Float32:
if value, err := strconv.ParseFloat(opt, 32); err == nil {
return float32(value)
}
return 0
case reflect.Float64:
if value, err := strconv.ParseFloat(opt, 64); err == nil {
return value
}
return 0
case reflect.Bool:
if value, err := strconv.ParseBool(string(opt)); err == nil {
return value
}
return false
case reflect.String:
return string(opt)
case reflect.Struct:
if t, err := StringToDate(opt); err == nil {
return t
}
if sec, err := strconv.ParseInt(opt, 10, 64); err == nil {
return time.Unix(sec, 0)
}
return time.Time{}
}
return nil
}
// From https://github.com/spf13/cast/blob/master/caste.go
// Copyright © 2014 Steve Francia <spf@spf13.com>.
// StringToDate attempts to parse a string into a time.Time type using a
// predefined list of formats. If no suitable format is found, an error is
// returned.
func StringToDate(s string) (time.Time, error) {
return parseDateWith(s, []string{
time.RFC3339,
time.RFC3339Nano,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
time.RFC822,
time.RFC850,
time.ANSIC,
time.UnixDate,
time.RubyDate,
"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
"2006-01-02",
"02 Jan 2006",
"2006-01-02T15:04:05-0700", // RFC3339 without timezone hh:mm colon
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05Z07:00", // RFC3339 without T
"2006-01-02 15:04:05Z0700", // RFC3339 without T or timezone hh:mm colon
"2006-01-02 15:04:05",
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
})
}
// From https://github.com/spf13/cast/blob/master/caste.go
// Copyright © 2014 Steve Francia <spf@spf13.com>.
// parseDateWith takes the string s representing the date and a list of supported
// date formats.
//
// When a format matches s, it is used and the time parsed and returned.
func parseDateWith(s string, dates []string) (d time.Time, e error) {
for _, dateType := range dates {
if d, e = time.Parse(dateType, s); e == nil {
return
}
}
return d, fmt.Errorf("unable to parse date: %s", s)
}