-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
57 lines (41 loc) · 1.4 KB
/
options.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
package xload
const defaultKey = "env"
// Option configures the xload behaviour.
type Option interface{ apply(*options) }
// FieldTagName allows customising the struct tag name to use.
type FieldTagName string
func (k FieldTagName) apply(opts *options) { opts.tagName = string(k) }
// Concurrency allows customising the number of goroutines to use.
// Default is 1.
type Concurrency int
func (c Concurrency) apply(opts *options) { opts.concurrency = int(c) }
// WithLoader allows customising the loader to use.
func WithLoader(loader Loader) Option {
return optionFunc(func(opts *options) { opts.loader = loader })
}
// SkipCollisionDetection disables detecting any key collisions while trying to load full keys.
var SkipCollisionDetection = &applier{f: func(o *options) { o.detectCollisions = false }}
// optionFunc allows using a function as an Option.
type optionFunc func(*options)
func (f optionFunc) apply(opts *options) { f(opts) }
type applier struct{ f func(*options) }
func (a *applier) apply(opts *options) { a.f(opts) }
// options holds the configuration.
type options struct {
tagName string
loader Loader
concurrency int
detectCollisions bool
}
func newOptions(opts ...Option) *options {
o := &options{
tagName: defaultKey,
loader: OSLoader(),
concurrency: 1,
detectCollisions: true,
}
for _, opt := range opts {
opt.apply(o)
}
return o
}