/
util.go
881 lines (805 loc) · 18.4 KB
/
util.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
package common
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"os"
"os/signal"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"unsafe"
)
// ExtractRefTuple 抽取反射的val:ValueOf ,ind:Indirect,typ:ind.Type
func ExtractRefTuple(obj interface{}) (val reflect.Value, ind reflect.Value, typ reflect.Type) {
val = reflect.ValueOf(obj)
ind = reflect.Indirect(val)
typ = ind.Type()
return
}
func getFieldType(structObj interface{}, fieldIndex int) reflect.Type {
val := reflect.Indirect(reflect.ValueOf(structObj))
return val.Field(fieldIndex).Type()
}
// GetFieldType 取得structObje的指定字段的类型
func GetFieldType(structObj interface{}, fieldIndex int) reflect.Type {
return getFieldType(structObj, fieldIndex)
}
// GetFirstFieldType 取得structObj的第一个字段的类型
func GetFirstFieldType(structObj interface{}) reflect.Type {
return getFieldType(structObj, 0)
}
// Shutdownhook 停止hook
type Shutdownhook struct {
ch chan os.Signal //接收信号的channel
hooks []func() //停机时需要调用的方法列表
sync.Mutex //同步锁
stop int32
osStop int32
}
// NewShutdownhook 创建一个Shutdownhook,sig是要监听的信号,默认会监听syscall.SIGINT,syscall.SIGTERM
func NewShutdownhook(sig ...os.Signal) *Shutdownhook {
if len(sig) == 0 {
sig = []os.Signal{syscall.SIGINT, syscall.SIGTERM}
}
ch := make(chan os.Signal, len(sig))
signal.Notify(ch, sig...)
return &Shutdownhook{ch: ch}
}
func (p *Shutdownhook) getCh() chan os.Signal {
p.Lock()
defer p.Unlock()
return p.ch
}
// AddHook 增加一个Hook函数
func (p *Shutdownhook) AddHook(hookFunc func()) {
p.Lock()
defer p.Unlock()
p.hooks = append(p.hooks, hookFunc)
}
// WaitShutdown 等待进程退出的信号,当收到进程退出的信号后,依次执行注册的hook函数
func (p *Shutdownhook) WaitShutdown() {
ch := p.getCh()
if ch == nil {
return
}
if s, ok := <-ch; ok {
Infof("Receive signal:%v,Run hooks", s)
atomic.StoreInt32(&p.osStop, 1)
p.Stop()
} else {
Warnf("Receive signal error,%v", ok)
}
p.Lock()
defer p.Unlock()
Infof("begin run hooks")
for _, f := range p.hooks {
f()
}
Infof("finished run hooks")
if logger != nil {
logger.Sync()
}
}
// Stop the shutdownhook
func (p *Shutdownhook) Stop() {
p.Lock()
defer p.Unlock()
ch := p.ch
if ch != nil {
signal.Stop(p.ch)
close(p.ch)
}
p.ch = nil
p.stop = 1
}
// IsOSStop 是否OS触发的停止
func (p *Shutdownhook) IsOSStop() bool {
return atomic.LoadInt32(&p.osStop) == 1
}
// RandomUUID 生成随机的UUID
func RandomUUID() (uuid string) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
Errorf("UUID Err:%s", err)
return
}
b[6] = (b[6] & 0x0f) | 0x40 // Version 4
b[8] = (b[8] & 0x3f) | 0x80 // Variant is 10
uuid = fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
// FileBasename 确定不待扩展的文件名
func FileBasename(s string) string {
n := strings.LastIndexByte(s, '.')
if n > 0 {
return s[:n]
}
return s
}
// SplitTrimOmitEmpty 对str按sep分隔,去掉为空的项
func SplitTrimOmitEmpty(str, sep string) []string {
return TrimOmitEmpty(strings.Split(str, sep))
}
// TrimOmitEmpty 去掉为空的值
func TrimOmitEmpty(str []string) []string {
var ret = make([]string, 0, len(str))
for _, item := range str {
item = strings.TrimSpace(item)
if item != "" {
ret = append(ret, item)
}
}
return ret
}
// StringSlice string slice
type StringSlice []string
// ToInt 转为[]int
func (p StringSlice) ToInt() ([]int, error) {
if p == nil {
return nil, nil
}
ret := make([]int, 0, len(p))
for _, item := range p {
if val64, err := strconv.ParseInt(item, 10, 64); err == nil {
ret = append(ret, int(val64))
} else {
return nil, err
}
}
return ret, nil
}
//ToNumber 转为数字slice
func (p StringSlice) ToNumber(typ interface{}) (interface{}, error) {
sliceType := reflect.TypeOf(typ)
intSlice := reflect.MakeSlice(sliceType, 0, len(p))
sliceElemType := sliceType.Elem()
k := sliceElemType.Kind()
var toAdd = make([]reflect.Value, 0, len(p))
for _, item := range p {
v := reflect.New(sliceElemType)
indV := reflect.Indirect(v)
switch k {
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
if val64, err := strconv.ParseInt(item, 10, 64); err == nil {
indV.SetInt(val64)
toAdd = append(toAdd, indV)
} else {
return nil, err
}
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
if val64, err := strconv.ParseUint(item, 10, 64); err == nil {
indV.SetUint(val64)
toAdd = append(toAdd, indV)
} else {
return nil, err
}
case reflect.Float32:
fallthrough
case reflect.Float64:
if val64, err := strconv.ParseFloat(item, 64); err == nil {
indV.SetFloat(val64)
toAdd = append(toAdd, indV)
} else {
return nil, err
}
}
}
intSlice = reflect.Append(intSlice, toAdd...)
return intSlice.Interface(), nil
}
// ToInt8 转为[]int8
func (p StringSlice) ToInt8() ([]int8, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]int8{})
if err != nil {
return nil, err
}
return val.([]int8), nil
}
// ToInt16 转为[]int16
func (p StringSlice) ToInt16() ([]int16, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]int16{})
if err != nil {
return nil, err
}
return val.([]int16), nil
}
// ToInt32 转为[]int32
func (p StringSlice) ToInt32() ([]int32, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]int32{})
if err != nil {
return nil, err
}
return val.([]int32), nil
}
// ToInt64 转为[]int64
func (p StringSlice) ToInt64() ([]int64, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]int64{})
if err != nil {
return nil, err
}
return val.([]int64), nil
}
// ToUint 转为[]uint
func (p StringSlice) ToUint() ([]uint, error) {
if p == nil {
return nil, nil
}
ret := make([]uint, 0, len(p))
for _, item := range p {
if val64, err := strconv.ParseUint(item, 10, 64); err == nil {
ret = append(ret, uint(val64))
} else {
return nil, err
}
}
return ret, nil
}
// ToUint8 转为[]uint8
func (p StringSlice) ToUint8() ([]uint8, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]uint8{})
if err != nil {
return nil, err
}
return val.([]uint8), nil
}
// ToUint16 转为[]uint16
func (p StringSlice) ToUint16() ([]uint16, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]uint16{})
if err != nil {
return nil, err
}
return val.([]uint16), nil
}
// ToUint32 转为[]uint32
func (p StringSlice) ToUint32() ([]uint32, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]uint32{})
if err != nil {
return nil, err
}
return val.([]uint32), nil
}
// ToUint64 转为[]uint64
func (p StringSlice) ToUint64() ([]uint64, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]uint64{})
if err != nil {
return nil, err
}
return val.([]uint64), nil
}
// ToFloat32 转为[]float32
func (p StringSlice) ToFloat32() ([]float32, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]float32{})
if err != nil {
return nil, err
}
return val.([]float32), nil
}
// ToFloat64 转为[]floa64t
func (p StringSlice) ToFloat64() ([]float64, error) {
if p == nil {
return nil, nil
}
val, err := p.ToNumber([]float64{})
if err != nil {
return nil, err
}
return val.([]float64), nil
}
// ToInterface 转为interface slice
func (p StringSlice) ToInterface() []interface{} {
if len(p) == 0 {
return nil
}
val := make([]interface{}, 0, len(p))
for _, item := range p {
val = append(val, item)
}
return val
}
//Int64 转为int64类型
func Int64(p interface{}) (i int64, err error) {
switch v := p.(type) {
case int:
i = int64(v)
case int8:
i = int64(v)
case int16:
i = int64(v)
case int32:
i = int64(v)
case int64:
i = int64(v)
case float32:
i = int64(v)
case float64:
i = int64(v)
case string:
i, err = strconv.ParseInt(v, 10, 64)
case json.Number:
i, err = v.Int64()
default:
err = fmt.Errorf("unsupported type %T", p)
}
return
}
//Int 转为int
func Int(p interface{}) (i int, err error) {
switch v := p.(type) {
case int:
i = int(v)
case int8:
i = int(v)
case int16:
i = int(v)
case int32:
i = int(v)
case int64:
i = int(v)
case float32:
i = int(v)
case float64:
i = int(v)
case string:
var i64 int64
i64, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return
}
i = int(i64)
case json.Number:
var i64 int64
i64, err = v.Int64()
if err != nil {
return
}
i = int(i64)
default:
err = fmt.Errorf("unsupported type %T", p)
}
return
}
//Float64 转为float64类型
func Float64(p interface{}) (i float64, err error) {
switch v := p.(type) {
case int:
i = float64(v)
case int8:
i = float64(v)
case int16:
i = float64(v)
case int32:
i = float64(v)
case int64:
i = float64(v)
case float32:
i = float64(v)
case float64:
i = float64(v)
case string:
i, err = strconv.ParseFloat(v, 64)
case json.Number:
i, err = v.Float64()
default:
err = fmt.Errorf("unsupported type %T", p)
}
return
}
//Float32 转为float32
func Float32(p interface{}) (i float32, err error) {
switch v := p.(type) {
case int:
i = float32(v)
case int8:
i = float32(v)
case int16:
i = float32(v)
case int32:
i = float32(v)
case int64:
i = float32(v)
case float32:
i = float32(v)
case float64:
i = float32(v)
case string:
var f64 float64
f64, err = strconv.ParseFloat(v, 64)
if err != nil {
return
}
i = float32(f64)
case json.Number:
var f64 float64
f64, err = v.Float64()
if err != nil {
return
}
i = float32(f64)
default:
err = fmt.Errorf("unsupported type %T", p)
}
return
}
// IsEmpty 是否有空字符串
func IsEmpty(strs ...string) bool {
for _, str := range strs {
if str == "" {
return true
}
}
return false
}
// HasNil check does any args is nil
func HasNil(args ...interface{}) bool {
for _, arg := range args {
if arg == nil {
return true
}
var val = reflect.ValueOf(arg)
var k = val.Kind()
if (k == reflect.Ptr || k == reflect.Chan || k == reflect.Func || k == reflect.Map || k == reflect.Interface || k == reflect.Slice) &&
val.IsNil() {
return true
}
}
return false
}
// Fnv32Hashcode calculate abs hash code for data
func Fnv32Hashcode(data string) int {
hash := fnv.New32a()
hash.Write([]byte(data))
hashCode := int(hash.Sum32())
if hashCode < 0 {
hashCode = -hashCode
}
return hashCode
}
// MGet get value from m
func MGet(m map[string]interface{}, key string) interface{} {
if val, ok := m[key]; ok {
return val
}
return nil
}
// MGetInt32 get int32 from m
func MGetInt32(m map[string]interface{}, key string) (int32, error) {
data := MGet(m, key)
switch inst := data.(type) {
case int:
return int32(inst), nil
case int8:
return int32(inst), nil
case int16:
return int32(inst), nil
case int32:
return int32(inst), nil
case int64:
return int32(inst), nil
case float32:
return int32(inst), nil
case float64:
return int32(data.(float64)), nil
case json.Number:
v, err := inst.Int64()
if err != nil {
return 0, err
}
return int32(v), nil
default:
return 0, fmt.Errorf("invalid int32 type:%T", inst)
}
}
// MGetInt64 get int64 from m
func MGetInt64(m map[string]interface{}, key string) (int64, error) {
data := MGet(m, key)
switch inst := data.(type) {
case int:
return int64(inst), nil
case int8:
return int64(inst), nil
case int16:
return int64(inst), nil
case int32:
return int64(inst), nil
case int64:
return int64(inst), nil
case float32:
return int64(inst), nil
case float64:
return int64(inst), nil
case json.Number:
return inst.Int64()
default:
return 0, fmt.Errorf("invalid int64 type:%T", inst)
}
}
// MGetString get string from m
func MGetString(m map[string]interface{}, key string) (string, error) {
data := MGet(m, key)
switch data.(type) {
case string:
return data.(string), nil
}
return "", errors.New("invalid string type")
}
// FillSlice create slice
func FillSlice(num int, fillFunc func(index int)) {
for i := 0; i < num; i++ {
fillFunc(i)
}
}
// ToSlice to slice
func ToSlice(num int, fillFunc func(index int, dest []interface{})) []interface{} {
var ret = make([]interface{}, num)
for i := 0; i < num; i++ {
fillFunc(i, ret)
}
return ret
}
// ByteSlice2String convert []byte to string
func ByteSlice2String(bs []byte) (str string) {
return *(*string)(unsafe.Pointer(&bs))
}
// String2ByteSlice convert string to []byte
func String2ByteSlice(str string) (bs []byte) {
var bh reflect.SliceHeader
sh := (*reflect.StringHeader)(unsafe.Pointer(&str))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
bs = *(*[]byte)(unsafe.Pointer(&bh))
runtime.KeepAlive(&str)
return
}
// StackTrace record the stack trace
func StackTrace(all bool) string {
// Reserve 10K buffer at first
buf := make([]byte, 10240)
for {
size := runtime.Stack(buf, all)
// The size of the buffer may be not enough to hold the stacktrace,
// so double the buffer size
if size == len(buf) {
buf = make([]byte, len(buf)<<1)
continue
}
break
}
return string(buf)
}
// StructCopier struct结构拷贝
type StructCopier func(from interface{}, to interface{}) (err error)
// NewStructCopier 拷贝
func NewStructCopier(from interface{}, to interface{}) (copier StructCopier, err error) {
if from == nil || to == nil {
err = errors.New("from and to must not be nil")
return
}
fromVal, fromInd, fromTyp := ExtractRefTuple(from)
toVal, toInd, toTyp := ExtractRefTuple(to)
if fromVal.Kind() != reflect.Ptr || toVal.Kind() != reflect.Ptr {
err = errors.New("from and to must be pointer")
return
}
if fromInd.Kind() != reflect.Struct || toInd.Kind() != reflect.Struct {
err = errors.New("from and to must be struct")
return
}
var noCopy = func(field reflect.StructField) bool {
tag := field.Tag
_, exist := tag.Lookup("nocopy")
if exist {
return true
}
return false
}
var (
fromIndexes, toIndexes [][]int
)
var parseFields func(typ reflect.Type, baseFieldIndex []int) (err error)
parseFields = func(typ reflect.Type, baseFieldIndex []int) (err error) {
fieldCount := typ.NumField()
for i := 0; i < fieldCount; i++ {
field := typ.Field(i)
if field.Anonymous {
newIndex := make([]int, len(baseFieldIndex))
copy(newIndex, baseFieldIndex)
err = parseFields(field.Type, append(newIndex, field.Index...))
if err != nil {
return
}
continue
} else if field.PkgPath != "" {
continue
}
if noCopy(field) {
continue
}
name := field.Name
toField, found := toTyp.FieldByName(name)
if !found {
continue
}
if noCopy(toField) {
continue
}
newIndex := make([]int, len(baseFieldIndex))
copy(newIndex, baseFieldIndex)
fromIndex := append(newIndex, field.Index...)
toIndex := toField.Index
var typeMatch bool
if field.Type.AssignableTo(toField.Type) {
typeMatch = true
} else if field.Type.Kind() == toField.Type.Kind() && field.Type.ConvertibleTo(toField.Type) {
typeMatch = true
}
if !typeMatch {
err = fmt.Errorf("name %s can't assign %s to %s", name, field.Type, toField.Type)
return
}
fromIndexes = append(fromIndexes, fromIndex)
toIndexes = append(toIndexes, toIndex)
}
return
}
err = parseFields(fromTyp, nil)
if err != nil {
return
}
var (
fromValType = fromVal.Type()
fromValKind = fromVal.Kind()
toValType = toVal.Type()
toValKind = toVal.Kind()
)
copier = func(f, t interface{}) error {
if f == nil || t == nil {
return errors.New("from and to must not be nil")
}
fval, find, ftype := ExtractRefTuple(f)
tval, tind, ttype := ExtractRefTuple(t)
if ftype != fromTyp || fval.Kind() != fromValKind {
return fmt.Errorf("expect the type of from is %s,but is %s", fromValType, fval.Type())
}
if ttype != toTyp || tval.Kind() != toValKind {
return fmt.Errorf("expect the type of to is %s,but is %s", toValType, tval.Type())
}
for i := 0; i < len(fromIndexes); i++ {
fromVal := find.FieldByIndex(fromIndexes[i])
toVal := tind.FieldByIndex(toIndexes[i])
if fromVal.Type() != toVal.Type() {
toVal.Set(fromVal.Convert(toVal.Type()))
} else {
toVal.Set(fromVal)
}
}
return nil
}
return
}
type emptyInterface struct {
typ *struct{}
word *struct{}
}
type emptySliceInterface struct {
typ *struct{}
word *reflect.SliceHeader
}
// IsValNil 检查v的值是不是nil
func IsValNil(v interface{}) bool {
if v == nil {
return true
}
typ := reflect.TypeOf(v)
kind := typ.Kind()
if kind == reflect.Ptr {
ei := (*emptyInterface)(unsafe.Pointer(&v))
return ei.word == nil
} else if kind == reflect.Slice {
ei := (*emptySliceInterface)(unsafe.Pointer(&v))
return ei.word.Data == 0
} else {
switch kind {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Interface:
return reflect.ValueOf(v).IsNil()
}
}
return false
}
// IntSlice int slice
type IntSlice []int
// ToInterface 转为interface slice
func (p IntSlice) ToInterface() []interface{} {
if len(p) == 0 {
return nil
}
val := make([]interface{}, 0, len(p))
for _, item := range p {
val = append(val, item)
}
return val
}
func (p IntSlice) Len() int { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Int64Slice int64 slice
type Int64Slice []int64
// ToInterface 转为interface slice
func (p Int64Slice) ToInterface() []interface{} {
if len(p) == 0 {
return nil
}
val := make([]interface{}, 0, len(p))
for _, item := range p {
val = append(val, item)
}
return val
}
func (p Int64Slice) Len() int { return len(p) }
func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Int32Slice int32 slice
type Int32Slice []int32
// ToInterface 转为interface slice
func (p Int32Slice) ToInterface() []interface{} {
if len(p) == 0 {
return nil
}
val := make([]interface{}, 0, len(p))
for _, item := range p {
val = append(val, item)
}
return val
}
func (p Int32Slice) Len() int { return len(p) }
func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }