generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
visit.go
35 lines (33 loc) · 1.05 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
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
})
}
// 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 _, 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
})
}