-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvenom.go
More file actions
209 lines (179 loc) · 6.12 KB
/
Copy pathvenom.go
File metadata and controls
209 lines (179 loc) · 6.12 KB
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
package venom
// Default the default set of available config levels
const (
DefaultLevel ConfigLevel = iota
FileLevel
EnvironmentLevel
FlagLevel
OverrideLevel ConfigLevel = 99
)
const defaultDelim = "."
// Delim is the delimiter used for separating nested key spaces. The default is
// to separate on "." characters.
var Delim = defaultDelim
// A KeyTranslator is used for translating portions of config keys in a
// level-agnostic manner.
//
// Functions like these may be useful for performing operations such as
// normalizing hyphens ('-') to underscores ('_') when performing environment
// variable lookups, or perhaps the inverse when performing command line flag
// lookups.
type KeyTranslator func(b byte) byte
// The NoOpKeyTranslator is a KeyTranslator which returns all input bytes
// unmodified.
func NoOpKeyTranslator(b byte) byte {
return b
}
// ConfigLevel is a type alias used to identify various configuration levels
type ConfigLevel int
// ConfigMap defines the inner map type which holds actual config data. These
// are nested under a ConfigLevel which determines their priority
type ConfigMap map[string]interface{}
func (c ConfigMap) merge(d ConfigMap) ConfigMap {
for key, val := range d {
switch actual := val.(type) {
case map[string]interface{}:
var existing ConfigMap
if _, ok := c[key]; !ok {
existing = make(ConfigMap)
} else {
existing = c[key].(ConfigMap)
}
c[key] = existing.merge(actual)
case map[interface{}]interface{}:
var existing ConfigMap
if _, ok := c[key]; !ok {
existing = make(ConfigMap)
} else {
existing = c[key].(ConfigMap)
}
c[key] = existing.merge(mapInterfaceInterfaceToStrInterface(actual))
default:
c[key] = val
}
}
return c
}
func mapInterfaceInterfaceToStrInterface(src map[interface{}]interface{}) map[string]interface{} {
data := make(map[string]interface{})
for key, value := range src {
if actualKey, ok := key.(string); ok {
data[actualKey] = value
}
}
return data
}
// ConfigLevelMap is a mapping of config levels to the maps which contain
// various configuration values at those levels
type ConfigLevelMap map[ConfigLevel]ConfigMap
// Venom is the configuration registry responsible for storing and managing
// arbitrary configuration keys and values.
type Venom struct {
Store ConfigStore
}
// New returns a newly initialized Venom instance.
//
// The internal config map is created empty, only allocating space for a given
// config level once a value is set to that level.
func New() *Venom {
return NewWithStore(NewDefaultConfigStore())
}
// NewSafe returns a newly initialized Venom instance that is safe to read and
// write from multiple goroutines.
//
// The internal config map is created empty, only allocating space for a given
// config level once a value is set to that level.
func NewSafe() *Venom {
return NewWithStore(NewSafeConfigStore())
}
// NewLoggable takes a Logger and returns a newly initialized Venom
// instance that will log to a Logger interface upon reads and writes.
func NewLoggableWith(l Logger) *Venom {
lcs := NewLoggableConfigStoreWith(l)
return NewWithStore(lcs)
}
// NewLoggable returns a Venom instance with a default log set to standard out.
func NewLoggable() *Venom {
return NewWithStore(NewLoggableConfigStore())
}
// NewWithStore returns a newly initialized Venom instance that wraps the
// provided ConfigStore.
func NewWithStore(s ConfigStore) *Venom {
return &Venom{
Store: s,
}
}
// Default returns a new venom instance with some default resolver
// configuration applied to it.
func Default() *Venom {
ven := New()
ven.RegisterResolver(EnvironmentLevel, defaultEnvResolver)
return ven
}
// Default returns a new goroutine-safe venom instance with some default
// resolver configuration applied to it.
func DefaultSafe() *Venom {
ven := NewSafe()
ven.RegisterResolver(EnvironmentLevel, defaultEnvResolver)
return ven
}
// RegisterResolver registers a custom config resolver for the specified
// ConfigLevel.
//
// Additionally, if the provided level is not already in the current collection
// of active config levels, it will be added automatically
func (v *Venom) RegisterResolver(level ConfigLevel, r Resolver) {
v.Store.RegisterResolver(level, r)
}
// Alias registers an alias for a given key. This allows consumers to access
// the same config via a different key, increasing the backwards
// compatibility of an application.
func (v *Venom) Alias(from, to string) {
v.Store.Alias(from, to)
}
// SetLevel is a generic key/value setter method. It sets the provided k/v at
// the specified level inside the map, conditionally creating a new ConfigMap if
// one didn't previously exist.
func (v *Venom) SetLevel(level ConfigLevel, key string, value interface{}) {
v.Store.SetLevel(level, key, value)
}
// SetDefault sets the provided key and value into the DefaultLevel of the
// config collection.
func (v *Venom) SetDefault(key string, value interface{}) {
v.Store.SetLevel(DefaultLevel, key, value)
}
// SetOverride sets the provided key and value into the OverrideLevel of the
// config collection.
func (v *Venom) SetOverride(key string, value interface{}) {
v.Store.SetLevel(OverrideLevel, key, value)
}
// Get performs a fetch on a given key from the inner config collection.
func (v *Venom) Get(key string) interface{} {
val, _ := v.Store.Find(key)
return val
}
// Find searches for the given key, returning the discovered value and a
// boolean indicating whether or not the key was found
func (v *Venom) Find(key string) (interface{}, bool) {
return v.Store.Find(key)
}
// Merge merges the provided config map into the ConfigLevel l, allocating
// space for ConfigLevel l if the level hasn't already been allocated.
func (v *Venom) Merge(l ConfigLevel, data ConfigMap) {
v.Store.Merge(l, data)
}
// Clear removes all data from the ConfigLevelMap and resets the heap of config
// levels
func (v *Venom) Clear() {
v.Store.Clear()
}
// Debug returns the current venom ConfigLevelMap as a pretty-printed JSON
// string.
func (v *Venom) Debug() string {
return v.Store.Debug()
}
// Size returns the number of config levels stored in the underlying
// ConfigStore.
func (v *Venom) Size() int {
return v.Store.Size()
}