-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.go
205 lines (166 loc) · 4.66 KB
/
configuration.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
package structconf
import (
"reflect"
"sync"
"github.com/anexia-it/go-structconf/encoding"
"github.com/anexia-it/go-structconf/storage"
"github.com/hashicorp/go-multierror"
"gopkg.in/anexia-it/go-structmapper.v1"
)
// Default configuration options
var defaultOptions = []Option{
OptionTagName("config"),
}
// Configuration represents a configuration backed by a struct
type Configuration struct {
tagName string
config interface{}
pendingDefaults interface{}
configType reflect.Type
storage storage.Storage
encoding encoding.Encoding
mapper *structmapper.Mapper
}
func (c *Configuration) mergeAndSet(a, b map[string]interface{}) error {
// Merge both maps...
mergedMap, err := MergeMaps(a, b)
if err != nil {
return err
}
// If the configuration implements the sync.Locker interface, use it.
// This allows configuration structs to ensure no race-conditions are created
// by a write during a read.
locker, ok := c.config.(sync.Locker)
if ok {
locker.Lock()
// It is safe to defer this call inside the if block here, as it will only be executed
// when leaving this function, not the if block.
defer locker.Unlock()
}
// Apply the merged result to the config
err = c.mapper.ToStruct(mergedMap, c.config)
return err
}
// SetDefaults sets the defaults value for the configuration
func (c *Configuration) SetDefaults(defaults interface{}) error {
// Defaults must be set to a non-nil value
if defaults == nil {
return ErrConfigStructIsNil
}
defaultsValue := reflect.ValueOf(defaults)
// Traverse the pointer, if defaults is a pointer
if defaultsValue.Kind() == reflect.Ptr {
defaultsValue = defaultsValue.Elem()
}
if defaultsValue.Type() != c.configType {
return ErrConfigStructTypeMismatch
}
// Convert both the current config, as well as the defaults to a map[string]interface{}
defaultsMap, err := c.mapper.ToMap(defaults)
if err != nil {
return err
}
configMap, err := c.mapper.ToMap(c.config)
if err != nil {
return err
}
return c.mergeAndSet(defaultsMap, configMap)
}
// Load loads the configuration from the underlying storage
func (c *Configuration) Load() error {
// Check if encoding and storage were configured
if c.encoding == nil {
return ErrEncodingNotConfigured
} else if c.storage == nil {
return ErrStorageNotConfigured
}
buf, err := c.storage.ReadConfig()
if err != nil {
// Storage reported error
return err
}
// Decode onto map[string]interface{}
loadedMap := make(map[string]interface{})
if err := c.encoding.UnmarshalTo(buf, loadedMap); err != nil {
// Encoding error
return err
}
// Create a map from the current configuration
currentMap, mapErr := c.mapper.ToMap(c.config)
if mapErr != nil {
return mapErr
}
return c.mergeAndSet(currentMap, loadedMap)
}
// Save writes the configuration to the underlying storage
func (c *Configuration) Save() error {
// Check if encoding and storage were configured
if c.encoding == nil {
return ErrEncodingNotConfigured
} else if c.storage == nil {
return ErrStorageNotConfigured
}
// Convert the configuration to a map
configData, err := c.mapper.ToMap(c.config)
if err != nil {
return err
}
// Encode the configuration using the encoding
encoded, err := c.encoding.MarshalFrom(configData)
if err != nil {
return err
}
// Write the configuration to the storage
if err := c.storage.WriteConfig(encoded); err != nil {
return err
}
return nil
}
// NewConfiguration initializes a new configuration with the given options
func NewConfiguration(config interface{}, options ...Option) (*Configuration, error) {
if config == nil {
return nil, ErrConfigStructIsNil
}
configValue := reflect.ValueOf(config)
if configValue.Kind() != reflect.Ptr {
return nil, ErrNotAStructPointer
} else if configValue.Elem().Kind() != reflect.Struct {
return nil, ErrNotAStructPointer
}
c := &Configuration{
config: config,
configType: configValue.Elem().Type(),
}
var err error
// Apply default options
for _, opt := range defaultOptions {
if optErr := opt(c); optErr != nil {
panic(optErr)
}
}
// Apply options
for _, opt := range options {
if optErr := opt(c); optErr != nil {
err = multierror.Append(err, optErr)
}
}
// If setting any option caused an error, return it
if err != nil {
return nil, err
}
// Configure the mapper...
mapper, err := structmapper.NewMapper(structmapper.OptionTagName(c.tagName))
if err != nil {
return nil, err
}
c.mapper = mapper
if c.pendingDefaults != nil {
// OptionDefaults was used, apply defaults now...
if err := c.SetDefaults(c.pendingDefaults); err != nil {
return nil, err
}
// Clear pendingDefaults again
c.pendingDefaults = nil
}
return c, nil
}