-
Notifications
You must be signed in to change notification settings - Fork 93
/
type_explorer.go
52 lines (41 loc) · 1.05 KB
/
type_explorer.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
package main
import (
"fmt"
"go/ast"
)
type TypeExplorer struct {
TypeName string
Methods []string
IsInterface bool
}
func NewTypeExplorer(pkg *ast.Package, typeName string) *TypeExplorer {
explorer := &TypeExplorer{
TypeName: typeName,
}
ast.Walk(explorer, pkg)
return explorer
}
func (explorer *TypeExplorer) Visit(node ast.Node) ast.Visitor {
if n, ok := node.(*ast.FuncDecl); ok && n.Recv != nil {
receiver := getIdentName(n.Recv.List[0].Type)
if receiver == explorer.TypeName {
method := fmt.Sprintf("%s(%v)", getIdentName(n.Name), getIdentName(n.Recv.List[0].Type))
explorer.Methods = append(explorer.Methods, method)
}
}
return explorer
}
func (explorer *TypeExplorer) HasEquals() bool {
return explorer.HasMethod(fmt.Sprintf("Equals(%s)", explorer.TypeName))
}
func (explorer *TypeExplorer) HasString() bool {
return explorer.HasMethod("String()")
}
func (explorer *TypeExplorer) HasMethod(lookingFor string) bool {
for _, method := range explorer.Methods {
if method == lookingFor {
return true
}
}
return false
}