diff --git a/config/application_config.go b/config/application_config.go index af4ffd6acf4813c4e5496df64bcc31943b06a16f..fcd4d38c9b55963c32d58fdd1b80375083a76d8c 100644 --- a/config/application_config.go +++ b/config/application_config.go @@ -17,7 +17,13 @@ package config -import "github.com/apache/dubbo-go/common/constant" +import ( + "github.com/creasty/defaults" +) + +import ( + "github.com/apache/dubbo-go/common/constant" +) type ApplicationConfig struct { Organization string `yaml:"organization" json:"organization,omitempty" property:"organization"` @@ -37,3 +43,13 @@ func (c *ApplicationConfig) Id() string { func (c *ApplicationConfig) SetId(id string) { } +func (c *ApplicationConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain ApplicationConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} diff --git a/config/config_center_config.go b/config/config_center_config.go index e7bbd8e24f259fb8708aee6e2ef7351ed7de19db..9a0bd1ee1dcfdd37476540568baaadb824db2d72 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -22,6 +22,10 @@ import ( "time" ) +import ( + "github.com/creasty/defaults" +) + type ConfigCenterConfig struct { context context.Context Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty"` @@ -35,3 +39,14 @@ type ConfigCenterConfig struct { TimeoutStr string `yaml:"timeout" json:"timeout,omitempty"` timeout time.Duration } + +func (c *ConfigCenterConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain ConfigCenterConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} diff --git a/config/consumer_config.go b/config/consumer_config.go index 48d1a27609019569dbc7758ed37b15556174e221..9a9f734c4e55924fb7137f090a5d2e4237534a6a 100644 --- a/config/consumer_config.go +++ b/config/consumer_config.go @@ -24,6 +24,7 @@ import ( ) import ( + "github.com/creasty/defaults" perrors "github.com/pkg/errors" "gopkg.in/yaml.v2" ) @@ -58,6 +59,17 @@ type ConsumerConfig struct { FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` } +func (c *ConsumerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain ConsumerConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} + func (*ConsumerConfig) Prefix() string { return constant.ConsumerConfigPrefix } diff --git a/config/method_config.go b/config/method_config.go index 95479d1b6586683632ede12e9ce469eb5268fb00..aff322535a0fe50bf3a6e2fbf5099ec81fdbcdcd 100644 --- a/config/method_config.go +++ b/config/method_config.go @@ -16,6 +16,10 @@ */ package config +import ( + "github.com/creasty/defaults" +) + import ( "github.com/apache/dubbo-go/common/constant" ) @@ -36,3 +40,14 @@ func (c *MethodConfig) Prefix() string { return constant.DUBBO + "." + c.InterfaceName + "." + c.Name + "." } } + +func (c *MethodConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain MethodConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} diff --git a/config/provider_config.go b/config/provider_config.go index 726d05ae6e5eb09773621bfa42a4d7f47a862297..2302cdf0b23f35649065bd50a9ffd2b9a685e8d4 100644 --- a/config/provider_config.go +++ b/config/provider_config.go @@ -23,6 +23,7 @@ import ( ) import ( + "github.com/creasty/defaults" perrors "github.com/pkg/errors" "gopkg.in/yaml.v2" ) @@ -50,6 +51,17 @@ type ProviderConfig struct { FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` } +func (c *ProviderConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain ProviderConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} + func (*ProviderConfig) Prefix() string { return constant.ProviderConfigPrefix } diff --git a/config/provider_config_test.go b/config/provider_config_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e3db2221756731acd3cfc9ff0f29cd035897713e --- /dev/null +++ b/config/provider_config_test.go @@ -0,0 +1,19 @@ +package config + +import ( + "path/filepath" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +func TestProviderInit(t *testing.T) { + conPath, err := filepath.Abs("./testdata/consumer_config_with_configcenter.yml") + assert.NoError(t, err) + assert.NoError(t, ConsumerInit(conPath)) + assert.Equal(t, "default", consumerConfig.ProxyFactory) + assert.Equal(t, "dubbo.properties", consumerConfig.ConfigCenterConfig.ConfigFile) + assert.Equal(t, "100ms", consumerConfig.Connect_Timeout) +} diff --git a/config/reference_config.go b/config/reference_config.go index b3a3bae447d370be281d1786131521e8ba59a22f..4b063bc60bc64961f2e791eca4ea16f26d8de5b1 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -25,6 +25,10 @@ import ( "time" ) +import ( + "github.com/creasty/defaults" +) + import ( "github.com/apache/dubbo-go/cluster/directory" "github.com/apache/dubbo-go/common" @@ -68,6 +72,7 @@ func NewReferenceConfig(id string, ctx context.Context) *ReferenceConfig { } func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + type rf ReferenceConfig raw := rf{} // Put your defaults here if err := unmarshal(&raw); err != nil { @@ -75,6 +80,10 @@ func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) erro } *refconfig = ReferenceConfig(raw) + if err := defaults.Set(refconfig); err != nil { + return err + } + return nil } diff --git a/config/registry_config.go b/config/registry_config.go index 480377754adeab7da0815a2ee918753b42ccd195..9ffa41eb5b5b3b5ae4dc9f77812c0aef5ce9835f 100644 --- a/config/registry_config.go +++ b/config/registry_config.go @@ -24,6 +24,10 @@ import ( "strings" ) +import ( + "github.com/creasty/defaults" +) + import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" @@ -42,6 +46,17 @@ type RegistryConfig struct { Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` } +func (c *RegistryConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain RegistryConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} + func (*RegistryConfig) Prefix() string { return constant.RegistryConfigPrefix + "|" + constant.SingleRegistryConfigPrefix } diff --git a/config/service_config.go b/config/service_config.go index 05cdc84f5b48528788ccc94d443ea182fbd0f263..85af4d048edf62b8b014dba69a31f76dbce3eef2 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -28,6 +28,7 @@ import ( ) import ( + "github.com/creasty/defaults" perrors "github.com/pkg/errors" "go.uber.org/atomic" ) @@ -67,6 +68,17 @@ func (c *ServiceConfig) Prefix() string { return constant.ServiceConfigPrefix + c.InterfaceName + "." } +func (c *ServiceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + if err := defaults.Set(c); err != nil { + return err + } + type plain ServiceConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return nil +} + // The only way to get a new ServiceConfig func NewServiceConfig(id string, context context.Context) *ServiceConfig { diff --git a/go.mod b/go.mod index 37781aee510ddf191dd2388eb1f9509e753fbf84..fa66c6a6473e4b1d3c1f6757d6465d0426af2407 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect + github.com/creasty/defaults v1.3.0 github.com/dubbogo/getty v1.2.2 github.com/dubbogo/gost v1.1.1 github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect diff --git a/go.sum b/go.sum index 24b4a7afb0729e59557dcdb611c8e56dff372c9b..4add9fcf790eb8c97ef41c131b06a2dc794dc0ca 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f h1:JOrtw2xFKzlg+ github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/creasty/defaults v1.3.0 h1:uG+RAxYbJgOPCOdKEcec9ZJXeva7Y6mj/8egdzwmLtw= +github.com/creasty/defaults v1.3.0/go.mod h1:CIEEvs7oIVZm30R8VxtFJs+4k201gReYyuYHJxZc68I= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=