-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_parser.go
48 lines (41 loc) · 896 Bytes
/
ast_parser.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
package reader
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"github.com/bannzai/constructor/structure"
)
func parseASTFile(filePath structure.Path) *ast.File {
buf, err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}
fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, filePath, buf, 0)
if err != nil {
panic(err)
}
return astFile
}
func parseASTStructs(file *ast.File) (typeNameAndStruct map[string]*ast.StructType) {
typeNameAndStruct = map[string]*ast.StructType{}
ast.Inspect(file, func(node ast.Node) bool {
lastChildNode := node == nil
if lastChildNode {
return false
}
typeSpec, ok := node.(*ast.TypeSpec)
if !ok {
return true
}
name := typeSpec.Name.Name
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
return true
}
typeNameAndStruct[name] = structType
return true
})
return
}