Skip to content

Commit

Permalink
Add: user define config (#1640)
Browse files Browse the repository at this point in the history
* add user define config

* 'user define' to 'custom'

* format fix

* rename 2 files

* remove custom.Name

* remove custom.Version

* fix indent

* Fix: ut

Co-authored-by: LaurenceLiZhixin <[email protected]>
  • Loading branch information
A-Wanderer and LaurenceLiZhixin committed Dec 6, 2021
1 parent 22ec041 commit e4fa5df
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ const (
RouterConfigPrefix = "dubbo.router"
TracingConfigPrefix = "dubbo.tracing"
LoggerConfigPrefix = "dubbo.logger"
CustomConfigPrefix = "dubbo.custom"
)

const (
Expand Down
80 changes: 80 additions & 0 deletions config/custom_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"github.com/creasty/defaults"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
)

type CustomConfig struct {
ConfigMap map[string]interface{} `yaml:"config-map" json:"config-map,omitempty" property:"config-map"`
}

func (*CustomConfig) Prefix() string {
return constant.CustomConfigPrefix
}

func (c *CustomConfig) Init() error {
return c.check()
}

func (c *CustomConfig) check() error {
if err := defaults.Set(c); err != nil {
return err
}
return verify(c)
}

func (c *CustomConfig) GetDefineValue(key string, default_value interface{}) interface{} {
if define_value, ok := c.ConfigMap[key]; ok {
return define_value
}
return default_value
}

func GetDefineValue(key string, default_value interface{}) interface{} {
rt := GetRootConfig()
if rt.Custom == nil {
return default_value
}
return rt.Custom.GetDefineValue(key, default_value)
}

type CustomConfigBuilder struct {
customConfig *CustomConfig
}

func NewCustomConfigBuilder() *CustomConfigBuilder {
return &CustomConfigBuilder{customConfig: &CustomConfig{}}
}

func (ccb *CustomConfigBuilder) SetDefineConfig(key string, val interface{}) *CustomConfigBuilder {
if ccb.customConfig.ConfigMap == nil {
ccb.customConfig.ConfigMap = make(map[string]interface{})
}
ccb.customConfig.ConfigMap[key] = val
return ccb
}

func (ccb *CustomConfigBuilder) Build() *CustomConfig {
return ccb.customConfig
}
68 changes: 68 additions & 0 deletions config/custom_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestCustomInit(t *testing.T) {
t.Run("empty use default", func(t *testing.T) {
err := Load(WithPath("./testdata/config/custom/empty.yaml"))
assert.Nil(t, err)
assert.NotNil(t, rootConfig)
CustomConfig := rootConfig.Custom
assert.NotNil(t, CustomConfig)
assert.Equal(t, CustomConfig.ConfigMap, map[string]interface{}(nil))
assert.Equal(t, CustomConfig.GetDefineValue("test", "test"), "test")
assert.Equal(t, GetDefineValue("test", "test"), "test")
})

t.Run("use config", func(t *testing.T) {
err := Load(WithPath("./testdata/config/custom/custom.yaml"))
assert.Nil(t, err)
assert.NotNil(t, rootConfig)
CustomConfig := rootConfig.Custom
assert.NotNil(t, CustomConfig)
assert.Equal(t, CustomConfig.ConfigMap, 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)
assert.Equal(t, GetDefineValue("test-config", false), true)
assert.Equal(t, GetDefineValue("test-no-config", false), false)
})

t.Run("config builder", func(t *testing.T) {
CustomConfigBuilder := NewCustomConfigBuilder()
CustomConfigBuilder.SetDefineConfig("test-build", true)
CustomConfig := CustomConfigBuilder.Build()
assert.NotNil(t, CustomConfig)
assert.Equal(t, CustomConfig.GetDefineValue("test-build", false), true)
assert.Equal(t, CustomConfig.GetDefineValue("test-no-build", false), false)
// todo @(laurence) now we should guarantee rootConfig ptr can't be changed during test
tempRootConfig := rootConfig
rt := NewRootConfigBuilder().SetCustom(CustomConfig).Build()
SetRootConfig(*rt)
assert.Equal(t, GetDefineValue("test-build", false), true)
assert.Equal(t, GetDefineValue("test-no-build", false), false)
SetRootConfig(*tempRootConfig)
})
}
13 changes: 13 additions & 0 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type RootConfig struct {

// cache file used to store the current used configurations.
CacheFile string `yaml:"cache_file" json:"cache_file,omitempty" property:"cache_file"`

Custom *CustomConfig `yaml:"custom" json:"custom,omitempty" property:"custom"`
}

func SetRootConfig(r RootConfig) {
Expand Down Expand Up @@ -153,6 +155,11 @@ func (rc *RootConfig) Init() error {
return err
}

// init user define
if err := rc.Custom.Init(); err != nil {
return err
}

// init protocol
protocols := rc.Protocols
if len(protocols) <= 0 {
Expand Down Expand Up @@ -226,6 +233,7 @@ func newEmptyRootConfig() *RootConfig {
Consumer: NewConsumerConfigBuilder().Build(),
Metric: NewMetricConfigBuilder().Build(),
Logger: NewLoggerConfigBuilder().Build(),
Custom: NewCustomConfigBuilder().Build(),
}
return newRootConfig
}
Expand Down Expand Up @@ -313,6 +321,11 @@ func (rb *RootConfigBuilder) SetConfigCenter(configCenterConfig *CenterConfig) *
return rb
}

func (rb *RootConfigBuilder) SetCustom(customConfig *CustomConfig) *RootConfigBuilder {
rb.rootConfig.Custom = customConfig
return rb
}

func (rb *RootConfigBuilder) Build() *RootConfig {
return rb.rootConfig
}
Expand Down
13 changes: 13 additions & 0 deletions config/testdata/config/custom/custom.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dubbo:
registries:
nacos:
timeout: 5s
group: dev
address: nacos://127.0.0.1:8848
zk:
protocol: zookeeper
group: test
address: 127.0.0.1:2181
custom:
config-map:
test-config: true
11 changes: 11 additions & 0 deletions config/testdata/config/custom/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dubbo:
custom:
registries:
nacos:
timeout: 5s
group: dev
address: nacos://127.0.0.1:8848
zk:
protocol: zookeeper
group: test
address: 127.0.0.1:2181

0 comments on commit e4fa5df

Please sign in to comment.