-
Notifications
You must be signed in to change notification settings - Fork 2
/
dict.go
137 lines (124 loc) · 3.19 KB
/
dict.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
package dict
import (
"reflect"
)
func getValue(data interface{}, key interface{}) interface{} {
dataType := reflect.TypeOf(data).Kind()
if dataType != reflect.Map {
return nil
}
dataValue := reflect.ValueOf(data)
keyValue := reflect.ValueOf(key)
value := dataValue.MapIndex(keyValue)
if value.IsValid() {
return value.Interface()
}
return nil
}
func GetBool(data interface{}, key interface{}) bool {
result := false
value := getValue(data, key)
if value != nil && reflect.TypeOf(value).Kind() == reflect.Bool {
result = value.(bool)
}
return result
}
func GetString(data interface{}, key interface{}) string {
result := ""
value := getValue(data, key)
if value != nil && reflect.TypeOf(value).Kind() == reflect.String {
result = value.(string)
}
return result
}
func GetStringMap(data interface{}, key interface{}) map[string]interface{} {
var result map[string]interface{}
value := getValue(data, key)
if value != nil && reflect.TypeOf(value).Kind() == reflect.TypeOf(result).Kind() {
result = value.(map[string]interface{})
}
return result
}
func GetString2(data interface{}, key1 interface{}, key2 interface{}) string {
tmpMap := GetStringMap(data, key1)
result := GetString(tmpMap, key2)
return result
}
func GetUint16(data interface{}, key interface{}) uint16 {
var result uint16 = 0
value := getValue(data, key)
if value != nil {
valueType := reflect.TypeOf(value).Kind()
if valueType == reflect.Float64 {
result = uint16(value.(float64))
} else if valueType == reflect.Uint16 {
result = value.(uint16)
}
}
return result
}
func GetUint32(data interface{}, key interface{}) uint32 {
var result uint32 = 0
value := getValue(data, key)
if value != nil {
valueType := reflect.TypeOf(value).Kind()
if valueType == reflect.Float64 {
result = uint32(value.(float64))
} else if valueType == reflect.Uint32 {
result = value.(uint32)
}
}
return result
}
func GetUint64(data interface{}, key interface{}) uint64 {
var result uint64 = 0
value := getValue(data, key)
if value != nil {
valueType := reflect.TypeOf(value).Kind()
if valueType == reflect.Float64 {
result = uint64(value.(float64))
} else if valueType == reflect.Uint64 {
result = value.(uint64)
}
}
return result
}
func GetInt64(data interface{}, key interface{}) int64 {
var result int64 = 0
value := getValue(data, key)
if value != nil {
valueType := reflect.TypeOf(value).Kind()
if valueType == reflect.Float64 {
result = int64(value.(float64))
} else if valueType == reflect.Int64 {
result = value.(int64)
}
}
return result
}
func GetUint8(data interface{}, key interface{}) uint8 {
var result uint8 = 0
value := getValue(data, key)
if value != nil {
valueType := reflect.TypeOf(value).Kind()
if valueType == reflect.Float64 {
result = uint8(value.(float64))
} else if valueType == reflect.Uint8 {
result = value.(uint8)
}
}
return result
}
func GetInt(data interface{}, key interface{}) int {
var result int = 0
value := getValue(data, key)
if value != nil {
valueType := reflect.TypeOf(value).Kind()
if valueType == reflect.Float64 {
result = int(value.(float64))
} else if valueType == reflect.Int {
result = value.(int)
}
}
return result
}