Skip to content

Commit

Permalink
add user define config
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Wanderer committed Dec 3, 2021
1 parent a2c0731 commit 06a1e00
Show file tree
Hide file tree
Showing 6 changed files with 174 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"
UserDefineConfigPrefix = "dubbo.user-define"
)

const (
Expand Down
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"`

UserDefine *UserDefineConfig `yaml:"user-define" json:"user-define,omitempty" property:"user-define"`
}

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.UserDefine.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(),
UserDefine: NewUserDefineConfigBuilder().Build(),
}
return newRootConfig
}
Expand Down Expand Up @@ -313,6 +321,11 @@ func (rb *RootConfigBuilder) SetConfigCenter(configCenterConfig *CenterConfig) *
return rb
}

func (rb *RootConfigBuilder) SetUserDefine(userDefineConfig *UserDefineConfig) *RootConfigBuilder {
rb.rootConfig.UserDefine = userDefineConfig
return rb
}

func (rb *RootConfigBuilder) Build() *RootConfig {
return rb.rootConfig
}
Expand Down
11 changes: 11 additions & 0 deletions config/testdata/config/user_define/empty_log.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dubbo:
user-define:
registries:
nacos:
timeout: 5s
group: dev
address: nacos://127.0.0.1:8848
zk:
protocol: zookeeper
group: test
address: 127.0.0.1:2181
15 changes: 15 additions & 0 deletions config/testdata/config/user_define/user_define.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
user-define:
name: test
version: v2.0
define-config:
test-config: true
81 changes: 81 additions & 0 deletions config/user_define_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 UserDefineConfig 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 (udc *UserDefineConfig) Init() error {
return udc.check()
}

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

func (udc *UserDefineConfig) 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
}

func NewUserDefineConfigBuilder() *UserDefineConfigBuilder {
return &UserDefineConfigBuilder{userDefineConfig: &UserDefineConfig{}}
}

func (udcb *UserDefineConfigBuilder) SetName(name string) *UserDefineConfigBuilder {
udcb.userDefineConfig.Name = name
return udcb
}

func (udcb *UserDefineConfigBuilder) SetVersion(version string) *UserDefineConfigBuilder {
udcb.userDefineConfig.Version = version
return udcb
}

func (udcb *UserDefineConfigBuilder) SetDefineConfig(key string, val interface{}) *UserDefineConfigBuilder {
udcb.userDefineConfig.DefineConfig[key] = val
return udcb
}

func (udcb *UserDefineConfigBuilder) Build() *UserDefineConfig {
return udcb.userDefineConfig
}
53 changes: 53 additions & 0 deletions config/user_define_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 TestUserDefineInit(t *testing.T) {
t.Run("empty use default", func(t *testing.T) {
err := Load(WithPath("./testdata/config/user_define/empty_log.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")
})

t.Run("use config", func(t *testing.T) {
err := Load(WithPath("./testdata/config/user_define/user_define.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)
})
}

0 comments on commit 06a1e00

Please sign in to comment.