-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
131 lines (127 loc) · 2.34 KB
/
map.go
File metadata and controls
131 lines (127 loc) · 2.34 KB
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
package hbtp
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
type Map map[string]interface{}
func NewMap() *Map {
return &Map{}
}
func NewMaps(body interface{}) *Map {
e := &Map{}
if body == nil {
return e
}
switch body.(type) {
case string:
json.Unmarshal([]byte(body.(string)), e)
break
case []byte:
json.Unmarshal(body.([]byte), e)
break
case map[string]interface{}:
for k, v := range body.(map[string]interface{}) {
e.Set(k, v)
}
break
case *map[string]interface{}:
for k, v := range *(body.(*map[string]interface{})) {
e.Set(k, v)
}
break
default:
bts, err := json.Marshal(body)
if err == nil {
json.Unmarshal(bts, e)
}
break
}
return e
}
func (e *Map) Get(key string) (interface{}, bool) {
v, ok := (*e)[key]
return v, ok
}
func (e *Map) Set(key string, val interface{}) {
(*e)[key] = val
}
func (e *Map) Map() map[string]interface{} {
return *e
}
func (e *Map) ToBytes() []byte {
bts, err := json.Marshal(e)
if err != nil {
return nil
}
return bts
}
func (e *Map) ToString() string {
bts, err := json.Marshal(e)
if err != nil {
return ""
}
return string(bts)
}
func (e *Map) GetString(key string) string {
v, ok := e.Get(key)
if !ok {
return ""
}
switch v.(type) {
case float32:
return fmt.Sprintf("%d", int64(v.(float32)))
case float64:
return fmt.Sprintf("%d", int64(v.(float64)))
}
return fmt.Sprintf("%v", v)
}
func (e *Map) GetInt(key string) (int64, error) {
v, ok := e.Get(key)
if !ok {
return 0, errors.New("not found key")
}
switch v.(type) {
case int:
return v.(int64), nil
case string:
return strconv.ParseInt(v.(string), 10, 64)
case int64:
return v.(int64), nil
case float32:
return int64(v.(float32)), nil
case float64:
return int64(v.(float64)), nil
}
return 0, errors.New("not found")
}
func (e *Map) GetFloat(key string) (float64, error) {
v, ok := e.Get(key)
if !ok {
return 0, errors.New("not found key")
}
switch v.(type) {
case int:
return float64(v.(int)), nil
case string:
return strconv.ParseFloat(v.(string), 64)
case int64:
return float64(v.(int64)), nil
case float32:
return float64(v.(float32)), nil
case float64:
return v.(float64), nil
}
return 0, errors.New("not found")
}
func (e *Map) GetBool(key string) bool {
v, ok := e.Get(key)
if ok {
switch v.(type) {
case bool:
return v.(bool)
}
}
return false
}