-
Notifications
You must be signed in to change notification settings - Fork 0
/
revip.go
160 lines (134 loc) · 3.96 KB
/
revip.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
package revip
import (
"reflect"
"github.com/mitchellh/mapstructure"
)
const (
SchemeEmpty = ""
SchemeFile = "file" // file://./config.yml
SchemeEnviron = "env" // env://prefix
)
var (
// FromSchemes represents schemes supported for sources.
FromSchemes = []string{
SchemeFile,
SchemeEnviron,
}
// ToSchemes represents schemes supported for destrinations.
ToSchemes = []string{
SchemeFile,
}
)
// Config is a configuration represented by user-specified type.
type Config interface{}
// Defaultable is an interface which any `Config` could implement
// to define a custom default values for sub-tree it owns.
type Defaultable interface {
Default()
}
// Validatable is an interface which any `Config` could implement
// to define a validation rules for sub-tree it owns.
type Validatable interface {
Validate() error
}
// Expandable is an interface which any `Config` could implement
// to define an expansion rules for sub-tree it owns.
type Expandable interface {
Expand() error
}
// Container represents configuration loaded by `Load`.
type Container struct {
// config represents configuration data, it should always be a pointer.
config Config
index map[string]Tree
}
// Unwrap returns a pointer to the inner configuration data structure.
func (r *Container) Unwrap() Config { return r.config }
// EmptyClone returns empty configuration type clone.
func (r *Container) EmptyClone() Config {
t := indirectType(reflect.TypeOf(r.config))
return reflect.New(t).Interface()
}
// Empty allocates a new empty configuration, discarding any previously loaded data.
func (r *Container) Empty() {
cfg := r.EmptyClone()
r.config = cfg
}
// Replace overrides internally stored configuration with passed value.
func (r *Container) Replace(c Config) {
r.config = c
}
// Copy writes a shallow copy of the configuration into `v`.
func (r *Container) Copy(v Config) error {
return mapstructure.WeakDecode(r.config, v)
}
// Clone returns a shallow copy of the configuration with the same type.
func (r *Container) Clone() (Config, error) {
v := r.EmptyClone()
err := r.Copy(v)
if err != nil {
return nil, err
}
return v, nil
}
// DeepCopy writes a deep copy of the configuration into `v`.
func (r *Container) DeepCopy(v Config) error {
return mapstructure.Decode(r.config, v)
}
// DeepClone returns a deep copy of the configuration with the same type.
func (r *Container) DeepClone() (Config, error) {
v := r.EmptyClone()
err := r.DeepCopy(v)
if err != nil {
return nil, err
}
return v, nil
}
// Path uses dot notation to retrieve substruct addressable by `path` or
// return an error if key was not found(`ErrNotFound`) or
// something gone terribly wrong.
func (r *Container) Path(path string) (Config, error) {
if r.index == nil {
r.index = map[string]Tree{}
_, _ = NewTree(reflect.ValueOf(r.config), func(t Tree) error {
r.index[TreePathString(t)] = t
return nil
})
}
v, ok := r.index[path]
if !ok {
return nil, &ErrPathNotFound{Path: path}
}
return v, nil
}
// Default postprocess configuration with default values or returns an error.
func (r *Container) Default() error {
return Postprocess(r.config, WithDefaults())
}
// Validate postprocess configuration with validation or returns an error.
func (r *Container) Validate() error {
return Postprocess(r.config, WithValidation())
}
// Expand postprocess configuration with expansion or returns an error.
func (r *Container) Expand() error {
return Postprocess(r.config, WithExpansion())
}
// New wraps configuration represented by `c` with come useful methods.
func New(c Config) *Container {
if reflect.TypeOf(c).Kind() != reflect.Ptr {
panic("config must be a pointer")
}
return &Container{config: c}
}
// Load applies each `options` in order to fill the configuration in `v` and
// constructs a `*Revip` data-structure.
func Load(v Config, options ...SourceOption) (*Container, error) {
var err error
for _, f := range options {
err = f(v)
if err != nil {
return nil, err
}
}
return New(v), nil
}