-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject.go
198 lines (172 loc) · 3.64 KB
/
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package jsons
import (
"database/sql/driver"
"encoding/json"
"errors"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func (o Object) Value() (driver.Value, error) {
return json.Marshal(o)
}
func (o *Object) Scan(v interface{}) error {
if v == nil {
*o = nil
return nil
}
switch val := v.(type) {
case []byte:
return json.Unmarshal(val, o)
case string:
return json.Unmarshal([]byte(val), o)
}
return errors.New("invalid scan object source")
}
func (Object) GormDBDataType(*gorm.DB, *schema.Field) string {
return "json"
}
func (o Object) Raw(keys ...interface{})Raw {
return o.JSONValue(keys...).Raw()
}
func (o Object) Get(keys ...interface{}) (val Value) {
if o == nil {
return
}
switch len(keys) {
case 0:
return value(o)
case 1:
if key, ok := keys[0].(string); ok {
if _, exists := o[key]; exists {
return value(o[key])
}
}
default:
var v = value(o)
for _, k := range keys {
switch key := k.(type) {
case int:
v = v.Array().Get(key)
case string:
v = v.Object().Get(key)
}
}
return v
}
return
}
func (o Object) Set(keys ...interface{}) {
if length := len(keys); length > 1 {
key := keys[length-2]
val := value(keys[length-1])
switch value := o.Get(keys[:length-2]...).value.(type) {
case Value:
value.Set(key, val)
case Array:
value.Set(key, val)
case Object:
if key, ok := key.(string); ok && value != nil {
value[key] = val
}
}
}
}
func (o Object) Int(keys ...interface{}) int64 {
return o.Get(keys...).Int()
}
func (o Object) Float(keys ...interface{}) float64 {
return o.Get(keys...).Float()
}
func (o Object) Number(keys ...interface{}) Number {
return o.Get(keys...).Number()
}
func (o Object) Bool(keys ...interface{}) bool {
return o.Get(keys...).Bool()
}
func (o Object) String(keys ...interface{}) string {
return o.Get(keys...).String()
}
func (o Object) Object(keys ...interface{}) Object {
return o.Get(keys...).Object()
}
func (o Object) Array(keys ...interface{}) Array {
return o.Get(keys...).Array()
}
func (o Object) Interface(keys ...interface{}) interface{} {
return o.Get(keys...).Interface()
}
func (o Object) Len(keys ...interface{}) int {
switch len(keys) {
case 0:
return len(o)
default:
return o.Get(keys...).Len()
}
}
func (o Object) Keys(keys ...interface{}) []string {
var object = o.Object(keys...)
var key = make([]string, 0, len(object))
for k, _ := range object {
key = append(key, k)
}
return key
}
func (o Object) Exist(keys ...interface{}) bool {
if o == nil {
return false
}
switch len(keys) {
case 0:
return o != nil
case 1:
if key, ok := keys[0].(string); ok {
_, exists := o[key]
return exists
}
default:
var end = len(keys) - 1
return o.Object(keys[:end]...).Exist(keys[end])
}
return false
}
func (o Object) Delete(keys ...interface{}) {
switch len(keys) {
case 0:
return
case 1:
if key, ok := keys[0].(string); ok {
delete(o, key)
}
default:
var end = len(keys) - 1
o.Object(keys[:end]...).Delete(keys[end])
}
}
func (o Object) Clone(keys ...interface{}) Value {
switch len(keys) {
case 0:
var object = make(Object)
data, _ := json.Marshal(o)
_ = json.Unmarshal(data, &object)
return value(object)
default:
return o.Get(keys...).Clone()
}
}
func (o Object) Range(fn func(key string, value Value) (continued bool)) bool {
for key, val := range o {
if !fn(key, value(val)) {
return false
}
}
return true
}
func (o Object) JSON(keys ...interface{}) []byte {
return o.Get(keys...).JSON()
}
func (o Object) JSONValue(keys ...interface{}) Value {
return o.Get(keys...)
}
func (o Object) JSONString(keys ...interface{}) string {
return o.Get(keys...).JSONString()
}