-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
263 lines (183 loc) · 8.33 KB
/
doc.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
/*
package cfg loads configuration files into Go structs with extra juice for validating fields and setting defaults.
Config files may be defined in yaml, json or toml format.
When you call `Load()`, cfg takes the following steps:
1. Fills config struct from the config file (if enabled)
2. Fills config struct from the environment (if enabled)
3. Sets defaults (where applicable)
4. Validates required fields (where applicable)
Example
Define your configuration file in the root of your project:
# config.yaml
build: "2020-01-09T12:30:00Z"
server:
ports:
- 8080
cleanup: 1h
logger:
level: "warn"
trace: true
Define your struct and load it:
package main
import (
"fmt"
"github.com/notnull-co/cfg"
)
type Config struct {
Build time.Time `cfg:"build" validate:"required"`
Server struct {
Host string `cfg:"host" default:"127.0.0.1"`
Ports []int `cfg:"ports" default:"[80,443]"`
Cleanup time.Duration `cfg:"cleanup" default:"30m"`
}
Logger struct {
Level string `cfg:"level" default:"info"`
Trace bool `cfg:"trace"`
}
}
func main() {
var cfg Config
_ = cfg.Load(&cfg)
fmt.Printf("%+v\n", cfg)
// Output: {Build:2019-12-25 00:00:00 +0000 UTC Server:{Host:127.0.0.1 Ports:[8080] Cleanup:1h0m0s} Logger:{Level:warn Trace:true}}
}
By default cfg searches for a file named `config.yaml` in the directory it is run from.
It can be configured to look elsewhere.
Configuration
Pass options as additional parameters to `Load()` to configure cfg's behaviour.
IgnoreFile
Change the file and directories cfg searches in with `File()`.
cfg.Load(&cfg,
cfg.File("settings.json"),
cfg.Dirs(".", "home/user/myapp", "/opt/myapp"),
)
Cfg searches for the file in dirs sequentially and uses the first matching file.
The decoder (yaml/json/toml) used is picked based on the file's extension.
Tag
The struct tag key tag cfg looks for to find the field's alt name can be changed using `Tag()`.
type Config struct {
Host string `yaml:"host" validate:"required"`
Level string `yaml:"level" default:"info"`
}
var cfg Config
cfg.Load(&cfg, cfg.Tag("yaml"))
By default cfg uses the tag key `cfg`.
Environment
Cfg can be configured to additionally set fields using the environment.
This behaviour can be enabled using the option `UseEnv(prefix)`. If loading from file is also enabled then first the struct is loaded
from a config file and thus any values found in the environment will overwrite existing values in the struct.
Prefix is a string that will be prepended to the keys that are searched in the environment. Although discouraged, prefix may be left empty.
Cfg searches for keys in the form PREFIX_FIELD_PATH, or if prefix is left empty then FIELD_PATH.
A field's path is formed by prepending its name with the names of all the surrounding structs up to the root struct, upper-cased and separated by an underscore.
If a field has an alt name defined in its struct tag then that name is preferred over its struct name.
type Config struct {
Build time.Time
LogLevel string `cfg:"log_level"`
Server struct {
Host string
}
}
With the struct above and `UseEnv("myapp")` cfg would search for the following
environment variables:
MYAPP_BUILD
MYAPP_LOG_LEVEL
MYAPP_SERVER_HOST
Fields contained in struct slices whose elements already exists can be also be set via the environment in the form PARENT_IDX_FIELD, where idx is the index of the field in the slice.
type Config struct {
Server []struct {
Host string
}
}
With the config above individual servers may be configured with the following environment variable:
MYAPP_SERVER_0_HOST
MYAPP_SERVER_1_HOST
...
Note: the Server slice must already have members inside it (i.e. from loading of the configuration file) for the containing fields to be altered via the environment. cfg will not instantiate and insert elements into the slice.
Time
Change the layout cfg uses to parse times using `TimeLayout()`.
type Config struct {
Date time.Time `cfg:"date" default:"12-25-2019"`
}
var cfg Config
cfg.Load(&cfg, cfg.TimeLayout("01-02-2006"))
fmt.Printf("%+v", cfg)
// Output: {Date:2019-12-25 00:00:00 +0000 UTC}
By default cfg parses time using the `RFC.3339` layout (`2006-01-02T15:04:05Z07:00`).
# Strict Parsing
By default cfg ignores any fields in the config file that are not present in the struct. This behaviour can be changed using `UseStrict()` to achieve strict parsing.
When strict parsing is enabled, extra fields in the config file will cause an error.
Required
A validate key with a required value in the field's struct tag makes cfg check if the field has been set after it's been loaded. Required fields that are not set are returned as an error.
type Config struct {
Host string `cfg:"host" validate:"required"` // or simply `validate:"required"`
}
Cfg uses the following properties to check if a field is set:
basic types: != to its zero value ("" for str, 0 for int, etc.)
slices, arrays: len() > 0
pointers*, interfaces: != nil
structs: always true (use a struct pointer to check for struct presence)
time.Time: !time.IsZero()
time.Duration: != 0
*pointers to non-struct types (with the exception of time.Time) are de-referenced if they are non-nil and then checked
See example below to help understand:
type Config struct {
A string `validate:"required"`
B *string `validate:"required"`
C int `validate:"required"`
D *int `validate:"required"`
E []float32 `validate:"required"`
F struct{} `validate:"required"`
G *struct{} `validate:"required"`
H struct {
I interface{} `validate:"required"`
J interface{} `validate:"required"`
} `validate:"required"`
K *[]bool `validate:"required"`
L []uint `validate:"required"`
M *time.Time `validate:"required"`
N *regexp.Regexp `validate:"required"`
}
var cfg Config
// simulate loading of config file
b := ""
cfg.B = &b
cfg.H.I = 5.5
cfg.K = &[]bool{}
cfg.L = []uint{5}
m := time.Time{}
cfg.M = &m
err := cfg.Load(&cfg)
fmt.Print(err)
// A: required validation failed, B: required validation failed, C: required validation failed, D: required validation failed, E: required validation failed, G: required validation failed, H.J: required validation failed, K: required validation failed, M: required validation failed, N: required validation failed
Default
A default key in the field tag makes cfg fill the field with the value specified when the field is not otherwise set.
Cfg attempts to parse the value based on the field's type. If parsing fails then an error is returned.
type Config struct {
Port int `cfg:"port" default:"8000"` // or simply `default:"8000"`
}
A default value can be set for the following types:
all basic types except bool and complex
time.Time
time.Duration
*regexp.Regexp
slices (of above types)
Successive elements of slice defaults should be separated by a comma. The entire slice can optionally be enclosed in square brackets:
type Config struct {
Durations []time.Duration `default:"[30m,1h,90m,2h]"` // or `default:"30m,1h,90m,2h"`
}
Note: the default setter knows if it should fill a field or not by comparing if the current value of the field is equal to the corresponding zero value for that field's type. This happens after the configuration is loaded and has the implication that the zero value set explicitly by the user will get overwritten by any default value registered for that field. It's for this reason that defaults on booleans are not permitted, as a boolean field with a default value of `true` would always be true (since if it were set to false it'd be overwritten).
Mutual exclusion
The required validation and the default field tags are mutually exclusive as they are contradictory.
This is not allowed:
type Config struct {
Level string `validate:"required" default:"warn"` // will result in an error
}
Errors
A wrapped error `ErrFileNotFound` is returned when cfg is not able to find a config file to load. This can be useful for instance to fallback to a different configuration loading mechanism.
var cfg Config
err := cfg.Load(&cfg)
if errors.Is(err, cfg.ErrFileNotFound) {
// load config from elsewhere
}
*/
package cfg