forked from XSAM/optionGen
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gen_config_optiongen.go
277 lines (245 loc) · 10 KB
/
gen_config_optiongen.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package optiongen
import (
"sync/atomic"
"unsafe"
)
// Config should use NewTestConfig to initialize it
type Config struct {
OptionPrefix string `xconf:"option_prefix" usage:"option func name prefix, like: With, WithRedis"` // annotation@OptionPrefix(comment="option func name prefix, like: With, WithRedis")
OptionWithStructName bool `xconf:"option_with_struct_name" usage:"should the option func with struct name?"` // annotation@OptionWithStructName(comment="should the option func with struct name?")
OptionReturnPrevious bool `xconf:"option_return_previous" usage:"should option func return the previous ones?"` // annotation@OptionReturnPrevious(comment="should option func return the previous ones?")
NewFunc string `xconf:"new_func" usage:"new function name"` // annotation@NewFunc(comment="new function name")
NewFuncReturn string `xconf:"new_func_return" usage:"valid data: pointer,interface,visitor"` // annotation@NewFuncReturn(comment="valid data: pointer,interface,visitor")
Verbose bool `xconf:"v,deprecated" usage:"Deprecated: use --debug instead"` // annotation@Verbose(xconf="v",deprecated="use --debug instead")
UsageTagName string `xconf:"usage_tag_name" usage:"usage tag name,if not empty,will gen usage support for xconf/xflag"` // annotation@UsageTagName(comment="usage tag name,if not empty,will gen usage support for xconf/xflag")
EmptyCompositeNil bool `xconf:"empty_composite_nil" usage:"should empty slice or map to be nil? otherwise will be make(XXXX,0)"` // annotation@EmptyCompositeNil(comment="should empty slice or map to be nil? otherwise will be make(XXXX,0)")
Debug bool `xconf:"debug" usage:"debug will print more detail info"` // annotation@Debug(comment="debug will print more detail info")
XConf bool `xconf:"xconf" usage:"should gen xconf tag support?"` // annotation@XConf(xconf="xconf",comment="should gen xconf tag support?")
XConfTrimPrefix string `xconf:"x_conf_trim_prefix" usage:"if enable xconf tag, the tag value will trim prefix [XConfTrimPrefix]"` // annotation@XConfTrimPrefix(comment="if enable xconf tag, the tag value will trim prefix [XConfTrimPrefix]")
SliceOnlyAppend bool `xconf:"slice_only_append" usage:"slice only has append func, has not option func, like: With, WithRedis"` // annotation@SliceOnlyAppend(comment="slice only has append func, has not option func, like: With, WithRedis ")
}
// NewTestConfig new Config
func NewTestConfig(opts ...ConfigOption) *Config {
cc := newDefaultConfig()
for _, opt := range opts {
opt(cc)
}
if watchDogConfig != nil {
watchDogConfig(cc)
}
return cc
}
// ApplyOption apply multiple new option and return the old ones
// sample:
// old := cc.ApplyOption(WithTimeout(time.Second))
// defer cc.ApplyOption(old...)
func (cc *Config) ApplyOption(opts ...ConfigOption) []ConfigOption {
var previous []ConfigOption
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// ConfigOption option func
type ConfigOption func(cc *Config) ConfigOption
// WithOptionPrefix option func name prefix, like: With, WithRedis
func WithOptionPrefix(v string) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.OptionPrefix
cc.OptionPrefix = v
return WithOptionPrefix(previous)
}
}
// WithOptionWithStructName should the option func with struct name?
func WithOptionWithStructName(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.OptionWithStructName
cc.OptionWithStructName = v
return WithOptionWithStructName(previous)
}
}
// WithOptionReturnPrevious should option func return the previous ones?
func WithOptionReturnPrevious(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.OptionReturnPrevious
cc.OptionReturnPrevious = v
return WithOptionReturnPrevious(previous)
}
}
// WithNewFunc new function name
func WithNewFunc(v string) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.NewFunc
cc.NewFunc = v
return WithNewFunc(previous)
}
}
// WithNewFuncReturn valid data: pointer,interface,visitor
func WithNewFuncReturn(v string) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.NewFuncReturn
cc.NewFuncReturn = v
return WithNewFuncReturn(previous)
}
}
// WithVerbose option func for filed Verbose
//
// Deprecated: use --debug instead
func WithVerbose(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.Verbose
cc.Verbose = v
return WithVerbose(previous)
}
}
// WithUsageTagName usage tag name,if not empty,will gen usage support for xconf/xflag
func WithUsageTagName(v string) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.UsageTagName
cc.UsageTagName = v
return WithUsageTagName(previous)
}
}
// WithEmptyCompositeNil should empty slice or map to be nil? otherwise will be make(XXXX,0)
func WithEmptyCompositeNil(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.EmptyCompositeNil
cc.EmptyCompositeNil = v
return WithEmptyCompositeNil(previous)
}
}
// WithDebug debug will print more detail info
func WithDebug(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.Debug
cc.Debug = v
return WithDebug(previous)
}
}
// WithXConf should gen xconf tag support?
func WithXConf(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.XConf
cc.XConf = v
return WithXConf(previous)
}
}
// WithXConfTrimPrefix if enable xconf tag, the tag value will trim prefix [XConfTrimPrefix]
func WithXConfTrimPrefix(v string) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.XConfTrimPrefix
cc.XConfTrimPrefix = v
return WithXConfTrimPrefix(previous)
}
}
// WithSliceOnlyAppend slice only has append func, has not option func, like: With, WithRedis
func WithSliceOnlyAppend(v bool) ConfigOption {
return func(cc *Config) ConfigOption {
previous := cc.SliceOnlyAppend
cc.SliceOnlyAppend = v
return WithSliceOnlyAppend(previous)
}
}
// InstallConfigWatchDog the installed func will called when NewTestConfig called
func InstallConfigWatchDog(dog func(cc *Config)) { watchDogConfig = dog }
// watchDogConfig global watch dog
var watchDogConfig func(cc *Config)
// setConfigDefaultValue default Config value
func setConfigDefaultValue(cc *Config) {
for _, opt := range [...]ConfigOption{
WithOptionPrefix(""),
WithOptionWithStructName(false),
WithOptionReturnPrevious(true),
WithNewFunc(""),
WithNewFuncReturn(NewFuncReturnPointer),
WithVerbose(false),
WithUsageTagName(""),
WithEmptyCompositeNil(false),
WithDebug(false),
WithXConf(false),
WithXConfTrimPrefix(""),
WithSliceOnlyAppend(false),
} {
opt(cc)
}
}
// newDefaultConfig new default Config
func newDefaultConfig() *Config {
cc := &Config{}
setConfigDefaultValue(cc)
return cc
}
// AtomicSetFunc used for XConf
func (cc *Config) AtomicSetFunc() func(interface{}) { return AtomicConfigSet }
// atomicConfig global *Config holder
var atomicConfig unsafe.Pointer
// onAtomicConfigSet global call back when AtomicConfigSet called by XConf.
// use ConfigInterface.ApplyOption to modify the updated cc
// if passed in cc not valid, then return false, cc will not set to atomicConfig
var onAtomicConfigSet func(cc ConfigInterface) bool
// InstallCallbackOnAtomicConfigSet install callback
func InstallCallbackOnAtomicConfigSet(callback func(cc ConfigInterface) bool) {
onAtomicConfigSet = callback
}
// AtomicConfigSet atomic setter for *Config
func AtomicConfigSet(update interface{}) {
cc := update.(*Config)
if onAtomicConfigSet != nil && !onAtomicConfigSet(cc) {
return
}
atomic.StorePointer(&atomicConfig, (unsafe.Pointer)(cc))
}
// AtomicConfig return atomic *ConfigVisitor
func AtomicConfig() ConfigVisitor {
current := (*Config)(atomic.LoadPointer(&atomicConfig))
if current == nil {
defaultOne := newDefaultConfig()
if watchDogConfig != nil {
watchDogConfig(defaultOne)
}
atomic.CompareAndSwapPointer(&atomicConfig, nil, (unsafe.Pointer)(defaultOne))
return (*Config)(atomic.LoadPointer(&atomicConfig))
}
return current
}
// all getter func
func (cc *Config) GetOptionPrefix() string { return cc.OptionPrefix }
func (cc *Config) GetOptionWithStructName() bool { return cc.OptionWithStructName }
func (cc *Config) GetOptionReturnPrevious() bool { return cc.OptionReturnPrevious }
func (cc *Config) GetNewFunc() string { return cc.NewFunc }
func (cc *Config) GetNewFuncReturn() string { return cc.NewFuncReturn }
// GetVerbose visitor func for filed Verbose
//
// Deprecated: use --debug instead
func (cc *Config) GetVerbose() bool { return cc.Verbose }
func (cc *Config) GetUsageTagName() string { return cc.UsageTagName }
func (cc *Config) GetEmptyCompositeNil() bool { return cc.EmptyCompositeNil }
func (cc *Config) GetDebug() bool { return cc.Debug }
func (cc *Config) GetXConf() bool { return cc.XConf }
func (cc *Config) GetXConfTrimPrefix() string { return cc.XConfTrimPrefix }
func (cc *Config) GetSliceOnlyAppend() bool { return cc.SliceOnlyAppend }
// ConfigVisitor visitor interface for Config
type ConfigVisitor interface {
GetOptionPrefix() string
GetOptionWithStructName() bool
GetOptionReturnPrevious() bool
GetNewFunc() string
GetNewFuncReturn() string
// GetVerbose visitor func for filed Verbose
//
// Deprecated: use --debug instead
GetVerbose() bool
GetUsageTagName() string
GetEmptyCompositeNil() bool
GetDebug() bool
GetXConf() bool
GetXConfTrimPrefix() string
GetSliceOnlyAppend() bool
}
// ConfigInterface visitor + ApplyOption interface for Config
type ConfigInterface interface {
ConfigVisitor
ApplyOption(...ConfigOption) []ConfigOption
}