-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisitor.go
More file actions
102 lines (90 loc) · 2.75 KB
/
visitor.go
File metadata and controls
102 lines (90 loc) · 2.75 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
package jsonforms
// Visitor defines the interface for visiting UI schema elements
type Visitor interface {
VisitControl(*Control) error
VisitVerticalLayout(*VerticalLayout) error
VisitHorizontalLayout(*HorizontalLayout) error
VisitGroup(*Group) error
VisitCategorization(*Categorization) error
VisitCategory(*Category) error
VisitLabel(*Label) error
VisitCustomElement(*CustomElement) error
}
// Walk traverses a UI schema element tree and calls the appropriate visitor methods
func Walk(element UISchemaElement, visitor Visitor) error {
if element == nil {
return nil
}
switch e := element.(type) {
case *Control:
return visitor.VisitControl(e)
case *VerticalLayout:
if err := visitor.VisitVerticalLayout(e); err != nil {
return err
}
for _, child := range e.Elements {
if err := Walk(child, visitor); err != nil {
return err
}
}
case *HorizontalLayout:
if err := visitor.VisitHorizontalLayout(e); err != nil {
return err
}
for _, child := range e.Elements {
if err := Walk(child, visitor); err != nil {
return err
}
}
case *Group:
if err := visitor.VisitGroup(e); err != nil {
return err
}
for _, child := range e.Elements {
if err := Walk(child, visitor); err != nil {
return err
}
}
case *Categorization:
if err := visitor.VisitCategorization(e); err != nil {
return err
}
for _, child := range e.Elements {
if err := Walk(child, visitor); err != nil {
return err
}
}
case *Category:
if err := visitor.VisitCategory(e); err != nil {
return err
}
for _, child := range e.Elements {
if err := Walk(child, visitor); err != nil {
return err
}
}
case *Label:
return visitor.VisitLabel(e)
case *CustomElement:
if err := visitor.VisitCustomElement(e); err != nil {
return err
}
for _, child := range e.Elements {
if err := Walk(child, visitor); err != nil {
return err
}
}
}
return nil
}
// BaseVisitor provides default no-op implementations for all visitor methods
// This allows users to embed BaseVisitor and only override methods they care about
type BaseVisitor struct{}
func (b *BaseVisitor) VisitControl(*Control) error { return nil }
func (b *BaseVisitor) VisitVerticalLayout(*VerticalLayout) error { return nil }
func (b *BaseVisitor) VisitHorizontalLayout(*HorizontalLayout) error { return nil }
func (b *BaseVisitor) VisitGroup(*Group) error { return nil }
func (b *BaseVisitor) VisitCategorization(*Categorization) error { return nil }
func (b *BaseVisitor) VisitCategory(*Category) error { return nil }
func (b *BaseVisitor) VisitLabel(*Label) error { return nil }
func (b *BaseVisitor) VisitCustomElement(*CustomElement) error { return nil }