forked from duanbiaowu/go-examples-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor.go
156 lines (129 loc) · 3.07 KB
/
visitor.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package k8s
import (
"encoding/json"
"encoding/xml"
"fmt"
)
// 1. Simple Visitor Example ----------------------------------------
type Visitor func(shape Shape) ([]byte, error)
type Shape interface {
accept(Visitor) ([]byte, error)
}
type Circle struct {
Radius int
}
type Rectangle struct {
Width int
Height int
}
func (c *Circle) accept(v Visitor) ([]byte, error) {
return v(c)
}
func (r *Rectangle) accept(v Visitor) ([]byte, error) {
return v(r)
}
func JsonVisitor(shape Shape) ([]byte, error) {
bytes, err := json.Marshal(shape)
if err != nil {
return nil, err
}
return bytes, nil
}
func XmlVisitor(shape Shape) ([]byte, error) {
bytes, err := xml.Marshal(shape)
if err != nil {
return nil, err
}
return bytes, nil
}
// 2. Simplified Kubectl Example ----------------------------------------
type VisitorFunc func(*Info, error) error
type VisitorCtl interface {
Visit(VisitorFunc) error
}
type Info struct {
Namespace string
Name string
OtherThings string
}
func (info *Info) Visit(fn VisitorFunc) error {
return fn(info, nil)
}
type NameVisitor struct {
visitor VisitorCtl
}
func (v NameVisitor) Visit(fn VisitorFunc) error {
return v.visitor.Visit(func(info *Info, err error) error {
fmt.Println("NameVisitor() before call function")
err = fn(info, err)
if err == nil {
fmt.Printf("==> Name=%s, NameSpace=%s\n", info.Name, info.Namespace)
}
fmt.Println("NameVisitor() after call function")
return err
})
}
type OtherThingsVisitor struct {
visitor VisitorCtl
}
func (v OtherThingsVisitor) Visit(fn VisitorFunc) error {
return v.visitor.Visit(func(info *Info, err error) error {
fmt.Println("OtherThingsVisitor() before call function")
err = fn(info, err)
if err == nil {
fmt.Printf("==> OtherThings=%s\n", info.OtherThings)
}
fmt.Println("OtherThingsVisitor() after call function")
return err
})
}
type LogVisitor struct {
visitor VisitorCtl
}
func (v LogVisitor) Visit(fn VisitorFunc) error {
return v.visitor.Visit(func(info *Info, err error) error {
fmt.Println("LogVisitor() before call function")
err = fn(info, err)
fmt.Println("LogVisitor() after call function")
return err
})
}
// 3. Refactor with DecoratedVisitor ----------------------------------------
type DecoratedVisitor struct {
visitor VisitorCtl
decorators []VisitorFunc
}
func NewDecoratedVisitorCtl(v VisitorCtl, fn ...VisitorFunc) VisitorCtl {
if len(fn) == 0 {
return v
}
return DecoratedVisitor{v, fn}
}
func (v DecoratedVisitor) Visit(fn VisitorFunc) error {
return v.visitor.Visit(func(info *Info, err error) error {
if err != nil {
return nil
}
if err = fn(info, nil); err != nil {
return err
}
for i := range v.decorators {
if err = v.decorators[i](info, nil); err != nil {
return err
}
}
return nil
})
}
func NameVisitorFun(info *Info, err error) error {
if err == nil {
fmt.Printf("==> Name=%s, NameSpace=%s\n", info.Name, info.Namespace)
}
return err
}
func OtherThingsVisitorFun(info *Info, err error) error {
if err == nil {
fmt.Printf("==> OtherThings=%s\n", info.OtherThings)
}
return err
}