-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathval.go
226 lines (202 loc) · 5.27 KB
/
val.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
package m2obj
import "reflect"
// IsGroup
//
// If this Object is maintaining a(n) Group, return true, or else return false.
func (o *Object) IsGroup() bool {
if o.val == nil {
return false
}
switch o.val.(type) {
case *groupData:
return true
default:
return false
}
}
// IsArray
//
// If this Object is maintaining a(n) Array, return true, or else return false.
func (o *Object) IsArray() bool {
if o.val == nil {
return false
}
switch o.val.(type) {
case *arrayData:
return true
default:
return false
}
}
// IsValue
//
// If this Object is maintaining a(n) value neither Group nor Array, return true, or else return false.
func (o *Object) IsValue() bool {
if o.val == nil {
return false
}
return !o.IsGroup() && !o.IsArray()
}
// IsNil
//
// If this Object is not maintaining anything, return true, or else return false.
func (o *Object) IsNil() bool {
return o.val == nil
}
// Is
//
// If this Object is maintaining anything with type ty, return true, or else return false.
func (o *Object) Is(ty reflect.Type) bool {
return reflect.TypeOf(o.val) == ty
}
// IsLike
//
// If this Object is maintaining anything with the same type of v, return true, or else return false.
func (o *Object) IsLike(v interface{}) bool {
tv := getDeepestValue(v)
return reflect.TypeOf(o.val) == reflect.TypeOf(tv)
}
// SetVal
//
// Change the Object's val, no type stipulation to value, like New
func (o *Object) SetVal(value interface{}) {
o.val = getDeepestValue(value)
o.buildParentLink(o.Parent())
o.callOnChange()
}
func (o *Object) setVal(value interface{}, needCallOnChange bool) {
o.val = getDeepestValue(value)
if needCallOnChange {
o.callOnChange()
}
}
// Val
//
// Get the inner value of an Object
func (o *Object) Val() interface{} {
return o.val
}
// ValStr
//
// Get the inner value of an Object, and assert it is or transform it to a `string`.
func (o *Object) ValStr() string {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(""))
return v.String()
}
// ValBool
//
// Get the inner value of an Object, and assert it is or transform it to a `bool`.
func (o *Object) ValBool() bool {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(true))
return v.Bool()
}
// ValByte
//
// Get the inner value of an Object, and assert it is or transform it to a `byte`.
func (o *Object) ValByte() byte {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(byte(1)))
return v.Interface().(byte)
}
// ValBytes
//
// Get the inner value of an Object, and assert it is or transform it to a `[]byte`.
func (o *Object) ValBytes() []byte {
v := reflect.ValueOf(o.val)
// 针对rune转[]byte做特殊处理
if v.Type() == reflect.TypeOf(' ') {
return New(o.ValStr()).ValBytes()
}
// 针对[]rune转[]byte做特殊处理
if v.Type() == reflect.TypeOf([]rune{}) {
return New(o.ValStr()).ValBytes()
}
v = v.Convert(reflect.TypeOf([]byte{}))
return v.Bytes()
}
// ValRune
//
// Get the inner value of an Object, and assert it is or transform it to an `rune`.
func (o *Object) ValRune() rune {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(' '))
return v.Interface().(rune)
}
// ValRunes
//
// Get the inner value of an Object, and assert it is or transform it to an `[]rune`.
func (o *Object) ValRunes() []rune {
v := reflect.ValueOf(o.val)
// 针对[]byte转[]rune做特殊处理
if v.Type() == reflect.TypeOf([]byte{}) {
return New(o.ValStr()).ValRunes()
}
v = v.Convert(reflect.TypeOf([]rune{}))
return v.Interface().([]rune)
}
// ValInt
//
// Get the inner value of an Object, and assert it is or transform it to an `int`.
func (o *Object) ValInt() int {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(1))
return v.Interface().(int)
}
// ValInt8
//
// Get the inner value of an Object, and assert it is or transform it to an `int8`.
func (o *Object) ValInt8() int8 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(int8(1)))
return v.Interface().(int8)
}
// ValInt16
//
// Get the inner value of an Object, and assert it is or transform it to an `int16`.
func (o *Object) ValInt16() int16 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(int16(1)))
return v.Interface().(int16)
}
// ValInt32
//
// Get the inner value of an Object, and assert it is or transform it to an `int32`.
func (o *Object) ValInt32() int32 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(int32(1)))
return v.Interface().(int32)
}
// ValInt64
//
// Get the inner value of an Object, and assert it is or transform it to an `int64`.
func (o *Object) ValInt64() int64 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(int64(1)))
return v.Int()
}
// ValUint
//
// Get the inner value of an Object, and assert it is or transform it to an `uint64`.
func (o *Object) ValUint() uint64 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(uint64(1)))
return v.Uint()
}
// ValFloat32
//
// Get the inner value of an Object, and assert it is or transform it to a `float32`.
func (o *Object) ValFloat32() float32 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(float32(1)))
return v.Interface().(float32)
}
// ValFloat64
//
// Get the inner value of an Object, and assert it is or transform it to a `float64`.
func (o *Object) ValFloat64() float64 {
v := reflect.ValueOf(o.val)
v = v.Convert(reflect.TypeOf(float64(1)))
return v.Float()
}