-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.go
80 lines (74 loc) · 1.19 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
74
75
76
77
78
79
80
package wen
import "strings"
type Map map[string]interface{}
func (m Map) M(s string) Map {
if !m.Have(s) {
return nil
}
if t, ok := m[s].(map[string]interface{}); ok {
return t
} else {
return m[s].(Map)
}
}
func (m Map) S(s string) string {
if !m.Have(s) {
return ""
}
return m[s].(string)
}
func (m Map) Int(s string) int {
if !m.Have(s) {
return 0
}
return m[s].(int)
}
func (m Map) Bool(s string) bool {
if !m.Have(s) {
return false
}
return m[s].(bool)
}
func (m Map) I(s string) interface{} {
if !m.Have(s) {
return nil
}
return m[s]
}
func (m Map) Have(s string) bool {
_, ok := m[s]
return ok
}
func (m Map) Get(s string) interface{} {
splits := strings.Split(s, ".")
if len(splits) == 1 {
return m.I(s)
}
var r = m
for index, split := range splits {
if index == len(splits)-1 {
return r.I(split)
} else {
r = r.M(split)
}
}
return nil
}
func (m Map) Set(s string, v interface{}) {
splits := strings.Split(s, ".")
if len(splits) == 1 {
m[s] = v
return
}
var r = m
for index, split := range splits {
if index == len(splits)-1 {
r.Set(split, v)
} else {
if !r.Have(split) {
r.Set(split, Map{})
}
r = r.M(split)
}
}
}