-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.go
497 lines (448 loc) · 9.73 KB
/
value.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package jsons
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func value(v interface{}) Value {
var val Value
switch v := v.(type) {
case Value:
return v
case Bool, Number, String, Array, Object:
val.value = v
case bool:
val.value = Bool(v)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64:
var num Number
data, _ := json.Marshal(v)
_ = json.Unmarshal(data, &num)
val.value = num
case json.Number:
val.value = Number(v)
case string:
val.value = String(v)
case []interface{}:
if v != nil {
val.value = Array(v)
}
case map[string]interface{}:
if v != nil {
val.value = Object(v)
}
case nil:
val.value = nil
default:
// value of copy, change will not affect the original value
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Slice, reflect.Array:
if !vv.IsNil() {
var array = make(Array, vv.Len())
for i := 0; i < vv.Len(); i++ {
array[i] = vv.Index(i).Interface()
}
val.value = array
}
case reflect.Map:
if !vv.IsNil() {
var object = make(Object)
var keys = vv.MapKeys()
for _, k := range keys {
key := fmt.Sprint(k.Interface())
val := vv.MapIndex(k).Interface()
object[key] = val
}
val.value = object
}
case reflect.Ptr:
if !vv.IsNil() {
val.value = value(vv.Elem().Interface())
}
case reflect.Struct:
var object = make(Object)
var length = vv.NumField()
for i := 0; i < length; i++ {
key := vv.Type().Field(i).Name
val := vv.Field(i).Interface()
object[key] = val
}
val.value = object
}
}
return val
}
func original(value interface{}) interface{} {
switch v := value.(type) {
case Raw:
return json.RawMessage(v)
case Bool:
return bool(v)
case Number:
return json.Number(v)
case String:
return string(v)
case Array:
return []interface{}(v)
case Object:
return map[string]interface{}(v)
case Value:
return original(v.value)
}
return value
}
func (v Value) Value() (driver.Value, error) {
return json.Marshal(v.value)
}
func (v *Value) Scan(val interface{}) error {
if val == nil {
v.value = nil
return nil
}
switch val := val.(type) {
case []byte:
return v.UnmarshalJSON(val)
case string:
return v.UnmarshalJSON([]byte(val))
}
return errors.New("invalid scan json source")
}
func (Value) GormDBDataType(*gorm.DB, *schema.Field) string {
return "json"
}
func (v Value) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *Value) UnmarshalJSON(data []byte) error {
var err error
var val interface{}
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
if err = decoder.Decode(&val); err != nil {
return err
}
v.value = value(val)
return nil
}
func (v Value) Get(keys ...interface{}) (val Value) {
if len(keys) == 0 {
return v
}
switch value := v.value.(type) {
case Value:
return value.Get(keys...)
case Array:
return v.Array().Get(keys...)
case Object:
return v.Object().Get(keys...)
case []interface{}:
return Array(value).Get(keys...)
case map[string]interface{}:
return Object(value).Get(keys...)
}
return
}
func (v Value) Set(keys ...interface{}) {
if len(keys) == 0 {
return
}
switch value := v.value.(type) {
case Value:
value.Set(keys...)
case Array:
value.Set(keys...)
case Object:
value.Set(keys...)
case []interface{}:
Array(value).Set(keys...)
case map[string]interface{}:
Object(value).Set(keys...)
}
}
func (v Value) Raw(keys ...interface{}) Raw {
raw, _ := json.Marshal(v.Get(keys...).value)
return raw
}
func (v Value) Int(keys ...interface{}) int64 {
i, _ := v.Number(keys...).Int64()
return i
}
func (v Value) Uint(keys ...interface{}) uint64 {
u, _ := v.Number(keys...).Uint64()
return u
}
func (v Value) Float(keys ...interface{}) float64 {
f, _ := v.Number(keys...).Float64()
return f
}
func (v Value) Number(keys ...interface{}) Number {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Number()
case Number:
return value
case json.Number:
return Number(value)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
data, _ := json.Marshal(value)
return Number(data)
}
return ""
}
func (v Value) Bool(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Bool()
case Bool:
return bool(value)
case bool:
return value
}
return false
}
func (v Value) String(keys ...interface{}) string {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.String()
case String:
return string(value)
case string:
return value
case []byte:
return string(value)
}
return ""
}
func (v Value) Array(keys ...interface{}) Array {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Array()
case Array:
return value
case []interface{}:
return value
}
return nil
}
func (v Value) Object(keys ...interface{}) Object {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Object()
case Object:
return value
case map[string]interface{}:
return value
}
return nil
}
func (v Value) Interface(keys ...interface{}) interface{} {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Interface()
default:
return value
}
}
func (v Value) IsNull(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.IsNull()
case Number:
return value == ""
case Array:
return value == nil
case Object:
return value == nil
case nil:
return true
case []byte:
return len(value) == 0 || string(value) == "null"
case []interface{}:
return value == nil
case map[string]interface{}:
return value == nil
default:
return value == nil
}
}
func (v Value) IsBool(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.IsBool()
case Bool, bool:
return true
}
return false
}
func (v Value) IsNumber(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.IsNumber()
case Number, json.Number, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
return true
}
return false
}
func (v Value) IsString(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.IsString()
case String, string:
return true
}
return false
}
func (v Value) IsArray(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.IsArray()
case Array, []interface{}:
return true
}
return false
}
func (v Value) IsObject(keys ...interface{}) bool {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.IsObject()
case Object, map[string]interface{}:
return true
}
return false
}
func (v Value) Type(keys ...interface{}) string {
var value = v.Get(keys...)
switch {
case value.IsObject():
return "object"
case value.IsArray():
return "array"
case value.IsString():
return "string"
case value.IsNumber():
return "number"
case value.IsBool():
return "bool"
case value.IsNull():
return "null"
}
return "undefined"
}
func (v Value) Len(keys ...interface{}) int {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Len()
case Array:
return value.Len()
case Object:
return value.Len()
case String:
return len(value)
case []byte:
return len(value)
case string:
return len(value)
case []interface{}:
return len(value)
case map[string]interface{}:
return len(value)
}
return 0
}
func (v Value) Cap(keys ...interface{}) int {
switch value := v.Get(keys...).value.(type) {
case Value:
return value.Cap()
case Array:
return value.Cap()
case []interface{}:
return cap(value)
}
return 0
}
func (v Value) Range(fn func(key interface{}, value Value) (continued bool)) bool {
switch {
case v.IsArray():
return v.Array().Range(func(index int, value Value) (continued bool) {
return fn(index, value)
})
case v.IsObject():
return v.Object().Range(func(key string, value Value) (continued bool) {
return fn(key, value)
})
}
return false
}
func (v Value) Slice(begin, end int) Array {
return v.Array().Slice(begin, end)
}
func (v Value) Index(value interface{}) int {
return v.Array().Index(value)
}
func (v Value) Append(arr Array) Array {
return v.Array().Append(arr)
}
func (v Value) Contains(value interface{}) bool {
return v.Array().Contains(value)
}
func (v Value) Reverse(keys ...interface{}) Array {
return v.Array(keys...).Reverse()
}
func (v Value) Sort(less func(i, j int) bool) Array {
return v.Array().Sort(less)
}
func (v Value) Keys(keys ...interface{}) []string {
return v.Object(keys...).Keys()
}
func (v Value) Exist(keys ...interface{}) bool {
if len(keys) > 0 {
var end = len(keys) - 1
return v.Object(keys[:end]...).Exist(keys[end])
}
return false
}
func (v Value) Delete(keys ...interface{}) {
if len(keys) > 0 {
var end = len(keys) - 1
v.Object(keys[:end]...).Delete(keys[end])
}
}
func (v Value) Clone(keys ...interface{}) Value {
val := v.Get(keys...)
switch {
case val.IsArray():
return value(val.Array().Clone())
case val.IsObject():
return value(val.Object().Clone())
}
return v
}
func (v Value) JSON(keys ...interface{}) []byte {
data, _ := v.Get(keys...).MarshalJSON()
if len(data) == 0 {
return []byte("null")
}
return data
}
func (v Value) JSONString(keys ...interface{}) string {
return string(v.JSON(keys...))
}
func (v Value) Marshal(obj interface{}) error {
data, err := v.MarshalJSON()
if err != nil {
return err
}
return json.Unmarshal(data, obj)
}
func (v *Value) Unmarshal(obj interface{}) error {
data, err := json.Marshal(obj)
if err != nil {
return err
}
return v.UnmarshalJSON(data)
}