-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.go
159 lines (137 loc) · 3.38 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
package go_ohm
import (
"reflect"
)
type abstractObject interface {
renderValue() error
}
const (
ObjectOpLoad uint = iota
ObjectOpSave
)
type object struct {
name string
op uint
anonymous bool
parent *compoundObject
*ObjectOptions
// Reflected concrete type of the object. If original reflected type is
// multiple level Pointer or Interface (A.K.A. indirect), here stored the
// concrete type of the Pointer or Interface.
typ reflect.Type
// Reflected valid concrete value for the object. If typ is indirect, here
// stored the eventual valid value. EX:
//
// var a *int = nil
// var b **int = &a
// var c interface{} = &b
// var d *interface{} = &c
//
// Then *value is reflect.ValueOf(a), which type is *int, and indirect is 3.
//
// And sometimes value can be nil. EX, there are two structs and a variable:
//
// struct A{
// A int
// }
//
// struct B {
// B **A
// }
//
// var s struct B
//
// Now the object which represented s.B.A, has a nil value field.
value *reflect.Value
indirect int
abstractObject
}
var tagIdentifier = "go_ohm"
var rootObjectName = "__root_object"
func (o *object) isPlainObject() bool {
return (o.typ.Kind() != reflect.Struct && o.typ.Kind() != reflect.Map) ||
o.Json
}
func (o *object) isPromotedObject() bool {
return o.typ.Kind() == reflect.Struct && o.anonymous && !o.Json &&
o.Reference == "" && o.HashName == ""
}
func (o *object) createIndirectValues() {
createIndirectValues(o.value, o.indirect)
}
func isIgnoredType(typ reflect.Type) bool {
knd := typ.Kind()
return knd == reflect.Chan || knd == reflect.Func ||
knd == reflect.Invalid || knd == reflect.UnsafePointer ||
knd == reflect.Interface
}
func isPrimitiveType(typ reflect.Type) bool {
return (typ.Kind() == reflect.String) ||
(typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8) ||
(typ.Kind() >= reflect.Bool && typ.Kind() <= reflect.Complex128)
}
func advanceIndirectTypeAndValue(typ reflect.Type,
val *reflect.Value) (reflect.Type, *reflect.Value, int) {
if val != nil && val.IsValid() {
for val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
if val.IsZero() {
break
}
v := val.Elem()
val = &v
}
typ = val.Type()
}
indirect := 0
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
indirect++
}
return typ, val, indirect
}
func createIndirectValues(v *reflect.Value, indirect int) {
for i := 0; i < indirect; i++ {
t := v.Type().Elem()
p := reflect.New(t)
v.Set(p)
*v = v.Elem()
}
}
func newObject(name string, op uint, parent *compoundObject, opts *ObjectOptions,
typ reflect.Type, val *reflect.Value, indirect int, anon bool) (*object,
error) {
obj := &object{
name: name,
op: op,
anonymous: anon,
ObjectOptions: opts,
typ: typ,
value: val,
indirect: indirect,
parent: parent,
}
var err error
if obj.isPlainObject() {
_, err = newPlainObject(obj)
} else if typ.Kind() == reflect.Struct {
var co *compoundObject
co, err = newCompoundObject(obj)
if err != nil {
return nil, err
}
_, err = newStructObject(co)
} else if typ.Kind() == reflect.Map {
var co *compoundObject
co, err = newCompoundObject(obj)
if err != nil {
return nil, err
}
_, err = newMapObject(co)
} else {
err = newErrorUnsupportedObjectType(name)
}
if err != nil {
return nil, err
}
return obj, nil
}