Skip to content

Commit

Permalink
add default value
Browse files Browse the repository at this point in the history
  • Loading branch information
ZLBer authored and AlexStocks committed Oct 26, 2022
1 parent 14ce032 commit 2b5536b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
26 changes: 19 additions & 7 deletions config/config_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
21 changes: 20 additions & 1 deletion config/config_resolver_test.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -16,14 +33,16 @@ 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"})
assert.Nil(t, err)
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)

})
}
4 changes: 2 additions & 2 deletions config/testdata/config/resolver/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2b5536b

Please sign in to comment.