-
Notifications
You must be signed in to change notification settings - Fork 1
/
defaults.go
69 lines (59 loc) · 1.44 KB
/
defaults.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
package typenv
import (
"reflect"
"time"
)
var (
booleans map[string]bool
durations map[string]time.Duration
floats map[string]float64
integers map[string]int64
strings map[string]string
)
func init() {
booleans = map[string]bool{}
durations = map[string]time.Duration{}
floats = map[string]float64{}
integers = map[string]int64{}
strings = map[string]string{}
}
// SetGlobalDefault register a environment variable global defaul value
func SetGlobalDefault(l ...e) {
for _, i := range l {
switch reflect.TypeOf(i.fn) {
case reflect.TypeOf(Bool):
booleans[i.name] = i.val.(bool)
case reflect.TypeOf(Duration):
durations[i.name] = i.val.(time.Duration)
case reflect.TypeOf(Float64), reflect.TypeOf(Float32):
switch val := i.val.(type) {
case float64:
floats[i.name] = val
case float32:
floats[i.name] = float64(val)
}
case reflect.TypeOf(Int64), reflect.TypeOf(Int32), reflect.TypeOf(Int8), reflect.TypeOf(Int):
switch val := i.val.(type) {
case int64:
integers[i.name] = val
case int32:
integers[i.name] = int64(val)
case int8:
integers[i.name] = int64(val)
case int:
integers[i.name] = int64(val)
}
case reflect.TypeOf(String):
strings[i.name] = i.val.(string)
}
}
}
type e struct {
fn interface{}
name string
val interface{}
}
// E returns a instance of `e` struct
func E(fn interface{}, name string, val interface{}) e {
return e{fn: fn, name: name, val: val}
}