-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
57 lines (49 loc) · 1.28 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
package loader
import (
"fmt"
"go/types"
"strings"
"golang.org/x/tools/go/packages"
)
func LoadPackage(wd string, patterns ...string) (*packages.Package, error) {
cfg := &packages.Config{
Dir: wd,
Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedName,
}
if packages, err := packages.Load(cfg, patterns...); err != nil {
return nil, err
} else if len(packages) != 1 {
return nil, fmt.Errorf("cannot identify a unique package in %s", wd)
} else {
return packages[0], nil
}
}
func LoadPackageScope(wd string, patterns ...string) (*types.Scope, error) {
if pkg, err := LoadPackage(wd, patterns...); err != nil {
return nil, err
} else {
return pkg.Types.Scope(), nil
}
}
func GetTypeDeclaration(typeName types.Type) *TypeDecl {
return getTypeDeclarationFromString(typeName.String())
}
func getTypeDeclarationFromString(strVal string) *TypeDecl {
var isPointer bool
if strings.HasPrefix(strVal, "*") {
isPointer = true
strVal = strVal[1:]
}
if typeSeparator := strings.LastIndexByte(strVal, '.'); typeSeparator == -1 {
return &TypeDecl{
typeName: strVal,
isPointer: isPointer,
}
} else {
return &TypeDecl{
pkgImport: strVal[:typeSeparator],
typeName: strVal[typeSeparator+1:],
isPointer: isPointer,
}
}
}