-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
104 lines (85 loc) · 1.57 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
104
package gocoder
import (
"go/ast"
"os/exec"
"strings"
)
func astTypeToStringType(typ ast.Node) string {
exprStr := ""
selector := 0
mapChar := ""
ast.Inspect(typ, func(n ast.Node) bool {
switch ex := n.(type) {
case *ast.SelectorExpr:
{
selector += 1
}
case *ast.Ident:
exprStr += ex.Name
if selector > 0 {
exprStr += "."
selector--
}
if len(mapChar) > 0 {
exprStr += mapChar
mapChar = ""
}
case *ast.InterfaceType:
exprStr += "interface{}"
case *ast.MapType:
exprStr += "map["
mapChar = "]"
case *ast.StarExpr:
exprStr += "*"
case *ast.ArrayType:
exprStr += "[]"
case *ast.Ellipsis:
exprStr += "..."
}
return true
})
return exprStr
}
func isFieldTypeOf(field *ast.Field, strType string) bool {
return astTypeToStringType(field.Type) == strType
}
func isCallingFuncOf(expr interface{}, name string) bool {
switch ex := (expr).(type) {
case *ast.CallExpr:
{
switch fn := ex.Fun.(type) {
case *ast.SelectorExpr:
{
return fn.Sel.Name == name
}
case *ast.Ident:
{
return fn.Name == name
}
}
}
}
return false
}
func trimStarExpr(expr ast.Expr) ast.Expr {
typeExpr := expr
for {
starExpr, isStar := typeExpr.(*ast.StarExpr)
if isStar {
typeExpr = starExpr.X
continue
}
break
}
return typeExpr
}
func execCommand(name string, args ...string) (result string, err error) {
var out []byte
out, err = exec.Command(name, args...).Output()
if err != nil {
return
}
result = string(out)
result = strings.TrimSuffix(result, "\n")
return
}