/
store.go
72 lines (61 loc) · 1.57 KB
/
store.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
66
67
68
69
70
71
72
package config
import (
"encoding/json"
)
// Config encapsulates a map of polymorfic values and provides
// useful method to lookup and retrieve fields by key.
//
// Config is designed to be read-only and thread-safe.
type Config map[string]interface{}
// GetString lookups a config value by key and returns it as string.
func (c Config) GetString(key string) string {
if !c.Exists(key) {
return ""
}
return c[key].(string)
}
// GetBool lookups a config value by key and returns it as bool.
func (c Config) GetBool(key string) bool {
if !c.Exists(key) {
return false
}
return c[key].(bool)
}
// GetInt lookups a config value by key and returns it as int.
func (c Config) GetInt(key string) int {
if !c.Exists(key) {
return 0
}
return c[key].(int)
}
// GetInt64 lookups a config value by key and returns it as int64.
func (c Config) GetInt64(key string) int64 {
if !c.Exists(key) {
return int64(0)
}
return c[key].(int64)
}
// GetFloat lookups a config value by key and returns it as float64.
func (c Config) GetFloat(key string) float64 {
if !c.Exists(key) {
return float64(0)
}
return c[key].(float64)
}
// Get lookups a config value by key and returns it.
func (c Config) Get(key string) interface{} {
return c[key]
}
// Exists checks is config field is present.
func (c Config) Exists(key string) bool {
_, ok := c[key]
return ok
}
// Set sets a new field in the current config object.
func (c Config) Set(key string, value interface{}) {
c[key] = value
}
// JSON serializes the config fields a JSON.
func (c Config) JSON() ([]byte, error) {
return json.Marshal(c)
}