-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
73 lines (63 loc) · 1.16 KB
/
map.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
package value
import (
"context"
"strings"
"sync"
)
type Map struct {
mutex sync.Mutex
m map[string]Value
}
func WithMap(ctx context.Context) context.Context {
return context.WithValue(ctx, "valueMap", &Map{
m: make(map[string]Value),
})
}
func MapFromContext(ctx context.Context) *Map {
return ctx.Value("valueMap").(*Map)
}
func (m *Map) Get(k string) Value {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.get(k)
}
func (m *Map) get(k string) Value {
return m.m[strings.ToLower(k)]
}
func (m *Map) GetAll(keys ...string) []Value {
m.mutex.Lock()
defer m.mutex.Unlock()
var args []Value
for _, key := range keys {
args = append(args, m.get(key))
}
return args
}
func (m *Map) Put(k string, v Value) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.m[strings.ToLower(k)] = v
}
func (m *Map) Reset() {
m.mutex.Lock()
defer m.mutex.Unlock()
if m != nil {
m.m = make(map[string]Value)
}
}
func ResetMap(ctx context.Context) error {
m := MapFromContext(ctx)
if m != nil {
m.Reset()
}
return nil
}
func (m *Map) GetKeys() []string {
m.mutex.Lock()
defer m.mutex.Unlock()
var a []string
for k, _ := range m.m {
a = append(a, k)
}
return a
}