-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmemory.go
53 lines (46 loc) · 1.16 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
package gonfig
// MemoryConfig is a simple abstraction to map[]interface{} for in process memory backed configuration
// only implements Configurable use JsonConfig to save/load if needed
type MemoryConfig struct {
data map[string]string
}
// Returns a new memory backed Configurable
// The most basic Configurable simply backed by a map[string]interface{}
func NewMemoryConfig() *MemoryConfig {
cfg := &MemoryConfig{make(map[string]string)}
cfg.init()
return cfg
}
func (self *MemoryConfig) init() {
self.data = make(map[string]string)
}
// if no arguments are proced Reset() re-creates the underlaying map
func (self *MemoryConfig) Reset(datas ...map[string]string) {
if len(datas) >= 1 {
self.data = datas[0]
} else {
self.data = make(map[string]string)
}
return
}
// Get key from map
func (self *MemoryConfig) Get(key string) string {
if self.data == nil {
self.init()
}
return self.data[key]
}
// get all keys
func (self *MemoryConfig) All() map[string]string {
if self.data == nil {
self.init()
}
return self.data
}
// Set a key to value
func (self *MemoryConfig) Set(key, value string) {
if self.data == nil {
self.init()
}
self.data[key] = value
}