-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
172 lines (152 loc) · 3.05 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package mpack
import (
"log"
"reflect"
)
type Map struct {
raw map[interface{}]interface{}
}
func NewMap(generic interface{}) *Map {
result := new(Map)
result.raw = generic.(map[interface{}]interface{})
return result
}
func isInt(v reflect.Value) bool {
k := v.Kind()
return k == reflect.Int || k == reflect.Int8 || k == reflect.Int16 || k == reflect.Int32 || k == reflect.Int64
}
func isUint(v reflect.Value) bool {
k := v.Kind()
return k == reflect.Uint || k == reflect.Uint8 || k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64
}
func isFloat(v reflect.Value) bool {
k := v.Kind()
return k == reflect.Float32 || k == reflect.Float64
}
func (m Map) IntIndex(key interface{}) (int64, bool) {
index, present := m.raw[key]
if !present {
return 0, false
}
v := reflect.ValueOf(index)
if isInt(v) {
return v.Int(), true
}
if isUint(v) {
return int64(v.Uint()), true
}
return 0, false
}
func (m Map) IntPlainIndex(key interface{}) (int, bool) {
index, present := m.raw[key]
if !present {
return 0, false
}
v := reflect.ValueOf(index)
if isInt(v) {
return int(v.Int()), true
}
if isUint(v) {
return int(v.Uint()), true
}
return 0, false
}
func (m Map) Int32Index(key interface{}) (int32, bool) {
index, present := m.raw[key]
if !present {
return 0, false
}
v := reflect.ValueOf(index)
if isInt(v) {
return int32(v.Int()), true
}
if isUint(v) {
return int32(v.Uint()), true
}
return 0, false
}
func (m Map) UintIndex(key interface{}) (uint64, bool) {
index, present := m.raw[key]
if !present {
return 0, false
}
v := reflect.ValueOf(index)
if isUint(v) {
return v.Uint(), true
}
if isInt(v) {
return uint64(v.Int()), true
}
return 0, false
}
func (m Map) UintSmallIndex(key interface{}) (uint, bool) {
index, present := m.raw[key]
if !present {
return 0, false
}
v := reflect.ValueOf(index)
if isUint(v) {
return uint(v.Uint()), true
}
if isInt(v) {
return uint(v.Int()), true
}
return 0, false
}
func (m Map) FloatIndex(key interface{}) (float64, bool) {
index, present := m.raw[key]
if !present {
return 0, false
}
v := reflect.ValueOf(index)
if isFloat(v) {
return v.Float(), true
}
if isInt(v) {
return float64(v.Int()), true
}
if isUint(v) {
return float64(v.Uint()), true
}
return 0, false
}
func (m Map) StringIndex(key interface{}) (string, bool) {
index, present := m.raw[key]
if !present {
return "", false
}
v := reflect.ValueOf(index)
if v.IsValid() == false {
return "", true
}
if v.IsNil() {
return "", true
}
return string(index.([]uint8)), true
}
func (m Map) ArrayIndex(key interface{}) (*Array, bool) {
index, present := m.raw[key]
if !present {
return nil, false
}
return NewArray(index), true
}
func (m Map) MapIndex(key interface{}) (*Map, bool) {
index, present := m.raw[key]
if !present {
return nil, false
}
v := reflect.ValueOf(index)
if v.IsValid() == false {
return nil, true
}
if v.IsNil() {
return nil, true
}
return NewMap(index), true
}
func (m Map) DumpKeys() {
for k := range m.raw {
log.Printf(k.(string))
}
}