-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_conf_optiongen.go
146 lines (123 loc) · 3.65 KB
/
gen_conf_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
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package migration
import (
"sync/atomic"
"unsafe"
)
// Conf should use NewConf to initialize it
type Conf struct {
FileName string `xconf:"file_name" usage:"migration 脚本名"`
ScriptRoot string `xconf:"script_root" usage:"migration 脚本根路径"`
CommitID string `xconf:"commit_id" usage:"repo commitID"`
}
// NewConf new Conf
func NewConf(opts ...ConfOption) *Conf {
cc := newDefaultConf()
for _, opt := range opts {
opt(cc)
}
if watchDogConf != nil {
watchDogConf(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 *Conf) ApplyOption(opts ...ConfOption) []ConfOption {
var previous []ConfOption
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// ConfOption option func
type ConfOption func(cc *Conf) ConfOption
// WithFileName migration 脚本名
func WithFileName(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.FileName
cc.FileName = v
return WithFileName(previous)
}
}
// WithScriptRoot migration 脚本根路径
func WithScriptRoot(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.ScriptRoot
cc.ScriptRoot = v
return WithScriptRoot(previous)
}
}
// WithCommitID repo commitID
func WithCommitID(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.CommitID
cc.CommitID = v
return WithCommitID(previous)
}
}
// InstallConfWatchDog the installed func will called when NewConf called
func InstallConfWatchDog(dog func(cc *Conf)) { watchDogConf = dog }
// watchDogConf global watch dog
var watchDogConf func(cc *Conf)
// newDefaultConf new default Conf
func newDefaultConf() *Conf {
cc := &Conf{}
for _, opt := range [...]ConfOption{
WithFileName("migration"),
WithScriptRoot("."),
WithCommitID(""),
} {
opt(cc)
}
return cc
}
// AtomicSetFunc used for XConf
func (cc *Conf) AtomicSetFunc() func(interface{}) { return AtomicConfSet }
// atomicConf global *Conf holder
var atomicConf unsafe.Pointer
// onAtomicConfSet global call back when AtomicConfSet called by XConf.
// use ConfInterface.ApplyOption to modify the updated cc
// if passed in cc not valid, then return false, cc will not set to atomicConf
var onAtomicConfSet func(cc ConfInterface) bool
// InstallCallbackOnAtomicConfSet install callback
func InstallCallbackOnAtomicConfSet(callback func(cc ConfInterface) bool) { onAtomicConfSet = callback }
// AtomicConfSet atomic setter for *Conf
func AtomicConfSet(update interface{}) {
cc := update.(*Conf)
if onAtomicConfSet != nil && !onAtomicConfSet(cc) {
return
}
atomic.StorePointer(&atomicConf, (unsafe.Pointer)(cc))
}
// AtomicConf return atomic *ConfVisitor
func AtomicConf() ConfVisitor {
current := (*Conf)(atomic.LoadPointer(&atomicConf))
if current == nil {
defaultOne := newDefaultConf()
if watchDogConf != nil {
watchDogConf(defaultOne)
}
atomic.CompareAndSwapPointer(&atomicConf, nil, (unsafe.Pointer)(defaultOne))
return (*Conf)(atomic.LoadPointer(&atomicConf))
}
return current
}
// all getter func
func (cc *Conf) GetFileName() string { return cc.FileName }
func (cc *Conf) GetScriptRoot() string { return cc.ScriptRoot }
func (cc *Conf) GetCommitID() string { return cc.CommitID }
// ConfVisitor visitor interface for Conf
type ConfVisitor interface {
GetFileName() string
GetScriptRoot() string
GetCommitID() string
}
// ConfInterface visitor + ApplyOption interface for Conf
type ConfInterface interface {
ConfVisitor
ApplyOption(...ConfOption) []ConfOption
}