This is an automated email from the ASF dual-hosted git repository. alexstocks pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
commit 2b5536bfc36005dffdd6874af20f1c411192987a Author: zlb <[email protected]> AuthorDate: Fri Aug 12 23:18:10 2022 +0800 add default value --- config/config_resolver.go | 26 +++++++++++++++++------- config/config_resolver_test.go | 21 ++++++++++++++++++- config/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 fe84e9815..048bb7819 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 6c7cc8d43..e50af31e3 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 91714ddff..92b157e91 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:
