-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmap.go
89 lines (74 loc) · 1.69 KB
/
gmap.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
/**
* @Author: DollarKiller
* @Description: gmap json to map
* @Github: https://github.com/dollarkillerx
* @Date: Create in 13:51 2019-10-08
*/
package gmap
import "encoding/json"
type mapun struct {
dataStr string
mc map[string]interface{}
}
func Unmarshal(jsn string) (*mapun, error) {
m := make(map[string]interface{})
err := json.Unmarshal([]byte(jsn), &m)
if err != nil {
return nil, err
}
return &mapun{
dataStr: jsn,
mc: m,
}, nil
}
func (m *mapun) GetString(key string) (string, bool) {
s, ok := m.mc[key].(string)
return s, ok
}
func (m *mapun) GetInterface(key string) (interface{}, bool) {
s, ok := m.mc[key]
return s, ok
}
func (m *mapun) GetInt(key string) (int, bool) {
s, ok := m.mc[key].(int)
return s, ok
}
func (m *mapun) GetSlice(key string) ([]interface{}, bool) {
i, ok := m.mc[key].([]interface{})
return i, ok
}
func (m *mapun) GetSlice2(data interface{}) ([]interface{}, bool) {
i, ok := data.([]interface{})
return i, ok
}
func (m *mapun) GetMap(key string) (map[string]interface{}, bool) {
i, ok := m.mc[key].(map[string]interface{})
return i, ok
}
func (m *mapun) GetMap2(data interface{}) (map[string]interface{}, bool) {
i, ok := data.(map[string]interface{})
return i, ok
}
func (m *mapun) GetSliceMap(key string) ([]map[string]interface{}, bool) {
data := make([]map[string]interface{}, 0)
slice, b := m.GetSlice(key)
if !b {
return nil, b
}
for _, item := range slice {
map2, i := m.GetMap2(item)
if !i {
continue
}
data = append(data, map2)
}
return data, true
}
// 消耗大
//func (m *mapun) ToMap() (map[string]interface{}, bool) {
// data := make(map[string]interface{})
//
// for k,v := range m.mc {
//
// }
//}