This is an automated email from the ASF dual-hosted git repository.

laurence pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new e4fa5df  Add: user define config (#1640)
e4fa5df is described below

commit e4fa5dfc3d1f0d0c6efe88cfaa5967f7fc9c29ee
Author: A-Wanderer <[email protected]>
AuthorDate: Tue Dec 7 00:10:56 2021 +0800

    Add: user define config (#1640)
    
    * 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]>
---
 common/constant/key.go                    |  1 +
 config/custom_config.go                   | 80 +++++++++++++++++++++++++++++++
 config/custom_config_test.go              | 68 ++++++++++++++++++++++++++
 config/root_config.go                     | 13 +++++
 config/testdata/config/custom/custom.yaml | 13 +++++
 config/testdata/config/custom/empty.yaml  | 11 +++++
 6 files changed, 186 insertions(+)

diff --git a/common/constant/key.go b/common/constant/key.go
index 4c55383..871d271 100644
--- a/common/constant/key.go
+++ b/common/constant/key.go
@@ -209,6 +209,7 @@ const (
        RouterConfigPrefix         = "dubbo.router"
        TracingConfigPrefix        = "dubbo.tracing"
        LoggerConfigPrefix         = "dubbo.logger"
+       CustomConfigPrefix         = "dubbo.custom"
 )
 
 const (
diff --git a/config/custom_config.go b/config/custom_config.go
new file mode 100644
index 0000000..93607ed
--- /dev/null
+++ b/config/custom_config.go
@@ -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
+}
diff --git a/config/custom_config_test.go b/config/custom_config_test.go
new file mode 100644
index 0000000..1289584
--- /dev/null
+++ b/config/custom_config_test.go
@@ -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)
+       })
+}
diff --git a/config/root_config.go b/config/root_config.go
index 3b2e9c7..ad3c33a 100644
--- a/config/root_config.go
+++ b/config/root_config.go
@@ -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) {
@@ -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 {
@@ -226,6 +233,7 @@ func newEmptyRootConfig() *RootConfig {
                Consumer:       NewConsumerConfigBuilder().Build(),
                Metric:         NewMetricConfigBuilder().Build(),
                Logger:         NewLoggerConfigBuilder().Build(),
+               Custom:         NewCustomConfigBuilder().Build(),
        }
        return newRootConfig
 }
@@ -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
 }
diff --git a/config/testdata/config/custom/custom.yaml 
b/config/testdata/config/custom/custom.yaml
new file mode 100644
index 0000000..6129796
--- /dev/null
+++ b/config/testdata/config/custom/custom.yaml
@@ -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
diff --git a/config/testdata/config/custom/empty.yaml 
b/config/testdata/config/custom/empty.yaml
new file mode 100644
index 0000000..a4611db
--- /dev/null
+++ b/config/testdata/config/custom/empty.yaml
@@ -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

Reply via email to