Skip to content

Commit

Permalink
'user define' to 'custom'
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Wanderer committed Dec 4, 2021
1 parent 06a1e00 commit 35630bd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 47 deletions.
2 changes: 1 addition & 1 deletion common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const (
RouterConfigPrefix = "dubbo.router"
TracingConfigPrefix = "dubbo.tracing"
LoggerConfigPrefix = "dubbo.logger"
UserDefineConfigPrefix = "dubbo.user-define"
CustomConfigPrefix = "dubbo.custom"
)

const (
Expand Down
10 changes: 5 additions & 5 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -233,7 +233,7 @@ func newEmptyRootConfig() *RootConfig {
Consumer: NewConsumerConfigBuilder().Build(),
Metric: NewMetricConfigBuilder().Build(),
Logger: NewLoggerConfigBuilder().Build(),
UserDefine: NewUserDefineConfigBuilder().Build(),
Custom: NewCustomConfigBuilder().Build(),
}
return newRootConfig
}
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
test-config: true
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dubbo:
user-define:
custom:
registries:
nacos:
timeout: 5s
Expand All @@ -8,4 +8,4 @@ dubbo:
zk:
protocol: zookeeper
group: test
address: 127.0.0.1:2181
address: 127.0.0.1:2181
45 changes: 24 additions & 21 deletions config/user_define_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
52 changes: 36 additions & 16 deletions config/user_define_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

0 comments on commit 35630bd

Please sign in to comment.