-
Notifications
You must be signed in to change notification settings - Fork 1
/
bind_container.go
139 lines (136 loc) · 2.71 KB
/
bind_container.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
package mermaid
import (
"fmt"
"github.com/spf13/viper"
"go.uber.org/dig"
"net"
"time"
)
// BindContainer provide viper's flagset to container.
func BindContainer(cfg *viper.Viper, container *dig.Container) error {
for k, v := range cfg.AllSettings() {
key := k
val := v
// Skip help.
if key == "help" {
continue
}
var getter interface{}
switch value := val.(type) {
case bool:
getter = func() bool {
return cfg.GetBool(key)
}
case []bool:
getter = func() []bool {
return value
}
case []byte:
getter = func() []byte {
return value
}
case time.Duration:
getter = func() time.Duration {
return cfg.GetDuration(key)
}
case []time.Duration:
getter = func() []time.Duration {
return value
}
// viper will turn float32 to float64 when Get value.
// See https://godoc.org/github.com/spf13/viper#Viper.Get
case float32, float64:
getter = func() float64 {
return cfg.GetFloat64(key)
}
case []float32:
getter = func() []float32 {
return value
}
case []float64:
getter = func() []float64 {
return value
}
case uint:
getter = func() uint {
return cfg.GetUint(key)
}
case uint8:
getter = func() uint8 {
return value
}
case uint16:
getter = func() uint16 {
return value
}
case uint32:
getter = func() uint32 {
return cfg.GetUint32(key)
}
case uint64:
getter = func() uint64 {
return cfg.GetUint64(key)
}
case int: // count, int8, int16, int32
getter = func() int {
return cfg.GetInt(key)
}
case int64:
getter = func() int64 {
return cfg.GetInt64(key)
}
case []int:
getter = func() []int {
return value
}
case []int32:
getter = func() []int32 {
return value
}
case []int64:
getter = func() []int64 {
return value
}
case net.IP:
getter = func() net.IP {
return value
}
case []net.IP:
getter = func() []net.IP {
return value
}
case net.IPMask:
getter = func() net.IPMask {
return value
}
case net.IPNet:
getter = func() net.IPNet {
return value
}
case string:
getter = func() string {
return cfg.GetString(key)
}
case []string: // stringSlice, stringArray
getter = func() []string {
return cfg.GetStringSlice(key)
}
case map[string]int: // string_to_int
getter = func() map[string]int {
return value
}
case map[string]int64: // string_to_int64
getter = func() map[string]int64 {
return value
}
case map[string]string: // string_to_sting
getter = func() map[string]string {
return value
}
}
if err := container.Provide(getter, dig.Name(key)); err != nil {
return fmt.Errorf("Get key %s err: %s", key, err)
}
}
return nil
}