-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_options_optiongen.go
291 lines (258 loc) · 10.5 KB
/
gen_options_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package protokit
import (
"sync/atomic"
"unsafe"
)
// Options should use NewOptions to initialize it
type Options struct {
// annotation@GolangBasePackagePath(comment="golang基础package path")
GolangBasePackagePath string `xconf:"golang_base_package_path" usage:"golang基础package path"`
// annotation@GolangRelative(comment="是否启用golang relative模式")
GolangRelative bool `xconf:"golang_relative" usage:"是否启用golang relative模式"`
// annotation@ProtoImportPath(comment="proto import路径")
ProtoImportPath []string `xconf:"proto_import_path" usage:"proto import路径"`
// annotation@ProtoFileAccessor(comment="proto import路径")
ProtoFileAccessor FileAccessor `xconf:"proto_file_accessor" usage:"proto import路径"`
// annotation@ProtoFileExcludeFilter(comment="proto过滤")
ProtoFileExcludeFilter FileExcludeFilter `xconf:"proto_file_exclude_filter" usage:"proto过滤"`
// annotation@ZapLogMapKeyTypes(comment="以类型为key的map的MarshalLogObject实现,使得可以直接使用zap.Object函数打印map数据")
ZapLogMapKeyTypes []string `xconf:"zap_log_map_key_types" usage:"以类型为key的map的MarshalLogObject实现,使得可以直接使用zap.Object函数打印map数据"`
// annotation@ZapLogBytesMode(comment="zap以何种方式输出[]byte, 可以使用base64或者bytes, 默认bytes")
ZapLogBytesMode string `xconf:"zap_log_bytes_mode" usage:"zap以何种方式输出[]byte, 可以使用base64或者bytes, 默认bytes"`
// annotation@NamePattern(comment="名称格式化空自己",inline="true")
*NamePattern `xconf:"name_pattern" usage:"名称格式化空自己"`
// annotation@ImportSetExclude(comment="import set忽略指定name的package")
ImportSetExclude []string `xconf:"import_set_exclude" usage:"import set忽略指定name的package"`
// annotation@URIUsingGRPC(comment="service的uri是否使用GRPC模式")
URIUsingGRPC bool `xconf:"uri_using_grpc" usage:"service的uri是否使用GRPC模式"`
// annotation@InvalidServiceAnnotations(comment="非法的 service annotations")
InvalidServiceAnnotations []string `xconf:"invalid_service_annotations" usage:"非法的 service annotations"`
// annotation@URIUsingGRPCWithoutPackage(comment="service的uri使用GRPC模式时,是否带package名")
URIUsingGRPCWithoutPackage bool `xconf:"uri_using_grpc_without_package" usage:"service的uri使用GRPC模式时,是否带package名"`
// annotation@StrictMode(comment="是否为严格模式")
StrictMode bool `xconf:"strict_mode" usage:"是否为严格模式"`
}
// NewOptions new Options
func NewOptions(opts ...Option) *Options {
cc := newDefaultOptions()
for _, opt := range opts {
opt(cc)
}
if watchDogOptions != nil {
watchDogOptions(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 *Options) ApplyOption(opts ...Option) []Option {
var previous []Option
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// Option option func
type Option func(cc *Options) Option
// WithGolangBasePackagePath golang基础package path
func WithGolangBasePackagePath(v string) Option {
return func(cc *Options) Option {
previous := cc.GolangBasePackagePath
cc.GolangBasePackagePath = v
return WithGolangBasePackagePath(previous)
}
}
// WithGolangRelative 是否启用golang relative模式
func WithGolangRelative(v bool) Option {
return func(cc *Options) Option {
previous := cc.GolangRelative
cc.GolangRelative = v
return WithGolangRelative(previous)
}
}
// WithProtoImportPath proto import路径
func WithProtoImportPath(v ...string) Option {
return func(cc *Options) Option {
previous := cc.ProtoImportPath
cc.ProtoImportPath = v
return WithProtoImportPath(previous...)
}
}
// WithProtoFileAccessor proto import路径
func WithProtoFileAccessor(v FileAccessor) Option {
return func(cc *Options) Option {
previous := cc.ProtoFileAccessor
cc.ProtoFileAccessor = v
return WithProtoFileAccessor(previous)
}
}
// WithProtoFileExcludeFilter proto过滤
func WithProtoFileExcludeFilter(v FileExcludeFilter) Option {
return func(cc *Options) Option {
previous := cc.ProtoFileExcludeFilter
cc.ProtoFileExcludeFilter = v
return WithProtoFileExcludeFilter(previous)
}
}
// WithZapLogMapKeyTypes 以类型为key的map的MarshalLogObject实现,使得可以直接使用zap.Object函数打印map数据
func WithZapLogMapKeyTypes(v ...string) Option {
return func(cc *Options) Option {
previous := cc.ZapLogMapKeyTypes
cc.ZapLogMapKeyTypes = v
return WithZapLogMapKeyTypes(previous...)
}
}
// WithZapLogBytesMode zap以何种方式输出[]byte, 可以使用base64或者bytes, 默认bytes
func WithZapLogBytesMode(v string) Option {
return func(cc *Options) Option {
previous := cc.ZapLogBytesMode
cc.ZapLogBytesMode = v
return WithZapLogBytesMode(previous)
}
}
// WithNamePattern 名称格式化空自己
func WithNamePattern(v *NamePattern) Option {
return func(cc *Options) Option {
previous := cc.NamePattern
cc.NamePattern = v
return WithNamePattern(previous)
}
}
// WithImportSetExclude import set忽略指定name的package
func WithImportSetExclude(v ...string) Option {
return func(cc *Options) Option {
previous := cc.ImportSetExclude
cc.ImportSetExclude = v
return WithImportSetExclude(previous...)
}
}
// WithURIUsingGRPC service的uri是否使用GRPC模式
func WithURIUsingGRPC(v bool) Option {
return func(cc *Options) Option {
previous := cc.URIUsingGRPC
cc.URIUsingGRPC = v
return WithURIUsingGRPC(previous)
}
}
// WithInvalidServiceAnnotations 非法的 service annotations
func WithInvalidServiceAnnotations(v ...string) Option {
return func(cc *Options) Option {
previous := cc.InvalidServiceAnnotations
cc.InvalidServiceAnnotations = v
return WithInvalidServiceAnnotations(previous...)
}
}
// WithURIUsingGRPCWithoutPackage service的uri使用GRPC模式时,是否带package名
func WithURIUsingGRPCWithoutPackage(v bool) Option {
return func(cc *Options) Option {
previous := cc.URIUsingGRPCWithoutPackage
cc.URIUsingGRPCWithoutPackage = v
return WithURIUsingGRPCWithoutPackage(previous)
}
}
// WithStrictMode 是否为严格模式
func WithStrictMode(v bool) Option {
return func(cc *Options) Option {
previous := cc.StrictMode
cc.StrictMode = v
return WithStrictMode(previous)
}
}
// InstallOptionsWatchDog the installed func will called when NewOptions called
func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
// watchDogOptions global watch dog
var watchDogOptions func(cc *Options)
// newDefaultOptions new default Options
func newDefaultOptions() *Options {
cc := &Options{}
for _, opt := range [...]Option{
WithGolangBasePackagePath(""),
WithGolangRelative(true),
WithProtoImportPath(make([]string, 0)...),
WithProtoFileAccessor(nil),
WithProtoFileExcludeFilter(defaultFileExcludeFilter),
WithZapLogMapKeyTypes([]string{"int", "int32", "int64", "uint32", "uint64", "string"}...),
WithZapLogBytesMode("bytes"),
WithNamePattern(NewNamePattern()),
WithImportSetExclude([]string{"netutils"}...),
WithURIUsingGRPC(false),
WithInvalidServiceAnnotations(make([]string, 0)...),
WithURIUsingGRPCWithoutPackage(false),
WithStrictMode(true),
} {
opt(cc)
}
return cc
}
// AtomicSetFunc used for XConf
func (cc *Options) AtomicSetFunc() func(interface{}) { return AtomicOptionsSet }
// atomicOptions global *Options holder
var atomicOptions unsafe.Pointer
// onAtomicOptionsSet global call back when AtomicOptionsSet called by XConf.
// use OptionsInterface.ApplyOption to modify the updated cc
// if passed in cc not valid, then return false, cc will not set to atomicOptions
var onAtomicOptionsSet func(cc OptionsInterface) bool
// InstallCallbackOnAtomicOptionsSet install callback
func InstallCallbackOnAtomicOptionsSet(callback func(cc OptionsInterface) bool) {
onAtomicOptionsSet = callback
}
// AtomicOptionsSet atomic setter for *Options
func AtomicOptionsSet(update interface{}) {
cc := update.(*Options)
if onAtomicOptionsSet != nil && !onAtomicOptionsSet(cc) {
return
}
atomic.StorePointer(&atomicOptions, (unsafe.Pointer)(cc))
}
// AtomicOptions return atomic *OptionsVisitor
func AtomicOptions() OptionsVisitor {
current := (*Options)(atomic.LoadPointer(&atomicOptions))
if current == nil {
defaultOne := newDefaultOptions()
if watchDogOptions != nil {
watchDogOptions(defaultOne)
}
atomic.CompareAndSwapPointer(&atomicOptions, nil, (unsafe.Pointer)(defaultOne))
return (*Options)(atomic.LoadPointer(&atomicOptions))
}
return current
}
// all getter func
func (cc *Options) GetGolangBasePackagePath() string { return cc.GolangBasePackagePath }
func (cc *Options) GetGolangRelative() bool { return cc.GolangRelative }
func (cc *Options) GetProtoImportPath() []string { return cc.ProtoImportPath }
func (cc *Options) GetProtoFileAccessor() FileAccessor { return cc.ProtoFileAccessor }
func (cc *Options) GetProtoFileExcludeFilter() FileExcludeFilter { return cc.ProtoFileExcludeFilter }
func (cc *Options) GetZapLogMapKeyTypes() []string { return cc.ZapLogMapKeyTypes }
func (cc *Options) GetZapLogBytesMode() string { return cc.ZapLogBytesMode }
func (cc *Options) GetNamePattern() *NamePattern { return cc.NamePattern }
func (cc *Options) GetImportSetExclude() []string { return cc.ImportSetExclude }
func (cc *Options) GetURIUsingGRPC() bool { return cc.URIUsingGRPC }
func (cc *Options) GetInvalidServiceAnnotations() []string { return cc.InvalidServiceAnnotations }
func (cc *Options) GetURIUsingGRPCWithoutPackage() bool { return cc.URIUsingGRPCWithoutPackage }
func (cc *Options) GetStrictMode() bool { return cc.StrictMode }
// OptionsVisitor visitor interface for Options
type OptionsVisitor interface {
GetGolangBasePackagePath() string
GetGolangRelative() bool
GetProtoImportPath() []string
GetProtoFileAccessor() FileAccessor
GetProtoFileExcludeFilter() FileExcludeFilter
GetZapLogMapKeyTypes() []string
GetZapLogBytesMode() string
GetNamePattern() *NamePattern
GetImportSetExclude() []string
GetURIUsingGRPC() bool
GetInvalidServiceAnnotations() []string
GetURIUsingGRPCWithoutPackage() bool
GetStrictMode() bool
}
// OptionsInterface visitor + ApplyOption interface for Options
type OptionsInterface interface {
OptionsVisitor
ApplyOption(...Option) []Option
}