/
types.go
206 lines (153 loc) · 4.25 KB
/
types.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
package config
import "fmt"
type valueType int
const (
boolType valueType = iota + 1
floatType
intType
interfaceType
listType
stringType
uintType
)
// Bool returns a bool from the config file.
func Bool(key string, def ...interface{}) (bool, error) {
v, err := config.value(key, boolType)
if err != nil {
value := defaultValue(false, def...).(bool)
if err == ErrNoValueFound && len(def) > 0 {
return value, nil
}
return value, err
}
return defaultValue(v, def...).(bool), nil
}
// MustBool returns a bool from the config file,
// it will panic if a error is created.
func MustBool(key string, def ...interface{}) bool {
v, err := Bool(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}
// Float returns a float64 from the config file.
func Float(key string, def ...interface{}) (float64, error) {
v, err := config.value(key, floatType)
if err != nil {
if err == ErrNoValueFound && len(def) > 0 {
return defaultValue(0.0, def...).(float64), nil
}
return 0.0, err
}
return defaultValue(v, def...).(float64), nil
}
// MustFloat returns a float64 from the config file,
// it will panic if a error is created.
func MustFloat(key string, def ...interface{}) float64 {
v, err := Float(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}
// Get returns the value for the given key from the config file as a interface.
func Get(key string, def ...interface{}) (interface{}, error) {
v, err := config.value(key, interfaceType)
if err != nil {
if err == ErrNoValueFound && len(def) > 0 {
return defaultValue(nil, def...), nil
}
return nil, err
}
return v, nil
}
// MustGet returns a interface from the config file,
// it will panic if a error is created.
func MustGet(key string, def ...interface{}) interface{} {
v, err := Get(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}
// Int returns a int64 from the config file.
func Int(key string, def ...interface{}) (int64, error) {
v, err := config.value(key, intType)
if err != nil {
if err == ErrNoValueFound && len(def) > 0 {
return defaultValue(0, def...).(int64), nil
}
return 0, err
}
return defaultValue(v, def...).(int64), nil
}
// MustInt returns a int64 from the config file,
// it will panic if a error is created.
func MustInt(key string, def ...interface{}) int64 {
v, err := Int(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}
// List returns a slice of strings from the config file.
func List(key string, def ...interface{}) ([]string, error) {
v, err := config.value(key, listType)
if err != nil {
if err == ErrNoValueFound && len(def) > 0 {
return defaultValue([]string{}, def...).([]string), nil
}
return []string{}, err
}
return defaultValue(v, def...).([]string), nil
}
// MustList returns a slice of strings from the config file,
// it will panic if a error is created.
func MustList(key string, def ...interface{}) []string {
v, err := List(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}
// String returns a string from the config file.
func String(key string, def ...interface{}) (string, error) {
v, err := config.value(key, stringType)
if err != nil {
if err == ErrNoValueFound && len(def) > 0 {
return defaultValue("", def...).(string), nil
}
return "", err
}
return defaultValue(v, def...).(string), nil
}
// MustString returns a string from the config file,
// it will panic if a error is created.
func MustString(key string, def ...interface{}) string {
v, err := String(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}
// Uint returns a unsigned int64 from the config file.
func Uint(key string, def ...interface{}) (uint64, error) {
v, err := config.value(key, uintType)
if err != nil {
if err == ErrNoValueFound && len(def) > 0 {
return defaultValue(uint64(0), def...).(uint64), nil
}
return uint64(0), err
}
return defaultValue(v, def...).(uint64), nil
}
// MustUint returns a unsigned int64 from the config file,
// it will panic if a error is created.
func MustUint(key string, def ...interface{}) uint64 {
v, err := Uint(key, def...)
if err != nil {
panic(fmt.Errorf("%s: %s", err, key))
}
return v
}