forked from AirspaceTechnologies/go-psql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_object.go
153 lines (130 loc) · 2.83 KB
/
json_object.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
package psql
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strconv"
)
type JSONObject map[string]interface{}
// Scan and Value for db read / write
func (o *JSONObject) Scan(src interface{}) error {
if src == nil {
*o = nil
return nil
}
d, ok := src.([]byte)
if !ok {
return errors.New("source must be bytes")
}
return json.Unmarshal(d, o)
}
func (o JSONObject) Value() (driver.Value, error) {
return json.Marshal(o)
}
// Getters for json valid json types
var ErrNilValue = errors.New("value is nil")
func (o JSONObject) Object(key string) (JSONObject, error) {
var val = o[key]
if val == nil {
return nil, ErrNilValue
}
obj, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("incompatible type %T", val)
}
return obj, nil
}
func (o JSONObject) Slice(key string) ([]interface{}, error) {
var val = o[key]
if val == nil {
return nil, ErrNilValue
}
arr, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("incompatible type %T", val)
}
return arr, nil
}
func (o JSONObject) Float64(key string) (float64, error) {
var val = o[key]
switch casted := val.(type) {
case nil:
return 0, ErrNilValue
case float64:
return casted, nil
case int:
return float64(casted), nil
case int64:
return float64(casted), nil
case string:
return strconv.ParseFloat(casted, 64)
default:
return 0, fmt.Errorf("incompatible type %T", val)
}
}
func (o JSONObject) Bool(key string) (bool, error) {
var val = o[key]
switch casted := val.(type) {
case nil:
return false, ErrNilValue
case bool:
return casted, nil
case string:
return strconv.ParseBool(casted)
default:
return false, fmt.Errorf("incompatible type %T", val)
}
}
func (o JSONObject) String(key string) (string, error) {
var val = o[key]
switch casted := val.(type) {
case nil:
return "", ErrNilValue
case float64:
return fmt.Sprintf("%f", casted), nil
case int, int64:
return fmt.Sprintf("%d", casted), nil
case string:
return casted, nil
case bool:
return fmt.Sprintf("%t", casted), nil
default:
return "", fmt.Errorf("incompatible type %T", val)
}
}
// Convenience getters
func (o JSONObject) Int64(key string) (int64, error) {
var val = o[key]
switch casted := val.(type) {
case nil:
return 0, ErrNilValue
case float64:
return int64(casted), nil
case int:
return int64(casted), nil
case int64:
return casted, nil
case string:
return strconv.ParseInt(casted, 10, 64)
default:
return 0, fmt.Errorf("incompatible type %T", val)
}
}
func (o JSONObject) Int(key string) (int, error) {
var val = o[key]
switch casted := val.(type) {
case nil:
return 0, ErrNilValue
case float64:
return int(casted), nil
case int:
return casted, nil
case int64:
return int(casted), nil
case string:
return strconv.Atoi(casted)
default:
return 0, fmt.Errorf("incompatible type %T", val)
}
}