-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_source.go
40 lines (34 loc) · 1 KB
/
memory_source.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
package flags
import "github.com/xitonix/flags/internal"
// MemorySource represents an in-memory implementation of `core.Source` interface.
type MemorySource struct {
cache map[string]string
}
// NewMemorySource creates a new instance of MemorySource type.
func NewMemorySource() *MemorySource {
return &MemorySource{
cache: make(map[string]string),
}
}
// Read reads the in-memory value associated with the specified key.
func (m *MemorySource) Read(key string) (string, bool) {
value, ok := m.cache[key]
return value, ok
}
// Add adds a new key-value pair to the source.
//
// If the key already exists, it will override the current value.
func (m *MemorySource) Add(key, value string) {
if internal.IsEmpty(key) {
return
}
m.cache[key] = value
}
// AddRange adds a list of key-value pairs to the source.
//
// If any of the keys already exists, its value will override the current value.
func (m *MemorySource) AddRange(items map[string]string) {
for key, value := range items {
m.Add(key, value)
}
}