-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser_loader.go
More file actions
161 lines (148 loc) · 3.7 KB
/
parser_loader.go
File metadata and controls
161 lines (148 loc) · 3.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package inspector
import (
"go/types"
"strings"
"golang.org/x/tools/go/loader"
)
// Parse package.
func (c *Compiler) parsePkg(pkg *loader.PackageInfo) error {
for _, scope := range pkg.Info.Scopes {
// Only scopes without parents available.
if parent := scope.Parent(); parent != nil {
for _, name := range parent.Names() {
// Get the object representation of scope.
o := parent.Lookup(name)
if !c.isExported(o) {
continue
}
// Get type of object and parse it.
t := o.Type()
node, err := c.parsePkgType(t)
if err != nil {
return err
}
if node == nil || node.typ == typeBasic || reMap.MatchString(node.typn) || reSlc.MatchString(node.typn) {
continue
}
if node.typ == typeStruct {
// Make type name of struct node actual.
node.typn = o.Name()
}
node.pkg = o.Pkg().Name()
node.name = o.Name()
// Check and skip type if it is already parsed or blacklisted.
if _, ok := c.uniq[node.name]; ok {
continue
}
if len(c.bl) > 0 {
if _, ok := c.bl[node.name]; ok {
continue
}
}
c.uniq[node.name] = struct{}{}
c.nodes = append(c.nodes, node)
}
}
}
return nil
}
// Main parse method.
func (c *Compiler) parsePkgType(t types.Type) (*node, error) {
// Prepare and fill up default node.
node := &node{
typ: typeBasic,
typn: strings.Replace(t.String(), c.pkgDot, "", 1),
ptr: false,
}
if n, ok := t.(*types.Named); ok {
node.typn = n.Obj().Name()
if n.Obj().Pkg() == nil {
return nil, nil
}
node.pkg = n.Obj().Pkg().Name()
node.pkgi = c.clearPkg(n.Obj().Pkg().Path())
}
// Get the underlying type.
u := t.Underlying()
// Common skips considering by underlying type.
if _, ok := u.(*types.Interface); ok {
return nil, nil
}
if _, ok := u.(*types.Signature); ok {
return nil, nil
}
if p, ok := u.(*types.Pointer); ok {
// Dereference pointer type and go inside to parse.
e := p.Elem()
u = e.Underlying()
unode, err := c.parsePkgType(u)
if err != nil {
return node, err
}
if unode == nil {
return nil, nil
}
unode.ptr = true
if n, ok := e.(*types.Named); ok {
unode.typn = n.Obj().Name()
unode.pkg = n.Obj().Pkg().Name()
unode.pkgi = c.clearPkg(n.Obj().Pkg().Path())
}
return unode, err
}
if s, ok := u.(*types.Struct); ok {
// Walk over fields in struct and parse each of them as separate node.
node.typ = typeStruct
for i := 0; i < s.NumFields(); i++ {
f := s.Field(i)
if !f.Exported() {
continue
}
ch, err := c.parsePkgType(f.Type())
if err != nil {
return node, err
}
if ch == nil {
continue
}
ch.name = f.Name()
if ch.ptr {
ch.typn = strings.Replace(f.Type().String(), c.pkgDot, "", 1)
ch.typn = strings.Replace(ch.typn, "*", "", 1)
}
node.chld = append(node.chld, ch)
node.hasb = node.hasb || ch.hasb
node.hasc = node.hasc || ch.hasc
node.hasa = node.hasa || ch.hasa
}
return node, nil
}
if m, ok := u.(*types.Map); ok {
// Just parse key and value types of map.
var err error
node.typ = typeMap
node.mapk, err = c.parsePkgType(m.Key())
node.mapv, err = c.parsePkgType(m.Elem())
node.hasb = node.mapk.hasb || node.mapv.hasb
node.hasa = node.mapk.hasa || node.mapv.hasa
node.hasc = true
return node, err
}
if s, ok := u.(*types.Slice); ok {
// Just parse value type of slice.
var err error
node.typ = typeSlice
node.slct, err = c.parsePkgType(s.Elem())
node.hasb = node.typn == "[]byte" || node.slct.hasb
node.hasa = true
node.hasc = true
return node, err
}
if b, ok := u.(*types.Basic); ok {
// Fill up the underlying type of basic node.
node.typu = b.Name()
node.hasb = node.typu == "string"
node.hasc = node.hasb
}
return node, nil
}