forked from XSAM/optionGen
-
Notifications
You must be signed in to change notification settings - Fork 4
/
template.go
253 lines (227 loc) · 8.86 KB
/
template.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package optiongen
const templateTextWithPreviousSupport = `
{{- range $_, $comment := $.ClassComments }}
{{ $comment }}
{{- end }}
// {{ $.ClassName }} should use {{ $.ClassNewFuncName }} to initialize it
type {{ $.ClassName }} struct {
{{- range $index, $option := $.ClassOptionInfo }}
{{- range $_, $comment := $option.LastRowComments }}
{{ unescaped $comment }}
{{- end }}
{{- if $option.Inline }}
{{ $option.Type }} {{unescaped $option.TagString}} {{ unescaped $option.SameRowComment }}
{{- else }}
{{ $option.Name }} {{ $option.Type }} {{unescaped $option.TagString}} {{ unescaped $option.SameRowComment }}
{{- end }}
{{- end }}
}
// {{ $.ClassNewFuncName }} new {{ $.ClassName }}
{{ $.ClassNewFuncSignature }} {
cc := newDefault{{ $.ClassNameTitle }}()
{{- range $index, $option := $.ClassOptionInfo }}
{{- if eq $option.ArgIndex 0}}
{{- else}}
cc.{{$option.Name}} = {{$option.NameAsParameter}}
{{- end}}
{{- end }}
for _, opt := range opts {
opt.Apply(cc)
}
if watchDog{{$.ClassNameTitle}} != nil {
watchDog{{$.ClassNameTitle}}(cc)
}
return cc
}
{{- if $.OptionReturnPrevious }}
// ApplyOption apply multiple new option and return the old ones
// sample:
// old := cc.ApplyOption(WithTimeout(time.Second))
// defer cc.ApplyOption(old...)
func (cc *{{ $.ClassName }}) ApplyOption(opts... {{$.ClassOptionTypeName }}) []{{$.ClassOptionTypeName }}{
var previous []{{$.ClassOptionTypeName }}
for _, opt := range opts {
previous = append(previous,opt.Apply(cc))
}
return previous
}
{{- else}}
// ApplyOption apply multiple new option
func (cc *{{ $.ClassName }}) ApplyOption(opts... {{$.ClassOptionTypeName }}){
for _, opt := range opts {
opt.Apply(cc)
}
}
{{- end }}
// {{ $.ClassOptionFunctionTypeName }} option func
{{- if $.OptionReturnPrevious }}
type {{ $.ClassOptionTypeName }} interface {
Apply(cc *{{$.ClassName}}) {{ $.ClassOptionTypeName }}
}
var _ {{ $.ClassOptionTypeName }} = {{ $.ClassOptionFunctionTypeName }}(nil)
type {{ $.ClassOptionFunctionTypeName }} func(cc *{{$.ClassName}}) {{ $.ClassOptionFunctionTypeName }}
func (f {{ $.ClassOptionFunctionTypeName }}) Apply(cc *{{$.ClassName}}) {{ $.ClassOptionTypeName }} {
return f(cc)
}
{{- else}}
type {{ $.ClassOptionTypeName }} interface {
Apply(cc *{{$.ClassName}})
}
var _ {{ $.ClassOptionTypeName }} = {{ $.ClassOptionFunctionTypeName }}(nil)
type {{ $.ClassOptionFunctionTypeName }} func(cc *{{$.ClassName}})
func (f {{ $.ClassOptionFunctionTypeName }}) Apply(cc *{{$.ClassName}}) {
f(cc)
}
{{- end }}
{{ range $index, $option := $.ClassOptionInfo }}
{{- if eq $option.GenOptionFunc true }}
{{- if eq $option.Slice true }}
{{- if not $option.OnlyAppend }}
{{- if $.OptionReturnPrevious }}
{{ unescaped $option.OptionComment }}
func {{$option.OptionFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) {{ $.ClassOptionFunctionTypeName }} {
previous := cc.{{$option.Name}}
cc.{{$option.Name}} = v
return {{$option.OptionFuncName}}(previous...)
} }
{{- else }}
{{ unescaped $option.OptionComment }}
func {{$option.OptionFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) {
cc.{{$option.Name}} = v
} }
{{- end }}
{{- end }}
{{ unescaped $option.AppendComment }}
{{- if $.OptionReturnPrevious }}
func {{$option.AppendFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) {{ $.ClassOptionFunctionTypeName }} {
previous := cc.{{$option.Name}}
cc.{{$option.Name}} = append(cc.{{$option.Name}}, v...)
{{- $OptionFuncName := $option.OptionFuncName}}
{{- if $option.OnlyAppend }}
{{- $OptionFuncName = $option.AppendFuncName}}
{{- end }}
return {{$OptionFuncName}}(previous...)
} }
{{- else }}
func {{$option.AppendFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) {
cc.{{$option.Name}} = append(cc.{{$option.Name}}, v...)
} }
{{- end }}
{{- else }}
{{ unescaped $option.OptionComment }}
{{- if $.OptionReturnPrevious }}
func {{$option.OptionFuncName}}(v {{$option.Type}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) {{ $.ClassOptionFunctionTypeName }} {
previous := cc.{{$option.Name}}
cc.{{$option.Name}} = v
return {{$option.OptionFuncName}}(previous)
} }
{{- else }}
func {{$option.OptionFuncName}}(v {{$option.Type}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) {
cc.{{$option.Name}} = v
} }
{{- end }}
{{- end }}
{{- end }}
{{ end }}
// Install{{$.ClassNameTitle}}WatchDog the installed func will called when {{ $.ClassNewFuncName }} called
func Install{{$.ClassNameTitle}}WatchDog(dog func(cc *{{$.ClassName}})) { watchDog{{$.ClassNameTitle}} = dog }
// watchDog{{$.ClassNameTitle}} global watch dog
var watchDog{{$.ClassNameTitle}} func(cc *{{$.ClassName}})
// set{{ $.ClassNameTitle }}DefaultValue default {{ $.ClassName }} value
func set{{ $.ClassNameTitle }}DefaultValue (cc *{{ $.ClassName }}) {
{{- range $index, $option := $.ClassOptionInfo }}
{{- if eq $option.GenOptionFunc false }}
{{- if eq $option.FieldType 0 }}
cc.{{$option.Name}} = {{ $option.Type }} {{ $option.Body}}
{{- else }}
cc.{{$option.Name}} = {{ $option.Body}}
{{- end }}
{{- end }}
{{- end }}
for _, opt := range [...]{{ $.ClassOptionFunctionTypeName }} {
{{- range $index, $option := $.ClassOptionInfo }}
{{- if eq $option.GenOptionFunc true }}
{{- if eq $option.Slice true }}
{{- $OptionFuncName := $option.OptionFuncName}}
{{- if $option.OnlyAppend }}
{{- $OptionFuncName = $option.AppendFuncName}}
{{- end }}
{{- if eq $option.FieldType 0 }}
{{$OptionFuncName}}({{ $option.Type }} {{ $option.Body}}...),
{{- else }}
{{$OptionFuncName}}({{ $option.Body}}...),
{{- end }}
{{- else }}
{{- if eq $option.FieldType 0 }}
{{$option.OptionFuncName}}({{ $option.Type }} {{ $option.Body}}),
{{- else }}
{{$option.OptionFuncName}}({{ $option.Body}}),
{{- end }}
{{- end }}
{{- end }}
{{- end }}
} {
opt(cc)
}
}
// newDefault{{ $.ClassNameTitle }} new default {{ $.ClassName }}
func newDefault{{ $.ClassNameTitle }} () *{{ $.ClassName }} {
cc := &{{ $.ClassName }}{}
set{{ $.ClassNameTitle }}DefaultValue(cc)
return cc
}
{{- if $.XConf }}
// AtomicSetFunc used for XConf
func (cc *{{ $.ClassName }}) AtomicSetFunc() func(interface{}) { return Atomic{{ $.ClassNameTitle }}Set }
// atomic{{ $.ClassName }} global *{{ $.ClassName }} holder
var atomic{{ $.ClassNameTitle }} unsafe.Pointer
// onAtomic{{ $.ClassNameTitle }}Set global call back when Atomic{{ $.ClassNameTitle }}Set called by XConf.
// use {{ $.InterfaceName }}.ApplyOption to modify the updated cc
// if passed in cc not valid, then return false, cc will not set to atomic{{ $.ClassNameTitle }}
var onAtomic{{ $.ClassNameTitle }}Set func(cc {{ $.InterfaceName }}) bool
// InstallCallbackOnAtomic{{ $.ClassNameTitle }}Set install callback
func InstallCallbackOnAtomic{{ $.ClassNameTitle }}Set(callback func(cc {{ $.InterfaceName }}) bool) { onAtomic{{ $.ClassNameTitle }}Set = callback}
// Atomic{{ $.ClassNameTitle }}Set atomic setter for *{{ $.ClassName }}
func Atomic{{ $.ClassNameTitle }}Set(update interface{}) {
cc := update.(*{{ $.ClassName }})
if onAtomic{{ $.ClassNameTitle }}Set != nil && !onAtomic{{ $.ClassNameTitle }}Set(cc) {
return
}
atomic.StorePointer(&atomic{{ $.ClassNameTitle }}, (unsafe.Pointer)(cc))
}
// Atomic{{ $.ClassNameTitle }} return atomic *{{ $.VisitorName }}
func Atomic{{ $.ClassNameTitle }}() {{ $.VisitorName }} {
current := (*{{ $.ClassName }})(atomic.LoadPointer(&atomic{{ $.ClassNameTitle }}))
if current == nil {
defaultOne := newDefault{{ $.ClassNameTitle }}()
if watchDog{{$.ClassNameTitle}} != nil {
watchDog{{$.ClassNameTitle}}(defaultOne)
}
atomic.CompareAndSwapPointer(&atomic{{ $.ClassNameTitle }}, nil, (unsafe.Pointer)(defaultOne))
return (*{{ $.ClassName }})(atomic.LoadPointer(&atomic{{ $.ClassNameTitle }}))
}
return current
}
{{- end}}
// all getter func
{{- range $index, $option := $.ClassOptionInfo }}{{- if eq $option.GenVisitFunc true }}{{ unescaped $option.VisitFuncComment }}
func (cc *{{ $.ClassName }}) {{$option.VisitFuncName}}() {{ $option.VisitFuncReturnType }} { return cc.{{$option.Name}} }
{{- end }}
{{- end }}
// {{ $.VisitorName }} visitor interface for {{ $.ClassName }}
type {{ $.VisitorName }} interface {
{{- range $index, $option := $.ClassOptionInfo }}{{- if eq $option.GenVisitFunc true }}{{ unescaped $option.VisitFuncComment }}
{{$option.VisitFuncName}}() {{ $option.VisitFuncReturnType }}
{{- end }}
{{- end }}
}
// {{ $.InterfaceName }} visitor + ApplyOption interface for {{ $.ClassName }}
type {{ $.InterfaceName }} interface {
{{ $.VisitorName }}
{{- if $.OptionReturnPrevious }}
ApplyOption(... {{$.ClassOptionTypeName }}) []{{$.ClassOptionTypeName }}
{{- else }}
ApplyOption(... {{$.ClassOptionTypeName }})
{{- end }}
}
`