-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2.go
87 lines (73 loc) · 1.62 KB
/
d2.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
package d2
import (
"github.com/gogather/safemap/v2"
)
type D2 struct {
md2 *safemap.SafeMap
}
func NewD2() *D2 {
return &D2{
md2: safemap.New(),
}
}
func (md2 *D2) Sections() []string {
return md2.md2.Keys()
}
func (md2 *D2) Get(section, key string) (value interface{}, exist bool) {
sect, ok := md2.md2.Get(section)
if !ok {
return nil, ok
}
sectMap, _ := sect.(*safemap.SafeMap)
return sectMap.Get(key)
}
func (md2 *D2) GetSection(section string) (value *safemap.SafeMap, exist bool) {
sect, ok := md2.md2.Get(section)
if !ok {
return nil, ok
}
sectMap, ok := sect.(*safemap.SafeMap)
return sectMap, ok
}
func (md2 *D2) Add(section, key string, value interface{}) {
sect, ok := md2.md2.Get(section)
if !ok {
sect = safemap.New()
}
sectMap, _ := sect.(*safemap.SafeMap)
sectMap.Put(key, value)
md2.md2.Put(section, sectMap)
}
func (md2 *D2) RemoveKey(section, key string) {
sectMap, ok := md2.md2.Get(section)
if !ok {
return
}
sect := sectMap.(*safemap.SafeMap)
_, ok = sect.Get(key)
if ok {
sect.Remove(key)
}
if len(sect.GetMap()) <= 0 {
md2.md2.Remove(section)
} else {
md2.md2.Put(section, sect)
}
}
func (md2 *D2) RemoveSection(section string) {
md2.md2.Remove(section)
}
func (md2 *D2) Clear() {
md2.md2 = safemap.New()
}
func (md2 *D2) GetMapMap() map[string]map[string]interface{} {
dumpResult := map[string]map[string]interface{}{}
for section, lowerMap := range md2.md2.GetMap() {
sectionMap := map[string]interface{}{}
for key, value := range lowerMap.(*safemap.SafeMap).GetMap() {
sectionMap[key] = value
}
dumpResult[section] = sectionMap
}
return dumpResult
}