-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathzconfig.go
54 lines (44 loc) · 1.37 KB
/
zconfig.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
package zconfig
import (
"context"
)
var (
DefaultRepository Repository
DefaultProcessor Processor
Args = NewArgsProvider()
Env = NewEnvProvider()
)
func init() {
DefaultRepository.AddProviders(Args, Env)
DefaultRepository.AddParsers(ParseString)
DefaultProcessor.AddHooks(DefaultRepository.Hook, Initialize)
}
// Configure a service using the default processor.
func Configure(ctx context.Context, s interface{}) error {
return DefaultProcessor.Process(ctx, s)
}
// A Hook can be used to act upon every field visited by the repository when
// configuring a service.
type Hook func(ctx context.Context, field *Field) error
// Add a hook to the default repository.
func AddHooks(hooks ...Hook) {
DefaultProcessor.AddHooks(hooks...)
}
// Provider is the interface implemented by all entity a configuration key can
// be retrieved from.
type Provider interface {
Retrieve(key string) (value interface{}, found bool, err error)
Name() string
Priority() int
}
// Add a provider to the default repository.
func AddProviders(providers ...Provider) {
DefaultRepository.AddProviders(providers...)
}
// Parser is the type of function that can convert a raw representation to a
// given type.
type Parser func(interface{}, interface{}) error
// Add a parser to the default repository.
func AddParsers(parsers ...Parser) {
DefaultRepository.AddParsers(parsers...)
}