-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeutil.go
53 lines (51 loc) · 1.7 KB
/
typeutil.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
package selectivetesting
import "go/types"
func getUsedTypeNames(t types.Type) []*types.TypeName {
typeNames := make([]*types.TypeName, 0)
switch ct := t.(type) {
case *types.Array:
typeNames = append(typeNames, getUsedTypeNames(ct.Elem())...)
case *types.Basic:
break
case *types.Chan:
typeNames = append(typeNames, getUsedTypeNames(ct.Elem())...)
case *types.Interface:
for i := 0; i < ct.NumExplicitMethods(); i++ {
typeNames = append(typeNames, getUsedTypeNames(ct.ExplicitMethod(i).Type())...)
}
for i := 0; i < ct.NumEmbeddeds(); i++ {
typeNames = append(typeNames, getUsedTypeNames(ct.EmbeddedType(i))...)
}
case *types.Map:
typeNames = append(typeNames, getUsedTypeNames(ct.Key())...)
typeNames = append(typeNames, getUsedTypeNames(ct.Elem())...)
case *types.Named:
if obj := ct.Obj(); obj != nil {
typeNames = append(typeNames, ct.Obj())
}
case *types.Pointer:
typeNames = append(typeNames, getUsedTypeNames(ct.Elem())...)
case *types.Signature:
typeNames = append(typeNames, getUsedTypeNames(ct.Params())...)
typeNames = append(typeNames, getUsedTypeNames(ct.Results())...)
case *types.Slice:
typeNames = append(typeNames, getUsedTypeNames(ct.Elem())...)
case *types.Struct:
for i := 0; i < ct.NumFields(); i++ {
typeNames = append(typeNames, getUsedTypeNames(ct.Field(i).Type())...)
}
case *types.Tuple:
for i := 0; i < ct.Len(); i++ {
typeNames = append(typeNames, getUsedTypeNames(ct.At(i).Type())...)
}
case *types.TypeParam:
if obj := ct.Obj(); obj != nil {
typeNames = append(typeNames, ct.Obj())
}
case *types.Union:
for i := 0; i < ct.Len(); i++ {
typeNames = append(typeNames, getUsedTypeNames(ct.Term(i).Type())...)
}
}
return typeNames
}