-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
57 lines (47 loc) · 1.15 KB
/
options.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
package util
import "sync"
// Options is an interface to get and set string-like options for components
type Options interface {
Set(key string, value string)
Get(key string, fallback string) string
IsSet(key string) bool
}
// GetOptions gets the static options structure
func GetOptions() Options {
return options
}
// NewOptions returns a new Options structure
func NewOptions() Options {
return &optionMap{
values: make(map[string]string),
}
}
var options = NewOptions()
// optionMap implements the Options interface with a map
type optionMap struct {
values map[string]string
mutex sync.Mutex
}
// Set sets the option key with value
func (o *optionMap) Set(key string, value string) {
o.mutex.Lock()
o.values[key] = value
o.mutex.Unlock()
}
// Get gets the value of a key or if not available, returns the fallback
func (o *optionMap) Get(key string, fallback string) string {
o.mutex.Lock()
ret, ok := o.values[key]
if !ok {
ret = fallback
}
o.mutex.Unlock()
return ret
}
// IsSet returns true if the key has been set
func (o *optionMap) IsSet(key string) (ret bool) {
o.mutex.Lock()
_, ret = o.values[key]
o.mutex.Unlock()
return
}