diff --git a/tests/all_test.go b/tests/all_test.go index d2376bc..31e8334 100644 --- a/tests/all_test.go +++ b/tests/all_test.go @@ -242,48 +242,6 @@ func TestMapMerge(t *testing.T) { }) } -type confTestNestedSquash struct { - Nested1 `xconf:",squash"` - Nested2 `xconf:",squash"` -} -type confTestNestedSquashOff struct { - Nested1 `xconf:"nested1"` - Nested2 `xconf:"nested2"` -} - -func TestSquash(t *testing.T) { - Convey("TestSquash Enable", t, func(c C) { - cc := &confTestNestedSquash{} - cc.Nested1.Deadline = time.Now() - cc.TimeoutMap = map[string]time.Duration{"read": time.Second} - x := xconf.New( - xconf.WithFlagSet(flag.NewFlagSet("suqash_anable", flag.ContinueOnError)), - xconf.WithFlagArgs(), - xconf.WithDebug(true), - xconf.WithMapMerge(true), - ) - So(x.Parse(cc), ShouldBeNil) - So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeFalse) - So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeFalse) - So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeFalse) - }) - Convey("TestSquash Disable", t, func(c C) { - cc := &confTestNestedSquashOff{} - cc.Nested1.Deadline = time.Now() - cc.Nested2.TimeoutMap = map[string]time.Duration{"read": time.Second} - x := xconf.New( - xconf.WithFlagSet(flag.NewFlagSet("suqash_disable", flag.ContinueOnError)), - xconf.WithFlagArgs(), - xconf.WithDebug(true), - xconf.WithMapMerge(true), - ) - So(x.Parse(cc), ShouldBeNil) - So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeTrue) - So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeTrue) - So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeTrue) - }) -} - type TestConf1 struct { HTTPAddress string `xconf:"http_address" default:"0.0.0.0:0000"` Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"` diff --git a/tests/bugs/bugs_test.go b/tests/bugs/bugs_test.go index 275f3c6..8133b24 100644 --- a/tests/bugs/bugs_test.go +++ b/tests/bugs/bugs_test.go @@ -2,7 +2,9 @@ package bugs import ( "bytes" + "flag" "os" + "strings" "testing" "time" @@ -62,3 +64,52 @@ func TestBug_7(t *testing.T) { So(xconf.SaveVarToWriterAsYAML(ss, os.Stderr), ShouldBeNil) }) } + +type Nested1 struct { + Deadline time.Time `xconf:"deadline"` + DeadlineAsSecond int +} +type Nested2 struct { + TimeoutMap map[string]time.Duration `xconf:"timeout_map"` +} +type confTestNestedSquash struct { + Nested1 `xconf:",squash"` + Nested2 `xconf:",squash"` +} +type confTestNestedSquashOff struct { + Nested1 `xconf:"nested1"` + Nested2 `xconf:"nested2"` +} + +func TestBug_8(t *testing.T) { + Convey("TestSquash Enable", t, func(c C) { + cc := &confTestNestedSquash{} + cc.Nested1.Deadline = time.Now() + cc.TimeoutMap = map[string]time.Duration{"read": time.Second} + x := xconf.New( + xconf.WithFlagSet(flag.NewFlagSet("suqash_anable", flag.ContinueOnError)), + xconf.WithFlagArgs(), + xconf.WithDebug(true), + xconf.WithMapMerge(true), + ) + So(x.Parse(cc), ShouldBeNil) + So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeFalse) + So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeFalse) + So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeFalse) + }) + Convey("TestSquash Disable", t, func(c C) { + cc := &confTestNestedSquashOff{} + cc.Nested1.Deadline = time.Now() + cc.Nested2.TimeoutMap = map[string]time.Duration{"read": time.Second} + x := xconf.New( + xconf.WithFlagSet(flag.NewFlagSet("suqash_disable", flag.ContinueOnError)), + xconf.WithFlagArgs(), + xconf.WithDebug(true), + xconf.WithMapMerge(true), + ) + So(x.Parse(cc), ShouldBeNil) + So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeTrue) + So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeTrue) + So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeTrue) + }) +} diff --git a/tests/conf.go b/tests/conf.go index 77c6e69..cfadcb8 100644 --- a/tests/conf.go +++ b/tests/conf.go @@ -29,16 +29,6 @@ type RedisTimeout = redis.Timeout var optionUsage = `在这里描述一些应用级别的配置规则` -type Nested1 struct { - Deadline time.Time `xconf:"deadline"` - DeadlineAsSecond int -} -type Nested2 struct { - TimeoutMap map[string]time.Duration `xconf:"timeout_map"` -} - -var defaultNested1 = Nested1{Deadline: time.Now()} - // ConfigOptionDeclareWithDefault go-lint //go:generate optiongen --option_with_struct_name=false --new_func=NewTestConfig --xconf=true --empty_composite_nil=true --usage_tag_name=usage func ConfigOptionDeclareWithDefault() interface{} { @@ -71,9 +61,5 @@ func ConfigOptionDeclareWithDefault() interface{} { "RedisAsPointer": (*Redis)(&redis.Conf{}), "Redis": (Redis)(redis.Conf{}), "RedisTimeout": (*RedisTimeout)(&redis.Timeout{}), - // annotation@Nested1(xconf=",squash",inline="true") - "Nested1": (Nested1)(defaultNested1), - // annotation@Nested2(inline="true") - "Nested2": (*Nested2)(nil), } } diff --git a/tests/gen_config_optiongen.go b/tests/gen_config_optiongen.go index 78af4dd..9346032 100644 --- a/tests/gen_config_optiongen.go +++ b/tests/gen_config_optiongen.go @@ -36,10 +36,6 @@ type Config struct { RedisAsPointer *Redis `xconf:"redis_as_pointer"` Redis Redis `xconf:"redis"` RedisTimeout *RedisTimeout `xconf:"redis_timeout"` - // annotation@Nested1(xconf=",squash",inline="true") - Nested1 `xconf:",squash"` - // annotation@Nested2(inline="true") - *Nested2 `xconf:"nested2"` } // NewTestConfig new Config @@ -260,24 +256,6 @@ func WithRedisTimeout(v *RedisTimeout) ConfigOption { } } -// WithNested1 option func for filed Nested1 -func WithNested1(v Nested1) ConfigOption { - return func(cc *Config) ConfigOption { - previous := cc.Nested1 - cc.Nested1 = v - return WithNested1(previous) - } -} - -// WithNested2 option func for filed Nested2 -func WithNested2(v *Nested2) ConfigOption { - return func(cc *Config) ConfigOption { - previous := cc.Nested2 - cc.Nested2 = v - return WithNested2(previous) - } -} - // InstallConfigWatchDog the installed func will called when NewTestConfig called func InstallConfigWatchDog(dog func(cc *Config)) { watchDogConfig = dog } @@ -310,8 +288,6 @@ func newDefaultConfig() *Config { WithRedisAsPointer(&redis.Conf{}), WithRedis(redis.Conf{}), WithRedisTimeout(&redis.Timeout{}), - WithNested1(defaultNested1), - WithNested2(nil), } { opt(cc) } @@ -384,8 +360,6 @@ func (cc *Config) GetTestBoolTrue() bool { return cc.TestBoolTrue } func (cc *Config) GetRedisAsPointer() *Redis { return cc.RedisAsPointer } func (cc *Config) GetRedis() Redis { return cc.Redis } func (cc *Config) GetRedisTimeout() *RedisTimeout { return cc.RedisTimeout } -func (cc *Config) GetNested1() Nested1 { return cc.Nested1 } -func (cc *Config) GetNested2() *Nested2 { return cc.Nested2 } // ConfigVisitor visitor interface for Config type ConfigVisitor interface { @@ -413,8 +387,6 @@ type ConfigVisitor interface { GetRedisAsPointer() *Redis GetRedis() Redis GetRedisTimeout() *RedisTimeout - GetNested1() Nested1 - GetNested2() *Nested2 } // ConfigInterface visitor + ApplyOption interface for Config