From 35630bd5b13bc34e139fa3e5a9ca36aa6d169de4 Mon Sep 17 00:00:00 2001 From: Ubuntu <675502062@qq.com> Date: Sat, 4 Dec 2021 01:03:54 -0800 Subject: [PATCH] 'user define' to 'custom' --- common/constant/key.go | 2 +- config/root_config.go | 10 ++-- .../user_define.yaml => custom/custom.yaml} | 4 +- .../empty_log.yaml => custom/empty.yaml} | 4 +- config/user_define_config.go | 45 ++++++++-------- config/user_define_config_test.go | 52 +++++++++++++------ 6 files changed, 70 insertions(+), 47 deletions(-) rename config/testdata/config/{user_define/user_define.yaml => custom/custom.yaml} (85%) rename config/testdata/config/{user_define/empty_log.yaml => custom/empty.yaml} (78%) diff --git a/common/constant/key.go b/common/constant/key.go index d5f866faf2..871d2719dd 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -209,7 +209,7 @@ const ( RouterConfigPrefix = "dubbo.router" TracingConfigPrefix = "dubbo.tracing" LoggerConfigPrefix = "dubbo.logger" - UserDefineConfigPrefix = "dubbo.user-define" + CustomConfigPrefix = "dubbo.custom" ) const ( diff --git a/config/root_config.go b/config/root_config.go index 91fe760903..ad3c33acea 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -81,7 +81,7 @@ type RootConfig struct { // cache file used to store the current used configurations. CacheFile string `yaml:"cache_file" json:"cache_file,omitempty" property:"cache_file"` - UserDefine *UserDefineConfig `yaml:"user-define" json:"user-define,omitempty" property:"user-define"` + Custom *CustomConfig `yaml:"custom" json:"custom,omitempty" property:"custom"` } func SetRootConfig(r RootConfig) { @@ -156,7 +156,7 @@ func (rc *RootConfig) Init() error { } // init user define - if err := rc.UserDefine.Init(); err != nil { + if err := rc.Custom.Init(); err != nil { return err } @@ -233,7 +233,7 @@ func newEmptyRootConfig() *RootConfig { Consumer: NewConsumerConfigBuilder().Build(), Metric: NewMetricConfigBuilder().Build(), Logger: NewLoggerConfigBuilder().Build(), - UserDefine: NewUserDefineConfigBuilder().Build(), + Custom: NewCustomConfigBuilder().Build(), } return newRootConfig } @@ -321,8 +321,8 @@ func (rb *RootConfigBuilder) SetConfigCenter(configCenterConfig *CenterConfig) * return rb } -func (rb *RootConfigBuilder) SetUserDefine(userDefineConfig *UserDefineConfig) *RootConfigBuilder { - rb.rootConfig.UserDefine = userDefineConfig +func (rb *RootConfigBuilder) SetCustom(customConfig *CustomConfig) *RootConfigBuilder { + rb.rootConfig.Custom = customConfig return rb } diff --git a/config/testdata/config/user_define/user_define.yaml b/config/testdata/config/custom/custom.yaml similarity index 85% rename from config/testdata/config/user_define/user_define.yaml rename to config/testdata/config/custom/custom.yaml index e820dc00ea..7c11042321 100644 --- a/config/testdata/config/user_define/user_define.yaml +++ b/config/testdata/config/custom/custom.yaml @@ -8,8 +8,8 @@ dubbo: protocol: zookeeper group: test address: 127.0.0.1:2181 - user-define: + custom: name: test version: v2.0 define-config: - test-config: true \ No newline at end of file + test-config: true diff --git a/config/testdata/config/user_define/empty_log.yaml b/config/testdata/config/custom/empty.yaml similarity index 78% rename from config/testdata/config/user_define/empty_log.yaml rename to config/testdata/config/custom/empty.yaml index c37cdd0e6b..a4611db1e6 100644 --- a/config/testdata/config/user_define/empty_log.yaml +++ b/config/testdata/config/custom/empty.yaml @@ -1,5 +1,5 @@ dubbo: - user-define: + custom: registries: nacos: timeout: 5s @@ -8,4 +8,4 @@ dubbo: zk: protocol: zookeeper group: test - address: 127.0.0.1:2181 \ No newline at end of file + address: 127.0.0.1:2181 diff --git a/config/user_define_config.go b/config/user_define_config.go index e82cead2fe..c13bbf88a7 100644 --- a/config/user_define_config.go +++ b/config/user_define_config.go @@ -25,57 +25,60 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" ) -type UserDefineConfig struct { +type CustomConfig struct { Name string `default:"user-config" yaml:"name" json:"name,omitempty" property:"name"` Version string `default:"v1.0" yaml:"version" json:"version,omitempty" property:"version"` DefineConfig map[string]interface{} `yaml:"define-config" json:"define-config,omitempty" property:"define-config"` } -func (*UserDefineConfig) Prefix() string { - return constant.UserDefineConfigPrefix +func (*CustomConfig) Prefix() string { + return constant.CustomConfigPrefix } -func (udc *UserDefineConfig) Init() error { +func (udc *CustomConfig) Init() error { return udc.check() } -func (udc *UserDefineConfig) check() error { +func (udc *CustomConfig) check() error { if err := defaults.Set(udc); err != nil { return err } return verify(udc) } -func (udc *UserDefineConfig) GetDefineValue(key string, default_value interface{}) interface{} { +func (udc *CustomConfig) GetDefineValue(key string, default_value interface{}) interface{} { if define_value, ok := udc.DefineConfig[key]; ok { return define_value } return default_value } -type UserDefineConfigBuilder struct { - userDefineConfig *UserDefineConfig +type CustomConfigBuilder struct { + customConfig *CustomConfig } -func NewUserDefineConfigBuilder() *UserDefineConfigBuilder { - return &UserDefineConfigBuilder{userDefineConfig: &UserDefineConfig{}} +func NewCustomConfigBuilder() *CustomConfigBuilder { + return &CustomConfigBuilder{customConfig: &CustomConfig{}} } -func (udcb *UserDefineConfigBuilder) SetName(name string) *UserDefineConfigBuilder { - udcb.userDefineConfig.Name = name - return udcb +func (ccb *CustomConfigBuilder) SetName(name string) *CustomConfigBuilder { + ccb.customConfig.Name = name + return ccb } -func (udcb *UserDefineConfigBuilder) SetVersion(version string) *UserDefineConfigBuilder { - udcb.userDefineConfig.Version = version - return udcb +func (ccb *CustomConfigBuilder) SetVersion(version string) *CustomConfigBuilder { + ccb.customConfig.Version = version + return ccb } -func (udcb *UserDefineConfigBuilder) SetDefineConfig(key string, val interface{}) *UserDefineConfigBuilder { - udcb.userDefineConfig.DefineConfig[key] = val - return udcb +func (ccb *CustomConfigBuilder) SetDefineConfig(key string, val interface{}) *CustomConfigBuilder { + if ccb.customConfig.DefineConfig == nil { + ccb.customConfig.DefineConfig = make(map[string]interface{}) + } + ccb.customConfig.DefineConfig[key] = val + return ccb } -func (udcb *UserDefineConfigBuilder) Build() *UserDefineConfig { - return udcb.userDefineConfig +func (ccb *CustomConfigBuilder) Build() *CustomConfig { + return ccb.customConfig } diff --git a/config/user_define_config_test.go b/config/user_define_config_test.go index 6438baf14d..22e183d7f1 100644 --- a/config/user_define_config_test.go +++ b/config/user_define_config_test.go @@ -25,29 +25,49 @@ import ( "github.com/stretchr/testify/assert" ) -func TestUserDefineInit(t *testing.T) { +func TestCustomInit(t *testing.T) { t.Run("empty use default", func(t *testing.T) { - err := Load(WithPath("./testdata/config/user_define/empty_log.yaml")) + err := Load(WithPath("./testdata/config/custom/empty.yaml")) assert.Nil(t, err) assert.NotNil(t, rootConfig) - UserDefineConfig := rootConfig.UserDefine - assert.NotNil(t, UserDefineConfig) - assert.Equal(t, UserDefineConfig.Name, "user-config") - assert.Equal(t, UserDefineConfig.Version, "v1.0") - assert.Equal(t, UserDefineConfig.DefineConfig, map[string]interface{}(nil)) - assert.Equal(t, UserDefineConfig.GetDefineValue("test", "test"), "test") + CustomConfig := rootConfig.Custom + assert.NotNil(t, CustomConfig) + assert.Equal(t, CustomConfig.Name, "user-config") + assert.Equal(t, CustomConfig.Version, "v1.0") + assert.Equal(t, CustomConfig.DefineConfig, map[string]interface{}(nil)) + assert.Equal(t, CustomConfig.GetDefineValue("test", "test"), "test") }) t.Run("use config", func(t *testing.T) { - err := Load(WithPath("./testdata/config/user_define/user_define.yaml")) + err := Load(WithPath("./testdata/config/custom/custom.yaml")) assert.Nil(t, err) assert.NotNil(t, rootConfig) - UserDefineConfig := rootConfig.UserDefine - assert.NotNil(t, UserDefineConfig) - assert.Equal(t, UserDefineConfig.Name, "test") - assert.Equal(t, UserDefineConfig.Version, "v2.0") - assert.Equal(t, UserDefineConfig.DefineConfig, map[string]interface{}{"test-config": true}) - assert.Equal(t, UserDefineConfig.GetDefineValue("test-config", false), true) - assert.Equal(t, UserDefineConfig.GetDefineValue("test-no-config", false), false) + CustomConfig := rootConfig.Custom + assert.NotNil(t, CustomConfig) + assert.Equal(t, CustomConfig.Name, "test") + assert.Equal(t, CustomConfig.Version, "v2.0") + assert.Equal(t, CustomConfig.DefineConfig, map[string]interface{}{"test-config": true}) + assert.Equal(t, CustomConfig.GetDefineValue("test-config", false), true) + assert.Equal(t, CustomConfig.GetDefineValue("test-no-config", false), false) + }) + + t.Run("config builder", func(t *testing.T) { + CustomConfigBuilder := NewCustomConfigBuilder() + CustomConfigBuilder.SetName("builder") + CustomConfigBuilder.SetVersion("v3.0") + CustomConfigBuilder.SetDefineConfig("test-build", true) + CustomConfig := CustomConfigBuilder.Build() + assert.NotNil(t, CustomConfig) + assert.Equal(t, CustomConfig.Name, "builder") + assert.Equal(t, CustomConfig.Version, "v3.0") + assert.Equal(t, CustomConfig.GetDefineValue("test-build", false), true) + assert.Equal(t, CustomConfig.GetDefineValue("test-no-build", false), false) + rt := NewRootConfigBuilder().Build() + assert.Equal(t, rt.Custom.Name, "") + assert.Equal(t, rt.Custom.Version, "") + rt = NewRootConfigBuilder().SetCustom(CustomConfig).Build() + rt.Init() + assert.Equal(t, rt.Custom.Name, "builder") + assert.Equal(t, rt.Custom.Version, "v3.0") }) }