From 374c44d08f05ebfccc433775bc4dd7a63f563ef2 Mon Sep 17 00:00:00 2001 From: zlb <1098294815@qq.com> Date: Thu, 11 Aug 2022 23:43:05 +0800 Subject: [PATCH 1/5] resolve placeholder --- config/config_resolver.go | 39 ++++++++++++++++++- config/config_resolver_test.go | 25 ++++++++++++ .../testdata/config/resolver/application.yaml | 36 +++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 config/config_resolver_test.go create mode 100644 config/testdata/config/resolver/application.yaml diff --git a/config/config_resolver.go b/config/config_resolver.go index 0e4eefd4a6..95b4c60e82 100644 --- a/config/config_resolver.go +++ b/config/config_resolver.go @@ -18,11 +18,14 @@ package config import ( + log "github.com/dubbogo/gost/log/logger" "github.com/knadh/koanf" "github.com/knadh/koanf/parsers/json" "github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/parsers/yaml" + "github.com/knadh/koanf/providers/confmap" "github.com/knadh/koanf/providers/rawbytes" + "strings" "github.com/pkg/errors" ) @@ -66,5 +69,39 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf { if err != nil { panic(err) } - return k + return resolvePlaceholder(k) +} + +const ( + PlaceholderPrefix = "${" + PlaceholderSuffix = "}" +) + +// resolvePlaceholder replace ${xx} with real value +func resolvePlaceholder(resolver *koanf.Koanf) *koanf.Koanf { + m := make(map[string]interface{}) + for k, v := range resolver.All() { + if _, ok := v.(string); !ok { + continue + } + s := v.(string) + newKey := checkPlaceholder(s) + if newKey == "" { + continue + } + m[k] = resolver.Get(newKey) + } + err := resolver.Load(confmap.Provider(m, resolver.Delim()), nil) + if err != nil { + log.Errorf("resolvePlaceholder error %s", err) + } + return resolver +} + +func checkPlaceholder(s string) string { + s = strings.Trim(s, " ") + if !strings.HasPrefix(s, PlaceholderPrefix) || !strings.HasSuffix(s, PlaceholderSuffix) { + return "" + } + return strings.Trim(s[len(PlaceholderPrefix):len(s)-len(PlaceholderSuffix)], " ") } diff --git a/config/config_resolver_test.go b/config/config_resolver_test.go new file mode 100644 index 0000000000..97b496cd36 --- /dev/null +++ b/config/config_resolver_test.go @@ -0,0 +1,25 @@ +package config + +import ( + "github.com/knadh/koanf" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestResolvePlaceHolder(t *testing.T) { + t.Run("test resolver", func(t *testing.T) { + conf := NewLoaderConf(WithPath("/Users/zlb/GolandProjects/dubbo-go/config/testdata/config/resolver/application.yaml")) + koan := GetConfigResolver(conf) + assert.Equal(t, koan.Get("dubbo.config-center.address"), koan.Get("dubbo.registries.nacos.address")) + assert.Equal(t, koan.Get("localhost"), koan.Get("dubbo.protocols.dubbo.ip")) + assert.Equal(t, nil, koan.Get("dubbo.registries.nacos.group")) + + rc := NewRootConfigBuilder().Build() + err := koan.UnmarshalWithConf(rc.Prefix(), rc, koanf.UnmarshalConf{Tag: "yaml"}) + assert.Nil(t, err) + assert.Equal(t, rc.ConfigCenter.Address, rc.Registries["nacos"].Address) + //not exist, default + assert.Equal(t, "", rc.Registries["nacos"].Group) + + }) +} diff --git a/config/testdata/config/resolver/application.yaml b/config/testdata/config/resolver/application.yaml new file mode 100644 index 0000000000..91714ddff3 --- /dev/null +++ b/config/testdata/config/resolver/application.yaml @@ -0,0 +1,36 @@ +localhost: 127.0.0.1 +dubbo: + application: + name: dubbo-go + module: local + version: 1.0.0 + owner: zhaoyunxing + config-center: + address: nacos://127.0.0.1:8848 + cluster: dev + namespace: dubbo + log-dir: ./logs + protocols: + dubbo: + name: dubbo + ip: ${localhost} + port: 20000 + registries: + nacos: + timeout: 5s + group: ${notexist} + address: ${dubbo.config-center.address} + zk: + protocol: zookeeper + group: dev + address: 127.0.0.1:2181 + services: + helloService: + interface: org.dubbo.service.HelloService + registry-ids: nacos,zk + orderService: + interface: org.dubbo.service.OrderService + registry-ids: nacos + provider: + register: true + services: \ No newline at end of file From 1133e07cd9f682d428fafb3d3e7e1a294e6cdf44 Mon Sep 17 00:00:00 2001 From: zlb <1098294815@qq.com> Date: Thu, 11 Aug 2022 23:44:44 +0800 Subject: [PATCH 2/5] resolve placeholder --- config/config_resolver.go | 6 +++++- config/config_resolver_test.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config/config_resolver.go b/config/config_resolver.go index 95b4c60e82..fe84e9815c 100644 --- a/config/config_resolver.go +++ b/config/config_resolver.go @@ -17,15 +17,19 @@ package config +import ( + "strings" +) + import ( log "github.com/dubbogo/gost/log/logger" + "github.com/knadh/koanf" "github.com/knadh/koanf/parsers/json" "github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/parsers/yaml" "github.com/knadh/koanf/providers/confmap" "github.com/knadh/koanf/providers/rawbytes" - "strings" "github.com/pkg/errors" ) diff --git a/config/config_resolver_test.go b/config/config_resolver_test.go index 97b496cd36..6c7cc8d43e 100644 --- a/config/config_resolver_test.go +++ b/config/config_resolver_test.go @@ -1,9 +1,13 @@ package config +import ( + "testing" +) + import ( "github.com/knadh/koanf" + "github.com/stretchr/testify/assert" - "testing" ) func TestResolvePlaceHolder(t *testing.T) { From d5d332a7187ad7b31d48f37c13ed114ef0ee6fff Mon Sep 17 00:00:00 2001 From: zlb <1098294815@qq.com> Date: Fri, 12 Aug 2022 23:18:10 +0800 Subject: [PATCH 3/5] add default value --- config/config_resolver.go | 26 ++++++++++++++----- config/config_resolver_test.go | 21 ++++++++++++++- .../testdata/config/resolver/application.yaml | 4 +-- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/config/config_resolver.go b/config/config_resolver.go index fe84e9815c..048bb7819e 100644 --- a/config/config_resolver.go +++ b/config/config_resolver.go @@ -85,15 +85,18 @@ const ( func resolvePlaceholder(resolver *koanf.Koanf) *koanf.Koanf { m := make(map[string]interface{}) for k, v := range resolver.All() { - if _, ok := v.(string); !ok { + s, ok := v.(string) + if !ok { continue } - s := v.(string) - newKey := checkPlaceholder(s) + newKey, defaultValue := checkPlaceholder(s) if newKey == "" { continue } m[k] = resolver.Get(newKey) + if m[k] == nil { + m[k] = defaultValue + } } err := resolver.Load(confmap.Provider(m, resolver.Delim()), nil) if err != nil { @@ -102,10 +105,19 @@ func resolvePlaceholder(resolver *koanf.Koanf) *koanf.Koanf { return resolver } -func checkPlaceholder(s string) string { - s = strings.Trim(s, " ") +func checkPlaceholder(s string) (newKey, defaultValue string) { + s = strings.TrimSpace(s) if !strings.HasPrefix(s, PlaceholderPrefix) || !strings.HasSuffix(s, PlaceholderSuffix) { - return "" + return + } + s = s[len(PlaceholderPrefix) : len(s)-len(PlaceholderSuffix)] + indexColon := strings.Index(s, ":") + if indexColon == -1 { + newKey = strings.TrimSpace(s) + return } - return strings.Trim(s[len(PlaceholderPrefix):len(s)-len(PlaceholderSuffix)], " ") + newKey = strings.TrimSpace(s[0:indexColon]) + defaultValue = strings.TrimSpace(s[indexColon+1:]) + + return } diff --git a/config/config_resolver_test.go b/config/config_resolver_test.go index 6c7cc8d43e..e50af31e3f 100644 --- a/config/config_resolver_test.go +++ b/config/config_resolver_test.go @@ -1,3 +1,20 @@ +/* + * 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 ( @@ -16,7 +33,8 @@ func TestResolvePlaceHolder(t *testing.T) { koan := GetConfigResolver(conf) assert.Equal(t, koan.Get("dubbo.config-center.address"), koan.Get("dubbo.registries.nacos.address")) assert.Equal(t, koan.Get("localhost"), koan.Get("dubbo.protocols.dubbo.ip")) - assert.Equal(t, nil, koan.Get("dubbo.registries.nacos.group")) + assert.Equal(t, "", koan.Get("dubbo.registries.nacos.group")) + assert.Equal(t, "dev", koan.Get("dubbo.registries.zk.group")) rc := NewRootConfigBuilder().Build() err := koan.UnmarshalWithConf(rc.Prefix(), rc, koanf.UnmarshalConf{Tag: "yaml"}) @@ -24,6 +42,7 @@ func TestResolvePlaceHolder(t *testing.T) { assert.Equal(t, rc.ConfigCenter.Address, rc.Registries["nacos"].Address) //not exist, default assert.Equal(t, "", rc.Registries["nacos"].Group) + assert.Equal(t, "dev", rc.Registries["zk"].Group) }) } diff --git a/config/testdata/config/resolver/application.yaml b/config/testdata/config/resolver/application.yaml index 91714ddff3..92b157e918 100644 --- a/config/testdata/config/resolver/application.yaml +++ b/config/testdata/config/resolver/application.yaml @@ -19,10 +19,10 @@ dubbo: nacos: timeout: 5s group: ${notexist} - address: ${dubbo.config-center.address} + address: ${dubbo.config-center.address:nacos://127.0.0.1:8848} zk: protocol: zookeeper - group: dev + group: ${notexist:dev} address: 127.0.0.1:2181 services: helloService: From 1d399641bef6448a2a698cf6ae02bb793318d4b4 Mon Sep 17 00:00:00 2001 From: zlb <1098294815@qq.com> Date: Mon, 15 Aug 2022 22:49:47 +0800 Subject: [PATCH 4/5] fix ut --- config/config_resolver_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_resolver_test.go b/config/config_resolver_test.go index e50af31e3f..449544744c 100644 --- a/config/config_resolver_test.go +++ b/config/config_resolver_test.go @@ -29,7 +29,7 @@ import ( func TestResolvePlaceHolder(t *testing.T) { t.Run("test resolver", func(t *testing.T) { - conf := NewLoaderConf(WithPath("/Users/zlb/GolandProjects/dubbo-go/config/testdata/config/resolver/application.yaml")) + conf := NewLoaderConf(WithPath("./testdata/config/resolver/application.yaml")) koan := GetConfigResolver(conf) assert.Equal(t, koan.Get("dubbo.config-center.address"), koan.Get("dubbo.registries.nacos.address")) assert.Equal(t, koan.Get("localhost"), koan.Get("dubbo.protocols.dubbo.ip")) From fb92c9461beda86f70138a8669378c9145c971fe Mon Sep 17 00:00:00 2001 From: zlb <1098294815@qq.com> Date: Mon, 15 Aug 2022 22:55:08 +0800 Subject: [PATCH 5/5] move constant --- common/constant/file/suffix.go | 5 +++++ config/config_resolver.go | 9 ++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/common/constant/file/suffix.go b/common/constant/file/suffix.go index bd8876c57e..fe1b41ebbd 100644 --- a/common/constant/file/suffix.go +++ b/common/constant/file/suffix.go @@ -26,3 +26,8 @@ const ( YML = Suffix("yml") PROPERTIES = Suffix("properties") ) + +const ( + PlaceholderPrefix = "${" + PlaceholderSuffix = "}" +) diff --git a/config/config_resolver.go b/config/config_resolver.go index 048bb7819e..517a419841 100644 --- a/config/config_resolver.go +++ b/config/config_resolver.go @@ -76,11 +76,6 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf { return resolvePlaceholder(k) } -const ( - PlaceholderPrefix = "${" - PlaceholderSuffix = "}" -) - // resolvePlaceholder replace ${xx} with real value func resolvePlaceholder(resolver *koanf.Koanf) *koanf.Koanf { m := make(map[string]interface{}) @@ -107,10 +102,10 @@ func resolvePlaceholder(resolver *koanf.Koanf) *koanf.Koanf { func checkPlaceholder(s string) (newKey, defaultValue string) { s = strings.TrimSpace(s) - if !strings.HasPrefix(s, PlaceholderPrefix) || !strings.HasSuffix(s, PlaceholderSuffix) { + if !strings.HasPrefix(s, file.PlaceholderPrefix) || !strings.HasSuffix(s, file.PlaceholderSuffix) { return } - s = s[len(PlaceholderPrefix) : len(s)-len(PlaceholderSuffix)] + s = s[len(file.PlaceholderPrefix) : len(s)-len(file.PlaceholderSuffix)] indexColon := strings.Index(s, ":") if indexColon == -1 { newKey = strings.TrimSpace(s)