-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
92 lines (79 loc) · 1.44 KB
/
file.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
package annotation
import (
"go/ast"
)
type SingleFileEntryVisitor struct {
file *fileVisitor
}
func (s *SingleFileEntryVisitor) Get() File {
if s.file != nil {
return s.file.Get()
}
return File{}
}
func (s *SingleFileEntryVisitor) Visit(node ast.Node) ast.Visitor {
file, ok := node.(*ast.File)
if ok {
s.file = &fileVisitor{
ans: newAnnotations(file, file.Doc),
}
return s.file
}
return s
}
type fileVisitor struct {
ans annotations
types []*typeVisitor
visited bool
}
func (f *fileVisitor) Get() File {
types := make([]Type, 0, len(f.types))
for _, t := range f.types {
types = append(types, t.Get())
}
return File{
annotations: f.ans,
Types: types,
}
}
func (f *fileVisitor) Visit(node ast.Node) ast.Visitor {
typ, ok := node.(*ast.TypeSpec)
if ok {
res := &typeVisitor{
ans: newAnnotations(typ, typ.Doc),
fields: make([]Field, 0, 0),
}
f.types = append(f.types, res)
return res
}
return f
}
type File struct {
annotations
Types []Type
}
type typeVisitor struct {
ans annotations
fields []Field
}
func (t *typeVisitor) Get() Type {
return Type{
annotations: t.ans,
Fields: t.fields,
}
}
func (t *typeVisitor) Visit(node ast.Node) (w ast.Visitor) {
fd, ok := node.(*ast.Field)
if ok {
t.fields = append(t.fields, Field{annotations: newAnnotations(fd, fd.Doc)})
return nil
}
return t
}
type Type struct {
annotations
Fields []Field
}
type Field struct {
annotations
}