-
Notifications
You must be signed in to change notification settings - Fork 2
/
any.go
83 lines (73 loc) · 1.13 KB
/
any.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
package easycb
import (
"encoding/json"
"sync"
)
type AnyMap map[string]interface{}
var mu = new(sync.RWMutex)
func (am AnyMap) Set(key string, value interface{}) AnyMap {
mu.Lock()
am[key] = value
mu.Unlock()
return am
}
func (am AnyMap) GetString(key string) string {
if am == nil {
return ""
}
mu.RLock()
defer mu.RUnlock()
value, ok := am[key]
if !ok {
return ""
}
v, ok := value.(string)
if !ok {
return convertToString(value)
}
return v
}
func (am AnyMap) GetInterface(key string) interface{} {
if am == nil {
return nil
}
mu.RLock()
defer mu.RUnlock()
return am[key]
}
func (am AnyMap) Remove(key string) {
mu.Lock()
delete(am, key)
mu.Unlock()
}
func (am AnyMap) Reset() {
mu.Lock()
for k := range am {
delete(am, k)
}
mu.Unlock()
}
func (am AnyMap) JsonBody() (jb string) {
mu.Lock()
defer mu.Unlock()
bs, err := json.Marshal(am)
if err != nil {
return ""
}
jb = string(bs)
return jb
}
func convertToString(v interface{}) (str string) {
if v == nil {
return ""
}
var (
bs []byte
err error
)
if bs, err = json.Marshal(v); err != nil {
return ""
}
str = string(bs)
return
}