-
Notifications
You must be signed in to change notification settings - Fork 15
/
object.go
79 lines (60 loc) · 1.39 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
package jsongo
import (
"errors"
"reflect"
"fmt"
)
type O map[string]interface{}
func Object() O {
return O{}
}
func (this O) Put(key string, value interface{}) O {
this[key] = value
return this
}
func (this O) Get(key string) interface{} {
return this[key]
}
func (this O) GetObject(key string) (value O, err error) {
switch this[key].(type) {
case map[string]interface{}:
object := Object()
for k,v := range this[key].(map[string]interface{}) {
object.Put(k,v)
}
return object, nil
case O:
return this[key].(O), nil
}
return nil, errors.New(fmt.Sprintf("Casting error. Interface is %s, not jsongo.object", reflect.TypeOf(this[key])))
}
func (this O) GetArray(key string) (newArray *A, err error) {
newArray = Array()
switch this[key].(type) {
case []interface{}:
values := this[key].([]interface{})
for _, value := range values {
newArray.Put(value)
}
return newArray, nil
case []string:
values := this[key].([]string)
for _, value := range values {
newArray.Put(value)
}
return newArray, nil
case *A:
return this[key].(*A), nil
}
return nil, errors.New(fmt.Sprintf("Casting error. Interface is %s, not jsongo.A or []interface{}", reflect.TypeOf(this[key])))
}
func (this O) Remove(key string) O {
delete(this, key)
return this
}
func (this O) Indent() string {
return indent(this)
}
func (this O) String() string {
return _string(this)
}