generated from things-labs/cicd-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_field_slice.go
464 lines (403 loc) · 15.7 KB
/
struct_field_slice.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
package structs
import (
"reflect"
"strconv"
)
// IntField returns a slice of int. For more info refer to Slice types IntField() method.
func IntField(s interface{}, fieldName string) []int {
return NewStructSlice(s).IntField(fieldName)
}
// UintField returns a slice of uint. For more info refer to Slice types v() method.
func UintField(s interface{}, fieldName string) []uint {
return NewStructSlice(s).UintField(fieldName)
}
// Int64Field returns a slice of int64. For more info refer to Slice types Int64Field() method.
func Int64Field(s interface{}, fieldName string) []int64 {
return NewStructSlice(s).Int64Field(fieldName)
}
// Uint64Field returns a slice of uint64. For more info refer to Slice types Uint64Field() method.
func Uint64Field(s interface{}, fieldName string) []uint64 {
return NewStructSlice(s).Uint64Field(fieldName)
}
// StringField returns a slice of string. For more info refer to Slice types StringField() method.
func StringField(s interface{}, fieldName string) []string {
return NewStructSlice(s).StringField(fieldName)
}
// Int returns a slice of int. For more info refer to Slice types Int() method.
func Int(s interface{}) []int {
return NewStructSlice(s).IntSlice()
}
// Uint returns a slice of uint. For more info refer to Slice types Uint() method.
func Uint(s interface{}) []uint {
return NewStructSlice(s).Uint()
}
// Int8 returns a slice of int8. For more info refer to Slice types Int8() method.
func Int8(s interface{}) []int8 {
return NewStructSlice(s).Int8()
}
// Uint8 returns a slice of uint8. For more info refer to Slice types Uint8() method.
func Uint8(s interface{}) []uint8 {
return NewStructSlice(s).Uint8()
}
// Int16 returns a slice of int16. For more info refer to Slice types Int16() method.
func Int16(s interface{}) []int16 {
return NewStructSlice(s).Int16()
}
// Uint16 returns a slice of uint16. For more info refer to Slice types Uint16() method.
func Uint16(s interface{}) []uint16 {
return NewStructSlice(s).Uint16()
}
// Int32 returns a slice of int32. For more info refer to Slice types Int32() method.
func Int32(s interface{}) []int32 {
return NewStructSlice(s).Int32()
}
// Uint32 returns a slice of uint32. For more info refer to Slice types Uint32() method.
func Uint32(s interface{}) []uint32 {
return NewStructSlice(s).Uint32()
}
// Int64 returns a slice of int64. For more info refer to Slice types Int64() method.
func Int64(s interface{}) []int64 {
return NewStructSlice(s).Int64()
}
// Uint64 returns a slice of uint64. For more info refer to Slice types Uint64() method.
func Uint64(s interface{}) []uint64 {
return NewStructSlice(s).Uint64()
}
// String returns a slice of string. For more info refer to Slice types String() method.
func String(s interface{}) []string {
return NewStructSlice(s).String()
}
// StructSlice hold a struct slice reflect.value
type StructSlice struct {
value reflect.Value
}
// NewStructSlice returns a new *Slice with the slice s. It panics if the s's kind is not slice.
func NewStructSlice(s interface{}) *StructSlice {
v := reflect.Indirect(reflect.ValueOf(s))
if kind := v.Kind(); !(kind == reflect.Slice || kind == reflect.Array) {
panic("NewStructSlice: require a slice or array")
}
return &StructSlice{v}
}
// IntField extracts the given s slice's every element, which is struct, to []int by the field.
// It panics if the s's element is not struct, or field is not exits, or the value of field is not integer.
func (s *StructSlice) IntField(fieldName string) []int {
length := s.value.Len()
slice := make([]int, length)
for i := 0; i < length; i++ {
v := s.structFieldVal(i, fieldName)
slice[i] = int(valueInteger(v))
}
return slice
}
// UintField extracts the given s slice's every element, which is struct, to []uint by the field.
// It panics if the s's element is not struct, or field is not exits, or the value of field is not integer.
func (s *StructSlice) UintField(fieldName string) []uint {
length := s.value.Len()
slice := make([]uint, length)
for i := 0; i < length; i++ {
v := s.structFieldVal(i, fieldName)
slice[i] = uint(valueInteger(v))
}
return slice
}
// Int64Field extracts the given s slice's every element, which is struct, to []int64 by the field.
// It panics if the s's element is not struct, or field is not exits, or the value of field is not integer.
func (s *StructSlice) Int64Field(fieldName string) []int64 {
length := s.value.Len()
slice := make([]int64, length)
for i := 0; i < length; i++ {
v := s.structFieldVal(i, fieldName)
slice[i] = int64(valueInteger(v))
}
return slice
}
// Uint64Field extracts the given s slice's every element, which is struct, to []uint64 by the field.
// It panics if the s's element is not struct, or field is not exits, or the value of field is not integer.
func (s *StructSlice) Uint64Field(fieldName string) []uint64 {
length := s.value.Len()
slice := make([]uint64, length)
for i := 0; i < length; i++ {
v := s.structFieldVal(i, fieldName)
slice[i] = valueInteger(v)
}
return slice
}
// StringField extracts the given s slice's every element, which is struct, to []string by the field.
// It panics if the s's element is not struct, or field is not exits, or the value of field is not integer or string.
func (s *StructSlice) StringField(fieldName string) []string {
length := s.value.Len()
slice := make([]string, length)
for i := 0; i < length; i++ {
v := s.structFieldVal(i, fieldName)
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = strconv.FormatInt(v.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = strconv.FormatUint(v.Uint(), 10)
case reflect.String:
slice[i] = v.String()
case reflect.Float32:
slice[i] = strconv.FormatFloat(v.Float(), 'f', -1, 32)
case reflect.Float64:
slice[i] = strconv.FormatFloat(v.Float(), 'f', -1, 64)
default:
panic("StringField: the value of field is not integer or float or string.")
}
}
return slice
}
// Int extracts the given s slice's every element, which is integer or float, to []int by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) IntSlice() []int {
length := s.value.Len()
slice := make([]int, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = int(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = int(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = int(v.Uint())
default:
panic("Int: the value of field is not integer or float.")
}
}
return slice
}
// Uint extracts the given s slice's every element, which is integer or float, to []uint by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Uint() []uint {
length := s.value.Len()
slice := make([]uint, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = uint(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = uint(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = uint(v.Uint())
default:
panic("Uint: the value of field is not integer or float.")
}
}
return slice
}
// Int8 extracts the given s slice's every element, which is integer or float, to []int8 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Int8() []int8 {
length := s.value.Len()
slice := make([]int8, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = int8(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = int8(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = int8(v.Uint())
default:
panic("Int8: the value of field is not integer or float.")
}
}
return slice
}
// Uint8 extracts the given s slice's every element, which is integer or float, to []uint8 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Uint8() []uint8 {
length := s.value.Len()
slice := make([]uint8, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = uint8(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = uint8(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = uint8(v.Uint())
default:
panic("Uint8: the value of field is not integer or float.")
}
}
return slice
}
// Int16 extracts the given s slice's every element, which is integer or float, to []int16 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Int16() []int16 {
length := s.value.Len()
slice := make([]int16, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = int16(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = int16(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = int16(v.Uint())
default:
panic("Int16: the value of field is not integer or float.")
}
}
return slice
}
// Uint16 extracts the given s slice's every element, which is integer or float, to []uint16 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Uint16() []uint16 {
length := s.value.Len()
slice := make([]uint16, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = uint16(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = uint16(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = uint16(v.Uint())
default:
panic("Uint16: the value of field is not integer or float.")
}
}
return slice
}
// Int32 extracts the given s slice's every element, which is integer or float, to []int32 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Int32() []int32 {
length := s.value.Len()
slice := make([]int32, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = int32(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = int32(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = int32(v.Uint())
default:
panic("Int32: the value of field is not integer or float.")
}
}
return slice
}
// Uint32 extracts the given s slice's every element, which is integer or float, to []uint32 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Uint32() []uint32 {
length := s.value.Len()
slice := make([]uint32, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = uint32(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = uint32(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = uint32(v.Uint())
default:
panic("Uint32: the value of field is not integer or float.")
}
}
return slice
}
// Int64 extracts the given s slice's every element, which is integer or float, to []int64 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Int64() []int64 {
length := s.value.Len()
slice := make([]int64, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = v.Int()
case reflect.Float32, reflect.Float64:
slice[i] = int64(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = int64(v.Uint())
default:
panic("Int64: the value of field is not integer or float.")
}
}
return slice
}
// Uint64 extracts the given s slice's every element, which is integer or float, to []uint64 by the field.
// It panics if the s's element is not integer or float, or field is not invalid.
func (s *StructSlice) Uint64() []uint64 {
length := s.value.Len()
slice := make([]uint64, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = uint64(v.Int())
case reflect.Float32, reflect.Float64:
slice[i] = uint64(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = v.Uint()
default:
panic("Uint64: the value of field is not integer or float.")
}
}
return slice
}
// String extracts the given s slice's every element, which is integer or float or string, to []string by the field.
// It panics if the s's element is not integer or float, string, or field is not invalid.
func (s *StructSlice) String() []string {
length := s.value.Len()
slice := make([]string, length)
for i := 0; i < length; i++ {
v := reflect.Indirect(s.value.Index(i))
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
slice[i] = strconv.FormatInt(v.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
slice[i] = strconv.FormatUint(v.Uint(), 10)
case reflect.String:
slice[i] = v.String()
case reflect.Float32:
slice[i] = strconv.FormatFloat(v.Float(), 'f', -1, 32)
case reflect.Float64:
slice[i] = strconv.FormatFloat(v.Float(), 'f', -1, 64)
default:
panic("String: the value of field is not integer or float or string.")
}
}
return slice
}
func (s *StructSlice) structFieldVal(i int, fieldName string) reflect.Value {
val := s.value.Index(i)
val = reflect.Indirect(val)
// check is struct
if !(val.Kind() != reflect.Invalid && val.Kind() == reflect.Struct) {
panic("structFieldVal: the slice's element is not struct or pointer of struct!")
}
v := val.FieldByName(fieldName)
if !v.IsValid() {
panic("structFieldVal: the struct of slice's element has not the field:" + fieldName)
}
return v
}
// Name returns the slice's type name within its package. For more info refer
// to Name() function.
func (s *StructSlice) Name() string {
return s.value.Type().Name()
}
func valueInteger(v reflect.Value) uint64 {
switch v.Kind() { // nolint: exhaustive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return uint64(v.Int())
case reflect.Float32, reflect.Float64:
return uint64(v.Float())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint()
default:
panic("valueInteger: the value of field is not integer or float.")
}
}