forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvdata.go
109 lines (94 loc) · 2.91 KB
/
kvdata.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package goutil
import "time"
// KVData key-value data
type KVData map[string]interface{}
// Get returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
func (k KVData) Get(key string) (value interface{}, exists bool) {
value, exists = k[key]
return
}
// MustGet returns the value for the given key if it exists, otherwise it panics.
func (k KVData) MustGet(key string) interface{} {
if value, exists := k.Get(key); exists {
return value
}
panic("Key \"" + key + "\" does not exist")
}
// GetString returns the value associated with the key as a string.
func (k KVData) GetString(key string) (s string) {
if val, ok := k.Get(key); ok && val != nil {
s, _ = val.(string)
}
return
}
// GetBool returns the value associated with the key as a boolean.
func (k KVData) GetBool(key string) (b bool) {
if val, ok := k.Get(key); ok && val != nil {
b, _ = val.(bool)
}
return
}
// GetInt returns the value associated with the key as an integer.
func (k KVData) GetInt(key string) (i int) {
if val, ok := k.Get(key); ok && val != nil {
i, _ = val.(int)
}
return
}
// GetInt64 returns the value associated with the key as an integer.
func (k KVData) GetInt64(key string) (i64 int64) {
if val, ok := k.Get(key); ok && val != nil {
i64, _ = val.(int64)
}
return
}
// GetFloat64 returns the value associated with the key as a float64.
func (k KVData) GetFloat64(key string) (f64 float64) {
if val, ok := k.Get(key); ok && val != nil {
f64, _ = val.(float64)
}
return
}
// GetTime returns the value associated with the key as time.
func (k KVData) GetTime(key string) (t time.Time) {
if val, ok := k.Get(key); ok && val != nil {
t, _ = val.(time.Time)
}
return
}
// GetDuration returns the value associated with the key as a duration.
func (k KVData) GetDuration(key string) (d time.Duration) {
if val, ok := k.Get(key); ok && val != nil {
d, _ = val.(time.Duration)
}
return
}
// GetStringSlice returns the value associated with the key as a slice of strings.
func (k KVData) GetStringSlice(key string) (ss []string) {
if val, ok := k.Get(key); ok && val != nil {
ss, _ = val.([]string)
}
return
}
// GetStringMap returns the value associated with the key as a map of interfaces.
func (k KVData) GetStringMap(key string) (sm map[string]interface{}) {
if val, ok := k.Get(key); ok && val != nil {
sm, _ = val.(map[string]interface{})
}
return
}
// GetStringMapString returns the value associated with the key as a map of strings.
func (k KVData) GetStringMapString(key string) (sms map[string]string) {
if val, ok := k.Get(key); ok && val != nil {
sms, _ = val.(map[string]string)
}
return
}
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func (k KVData) GetStringMapStringSlice(key string) (smss map[string][]string) {
if val, ok := k.Get(key); ok && val != nil {
smss, _ = val.(map[string][]string)
}
return
}