forked from phR0ze/n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
556 lines (480 loc) · 11.4 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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package n
import (
"time"
"github.com/pkg/errors"
)
// Object is a wrapper around an interface{} value providing a number of export methods
// for casting and converting to other types via the excellent cast package.
type Object struct {
o interface{} // value
}
// Obj creates a new Object from the given obj appending the Object methods
func Obj(obj interface{}) *Object {
return &Object{obj}
}
// Object interface methods
//--------------------------------------------------------------------------------------------------
// O returns the underlying data structure as is
func (p *Object) O() interface{} {
if p.Nil() {
return nil
}
return p.o
}
// Nil tests if the numerable is nil
func (p *Object) Nil() bool {
if p == nil || p.o == nil {
return true
}
return false
}
// A is an alias to String for brevity
func (p *Object) A() string {
return p.String()
}
// M is an alias to ToStringMap
func (p *Object) M() *StringMap {
return p.ToStringMap()
}
// MG is an alias to ToStringMapG
func (p *Object) MG() map[string]interface{} {
return p.ToStringMapG()
}
// S is an alias to ToStringSlice for brevity
func (p *Object) S() *StringSlice {
return p.ToStringSlice()
}
// String returns a string representation of the Object, implements Stringer interface.
func (p *Object) String() string {
if p == nil {
return ""
}
return ToString(p.o)
}
// Bool
//--------------------------------------------------------------------------------------------------
// ToBool converts an interface to a bool type.
func (p *Object) ToBool() bool {
if p == nil {
return false
}
v, _ := ToBoolE(p.o)
return v
}
// ToBoolE converts an interface to a bool type.
func (p *Object) ToBoolE() (bool, error) {
if p == nil {
return false, nil
}
return ToBoolE(p.o)
}
// Char
//--------------------------------------------------------------------------------------------------
// C is an alias to ToChar for brevity
func (p *Object) C() *Char {
return p.ToChar()
}
// ToChar converts an interface to a *Char type.
func (p *Object) ToChar() *Char {
if p == nil {
val := Char(0)
return &val
}
return ToChar(p.o)
}
// R is an alias to ToRune for brevity
func (p *Object) R() rune {
return p.ToRune()
}
// ToRune converts an interface to a rune type.
func (p *Object) ToRune() rune {
if p == nil {
return rune(0)
}
return rune(*ToChar(p.o))
}
// Map
//--------------------------------------------------------------------------------------------------
// Query an object if it is a StringMap type
func (p *Object) Query(key string) *Object {
obj, _ := p.QueryE(key)
return obj
}
// QueryE an object if it is a StringMap type
func (p *Object) QueryE(key string) (obj *Object, err error) {
obj = &Object{}
if p == nil {
return
}
var m *StringMap
if m, err = ToStringMapE(p.o); err != nil {
return
}
if obj, err = m.QueryE(key); err != nil {
return
}
if obj.o == nil {
err = errors.Errorf("invalid key")
}
return
}
// Time related
//--------------------------------------------------------------------------------------------------
// ToTime converts an interface to a time.Time type.
func (p *Object) ToTime() time.Time {
v, _ := ToTimeE(p.o)
return v
}
// ToTimeE converts an interface to a time.Time type.
func (p *Object) ToTimeE() (time.Time, error) {
return ToTimeE(p.o)
}
// ToDuration converts an interface to a time.Duration type.
func (p *Object) ToDuration() time.Duration {
v, _ := ToDurationE(p.o)
return v
}
// ToDurationE converts an interface to a time.Duration type.
func (p *Object) ToDurationE() (time.Duration, error) {
return ToDurationE(p.o)
}
// Float related
//--------------------------------------------------------------------------------------------------
// ToFloat32 converts an interface to a float32 type.
func (p *Object) ToFloat32() float32 {
if p == nil {
return float32(0)
}
return ToFloat32(p.o)
}
// ToFloat32E converts an interface to a float32 type.
func (p *Object) ToFloat32E() (float32, error) {
if p == nil {
return float32(0), nil
}
return ToFloat32E(p.o)
}
// ToFloat64 converts an interface to a float64 type.
func (p *Object) ToFloat64() float64 {
if p == nil {
return float64(0)
}
return ToFloat64(p.o)
}
// ToFloat64E converts an interface to a float64 type.
func (p *Object) ToFloat64E() (float64, error) {
if p == nil {
return float64(0), nil
}
return ToFloat64E(p.o)
}
// Int related
//--------------------------------------------------------------------------------------------------
// ToInt converts an interface to an int type.
func (p *Object) ToInt() int {
if p == nil {
return 0
}
v, _ := ToIntE(p.o)
return v
}
// ToIntE converts an interface to an int type.
func (p *Object) ToIntE() (int, error) {
if p == nil {
return 0, nil
}
return ToIntE(p.o)
}
// ToInt8 converts an interface to an int8 type.
func (p *Object) ToInt8() int8 {
if p == nil {
return 0
}
v, _ := ToInt8E(p.o)
return v
}
// ToInt8E converts an interface to an int8 type.
func (p *Object) ToInt8E() (int8, error) {
if p == nil {
return 0, nil
}
return ToInt8E(p.o)
}
// ToInt16 converts an interface to an int16 type.
func (p *Object) ToInt16() int16 {
if p == nil {
return 0
}
v, _ := ToInt16E(p.o)
return v
}
// ToInt16E converts an interface to an int16 type.
func (p *Object) ToInt16E() (int16, error) {
if p == nil {
return 0, nil
}
return ToInt16E(p.o)
}
// ToInt32 converts an interface to an int32 type.
func (p *Object) ToInt32() int32 {
if p == nil {
return 0
}
v, _ := ToInt32E(p.o)
return v
}
// ToInt32E converts an interface to an int32 type.
func (p *Object) ToInt32E() (int32, error) {
if p == nil {
return 0, nil
}
return ToInt32E(p.o)
}
// ToInt64 converts an interface to an int64 type.
func (p *Object) ToInt64() int64 {
if p == nil {
return 0
}
v, _ := ToInt64E(p.o)
return v
}
// ToInt64E converts an interface to an int64 type.
func (p *Object) ToInt64E() (int64, error) {
if p == nil {
return 0, nil
}
return ToInt64E(p.o)
}
// ToUint converts an interface to a uint type.
func (p *Object) ToUint() uint {
if p == nil {
return 0
}
v, _ := ToUintE(p.o)
return v
}
// ToUintE converts an interface to a uint type.
func (p *Object) ToUintE() (uint, error) {
if p == nil {
return 0, nil
}
return ToUintE(p.o)
}
// ToUint8 converts an interface to a uint8 type.
func (p *Object) ToUint8() uint8 {
if p == nil {
return 0
}
v, _ := ToUint8E(p.o)
return v
}
// ToUint8E converts an interface to a uint8 type.
func (p *Object) ToUint8E() (uint8, error) {
if p == nil {
return 0, nil
}
return ToUint8E(p.o)
}
// ToUint16 converts an interface to a uint16 type.
func (p *Object) ToUint16() uint16 {
if p == nil {
return 0
}
v, _ := ToUint16E(p.o)
return v
}
// ToUint16E converts an interface to a uint16 type.
func (p *Object) ToUint16E() (uint16, error) {
if p == nil {
return 0, nil
}
return ToUint16E(p.o)
}
// ToUint32 converts an interface to a uint32 type.
func (p *Object) ToUint32() uint32 {
if p == nil {
return 0
}
v, _ := ToUint32E(p.o)
return v
}
// ToUint32E converts an interface to a uint32 type.
func (p *Object) ToUint32E() (uint32, error) {
if p == nil {
return 0, nil
}
return ToUint32E(p.o)
}
// ToUint64 converts an interface to a uint64 type.
func (p *Object) ToUint64() uint64 {
if p == nil {
return 0
}
v, _ := ToUint64E(p.o)
return v
}
// ToUint64E converts an interface to a uint64 type.
func (p *Object) ToUint64E() (uint64, error) {
if p == nil {
return 0, nil
}
return ToUint64E(p.o)
}
// String related
//--------------------------------------------------------------------------------------------------
// ToStr converts object into a *Str
func (p *Object) ToStr() *Str {
if p == nil {
return ToStr(p)
}
return ToStr(p.o)
}
// ToString converts an interface to a string type.
func (p *Object) ToString() string {
if p == nil {
return ""
}
return p.String()
}
// ToStringE converts an interface to a string type.
func (p *Object) ToStringE() (string, error) {
if p == nil {
return "", nil
}
return p.ToString(), nil
}
// Map related
//--------------------------------------------------------------------------------------------------
// ToStringMap converts an interface to a *StringMap type.
func (p *Object) ToStringMap() *StringMap {
if p == nil {
return NewStringMapV()
}
return ToStringMap(p.o)
}
// ToStringMapE converts an interface to a *StringMap type.
func (p *Object) ToStringMapE() (*StringMap, error) {
if p == nil {
return NewStringMapV(), nil
}
return ToStringMapE(p.o)
}
// ToStringMapG converts an interface to a map[string]interface{} type.
func (p *Object) ToStringMapG() map[string]interface{} {
if p == nil {
return map[string]interface{}{}
}
v, _ := ToStringMapE(p.o)
return v.G()
}
// ToStringMapGE converts an interface to a map[string]interface{} type.
func (p *Object) ToStringMapGE() (map[string]interface{}, error) {
result := map[string]interface{}{}
if p == nil {
return result, nil
}
v, err := ToStringMapE(p.o)
if err != nil {
return result, err
}
return v.G(), err
}
// Slice related
//--------------------------------------------------------------------------------------------------
// ToSliceOfMap converts an interface to a *SliceOfMap type.
func (p *Object) ToSliceOfMap() *SliceOfMap {
if p == nil {
return NewSliceOfMapV()
}
return ToSliceOfMap(p.o)
}
// ToSliceOfMapE converts an interface to a *SliceOfMap type.
func (p *Object) ToSliceOfMapE() (*SliceOfMap, error) {
if p == nil {
return NewSliceOfMapV(), nil
}
return ToSliceOfMapE(p.o)
}
// ToSliceOfMapG converts an interface to a []map[string]interface{} type.
func (p *Object) ToSliceOfMapG() []map[string]interface{} {
if p == nil {
return NewSliceOfMapV().G()
}
return ToSliceOfMap(p.o).G()
}
// ToSliceOfMapGE converts an interface to a []map[string]interface{} type.
func (p *Object) ToSliceOfMapGE() ([]map[string]interface{}, error) {
if p == nil {
return NewSliceOfMapV().G(), nil
}
m, err := ToSliceOfMapE(p.o)
if err != nil {
return nil, err
}
return m.G(), nil
}
// ToStringSlice converts an interface to a *StringSlice type.
func (p *Object) ToStringSlice() *StringSlice {
if p == nil {
return NewStringSliceV()
}
return ToStringSlice(p.o)
}
// ToStringSliceE converts an interface to a *StringSlice type.
func (p *Object) ToStringSliceE() (*StringSlice, error) {
if p == nil {
return NewStringSliceV(), nil
}
return ToStringSliceE(p.o)
}
// ToStrs converts an interface to a []string type.
func (p *Object) ToStrs() []string {
return p.ToStringSlice().G()
}
// ToStrsE converts an interface to a []string type.
func (p *Object) ToStrsE() ([]string, error) {
result, err := p.ToStringSliceE()
if err != nil {
return []string{}, err
}
return result.G(), nil
}
// ToIntSlice converts an interface to a *IntSlice type.
func (p *Object) ToIntSlice() *IntSlice {
if p == nil {
return NewIntSliceV()
}
v, _ := ToIntSliceE(p.o)
return v
}
// ToIntSliceE converts an interface to a *IntSlice type.
func (p *Object) ToIntSliceE() (*IntSlice, error) {
if p == nil {
return NewIntSliceV(), nil
}
return ToIntSliceE(p.o)
}
// ToIntSliceG converts an interface to a []int type.
func (p *Object) ToIntSliceG() []int {
result := []int{}
if p == nil {
return result
}
v, err := ToIntSliceE(p.o)
if err != nil {
return result
}
return v.G()
}
// ToIntSliceGE converts an interface to a []int type.
func (p *Object) ToIntSliceGE() ([]int, error) {
result := []int{}
if p == nil {
return result, nil
}
v, err := ToIntSliceE(p.o)
if err != nil {
return result, err
}
return v.G(), err
}