-
Notifications
You must be signed in to change notification settings - Fork 5
/
parseref.go
147 lines (121 loc) · 3.14 KB
/
parseref.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package requestgen
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strconv"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
type TypeSelector struct {
Package string
Member string
IsSlice bool
}
func loadPackageFast(pattern string, tags []string) (*packages.Package, error) {
cfg := &packages.Config{
Mode: packages.NeedName |
packages.NeedImports |
packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedModule |
packages.NeedDeps,
Tests: false,
Logf: log.Debugf,
}
if len(tags) > 0 {
cfg.BuildFlags = []string{fmt.Sprintf("-tags=%s", strings.Join(tags, " "))}
}
log.Debugf("loading package: %s", pattern)
pkgs, err := packages.Load(cfg, pattern)
if err != nil {
return nil, err
}
if len(pkgs) == 0 {
return nil, nil
}
log.Debugf("loaded package: %s (pkgPath %s) -> %#v", pkgs[0].Name, pkgs[0].PkgPath, pkgs[0])
return pkgs[0], nil
}
func sanitizeImport(ts *TypeSelector) (*TypeSelector, error) {
log.Debugf("sanitizing import: %#v", ts)
pkg, err := loadPackageFast(ts.Package, nil)
if err != nil {
return nil, err
}
origPath := ts.Package
ts.Package = pkg.PkgPath
log.Debugf("sanitized import: %s => %s", origPath, ts.Package)
return ts, nil
}
func ParseTypeSelector(expr string) (*TypeSelector, error) {
if len(expr) == 0 {
return nil, errors.New("empty expression")
}
var spec TypeSelector
// dot references the current package
if expr[0] == '.' {
expr = `"."` + expr
} else if strings.HasPrefix(expr, ".[]") {
expr = expr[3:]
spec.IsSlice = true
} else if strings.HasPrefix(expr, "[]") {
expr = expr[2:]
spec.IsSlice = true
}
e, err := parser.ParseExpr(expr)
if err != nil {
return nil, errors.Wrapf(err, "invalid expression: %s", expr)
}
switch e := e.(type) {
case *ast.Ident:
spec.Package = "."
spec.Member = e.Name
// return spec
return &spec, nil
// return sanitizeImport(&spec)
case *ast.SelectorExpr:
x := unparen(e.X)
// Strip off star constructor, if any.
if star, ok := x.(*ast.StarExpr); ok {
x = star.X
}
if pkg := parseImportPath(x); pkg != "" {
// package member e.g. "encoding/json".HTMLEscape
spec.Package = pkg // e.g. "encoding/json"
spec.Member = e.Sel.Name // e.g. "HTMLEscape"
return sanitizeImport(&spec)
}
if x, ok := x.(*ast.SelectorExpr); ok {
// field/method of type e.g. ("encoding/json".Decoder).Decode
y := unparen(x.X)
if pkg := parseImportPath(y); pkg != "" {
spec.Package = pkg // e.g. "encoding/json"
spec.Member = x.Sel.Name // e.g. "Decoder"
return sanitizeImport(&spec)
}
}
default:
return nil, fmt.Errorf("expression is not an ident, selector expr or slice expr, %+v given", e)
}
return nil, fmt.Errorf("can not parse type selector: %s", expr)
}
func unparen(e ast.Expr) ast.Expr {
return astutil.Unparen(e)
}
func parseImportPath(e ast.Expr) string {
switch e := e.(type) {
case *ast.Ident:
return e.Name // e.g. bytes
case *ast.BasicLit:
if e.Kind == token.STRING {
pkgname, _ := strconv.Unquote(e.Value)
return pkgname // e.g. "encoding/json"
}
}
return ""
}