Skip to content

Commit 06a1e00

Browse files
committed
add user define config
1 parent a2c0731 commit 06a1e00

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed

common/constant/key.go

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ const (
209209
RouterConfigPrefix = "dubbo.router"
210210
TracingConfigPrefix = "dubbo.tracing"
211211
LoggerConfigPrefix = "dubbo.logger"
212+
UserDefineConfigPrefix = "dubbo.user-define"
212213
)
213214

214215
const (

config/root_config.go

+13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ type RootConfig struct {
8080

8181
// cache file used to store the current used configurations.
8282
CacheFile string `yaml:"cache_file" json:"cache_file,omitempty" property:"cache_file"`
83+
84+
UserDefine *UserDefineConfig `yaml:"user-define" json:"user-define,omitempty" property:"user-define"`
8385
}
8486

8587
func SetRootConfig(r RootConfig) {
@@ -153,6 +155,11 @@ func (rc *RootConfig) Init() error {
153155
return err
154156
}
155157

158+
// init user define
159+
if err := rc.UserDefine.Init(); err != nil {
160+
return err
161+
}
162+
156163
// init protocol
157164
protocols := rc.Protocols
158165
if len(protocols) <= 0 {
@@ -226,6 +233,7 @@ func newEmptyRootConfig() *RootConfig {
226233
Consumer: NewConsumerConfigBuilder().Build(),
227234
Metric: NewMetricConfigBuilder().Build(),
228235
Logger: NewLoggerConfigBuilder().Build(),
236+
UserDefine: NewUserDefineConfigBuilder().Build(),
229237
}
230238
return newRootConfig
231239
}
@@ -313,6 +321,11 @@ func (rb *RootConfigBuilder) SetConfigCenter(configCenterConfig *CenterConfig) *
313321
return rb
314322
}
315323

324+
func (rb *RootConfigBuilder) SetUserDefine(userDefineConfig *UserDefineConfig) *RootConfigBuilder {
325+
rb.rootConfig.UserDefine = userDefineConfig
326+
return rb
327+
}
328+
316329
func (rb *RootConfigBuilder) Build() *RootConfig {
317330
return rb.rootConfig
318331
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
dubbo:
2+
user-define:
3+
registries:
4+
nacos:
5+
timeout: 5s
6+
group: dev
7+
address: nacos://127.0.0.1:8848
8+
zk:
9+
protocol: zookeeper
10+
group: test
11+
address: 127.0.0.1:2181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dubbo:
2+
registries:
3+
nacos:
4+
timeout: 5s
5+
group: dev
6+
address: nacos://127.0.0.1:8848
7+
zk:
8+
protocol: zookeeper
9+
group: test
10+
address: 127.0.0.1:2181
11+
user-define:
12+
name: test
13+
version: v2.0
14+
define-config:
15+
test-config: true

config/user_define_config.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package config
19+
20+
import(
21+
"github.com/creasty/defaults"
22+
)
23+
24+
import (
25+
"dubbo.apache.org/dubbo-go/v3/common/constant"
26+
)
27+
28+
type UserDefineConfig struct {
29+
Name string `default:"user-config" yaml:"name" json:"name,omitempty" property:"name"`
30+
Version string `default:"v1.0" yaml:"version" json:"version,omitempty" property:"version"`
31+
DefineConfig map[string]interface{} `yaml:"define-config" json:"define-config,omitempty" property:"define-config"`
32+
}
33+
34+
func (*UserDefineConfig) Prefix() string {
35+
return constant.UserDefineConfigPrefix
36+
}
37+
38+
func (udc *UserDefineConfig) Init() error {
39+
return udc.check()
40+
}
41+
42+
func (udc *UserDefineConfig) check() error {
43+
if err := defaults.Set(udc); err != nil {
44+
return err
45+
}
46+
return verify(udc)
47+
}
48+
49+
func (udc *UserDefineConfig) GetDefineValue(key string, default_value interface{}) interface{} {
50+
if define_value, ok := udc.DefineConfig[key]; ok {
51+
return define_value
52+
}
53+
return default_value
54+
}
55+
56+
type UserDefineConfigBuilder struct {
57+
userDefineConfig *UserDefineConfig
58+
}
59+
60+
func NewUserDefineConfigBuilder() *UserDefineConfigBuilder {
61+
return &UserDefineConfigBuilder{userDefineConfig: &UserDefineConfig{}}
62+
}
63+
64+
func (udcb *UserDefineConfigBuilder) SetName(name string) *UserDefineConfigBuilder {
65+
udcb.userDefineConfig.Name = name
66+
return udcb
67+
}
68+
69+
func (udcb *UserDefineConfigBuilder) SetVersion(version string) *UserDefineConfigBuilder {
70+
udcb.userDefineConfig.Version = version
71+
return udcb
72+
}
73+
74+
func (udcb *UserDefineConfigBuilder) SetDefineConfig(key string, val interface{}) *UserDefineConfigBuilder {
75+
udcb.userDefineConfig.DefineConfig[key] = val
76+
return udcb
77+
}
78+
79+
func (udcb *UserDefineConfigBuilder) Build() *UserDefineConfig {
80+
return udcb.userDefineConfig
81+
}

config/user_define_config_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package config
19+
20+
import (
21+
"testing"
22+
)
23+
24+
import (
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestUserDefineInit(t *testing.T) {
29+
t.Run("empty use default", func(t *testing.T) {
30+
err := Load(WithPath("./testdata/config/user_define/empty_log.yaml"))
31+
assert.Nil(t, err)
32+
assert.NotNil(t, rootConfig)
33+
UserDefineConfig := rootConfig.UserDefine
34+
assert.NotNil(t, UserDefineConfig)
35+
assert.Equal(t, UserDefineConfig.Name, "user-config")
36+
assert.Equal(t, UserDefineConfig.Version, "v1.0")
37+
assert.Equal(t, UserDefineConfig.DefineConfig, map[string]interface{}(nil))
38+
assert.Equal(t, UserDefineConfig.GetDefineValue("test", "test"), "test")
39+
})
40+
41+
t.Run("use config", func(t *testing.T) {
42+
err := Load(WithPath("./testdata/config/user_define/user_define.yaml"))
43+
assert.Nil(t, err)
44+
assert.NotNil(t, rootConfig)
45+
UserDefineConfig := rootConfig.UserDefine
46+
assert.NotNil(t, UserDefineConfig)
47+
assert.Equal(t, UserDefineConfig.Name, "test")
48+
assert.Equal(t, UserDefineConfig.Version, "v2.0")
49+
assert.Equal(t, UserDefineConfig.DefineConfig, map[string]interface{}{"test-config": true})
50+
assert.Equal(t, UserDefineConfig.GetDefineValue("test-config", false), true)
51+
assert.Equal(t, UserDefineConfig.GetDefineValue("test-no-config", false), false)
52+
})
53+
}

0 commit comments

Comments
 (0)