From 68e1a19e414fe3bd0753d99c2becb90c652b7a92 Mon Sep 17 00:00:00 2001 From: "hui.wang" Date: Wed, 26 Jun 2024 11:52:53 +0800 Subject: [PATCH] feat: support option interface --- example/config.go | 2 +- example/config_test.go | 12 +- example/gen_allconfig_optiongen.go | 228 +++++++++++++++------------- example/gen_config_optionGen.go | 140 +++++++++-------- example/gen_etcd_optiongen.go | 28 ++-- example/gen_onlyappend_optiongen.go | 37 +++-- example/gen_redis_optiongen.go | 40 +++-- example/gen_spec_optiongen.go | 28 ++-- example/gen_xxxxxx_optiongen.go | 76 ++++++---- gen.go | 23 +-- template.go | 43 ++++-- 11 files changed, 383 insertions(+), 274 deletions(-) diff --git a/example/config.go b/example/config.go index 848b3ef..a348d85 100644 --- a/example/config.go +++ b/example/config.go @@ -89,7 +89,7 @@ func specOptionDeclareWithDefault() interface{} { } } -//go:generate optiongen --option_return_previous=true --slice_only_append=true +//go:generate optiongen --option_return_previous=true func OnlyAppendOptionDeclareWithDefault() interface{} { return map[string]interface{}{ "Address": []string{"10.0.0.1:6379", "10.0.0.2:6379"}, diff --git a/example/config_test.go b/example/config_test.go index f1dc74a..56458ad 100644 --- a/example/config_test.go +++ b/example/config_test.go @@ -2,8 +2,18 @@ package example import "testing" +// interface test +type interfaceTest struct { + data []int64 +} + +func (i *interfaceTest) Apply(cc *Config) ConfigOption { + return AppendTestSliceInt64(1, 2, 3, 4).Apply(cc) +} + func TestNewConfig(t *testing.T) { - tc := NewFuncNameSpecified(false, "", WithTestMapIntInt(map[int]int{2: 4})) + it := &interfaceTest{data: []int64{1, 2, 3, 4}} + tc := NewFuncNameSpecified(false, "", WithTestMapIntInt(map[int]int{2: 4}), it) if tc == nil { t.Fatal("new config error") } diff --git a/example/gen_allconfig_optiongen.go b/example/gen_allconfig_optiongen.go index cdd495b..40b0cf9 100644 --- a/example/gen_allconfig_optiongen.go +++ b/example/gen_allconfig_optiongen.go @@ -57,7 +57,7 @@ type AllConfig struct { func NewAllConfig(opts ...AllConfigOption) *AllConfig { cc := newDefaultAllConfig() for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogAllConfig != nil { watchDogAllConfig(cc) @@ -72,17 +72,27 @@ func NewAllConfig(opts ...AllConfigOption) *AllConfig { func (cc *AllConfig) ApplyOption(opts ...AllConfigOption) []AllConfigOption { var previous []AllConfigOption for _, opt := range opts { - previous = append(previous, opt(cc)) + previous = append(previous, opt.Apply(cc)) } return previous } -// AllConfigOption option func -type AllConfigOption func(cc *AllConfig) AllConfigOption +// AllConfigOptionFunc option func +type AllConfigOption interface { + Apply(cc *AllConfig) AllConfigOption +} + +var _ AllConfigOption = AllConfigOptionFunc(nil) + +type AllConfigOptionFunc func(cc *AllConfig) AllConfigOptionFunc + +func (f AllConfigOptionFunc) Apply(cc *AllConfig) AllConfigOption { + return f(cc) +} // WithTypeBool option func for filed TypeBool -func WithTypeBool(v bool) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeBool(v bool) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeBool cc.TypeBool = v return WithTypeBool(previous) @@ -90,8 +100,8 @@ func WithTypeBool(v bool) AllConfigOption { } // WithTypeString option func for filed TypeString -func WithTypeString(v string) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeString(v string) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeString cc.TypeString = v return WithTypeString(previous) @@ -99,8 +109,8 @@ func WithTypeString(v string) AllConfigOption { } // WithTypeDuration option func for filed TypeDuration -func WithTypeDuration(v time.Duration) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeDuration(v time.Duration) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeDuration cc.TypeDuration = v return WithTypeDuration(previous) @@ -108,8 +118,8 @@ func WithTypeDuration(v time.Duration) AllConfigOption { } // WithTypeFloat32 option func for filed TypeFloat32 -func WithTypeFloat32(v float32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeFloat32(v float32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeFloat32 cc.TypeFloat32 = v return WithTypeFloat32(previous) @@ -117,8 +127,8 @@ func WithTypeFloat32(v float32) AllConfigOption { } // WithTypeFloat64 option func for filed TypeFloat64 -func WithTypeFloat64(v float32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeFloat64(v float32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeFloat64 cc.TypeFloat64 = v return WithTypeFloat64(previous) @@ -126,8 +136,8 @@ func WithTypeFloat64(v float32) AllConfigOption { } // WithTypeInt option func for filed TypeInt -func WithTypeInt(v int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeInt(v int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeInt cc.TypeInt = v return WithTypeInt(previous) @@ -135,8 +145,8 @@ func WithTypeInt(v int) AllConfigOption { } // WithTypeUint option func for filed TypeUint -func WithTypeUint(v int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeUint(v int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeUint cc.TypeUint = v return WithTypeUint(previous) @@ -144,8 +154,8 @@ func WithTypeUint(v int) AllConfigOption { } // WithTypeInt8 option func for filed TypeInt8 -func WithTypeInt8(v int8) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeInt8(v int8) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeInt8 cc.TypeInt8 = v return WithTypeInt8(previous) @@ -153,8 +163,8 @@ func WithTypeInt8(v int8) AllConfigOption { } // WithTypeUint8 option func for filed TypeUint8 -func WithTypeUint8(v uint8) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeUint8(v uint8) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeUint8 cc.TypeUint8 = v return WithTypeUint8(previous) @@ -162,8 +172,8 @@ func WithTypeUint8(v uint8) AllConfigOption { } // WithTypeInt16 option func for filed TypeInt16 -func WithTypeInt16(v int16) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeInt16(v int16) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeInt16 cc.TypeInt16 = v return WithTypeInt16(previous) @@ -171,8 +181,8 @@ func WithTypeInt16(v int16) AllConfigOption { } // WithTypeUint16 option func for filed TypeUint16 -func WithTypeUint16(v uint16) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeUint16(v uint16) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeUint16 cc.TypeUint16 = v return WithTypeUint16(previous) @@ -180,8 +190,8 @@ func WithTypeUint16(v uint16) AllConfigOption { } // WithTypeInt32 option func for filed TypeInt32 -func WithTypeInt32(v int32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeInt32(v int32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeInt32 cc.TypeInt32 = v return WithTypeInt32(previous) @@ -189,8 +199,8 @@ func WithTypeInt32(v int32) AllConfigOption { } // WithTypeUint32 option func for filed TypeUint32 -func WithTypeUint32(v uint32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeUint32(v uint32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeUint32 cc.TypeUint32 = v return WithTypeUint32(previous) @@ -198,8 +208,8 @@ func WithTypeUint32(v uint32) AllConfigOption { } // WithTypeInt64 option func for filed TypeInt64 -func WithTypeInt64(v int64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeInt64(v int64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeInt64 cc.TypeInt64 = v return WithTypeInt64(previous) @@ -207,8 +217,8 @@ func WithTypeInt64(v int64) AllConfigOption { } // WithTypeUint64 option func for filed TypeUint64 -func WithTypeUint64(v uint64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeUint64(v uint64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeUint64 cc.TypeUint64 = v return WithTypeUint64(previous) @@ -216,8 +226,8 @@ func WithTypeUint64(v uint64) AllConfigOption { } // WithTypeSliceInt option func for filed TypeSliceInt -func WithTypeSliceInt(v ...int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceInt(v ...int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt cc.TypeSliceInt = v return WithTypeSliceInt(previous...) @@ -225,8 +235,8 @@ func WithTypeSliceInt(v ...int) AllConfigOption { } // AppendTypeSliceInt append func for filed TypeSliceInt -func AppendTypeSliceInt(v ...int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceInt(v ...int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt cc.TypeSliceInt = append(cc.TypeSliceInt, v...) return WithTypeSliceInt(previous...) @@ -234,8 +244,8 @@ func AppendTypeSliceInt(v ...int) AllConfigOption { } // WithTypeSliceUint option func for filed TypeSliceUint -func WithTypeSliceUint(v ...uint) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceUint(v ...uint) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint cc.TypeSliceUint = v return WithTypeSliceUint(previous...) @@ -243,8 +253,8 @@ func WithTypeSliceUint(v ...uint) AllConfigOption { } // AppendTypeSliceUint append func for filed TypeSliceUint -func AppendTypeSliceUint(v ...uint) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceUint(v ...uint) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint cc.TypeSliceUint = append(cc.TypeSliceUint, v...) return WithTypeSliceUint(previous...) @@ -252,8 +262,8 @@ func AppendTypeSliceUint(v ...uint) AllConfigOption { } // WithTypeSliceInt8 option func for filed TypeSliceInt8 -func WithTypeSliceInt8(v ...int8) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceInt8(v ...int8) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt8 cc.TypeSliceInt8 = v return WithTypeSliceInt8(previous...) @@ -261,8 +271,8 @@ func WithTypeSliceInt8(v ...int8) AllConfigOption { } // AppendTypeSliceInt8 append func for filed TypeSliceInt8 -func AppendTypeSliceInt8(v ...int8) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceInt8(v ...int8) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt8 cc.TypeSliceInt8 = append(cc.TypeSliceInt8, v...) return WithTypeSliceInt8(previous...) @@ -270,8 +280,8 @@ func AppendTypeSliceInt8(v ...int8) AllConfigOption { } // WithTypeSliceUint8 option func for filed TypeSliceUint8 -func WithTypeSliceUint8(v ...uint8) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceUint8(v ...uint8) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint8 cc.TypeSliceUint8 = v return WithTypeSliceUint8(previous...) @@ -279,8 +289,8 @@ func WithTypeSliceUint8(v ...uint8) AllConfigOption { } // AppendTypeSliceUint8 append func for filed TypeSliceUint8 -func AppendTypeSliceUint8(v ...uint8) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceUint8(v ...uint8) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint8 cc.TypeSliceUint8 = append(cc.TypeSliceUint8, v...) return WithTypeSliceUint8(previous...) @@ -288,8 +298,8 @@ func AppendTypeSliceUint8(v ...uint8) AllConfigOption { } // WithTypeSliceInt16 option func for filed TypeSliceInt16 -func WithTypeSliceInt16(v ...int16) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceInt16(v ...int16) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt16 cc.TypeSliceInt16 = v return WithTypeSliceInt16(previous...) @@ -297,8 +307,8 @@ func WithTypeSliceInt16(v ...int16) AllConfigOption { } // AppendTypeSliceInt16 append func for filed TypeSliceInt16 -func AppendTypeSliceInt16(v ...int16) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceInt16(v ...int16) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt16 cc.TypeSliceInt16 = append(cc.TypeSliceInt16, v...) return WithTypeSliceInt16(previous...) @@ -306,8 +316,8 @@ func AppendTypeSliceInt16(v ...int16) AllConfigOption { } // WithTypeSliceUin16 option func for filed TypeSliceUin16 -func WithTypeSliceUin16(v ...uint16) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceUin16(v ...uint16) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUin16 cc.TypeSliceUin16 = v return WithTypeSliceUin16(previous...) @@ -315,8 +325,8 @@ func WithTypeSliceUin16(v ...uint16) AllConfigOption { } // AppendTypeSliceUin16 append func for filed TypeSliceUin16 -func AppendTypeSliceUin16(v ...uint16) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceUin16(v ...uint16) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUin16 cc.TypeSliceUin16 = append(cc.TypeSliceUin16, v...) return WithTypeSliceUin16(previous...) @@ -324,8 +334,8 @@ func AppendTypeSliceUin16(v ...uint16) AllConfigOption { } // WithTypeSliceInt32 option func for filed TypeSliceInt32 -func WithTypeSliceInt32(v ...int32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceInt32(v ...int32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt32 cc.TypeSliceInt32 = v return WithTypeSliceInt32(previous...) @@ -333,8 +343,8 @@ func WithTypeSliceInt32(v ...int32) AllConfigOption { } // AppendTypeSliceInt32 append func for filed TypeSliceInt32 -func AppendTypeSliceInt32(v ...int32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceInt32(v ...int32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt32 cc.TypeSliceInt32 = append(cc.TypeSliceInt32, v...) return WithTypeSliceInt32(previous...) @@ -342,8 +352,8 @@ func AppendTypeSliceInt32(v ...int32) AllConfigOption { } // WithTypeSliceUint32 option func for filed TypeSliceUint32 -func WithTypeSliceUint32(v ...uint32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceUint32(v ...uint32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint32 cc.TypeSliceUint32 = v return WithTypeSliceUint32(previous...) @@ -351,8 +361,8 @@ func WithTypeSliceUint32(v ...uint32) AllConfigOption { } // AppendTypeSliceUint32 append func for filed TypeSliceUint32 -func AppendTypeSliceUint32(v ...uint32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceUint32(v ...uint32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint32 cc.TypeSliceUint32 = append(cc.TypeSliceUint32, v...) return WithTypeSliceUint32(previous...) @@ -360,8 +370,8 @@ func AppendTypeSliceUint32(v ...uint32) AllConfigOption { } // WithTypeSliceInt64 option func for filed TypeSliceInt64 -func WithTypeSliceInt64(v ...int64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceInt64(v ...int64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt64 cc.TypeSliceInt64 = v return WithTypeSliceInt64(previous...) @@ -369,8 +379,8 @@ func WithTypeSliceInt64(v ...int64) AllConfigOption { } // AppendTypeSliceInt64 append func for filed TypeSliceInt64 -func AppendTypeSliceInt64(v ...int64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceInt64(v ...int64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceInt64 cc.TypeSliceInt64 = append(cc.TypeSliceInt64, v...) return WithTypeSliceInt64(previous...) @@ -378,8 +388,8 @@ func AppendTypeSliceInt64(v ...int64) AllConfigOption { } // WithTypeSliceUint64 option func for filed TypeSliceUint64 -func WithTypeSliceUint64(v ...uint64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceUint64(v ...uint64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint64 cc.TypeSliceUint64 = v return WithTypeSliceUint64(previous...) @@ -387,8 +397,8 @@ func WithTypeSliceUint64(v ...uint64) AllConfigOption { } // AppendTypeSliceUint64 append func for filed TypeSliceUint64 -func AppendTypeSliceUint64(v ...uint64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceUint64(v ...uint64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceUint64 cc.TypeSliceUint64 = append(cc.TypeSliceUint64, v...) return WithTypeSliceUint64(previous...) @@ -396,8 +406,8 @@ func AppendTypeSliceUint64(v ...uint64) AllConfigOption { } // WithTypeSliceString option func for filed TypeSliceString -func WithTypeSliceString(v ...string) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceString(v ...string) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceString cc.TypeSliceString = v return WithTypeSliceString(previous...) @@ -405,8 +415,8 @@ func WithTypeSliceString(v ...string) AllConfigOption { } // AppendTypeSliceString append func for filed TypeSliceString -func AppendTypeSliceString(v ...string) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceString(v ...string) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceString cc.TypeSliceString = append(cc.TypeSliceString, v...) return WithTypeSliceString(previous...) @@ -414,8 +424,8 @@ func AppendTypeSliceString(v ...string) AllConfigOption { } // WithTypeSliceFloat32 option func for filed TypeSliceFloat32 -func WithTypeSliceFloat32(v ...float32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceFloat32(v ...float32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceFloat32 cc.TypeSliceFloat32 = v return WithTypeSliceFloat32(previous...) @@ -423,8 +433,8 @@ func WithTypeSliceFloat32(v ...float32) AllConfigOption { } // AppendTypeSliceFloat32 append func for filed TypeSliceFloat32 -func AppendTypeSliceFloat32(v ...float32) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceFloat32(v ...float32) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceFloat32 cc.TypeSliceFloat32 = append(cc.TypeSliceFloat32, v...) return WithTypeSliceFloat32(previous...) @@ -432,8 +442,8 @@ func AppendTypeSliceFloat32(v ...float32) AllConfigOption { } // WithTypeSliceFloat64 option func for filed TypeSliceFloat64 -func WithTypeSliceFloat64(v ...float64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceFloat64(v ...float64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceFloat64 cc.TypeSliceFloat64 = v return WithTypeSliceFloat64(previous...) @@ -441,8 +451,8 @@ func WithTypeSliceFloat64(v ...float64) AllConfigOption { } // AppendTypeSliceFloat64 append func for filed TypeSliceFloat64 -func AppendTypeSliceFloat64(v ...float64) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceFloat64(v ...float64) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceFloat64 cc.TypeSliceFloat64 = append(cc.TypeSliceFloat64, v...) return WithTypeSliceFloat64(previous...) @@ -450,8 +460,8 @@ func AppendTypeSliceFloat64(v ...float64) AllConfigOption { } // WithTypeSliceDuratuon option func for filed TypeSliceDuratuon -func WithTypeSliceDuratuon(v ...time.Duration) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeSliceDuratuon(v ...time.Duration) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceDuratuon cc.TypeSliceDuratuon = v return WithTypeSliceDuratuon(previous...) @@ -459,8 +469,8 @@ func WithTypeSliceDuratuon(v ...time.Duration) AllConfigOption { } // AppendTypeSliceDuratuon append func for filed TypeSliceDuratuon -func AppendTypeSliceDuratuon(v ...time.Duration) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func AppendTypeSliceDuratuon(v ...time.Duration) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeSliceDuratuon cc.TypeSliceDuratuon = append(cc.TypeSliceDuratuon, v...) return WithTypeSliceDuratuon(previous...) @@ -468,8 +478,8 @@ func AppendTypeSliceDuratuon(v ...time.Duration) AllConfigOption { } // WithTypeMapStringIntNotLeaf option func for filed TypeMapStringIntNotLeaf -func WithTypeMapStringIntNotLeaf(v map[string]int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeMapStringIntNotLeaf(v map[string]int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeMapStringIntNotLeaf cc.TypeMapStringIntNotLeaf = v return WithTypeMapStringIntNotLeaf(previous) @@ -477,8 +487,8 @@ func WithTypeMapStringIntNotLeaf(v map[string]int) AllConfigOption { } // WithTypeMapStringInt option func for filed TypeMapStringInt -func WithTypeMapStringInt(v map[string]int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeMapStringInt(v map[string]int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeMapStringInt cc.TypeMapStringInt = v return WithTypeMapStringInt(previous) @@ -486,8 +496,8 @@ func WithTypeMapStringInt(v map[string]int) AllConfigOption { } // WithTypeMapIntString option func for filed TypeMapIntString -func WithTypeMapIntString(v map[int]string) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeMapIntString(v map[int]string) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeMapIntString cc.TypeMapIntString = v return WithTypeMapIntString(previous) @@ -495,8 +505,8 @@ func WithTypeMapIntString(v map[int]string) AllConfigOption { } // WithTypeMapStringString option func for filed TypeMapStringString -func WithTypeMapStringString(v map[string]string) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeMapStringString(v map[string]string) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeMapStringString cc.TypeMapStringString = v return WithTypeMapStringString(previous) @@ -504,8 +514,8 @@ func WithTypeMapStringString(v map[string]string) AllConfigOption { } // WithTypeMapIntInt option func for filed TypeMapIntInt -func WithTypeMapIntInt(v map[int]int) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeMapIntInt(v map[int]int) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeMapIntInt cc.TypeMapIntInt = v return WithTypeMapIntInt(previous) @@ -513,8 +523,8 @@ func WithTypeMapIntInt(v map[int]int) AllConfigOption { } // WithTypeMapStringDuration option func for filed TypeMapStringDuration -func WithTypeMapStringDuration(v map[string]time.Duration) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTypeMapStringDuration(v map[string]time.Duration) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TypeMapStringDuration cc.TypeMapStringDuration = v return WithTypeMapStringDuration(previous) @@ -522,8 +532,8 @@ func WithTypeMapStringDuration(v map[string]time.Duration) AllConfigOption { } // WithRedis option func for filed Redis -func WithRedis(v *Redis) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithRedis(v *Redis) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.Redis cc.Redis = v return WithRedis(previous) @@ -531,8 +541,8 @@ func WithRedis(v *Redis) AllConfigOption { } // WithETCD option func for filed ETCD -func WithETCD(v *ETCD) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithETCD(v *ETCD) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.ETCD cc.ETCD = v return WithETCD(previous) @@ -540,8 +550,8 @@ func WithETCD(v *ETCD) AllConfigOption { } // WithTestInterface option func for filed TestInterface -func WithTestInterface(v interface{}) AllConfigOption { - return func(cc *AllConfig) AllConfigOption { +func WithTestInterface(v interface{}) AllConfigOptionFunc { + return func(cc *AllConfig) AllConfigOptionFunc { previous := cc.TestInterface cc.TestInterface = v return WithTestInterface(previous) @@ -556,7 +566,7 @@ var watchDogAllConfig func(cc *AllConfig) // setAllConfigDefaultValue default AllConfig value func setAllConfigDefaultValue(cc *AllConfig) { - for _, opt := range [...]AllConfigOption{ + for _, opt := range [...]AllConfigOptionFunc{ WithTypeBool(false), WithTypeString("a"), WithTypeDuration(time.Second), diff --git a/example/gen_config_optionGen.go b/example/gen_config_optionGen.go index f927fa1..7500ba3 100644 --- a/example/gen_config_optionGen.go +++ b/example/gen_config_optionGen.go @@ -57,7 +57,7 @@ func NewFuncNameSpecified(testParamterBool bool, testParamterStr string, opts .. cc.TestParamterBool = testParamterBool cc.TestParamterStr = testParamterStr for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogConfig != nil { watchDogConfig(cc) @@ -72,17 +72,27 @@ func NewFuncNameSpecified(testParamterBool bool, testParamterStr string, opts .. func (cc *Config) ApplyOption(opts ...ConfigOption) []ConfigOption { var previous []ConfigOption for _, opt := range opts { - previous = append(previous, opt(cc)) + previous = append(previous, opt.Apply(cc)) } return previous } -// ConfigOption option func -type ConfigOption func(cc *Config) ConfigOption +// ConfigOptionFunc option func +type ConfigOption interface { + Apply(cc *Config) ConfigOption +} + +var _ ConfigOption = ConfigOptionFunc(nil) + +type ConfigOptionFunc func(cc *Config) ConfigOptionFunc + +func (f ConfigOptionFunc) Apply(cc *Config) ConfigOption { + return f(cc) +} // WithTestMapIntInt option func for filed TestMapIntInt -func WithTestMapIntInt(v map[int]int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestMapIntInt(v map[int]int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestMapIntInt cc.TestMapIntInt = v return WithTestMapIntInt(previous) @@ -90,8 +100,8 @@ func WithTestMapIntInt(v map[int]int) ConfigOption { } // WithSpecSub option func for filed SpecSub -func WithSpecSub(v *spec) ConfigOption { - return func(cc *Config) ConfigOption { +func WithSpecSub(v *spec) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.SpecSub cc.SpecSub = v return WithSpecSub(previous) @@ -99,8 +109,8 @@ func WithSpecSub(v *spec) ConfigOption { } // WithTTTTTTTT option func for filed TestNil -func WithTTTTTTTT(v interface{}) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTTTTTTTT(v interface{}) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestNil cc.TestNil = v return WithTTTTTTTT(previous) @@ -108,8 +118,8 @@ func WithTTTTTTTT(v interface{}) ConfigOption { } // WithTestSliceInt option func for filed TestSliceInt -func WithTestSliceInt(v ...int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceInt(v ...int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceInt cc.TestSliceInt = v return WithTestSliceInt(previous...) @@ -117,8 +127,8 @@ func WithTestSliceInt(v ...int) ConfigOption { } // AppendTestSliceInt append func for filed TestSliceInt -func AppendTestSliceInt(v ...int) ConfigOption { - return func(cc *Config) ConfigOption { +func AppendTestSliceInt(v ...int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceInt cc.TestSliceInt = append(cc.TestSliceInt, v...) return WithTestSliceInt(previous...) @@ -126,8 +136,8 @@ func AppendTestSliceInt(v ...int) ConfigOption { } // WithTestSliceInt64 option func for filed TestSliceInt64 -func WithTestSliceInt64(v ...int64) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceInt64(v ...int64) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceInt64 cc.TestSliceInt64 = v return WithTestSliceInt64(previous...) @@ -135,8 +145,8 @@ func WithTestSliceInt64(v ...int64) ConfigOption { } // AppendTestSliceInt64 append func for filed TestSliceInt64 -func AppendTestSliceInt64(v ...int64) ConfigOption { - return func(cc *Config) ConfigOption { +func AppendTestSliceInt64(v ...int64) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceInt64 cc.TestSliceInt64 = append(cc.TestSliceInt64, v...) return WithTestSliceInt64(previous...) @@ -144,8 +154,8 @@ func AppendTestSliceInt64(v ...int64) ConfigOption { } // WithTestSliceString option func for filed TestSliceString -func WithTestSliceString(v ...string) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceString(v ...string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceString cc.TestSliceString = v return WithTestSliceString(previous...) @@ -153,8 +163,8 @@ func WithTestSliceString(v ...string) ConfigOption { } // AppendTestSliceString append func for filed TestSliceString -func AppendTestSliceString(v ...string) ConfigOption { - return func(cc *Config) ConfigOption { +func AppendTestSliceString(v ...string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceString cc.TestSliceString = append(cc.TestSliceString, v...) return WithTestSliceString(previous...) @@ -162,8 +172,8 @@ func AppendTestSliceString(v ...string) ConfigOption { } // WithTestSliceBool option func for filed TestSliceBool -func WithTestSliceBool(v ...bool) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceBool(v ...bool) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceBool cc.TestSliceBool = v return WithTestSliceBool(previous...) @@ -171,8 +181,8 @@ func WithTestSliceBool(v ...bool) ConfigOption { } // AppendTestSliceBool append func for filed TestSliceBool -func AppendTestSliceBool(v ...bool) ConfigOption { - return func(cc *Config) ConfigOption { +func AppendTestSliceBool(v ...bool) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceBool cc.TestSliceBool = append(cc.TestSliceBool, v...) return WithTestSliceBool(previous...) @@ -180,8 +190,8 @@ func AppendTestSliceBool(v ...bool) ConfigOption { } // WithTestSliceIntNil option func for filed TestSliceIntNil -func WithTestSliceIntNil(v ...int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceIntNil(v ...int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceIntNil cc.TestSliceIntNil = v return WithTestSliceIntNil(previous...) @@ -189,8 +199,8 @@ func WithTestSliceIntNil(v ...int) ConfigOption { } // AppendTestSliceIntNil append func for filed TestSliceIntNil -func AppendTestSliceIntNil(v ...int) ConfigOption { - return func(cc *Config) ConfigOption { +func AppendTestSliceIntNil(v ...int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceIntNil cc.TestSliceIntNil = append(cc.TestSliceIntNil, v...) return WithTestSliceIntNil(previous...) @@ -198,8 +208,8 @@ func AppendTestSliceIntNil(v ...int) ConfigOption { } // WithTestSliceByte option func for filed TestSliceByte -func WithTestSliceByte(v []byte) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceByte(v []byte) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceByte cc.TestSliceByte = v return WithTestSliceByte(previous) @@ -207,8 +217,8 @@ func WithTestSliceByte(v []byte) ConfigOption { } // WithTestSliceIntEmpty option func for filed TestSliceIntEmpty -func WithTestSliceIntEmpty(v ...int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestSliceIntEmpty(v ...int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceIntEmpty cc.TestSliceIntEmpty = v return WithTestSliceIntEmpty(previous...) @@ -216,8 +226,8 @@ func WithTestSliceIntEmpty(v ...int) ConfigOption { } // AppendTestSliceIntEmpty append func for filed TestSliceIntEmpty -func AppendTestSliceIntEmpty(v ...int) ConfigOption { - return func(cc *Config) ConfigOption { +func AppendTestSliceIntEmpty(v ...int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestSliceIntEmpty cc.TestSliceIntEmpty = append(cc.TestSliceIntEmpty, v...) return WithTestSliceIntEmpty(previous...) @@ -225,8 +235,8 @@ func AppendTestSliceIntEmpty(v ...int) ConfigOption { } // WithTestHTTPPort option func for filed TestHTTPPort -func WithTestHTTPPort(v string) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestHTTPPort(v string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestHTTPPort cc.TestHTTPPort = v return WithTestHTTPPort(previous) @@ -234,8 +244,8 @@ func WithTestHTTPPort(v string) ConfigOption { } // WithTestEmptyMap option func for filed TestEmptyMap -func WithTestEmptyMap(v map[int]int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestEmptyMap(v map[int]int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestEmptyMap cc.TestEmptyMap = v return WithTestEmptyMap(previous) @@ -243,8 +253,8 @@ func WithTestEmptyMap(v map[int]int) ConfigOption { } // WithTestInt64 option func for filed TestInt64 -func WithTestInt64(v int64) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestInt64(v int64) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestInt64 cc.TestInt64 = v return WithTestInt64(previous) @@ -252,8 +262,8 @@ func WithTestInt64(v int64) ConfigOption { } // WithTestInt 这里是函数注释1,"test",这里是函数注释2 -func WithTestInt(v int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestInt(v int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestInt cc.TestInt = v return WithTestInt(previous) @@ -261,8 +271,8 @@ func WithTestInt(v int) ConfigOption { } // WithTestMapIntString option func for filed TestMapIntString -func WithTestMapIntString(v map[int]string) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestMapIntString(v map[int]string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestMapIntString cc.TestMapIntString = v return WithTestMapIntString(previous) @@ -270,8 +280,8 @@ func WithTestMapIntString(v map[int]string) ConfigOption { } // WithTestMapStringString option func for filed TestMapStringString -func WithTestMapStringString(v map[string]string) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestMapStringString(v map[string]string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestMapStringString cc.TestMapStringString = v return WithTestMapStringString(previous) @@ -279,8 +289,8 @@ func WithTestMapStringString(v map[string]string) ConfigOption { } // WithTestString option func for filed TestString -func WithTestString(v string) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestString(v string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestString cc.TestString = v return WithTestString(previous) @@ -288,8 +298,8 @@ func WithTestString(v string) ConfigOption { } // WithFood option func for filed Food -func WithFood(v *string) ConfigOption { - return func(cc *Config) ConfigOption { +func WithFood(v *string) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.Food cc.Food = v return WithFood(previous) @@ -297,8 +307,8 @@ func WithFood(v *string) ConfigOption { } // WithWalk option func for filed Walk -func WithWalk(v func()) ConfigOption { - return func(cc *Config) ConfigOption { +func WithWalk(v func()) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.Walk cc.Walk = v return WithWalk(previous) @@ -306,8 +316,8 @@ func WithWalk(v func()) ConfigOption { } // WithTestNilFunc option func for filed TestNilFunc -func WithTestNilFunc(v func()) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestNilFunc(v func()) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestNilFunc cc.TestNilFunc = v return WithTestNilFunc(previous) @@ -315,8 +325,8 @@ func WithTestNilFunc(v func()) ConfigOption { } // WithSubTest option func for filed SubTest -func WithSubTest(v *SubTest) ConfigOption { - return func(cc *Config) ConfigOption { +func WithSubTest(v *SubTest) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.SubTest cc.SubTest = v return WithSubTest(previous) @@ -324,8 +334,8 @@ func WithSubTest(v *SubTest) ConfigOption { } // WithTestMapStringInt option func for filed TestMapStringInt -func WithTestMapStringInt(v map[string]int) ConfigOption { - return func(cc *Config) ConfigOption { +func WithTestMapStringInt(v map[string]int) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.TestMapStringInt cc.TestMapStringInt = v return WithTestMapStringInt(previous) @@ -333,8 +343,8 @@ func WithTestMapStringInt(v map[string]int) ConfigOption { } // WithFOO option func for filed FOO -func WithFOO(v *FOO) ConfigOption { - return func(cc *Config) ConfigOption { +func WithFOO(v *FOO) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.FOO cc.FOO = v return WithFOO(previous) @@ -342,8 +352,8 @@ func WithFOO(v *FOO) ConfigOption { } // WithPaths option func for filed Paths -func WithPaths(v Paths) ConfigOption { - return func(cc *Config) ConfigOption { +func WithPaths(v Paths) ConfigOptionFunc { + return func(cc *Config) ConfigOptionFunc { previous := cc.Paths cc.Paths = v return WithPaths(previous) @@ -361,7 +371,7 @@ func setConfigDefaultValue(cc *Config) { cc.TestProtected = nil cc.TestParamterBool = false cc.TestParamterStr = "" - for _, opt := range [...]ConfigOption{ + for _, opt := range [...]ConfigOptionFunc{ WithTestMapIntInt(map[int]int{1: 1, 2: 2, 3: 3}), WithSpecSub(NewSpec()), WithTTTTTTTT(nil), diff --git a/example/gen_etcd_optiongen.go b/example/gen_etcd_optiongen.go index c5bdc49..f96d5d2 100644 --- a/example/gen_etcd_optiongen.go +++ b/example/gen_etcd_optiongen.go @@ -22,7 +22,7 @@ func NewETCD(writeTimeout time.Duration, opts ...ETCDOption) *ETCD { cc := newDefaultETCD() cc.writeTimeout = writeTimeout for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogETCD != nil { watchDogETCD(cc) @@ -33,36 +33,46 @@ func NewETCD(writeTimeout time.Duration, opts ...ETCDOption) *ETCD { // ApplyOption apply multiple new option func (cc *ETCD) ApplyOption(opts ...ETCDOption) { for _, opt := range opts { - opt(cc) + opt.Apply(cc) } } -// ETCDOption option func -type ETCDOption func(cc *ETCD) +// ETCDOptionFunc option func +type ETCDOption interface { + Apply(cc *ETCD) +} + +var _ ETCDOption = ETCDOptionFunc(nil) + +type ETCDOptionFunc func(cc *ETCD) + +func (f ETCDOptionFunc) Apply(cc *ETCD) { + f(cc) +} // WithETCDEndpoints etcd地址 -func WithETCDEndpoints(v ...string) ETCDOption { +func WithETCDEndpoints(v ...string) ETCDOptionFunc { return func(cc *ETCD) { cc.Endpoints = v } } // AppendETCDEndpoints etcd地址 -func AppendETCDEndpoints(v ...string) ETCDOption { +func AppendETCDEndpoints(v ...string) ETCDOptionFunc { return func(cc *ETCD) { cc.Endpoints = append(cc.Endpoints, v...) } } // WithETCDTimeoutsPointer timeout设置 -func WithETCDTimeoutsPointer(v *Timeouts) ETCDOption { +func WithETCDTimeoutsPointer(v *Timeouts) ETCDOptionFunc { return func(cc *ETCD) { cc.TimeoutsPointer = v } } // WithETCDRedis option func for filed Redis -func WithETCDRedis(v *Redis) ETCDOption { +func WithETCDRedis(v *Redis) ETCDOptionFunc { return func(cc *ETCD) { cc.Redis = v } @@ -77,7 +87,7 @@ var watchDogETCD func(cc *ETCD) // setETCDDefaultValue default ETCD value func setETCDDefaultValue(cc *ETCD) { cc.writeTimeout = time.Second - for _, opt := range [...]ETCDOption{ + for _, opt := range [...]ETCDOptionFunc{ WithETCDEndpoints([]string{"10.0.0.1", "10.0.0.2"}...), WithETCDTimeoutsPointer(&Timeouts{}), WithETCDRedis(NewRedis()), diff --git a/example/gen_onlyappend_optiongen.go b/example/gen_onlyappend_optiongen.go index 13071e4..b2b808f 100644 --- a/example/gen_onlyappend_optiongen.go +++ b/example/gen_onlyappend_optiongen.go @@ -12,7 +12,7 @@ type OnlyAppend struct { func NewOnlyAppend(opts ...OnlyAppendOption) *OnlyAppend { cc := newDefaultOnlyAppend() for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogOnlyAppend != nil { watchDogOnlyAppend(cc) @@ -27,20 +27,39 @@ func NewOnlyAppend(opts ...OnlyAppendOption) *OnlyAppend { func (cc *OnlyAppend) ApplyOption(opts ...OnlyAppendOption) []OnlyAppendOption { var previous []OnlyAppendOption for _, opt := range opts { - previous = append(previous, opt(cc)) + previous = append(previous, opt.Apply(cc)) } return previous } -// OnlyAppendOption option func -type OnlyAppendOption func(cc *OnlyAppend) OnlyAppendOption +// OnlyAppendOptionFunc option func +type OnlyAppendOption interface { + Apply(cc *OnlyAppend) OnlyAppendOption +} + +var _ OnlyAppendOption = OnlyAppendOptionFunc(nil) + +type OnlyAppendOptionFunc func(cc *OnlyAppend) OnlyAppendOptionFunc + +func (f OnlyAppendOptionFunc) Apply(cc *OnlyAppend) OnlyAppendOption { + return f(cc) +} + +// WithAddress option func for filed Address +func WithAddress(v ...string) OnlyAppendOptionFunc { + return func(cc *OnlyAppend) OnlyAppendOptionFunc { + previous := cc.Address + cc.Address = v + return WithAddress(previous...) + } +} // AppendAddress append func for filed Address -func AppendAddress(v ...string) OnlyAppendOption { - return func(cc *OnlyAppend) OnlyAppendOption { +func AppendAddress(v ...string) OnlyAppendOptionFunc { + return func(cc *OnlyAppend) OnlyAppendOptionFunc { previous := cc.Address cc.Address = append(cc.Address, v...) - return AppendAddress(previous...) + return WithAddress(previous...) } } @@ -52,8 +71,8 @@ var watchDogOnlyAppend func(cc *OnlyAppend) // setOnlyAppendDefaultValue default OnlyAppend value func setOnlyAppendDefaultValue(cc *OnlyAppend) { - for _, opt := range [...]OnlyAppendOption{ - AppendAddress([]string{"10.0.0.1:6379", "10.0.0.2:6379"}...), + for _, opt := range [...]OnlyAppendOptionFunc{ + WithAddress([]string{"10.0.0.1:6379", "10.0.0.2:6379"}...), } { opt(cc) } diff --git a/example/gen_redis_optiongen.go b/example/gen_redis_optiongen.go index 820ce8e..0f86756 100644 --- a/example/gen_redis_optiongen.go +++ b/example/gen_redis_optiongen.go @@ -21,7 +21,7 @@ type Redis struct { func NewRedis(opts ...RedisOption) *Redis { cc := newDefaultRedis() for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogRedis != nil { watchDogRedis(cc) @@ -36,17 +36,27 @@ func NewRedis(opts ...RedisOption) *Redis { func (cc *Redis) ApplyOption(opts ...RedisOption) []RedisOption { var previous []RedisOption for _, opt := range opts { - previous = append(previous, opt(cc)) + previous = append(previous, opt.Apply(cc)) } return previous } -// RedisOption option func -type RedisOption func(cc *Redis) RedisOption +// RedisOptionFunc option func +type RedisOption interface { + Apply(cc *Redis) RedisOption +} + +var _ RedisOption = RedisOptionFunc(nil) + +type RedisOptionFunc func(cc *Redis) RedisOptionFunc + +func (f RedisOptionFunc) Apply(cc *Redis) RedisOption { + return f(cc) +} // WithRedisEndpoints option func for filed Endpoints -func WithRedisEndpoints(v ...string) RedisOption { - return func(cc *Redis) RedisOption { +func WithRedisEndpoints(v ...string) RedisOptionFunc { + return func(cc *Redis) RedisOptionFunc { previous := cc.Endpoints cc.Endpoints = v return WithRedisEndpoints(previous...) @@ -54,8 +64,8 @@ func WithRedisEndpoints(v ...string) RedisOption { } // AppendRedisEndpoints append func for filed Endpoints -func AppendRedisEndpoints(v ...string) RedisOption { - return func(cc *Redis) RedisOption { +func AppendRedisEndpoints(v ...string) RedisOptionFunc { + return func(cc *Redis) RedisOptionFunc { previous := cc.Endpoints cc.Endpoints = append(cc.Endpoints, v...) return WithRedisEndpoints(previous...) @@ -63,8 +73,8 @@ func AppendRedisEndpoints(v ...string) RedisOption { } // AppendRedisAddress append func for filed Address -func AppendRedisAddress(v ...string) RedisOption { - return func(cc *Redis) RedisOption { +func AppendRedisAddress(v ...string) RedisOptionFunc { + return func(cc *Redis) RedisOptionFunc { previous := cc.Address cc.Address = append(cc.Address, v...) return AppendRedisAddress(previous...) @@ -72,8 +82,8 @@ func AppendRedisAddress(v ...string) RedisOption { } // WithRedisCluster option func for filed Cluster -func WithRedisCluster(v bool) RedisOption { - return func(cc *Redis) RedisOption { +func WithRedisCluster(v bool) RedisOptionFunc { + return func(cc *Redis) RedisOptionFunc { previous := cc.Cluster cc.Cluster = v return WithRedisCluster(previous) @@ -81,8 +91,8 @@ func WithRedisCluster(v bool) RedisOption { } // WithRedisTimeoutsStruct option func for filed TimeoutsStruct -func WithRedisTimeoutsStruct(v Timeouts) RedisOption { - return func(cc *Redis) RedisOption { +func WithRedisTimeoutsStruct(v Timeouts) RedisOptionFunc { + return func(cc *Redis) RedisOptionFunc { previous := cc.TimeoutsStruct cc.TimeoutsStruct = v return WithRedisTimeoutsStruct(previous) @@ -97,7 +107,7 @@ var watchDogRedis func(cc *Redis) // setRedisDefaultValue default Redis value func setRedisDefaultValue(cc *Redis) { - for _, opt := range [...]RedisOption{ + for _, opt := range [...]RedisOptionFunc{ WithRedisEndpoints([]string{"192.168.0.1", "192.168.0.2"}...), AppendRedisAddress([]string{"10.0.0.1:6379", "10.0.0.2:6379"}...), WithRedisCluster(true), diff --git a/example/gen_spec_optiongen.go b/example/gen_spec_optiongen.go index 41ce939..d3b96a6 100644 --- a/example/gen_spec_optiongen.go +++ b/example/gen_spec_optiongen.go @@ -29,7 +29,7 @@ type spec struct { func NewSpec(opts ...SpecOption) *spec { cc := newDefaultSpec() for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogSpec != nil { watchDogSpec(cc) @@ -40,36 +40,46 @@ func NewSpec(opts ...SpecOption) *spec { // ApplyOption apply multiple new option func (cc *spec) ApplyOption(opts ...SpecOption) { for _, opt := range opts { - opt(cc) + opt.Apply(cc) } } -// SpecOption option func -type SpecOption func(cc *spec) +// SpecOptionFunc option func +type SpecOption interface { + Apply(cc *spec) +} + +var _ SpecOption = SpecOptionFunc(nil) + +type SpecOptionFunc func(cc *spec) + +func (f SpecOptionFunc) Apply(cc *spec) { + f(cc) +} // WithServerTestBool1 option func for filed TestBool1 -func WithServerTestBool1(v bool) SpecOption { +func WithServerTestBool1(v bool) SpecOptionFunc { return func(cc *spec) { cc.TestBool1 = v } } // WithServerTestInt1 这里是函数注释3,这里是函数注释4 -func WithServerTestInt1(v int) SpecOption { +func WithServerTestInt1(v int) SpecOptionFunc { return func(cc *spec) { cc.TestInt1 = v } } // WithServerTestNilFunc1 option func for filed TestNilFunc1 -func WithServerTestNilFunc1(v func()) SpecOption { +func WithServerTestNilFunc1(v func()) SpecOptionFunc { return func(cc *spec) { cc.TestNilFunc1 = v } } // WithServerTestReserved2Inner1 option func for filed TestReserved2Inner1 -func WithServerTestReserved2Inner1(v int) SpecOption { +func WithServerTestReserved2Inner1(v int) SpecOptionFunc { return func(cc *spec) { cc.TestReserved2Inner1 = v } @@ -85,7 +95,7 @@ var watchDogSpec func(cc *spec) func setSpecDefaultValue(cc *spec) { cc.TestNil1 = nil cc.TestReserved2_ = nil - for _, opt := range [...]SpecOption{ + for _, opt := range [...]SpecOptionFunc{ WithServerTestBool1(false), WithServerTestInt1(32), WithServerTestNilFunc1(nil), diff --git a/example/gen_xxxxxx_optiongen.go b/example/gen_xxxxxx_optiongen.go index e27835f..81c0561 100644 --- a/example/gen_xxxxxx_optiongen.go +++ b/example/gen_xxxxxx_optiongen.go @@ -29,7 +29,7 @@ type XXXXXX struct { func NewXXXXXX(opts ...XXXXXXOption) *XXXXXX { cc := newDefaultXXXXXX() for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDogXXXXXX != nil { watchDogXXXXXX(cc) @@ -44,17 +44,27 @@ func NewXXXXXX(opts ...XXXXXXOption) *XXXXXX { func (cc *XXXXXX) ApplyOption(opts ...XXXXXXOption) []XXXXXXOption { var previous []XXXXXXOption for _, opt := range opts { - previous = append(previous, opt(cc)) + previous = append(previous, opt.Apply(cc)) } return previous } -// XXXXXXOption option func -type XXXXXXOption func(cc *XXXXXX) XXXXXXOption +// XXXXXXOptionFunc option func +type XXXXXXOption interface { + Apply(cc *XXXXXX) XXXXXXOption +} + +var _ XXXXXXOption = XXXXXXOptionFunc(nil) + +type XXXXXXOptionFunc func(cc *XXXXXX) XXXXXXOptionFunc + +func (f XXXXXXOptionFunc) Apply(cc *XXXXXX) XXXXXXOption { + return f(cc) +} // WithXXXXXXOptionUsage option func for filed OptionUsage -func WithXXXXXXOptionUsage(v string) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXOptionUsage(v string) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.OptionUsage cc.OptionUsage = v return WithXXXXXXOptionUsage(previous) @@ -62,8 +72,8 @@ func WithXXXXXXOptionUsage(v string) XXXXXXOption { } // WithXXXXXXEndpoints option func for filed Endpoints -func WithXXXXXXEndpoints(v ...string) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXEndpoints(v ...string) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.Endpoints cc.Endpoints = v return WithXXXXXXEndpoints(previous...) @@ -71,8 +81,8 @@ func WithXXXXXXEndpoints(v ...string) XXXXXXOption { } // AppendXXXXXXEndpoints append func for filed Endpoints -func AppendXXXXXXEndpoints(v ...string) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func AppendXXXXXXEndpoints(v ...string) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.Endpoints cc.Endpoints = append(cc.Endpoints, v...) return WithXXXXXXEndpoints(previous...) @@ -80,8 +90,8 @@ func AppendXXXXXXEndpoints(v ...string) XXXXXXOption { } // WithXXXXXXReadTimeout option func for filed ReadTimeout -func WithXXXXXXReadTimeout(v time.Duration) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXReadTimeout(v time.Duration) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.ReadTimeout cc.ReadTimeout = v return WithXXXXXXReadTimeout(previous) @@ -89,8 +99,8 @@ func WithXXXXXXReadTimeout(v time.Duration) XXXXXXOption { } // WithXXXXXXTypeMapIntString option func for filed TypeMapIntString -func WithXXXXXXTypeMapIntString(v map[int]string) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXTypeMapIntString(v map[int]string) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.TypeMapIntString cc.TypeMapIntString = v return WithXXXXXXTypeMapIntString(previous) @@ -98,8 +108,8 @@ func WithXXXXXXTypeMapIntString(v map[int]string) XXXXXXOption { } // WithXXXXXXTypeSliceInt64 option func for filed TypeSliceInt64 -func WithXXXXXXTypeSliceInt64(v ...int64) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXTypeSliceInt64(v ...int64) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.TypeSliceInt64 cc.TypeSliceInt64 = v return WithXXXXXXTypeSliceInt64(previous...) @@ -107,8 +117,8 @@ func WithXXXXXXTypeSliceInt64(v ...int64) XXXXXXOption { } // AppendXXXXXXTypeSliceInt64 append func for filed TypeSliceInt64 -func AppendXXXXXXTypeSliceInt64(v ...int64) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func AppendXXXXXXTypeSliceInt64(v ...int64) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.TypeSliceInt64 cc.TypeSliceInt64 = append(cc.TypeSliceInt64, v...) return WithXXXXXXTypeSliceInt64(previous...) @@ -116,8 +126,8 @@ func AppendXXXXXXTypeSliceInt64(v ...int64) XXXXXXOption { } // WithXXXXXXTypeBool option func for filed TypeBool -func WithXXXXXXTypeBool(v bool) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXTypeBool(v bool) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.TypeBool cc.TypeBool = v return WithXXXXXXTypeBool(previous) @@ -125,8 +135,8 @@ func WithXXXXXXTypeBool(v bool) XXXXXXOption { } // WithXXXXXXMapRedis option func for filed MapRedis -func WithXXXXXXMapRedis(v map[string]*Redis) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXMapRedis(v map[string]*Redis) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.MapRedis cc.MapRedis = v return WithXXXXXXMapRedis(previous) @@ -136,8 +146,8 @@ func WithXXXXXXMapRedis(v map[string]*Redis) XXXXXXOption { // WithXXXXXXRedis option func for filed Redis // // Deprecated: use MapRedis intead -func WithXXXXXXRedis(v *Redis) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXRedis(v *Redis) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.Redis cc.Redis = v return WithXXXXXXRedis(previous) @@ -145,8 +155,8 @@ func WithXXXXXXRedis(v *Redis) XXXXXXOption { } // WithXXXXXXOnWatchError option func for filed OnWatchError -func WithXXXXXXOnWatchError(v WatchError) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXOnWatchError(v WatchError) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.OnWatchError cc.OnWatchError = v return WithXXXXXXOnWatchError(previous) @@ -154,8 +164,8 @@ func WithXXXXXXOnWatchError(v WatchError) XXXXXXOption { } // WithXXXXXXOnWatchErrorNotNil option func for filed OnWatchErrorNotNil -func WithXXXXXXOnWatchErrorNotNil(v func(loaderName string, confPath string, watchErr error)) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXOnWatchErrorNotNil(v func(loaderName string, confPath string, watchErr error)) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.OnWatchErrorNotNil cc.OnWatchErrorNotNil = v return WithXXXXXXOnWatchErrorNotNil(previous) @@ -163,8 +173,8 @@ func WithXXXXXXOnWatchErrorNotNil(v func(loaderName string, confPath string, wat } // WithXXXXXXTypeSliceDuratuon option func for filed TypeSliceDuratuon -func WithXXXXXXTypeSliceDuratuon(v ...time.Duration) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func WithXXXXXXTypeSliceDuratuon(v ...time.Duration) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.TypeSliceDuratuon cc.TypeSliceDuratuon = v return WithXXXXXXTypeSliceDuratuon(previous...) @@ -172,8 +182,8 @@ func WithXXXXXXTypeSliceDuratuon(v ...time.Duration) XXXXXXOption { } // AppendXXXXXXTypeSliceDuratuon append func for filed TypeSliceDuratuon -func AppendXXXXXXTypeSliceDuratuon(v ...time.Duration) XXXXXXOption { - return func(cc *XXXXXX) XXXXXXOption { +func AppendXXXXXXTypeSliceDuratuon(v ...time.Duration) XXXXXXOptionFunc { + return func(cc *XXXXXX) XXXXXXOptionFunc { previous := cc.TypeSliceDuratuon cc.TypeSliceDuratuon = append(cc.TypeSliceDuratuon, v...) return WithXXXXXXTypeSliceDuratuon(previous...) @@ -188,7 +198,7 @@ var watchDogXXXXXX func(cc *XXXXXX) // setXXXXXXDefaultValue default XXXXXX value func setXXXXXXDefaultValue(cc *XXXXXX) { - for _, opt := range [...]XXXXXXOption{ + for _, opt := range [...]XXXXXXOptionFunc{ WithXXXXXXOptionUsage(optionUsage), WithXXXXXXEndpoints([]string{"10.0.0.1", "10.0.0.2"}...), WithXXXXXXReadTimeout(time.Second), diff --git a/gen.go b/gen.go index 63603d9..1f6ffbf 100644 --- a/gen.go +++ b/gen.go @@ -10,11 +10,12 @@ import ( "sort" "strings" + "myitcv.io/gogenerate" + "github.com/sandwich-go/boost/annotation" "github.com/sandwich-go/boost/xmap" "github.com/sandwich-go/boost/xstrings" "github.com/sandwich-go/boost/xtag" - "myitcv.io/gogenerate" ) type FieldType int @@ -83,15 +84,16 @@ type optionField struct { } type templateData struct { - ClassOptionInfo []optionInfo - ClassComments []string - ClassName string - ClassNameTitle string - ClassOptionTypeName string - ClassNewFuncSignature string - ClassNewFuncName string - XConf bool - OptionReturnPrevious bool + ClassOptionInfo []optionInfo + ClassComments []string + ClassName string + ClassNameTitle string + ClassOptionTypeName string + ClassOptionFunctionTypeName string + ClassNewFuncSignature string + ClassNewFuncName string + XConf bool + OptionReturnPrevious bool VisitorName string InterfaceName string @@ -306,6 +308,7 @@ func (g fileOptionGen) gen() { } optionTypeName = strings.Title(optionTypeName) tmp.ClassOptionTypeName = optionTypeName + tmp.ClassOptionFunctionTypeName = fmt.Sprintf("%sFunc", optionTypeName) tmp.ClassComments = g.Comments tmp.ClassName = g.ClassName tmp.ClassNameTitle = strings.Title(tmp.ClassName) diff --git a/template.go b/template.go index 435c9f6..d8f048e 100644 --- a/template.go +++ b/template.go @@ -30,7 +30,7 @@ type {{ $.ClassName }} struct { {{- end}} {{- end }} for _, opt := range opts { - opt(cc) + opt.Apply(cc) } if watchDog{{$.ClassNameTitle}} != nil { watchDog{{$.ClassNameTitle}}(cc) @@ -46,7 +46,7 @@ type {{ $.ClassName }} struct { func (cc *{{ $.ClassName }}) ApplyOption(opts... {{$.ClassOptionTypeName }}) []{{$.ClassOptionTypeName }}{ var previous []{{$.ClassOptionTypeName }} for _, opt := range opts { - previous = append(previous,opt(cc)) + previous = append(previous,opt.Apply(cc)) } return previous } @@ -54,16 +54,33 @@ func (cc *{{ $.ClassName }}) ApplyOption(opts... {{$.ClassOptionTypeName }}) []{ // ApplyOption apply multiple new option func (cc *{{ $.ClassName }}) ApplyOption(opts... {{$.ClassOptionTypeName }}){ for _, opt := range opts { - opt(cc) + opt.Apply(cc) } } {{- end }} -// {{ $.ClassOptionTypeName }} option func +// {{ $.ClassOptionFunctionTypeName }} option func {{- if $.OptionReturnPrevious }} -type {{ $.ClassOptionTypeName }} func(cc *{{$.ClassName}}) {{ $.ClassOptionTypeName }} +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 }} func(cc *{{$.ClassName}}) +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 }} @@ -72,21 +89,21 @@ type {{ $.ClassOptionTypeName }} func(cc *{{$.ClassName}}) {{- if not $option.OnlyAppend }} {{- if $.OptionReturnPrevious }} {{ unescaped $option.OptionComment }} -func {{$option.OptionFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOptionTypeName }} { return func(cc *{{$.ClassName}}) {{ $.ClassOptionTypeName }} { +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}}) {{ $.ClassOptionTypeName }} { return func(cc *{{$.ClassName}}) { +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}}) {{ $.ClassOptionTypeName }} { return func(cc *{{$.ClassName}}) {{ $.ClassOptionTypeName }} { +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}} @@ -96,20 +113,20 @@ func {{$option.AppendFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOption return {{$OptionFuncName}}(previous...) } } {{- else }} -func {{$option.AppendFuncName}}(v ...{{$option.SliceElemType}}) {{ $.ClassOptionTypeName }} { return func(cc *{{$.ClassName}}) { +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}}) {{ $.ClassOptionTypeName }} { return func(cc *{{$.ClassName}}) {{ $.ClassOptionTypeName }} { +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}}) {{ $.ClassOptionTypeName }} { return func(cc *{{$.ClassName}}) { +func {{$option.OptionFuncName}}(v {{$option.Type}}) {{ $.ClassOptionFunctionTypeName }} { return func(cc *{{$.ClassName}}) { cc.{{$option.Name}} = v } } {{- end }} @@ -135,7 +152,7 @@ func set{{ $.ClassNameTitle }}DefaultValue (cc *{{ $.ClassName }}) { {{- end }} {{- end }} {{- end }} - for _, opt := range [...]{{ $.ClassOptionTypeName }} { + for _, opt := range [...]{{ $.ClassOptionFunctionTypeName }} { {{- range $index, $option := $.ClassOptionInfo }} {{- if eq $option.GenOptionFunc true }} {{- if eq $option.Slice true }}