-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
65 lines (56 loc) · 1.32 KB
/
memory.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
package goconf
import (
"os"
"strings"
)
type MemorySource struct {
InitialData map[string]string
}
func Memory(initialData map[string]string) *MemorySource {
if initialData == nil {
initialData = make(map[string]string)
}
return &MemorySource{
InitialData: initialData,
}
}
func EnvironmentVariable(prefix string) *MemorySource {
data := make(map[string]string)
for _, env := range os.Environ() {
if len(prefix) > 0 && !strings.HasPrefix(env, prefix) {
continue
}
kv := strings.SplitN(env, "=", 2)
if len(kv) != 2 {
continue
}
data[kv[0]] = kv[1]
}
return Memory(data)
}
func (source *MemorySource) BuildProvider(builder Builder) Provider {
return NewMemoryProvider(source)
}
type MemoryProvider struct {
ConfigurationProvider
source *MemorySource
}
func NewMemoryProvider(source *MemorySource) *MemoryProvider {
if source == nil {
panic("goconf.NewMemoryProvider: source is nil")
}
provider := &MemoryProvider{
ConfigurationProvider: *NewConfigurationProvider(),
source: source,
}
if source.InitialData == nil {
return provider
}
for key, value := range source.InitialData {
provider.Data[key] = NewExtractedValue(key, value, value)
}
return provider
}
func (provider *MemoryProvider) Add(key, value string) {
provider.Data[key] = NewExtractedValue(key, value, value)
}