generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
visit.go
53 lines (50 loc) · 1.55 KB
/
visit.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
package schema
// Visit all nodes in the schema.
func Visit(n Node, visit func(n Node, next func() error) error) error {
return visit(n, func() error {
for _, child := range n.schemaChildren() {
if err := Visit(child, visit); err != nil {
return err
}
}
return nil
})
}
// VisitWithParent all nodes in the schema providing the parent node when visiting its schema children.
func VisitWithParent(n Node, parent Node, visit func(n Node, parent Node, next func() error) error) error {
return visit(n, parent, func() error {
for _, child := range n.schemaChildren() {
if err := VisitWithParent(child, n, visit); err != nil {
return err
}
}
return nil
})
}
// VisitExcludingMetadataChildren visits all nodes in the schema except the children of metadata nodes.
// This is used when generating external modules to avoid adding imports only referenced in the bodies of
// stubbed verbs.
func VisitExcludingMetadataChildren(n Node, visit func(n Node, next func() error) error) error {
return visit(n, func() error {
if d, ok := n.(Decl); ok {
if !d.IsExported() {
// Skip non-exported nodes
return nil
}
}
if _, ok := n.(Metadata); !ok {
for _, child := range n.schemaChildren() {
_, isParentVerb := n.(*Verb)
_, isChildUnit := child.(*Unit)
if isParentVerb && isChildUnit {
// Skip visiting children of a verb that are units as the scaffolded code will not inclue them
continue
}
if err := VisitExcludingMetadataChildren(child, visit); err != nil {
return err
}
}
}
return nil
})
}