forked from biased-unit/planout-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typed_map.go
138 lines (94 loc) · 2.15 KB
/
typed_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
package planout
import (
"reflect"
)
type TypedMap struct {
data map[string]interface{}
}
func NewTypedMap(data map[string]interface{}) *TypedMap {
return &TypedMap{data: data}
}
func (t *TypedMap) get(key string) (interface{}, bool) {
value, exists := t.data[key]
return value, exists
}
func (t *TypedMap) getString(key string) (string, bool) {
value, exists := t.get(key)
if !exists {
return "", false
}
if reflect.TypeOf(value).String() != "string" {
return "", false
}
return value.(string), true
}
func (t *TypedMap) getBool(key string) (bool, bool) {
value, exists := t.get(key)
if !exists {
return false, false
}
if reflect.TypeOf(value).String() != "bool" {
return false, false
}
return value.(bool), true
}
func (t *TypedMap) getInt(key string) (int, bool) {
value, exists := t.get(key)
if !exists {
return 0, false
}
if reflect.TypeOf(value).String() != "int" {
return 0, false
}
return value.(int), true
}
func (t *TypedMap) getInt64(key string) (int64, bool) {
value, exists := t.get(key)
if !exists {
return 0, false
}
if reflect.TypeOf(value).String() != "int64" {
return 0, false
}
return value.(int64), true
}
func (t *TypedMap) getFloat32(key string) (float32, bool) {
value, exists := t.get(key)
if !exists {
return 0.0, false
}
if reflect.TypeOf(value).String() != "float32" {
return 0.0, false
}
return value.(float32), true
}
func (t *TypedMap) getFloat64(key string) (float64, bool) {
value, exists := t.get(key)
if !exists {
return 0.0, false
}
if reflect.TypeOf(value).String() != "float64" {
return 0.0, false
}
return value.(float64), true
}
func (t *TypedMap) getMap(key string) (map[string]interface{}, bool) {
value, exists := t.get(key)
if !exists {
return nil, false
}
if reflect.TypeOf(value).String() != "map[string]interface {}" {
return nil, false
}
return value.(map[string]interface{}), true
}
func (t *TypedMap) getArray(key string) ([]interface{}, bool) {
value, exists := t.get(key)
if !exists {
return nil, false
}
if reflect.TypeOf(value).String() != "[]interface {}" {
return nil, false
}
return value.([]interface{}), true
}