-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
122 lines (111 loc) · 2.7 KB
/
helpers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package stroo
import (
"flag"
"fmt"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
"log"
"os"
)
// loads one package
func LoadPackage(path string) (*packages.Package, error) {
conf := packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax,
Tests: false, //TODO : decide if tests are useful
}
loadedPackage, err := packages.Load(&conf, path) //supports variadic multiple paths but we're using only one
if err != nil {
log.Printf("error loading package %q : %v\n", path, err)
return nil, err
}
allErrors := ""
n := 0
packages.Visit(loadedPackage, nil, func(pkg *packages.Package) {
for _, err := range pkg.Errors {
allErrors += err.Error() + "\n"
n++
}
})
switch n {
case 0:
default:
return nil, fmt.Errorf("%d error(s) encountered during load:\n%s", n, allErrors)
}
switch len(loadedPackage) {
case 0:
return nil, fmt.Errorf("%q matched no packages\n", path)
case 1:
// only allowed one
default:
for _, p := range loadedPackage {
log.Printf("Package: %q\n", p.ID)
}
return nil, fmt.Errorf("%d packages found. we're looking for exactly one", len(loadedPackage))
}
return loadedPackage[0], nil
}
// print the current configuration
func Print(analyzer *analysis.Analyzer, withRunningFolder bool) string {
result := ""
if withRunningFolder {
workingDir, err := os.Getwd()
if err != nil {
log.Fatalf("could NOT get working dir : %v", err)
}
result = fmt.Sprintf("running in folder %q\n", workingDir)
}
analyzer.Flags.VisitAll(func(f *flag.Flag) {
if !withRunningFolder {
result += "-"
}
result += f.Name + "=" + f.Value.String() + " "
})
return result
}
func IsBasic(kind string) bool {
for _, basic := range []string{
"unsafe.Pointer", "bool", "byte",
"complex64", "complex128",
"error",
"float32", "float64",
"int", "int8", "int16", "int32", "int64",
"rune", "string",
"uint", "uint8", "uint16", "uint32", "uint64", "uintptr"} {
if basic == kind {
return true
}
}
return false
}
func IsComplex(kind string) bool {
for _, basic := range []string{"complex64", "complex128"} {
if basic == kind {
return true
}
}
return false
}
func IsFloat(kind string) bool {
for _, basic := range []string{"float32", "float64"} {
if basic == kind {
return true
}
}
return false
}
func IsInt(kind string) bool {
for _, basic := range []string{"int", "int8", "int16", "int32", "int64"} {
if basic == kind {
return true
}
}
return false
}
func IsUint(kind string) bool {
for _, basic := range []string{"uint", "uint8", "uint16", "uint32", "uint64", "uintptr"} {
if basic == kind {
return true
}
}
return false
}