-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
103 lines (83 loc) · 2.35 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
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
package container
import (
"log"
"reflect"
)
func isInvocable(typ reflect.Type) (bool, string) {
kind := typ.Kind()
if kind == reflect.Ptr {
typ = indirectType(typ)
kind = typ.Kind()
}
if kind == reflect.Struct {
return true, "struct"
}
if kind == reflect.Func {
return true, "func"
}
return false, ""
}
func indirectType(typ reflect.Type) reflect.Type {
switch typ.Kind() {
case reflect.Ptr, reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
return typ.Elem()
}
return typ
}
// getType - Allows us to get the type if it's not already a type. If it is a type...
// return it, just prevents us constantly doing .TypeOf() or this everywhere
func getType(t any) reflect.Type {
if _, ok := t.(reflect.Type); ok {
return t.(reflect.Type)
}
if _, ok := t.(reflect.Value); ok {
return t.(reflect.Value).Type()
}
return reflect.TypeOf(t)
}
// getVal - Allows us to get the val if it's not already a value. If it is a value...
// return it, just prevents us constantly doing .ValueOf() or this everywhere
func getVal(t any) reflect.Value {
if _, ok := t.(reflect.Value); ok {
return t.(reflect.Value)
}
return reflect.ValueOf(t)
}
// getAbstractReturnType - Convenience function, if our type is a pointer, we'll get the underlying type
// If it's also not an interface, nil will be returned
func getAbstractReturnType(abstractType reflect.Type) reflect.Type {
var abstract = abstractType
if abstractType.Kind() == reflect.Ptr {
abstract = abstractType.Elem()
}
if abstract.Kind() != reflect.Interface {
return nil
}
return abstract
}
// getConcreteReturnType - Allows us to pass a function and get it's first
// return arg or pass a struct and get the type of that
func getConcreteReturnType(concrete reflect.Type) reflect.Type {
returnType := concrete
if concrete.Kind() == reflect.Struct {
return returnType
}
if concrete.Kind() == reflect.Func {
numOut := concrete.NumOut()
if numOut == 0 {
log.Printf("Trying to get function return type for binding but it doesnt have a return type...")
return nil
}
if numOut > 1 {
log.Printf("Getting a function return type, but the function has > 1 return args. Only the first arg is handled.")
}
returnType = concrete.Out(0)
}
if returnType.Kind() == reflect.Pointer {
return indirectType(returnType)
}
if returnType != nil {
return returnType
}
return nil
}