/
ast.go
104 lines (101 loc) · 2.85 KB
/
ast.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
package fake
import (
"fmt"
"go/ast"
"strings"
)
func (f *ParsedInterface) printAstExpr(expr ast.Expr) string {
file := f.ParsedFile
gen := file.Generator
// Extract package and type name
switch fieldType := expr.(type) {
case *ast.Ident:
if strings.ToLower(fieldType.Name[:1]) == fieldType.Name[:1] {
return fieldType.Name
}
// If it's a generic type, we don't need to print package name with it.
for idx, name := range f.GenericsNames {
if name == fieldType.Name {
if len(f.TranslateGenericNames) > 0 {
return f.TranslateGenericNames[idx]
}
return fieldType.Name
}
}
// If we have an object, that means we need to translate the type from mock package to current package.
file.Imports[file.PkgName] = &PackageInfo{
Name: file.PkgName,
ImportPath: file.PkgPath,
}
file.UsedImports[file.PkgName] = struct{}{}
return fmt.Sprintf("%s.%s", file.PkgName, fieldType.Name)
case *ast.SelectorExpr:
// Type from another package
return fmt.Sprintf("%s.%s", fieldType.X, fieldType.Sel)
case *ast.StarExpr:
return fmt.Sprintf("*%s", f.printAstExpr(fieldType.X))
case *ast.ArrayType:
return fmt.Sprintf("[]%s", f.printAstExpr(fieldType.Elt))
case *ast.Ellipsis:
return fmt.Sprintf("...%s", f.printAstExpr(fieldType.Elt))
case *ast.ChanType:
switch fieldType.Dir {
case ast.RECV:
return fmt.Sprintf("<-chan %s", f.printAstExpr(fieldType.Value))
case ast.SEND:
return fmt.Sprintf("chan<-%s", f.printAstExpr(fieldType.Value))
case ast.SEND | ast.RECV:
return fmt.Sprintf("chan %s", f.printAstExpr(fieldType.Value))
}
case *ast.MapType:
return fmt.Sprintf("map[%s]%s", f.printAstExpr(fieldType.Key), f.printAstExpr(fieldType.Value))
case *ast.FuncType:
b := &strings.Builder{}
f.PrintMethodHeader(b, "func", &ParsedField{
Interface: f,
Ref: &ast.Field{
Type: expr,
},
Name: "func",
})
return b.String()
case *ast.InterfaceType:
if fieldType.Methods.NumFields() == 0 {
return "interface{}"
}
methods := gen.ListInterfaceFields(&ParsedInterface{
Ref: fieldType,
}, file.Imports)
b := &strings.Builder{}
for _, method := range methods {
methodName := method.Ref.Names[0].Name
b.WriteString("\t\t")
f.PrintMethodHeader(b, methodName, &ParsedField{
Interface: f,
Ref: &ast.Field{
Type: method.Ref.Type.(*ast.FuncType),
},
Name: methodName,
})
}
return fmt.Sprintf("interface{\n%s\n}", b.String())
}
return ""
}
func getAstTypeName(expr ast.Expr) string {
switch fieldType := expr.(type) {
case *ast.Ident:
return fieldType.Name
case *ast.SelectorExpr:
return fmt.Sprintf("%s", fieldType.X)
case *ast.StarExpr:
return getAstTypeName(fieldType.X)
case *ast.ArrayType:
return getAstTypeName(fieldType.Elt)
case *ast.Ellipsis:
return getAstTypeName(fieldType.Elt)
case *ast.ChanType:
return getAstTypeName(fieldType.Value)
}
return ""
}