-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
65 lines (57 loc) · 1.48 KB
/
utils.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
package faker
import (
"io"
"reflect"
)
func UniqueSlice(a interface{}) interface{} {
aReflect := reflect.ValueOf(a)
aType := reflect.TypeOf(a)
aMap := map[interface{}]reflect.Value{}
if aReflect.Kind() != reflect.Slice {
return nil
}
cReflect := reflect.MakeSlice(aType, 0, 0)
for i := 0; i < aReflect.Len(); i++ {
if !aMap[aReflect.Index(i).Interface()].IsValid() {
aMap[aReflect.Index(i).Interface()] = aReflect.Index(i)
cReflect = reflect.Append(cReflect, aReflect.Index(i))
}
}
return cReflect.Interface()
}
func UniqueSliceStr(a []string) []string {
return UniqueSlice(a).([]string)
}
func MakeReflectNew(ref reflect.Value) interface{} {
switch ref.Kind() {
case reflect.String:
return ""
case reflect.Bool:
return false
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return 0
case reflect.Float32, reflect.Float64:
return 0.0
case reflect.Map:
return reflect.MakeMap(reflect.MapOf(ref.Type().Key(), ref.Type().Elem()))
case reflect.Slice:
return reflect.MakeSlice(reflect.SliceOf(ref.Type()), 0, ref.Len())
case reflect.Chan:
return reflect.MakeChan(ref.Type(), 0)
case reflect.Func:
return reflect.MakeFunc(ref.Type(), func([]reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(io.EOF)}
})
default:
//Uintptr
//Complex64
//Complex128
//Array
//Interface
//Ptr
//Struct
//UnsafePointer
return nil
}
}