-
Notifications
You must be signed in to change notification settings - Fork 7
/
initialize.go
308 lines (289 loc) · 5.44 KB
/
initialize.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
package ameda
import (
"reflect"
)
// InitPointer initializes nil pointer with zero value.
func InitPointer(v reflect.Value) (done bool) {
for {
kind := v.Kind()
if kind == reflect.Interface {
v = v.Elem()
continue
}
if kind != reflect.Ptr {
return true
}
u := v.Elem()
if u.IsValid() {
v = u
continue
}
if !v.CanSet() {
return false
}
v2 := reflect.New(v.Type().Elem())
v.Set(v2)
v = v.Elem()
}
}
// InitString initializes empty string pointer with def.
func InitString(p *string, def string) (done bool) {
if p == nil {
return false
}
if *p == "" {
*p = def
}
return true
}
// InitBool initializes false bool pointer with def.
func InitBool(p *bool, def bool) (done bool) {
if p == nil {
return false
}
if *p == false {
*p = def
}
return true
}
// InitByte initializes zero byte pointer with def.
func InitByte(p *byte, def byte) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitInt initializes zero int pointer with def.
func InitInt(p *int, def int) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitInt8 initializes zero int8 pointer with def.
func InitInt8(p *int8, def int8) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitInt16 initializes zero int16 pointer with def.
func InitInt16(p *int16, def int16) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitInt32 initializes zero int32 pointer with def.
func InitInt32(p *int32, def int32) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitInt64 initializes zero int64 pointer with def.
func InitInt64(p *int64, def int64) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitUint initializes zero uint pointer with def.
func InitUint(p *uint, def uint) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitUint8 initializes zero uint8 pointer with def.
func InitUint8(p *uint8, def uint8) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitUint16 initializes zero uint16 pointer with def.
func InitUint16(p *uint16, def uint16) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitUint32 initializes zero uint32 pointer with def.
func InitUint32(p *uint32, def uint32) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitUint64 initializes zero uint64 pointer with def.
func InitUint64(p *uint64, def uint64) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitFloat32 initializes zero float32 pointer with def.
func InitFloat32(p *float32, def float32) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitFloat64 initializes zero float64 pointer with def.
func InitFloat64(p *float64, def float64) (done bool) {
if p == nil {
return false
}
if *p == 0 {
*p = def
}
return true
}
// InitSampleValue initialize the given type with some non-zero value( "?", $max_number, 0.1, true)
func InitSampleValue(t reflect.Type, maxNestingDeep int) reflect.Value {
if maxNestingDeep <= 0 {
maxNestingDeep = 10
}
ptrDepth := 0
for t.Kind() == reflect.Ptr {
t = t.Elem()
ptrDepth++
}
v := reflect.New(t)
v = initValue(v, 1, maxNestingDeep)
return ReferenceValue(v, ptrDepth-1)
}
func initValue(v reflect.Value, curDeep int, maxDeep int) reflect.Value {
InitPointer(v)
if curDeep >= maxDeep {
return v
}
var numPtr int
for v.Kind() == reflect.Ptr {
v = v.Elem()
numPtr++
}
if v.CanSet() {
switch v.Kind() {
case reflect.Struct:
curDeep++
fieldNum := v.Type().NumField()
for i := 0; i < fieldNum; i++ {
e := v.Field(i)
InitPointer(e)
if e.CanSet() {
e.Set(initValue(e, curDeep, maxDeep))
}
}
case reflect.Slice:
if v.Len() == 0 {
e := reflect.New(v.Type().Elem())
InitPointer(e)
e = e.Elem()
e = initValue(e, curDeep, maxDeep)
if v.CanSet() {
v.Set(reflect.Append(v, e))
}
}
case reflect.Array:
if v.Len() > 0 {
e := reflect.New(v.Type().Elem())
InitPointer(e)
e = e.Elem()
e = initValue(e, curDeep, maxDeep)
vv := v.Index(0)
if vv.CanSet() {
vv.Set(reflect.Append(v, e))
}
}
case reflect.Map:
if v.Len() == 0 {
v.Set(reflect.MakeMap(v.Type()))
k := reflect.New(v.Type().Key())
InitPointer(k)
k = k.Elem()
k = initValue(k, curDeep, maxDeep)
e := reflect.New(v.Type().Elem())
InitPointer(e)
e = e.Elem()
e = initValue(e, curDeep, maxDeep)
v.SetMapIndex(k, e)
}
case reflect.Int:
if Host32bit {
v.SetInt(-32)
} else {
v.SetInt(-64)
}
case reflect.Int8:
v.SetInt(-8)
case reflect.Int16:
v.SetInt(-16)
case reflect.Int32:
v.SetInt(-32)
case reflect.Int64:
v.SetInt(-64)
case reflect.Uint, reflect.Uintptr:
if Host32bit {
v.SetUint(32)
} else {
v.SetUint(64)
}
case reflect.Uint8:
v.SetUint(8)
case reflect.Uint16:
v.SetUint(16)
case reflect.Uint32:
v.SetUint(32)
case reflect.Uint64:
v.SetUint(64)
case reflect.Float32:
v.SetFloat(-0.32)
case reflect.Float64:
v.SetFloat(-0.64)
case reflect.Bool:
v.SetBool(true)
case reflect.String:
v.SetString("?")
default:
}
}
return ReferenceValue(v, numPtr)
}