forked from XSAM/optionGen
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoption.go
56 lines (49 loc) · 2.74 KB
/
option.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
package optiongen
import (
"fmt"
"strings"
)
//go:generate optiongen --option_with_struct_name=false --new_func=NewTestConfig --xconf=true --empty_composite_nil=true --usage_tag_name=usage
func ConfigOptionDeclareWithDefault() interface{} {
return map[string]interface{}{
"OptionPrefix": "", // annotation@OptionPrefix(comment="option func name prefix, like: With, WithRedis")
"OptionWithStructName": false, // annotation@OptionWithStructName(comment="should the option func with struct name?")
"OptionReturnPrevious": true, // annotation@OptionReturnPrevious(comment="should option func return the previous ones?")
"NewFunc": "", // annotation@NewFunc(comment="new function name")
"NewFuncReturn": string(NewFuncReturnPointer), // annotation@NewFuncReturn(comment="valid data: pointer,interface,visitor")
"Verbose": false, // annotation@Verbose(xconf="v",deprecated="use --debug instead")
"UsageTagName": "", // annotation@UsageTagName(comment="usage tag name,if not empty,will gen usage support for xconf/xflag")
"EmptyCompositeNil": false, // annotation@EmptyCompositeNil(comment="should empty slice or map to be nil? otherwise will be make(XXXX,0)")
"Debug": false, // annotation@Debug(comment="debug will print more detail info")
"XConf": false, // annotation@XConf(xconf="xconf",comment="should gen xconf tag support?")
"XConfTrimPrefix": "", // annotation@XConfTrimPrefix(comment="if enable xconf tag, the tag value will trim prefix [XConfTrimPrefix]")
"SliceOnlyAppend": false, // annotation@SliceOnlyAppend(comment="slice only has append func, has not option func, like: With, WithRedis ")
}
}
const NewFuncReturnPointer = "pointer"
const NewFuncReturnVisitor = "visitor"
const NewFuncReturnInterface = "interface"
var newFuncReturnAs = []string{NewFuncReturnPointer, NewFuncReturnVisitor, NewFuncReturnInterface}
func fixConfig(cc *Config) {
if cc.GetVerbose() {
cc.ApplyOption(WithDebug(true))
}
if !containStringEqualFold(newFuncReturnAs, cc.GetNewFuncReturn()) {
panic(fmt.Sprintf("new_func_return: %s, value not valid, valid list: %v", cc.GetNewFuncReturn(), newFuncReturnAs))
}
}
func init() {
InstallConfigWatchDog(fixConfig)
InstallCallbackOnAtomicConfigSet(func(cc ConfigInterface) bool {
fixConfig(cc.(*Config))
return true
})
}
func containStringEqualFold(s []string, v string) bool {
for _, vv := range s {
if strings.EqualFold(vv, v) {
return true
}
}
return false
}