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 3cd7828145e3882f6626a62d178bd83fce8b153b
Author: zlb <[email protected]>
AuthorDate: Thu Aug 11 23:43:05 2022 +0800

    resolve placeholder
---
 config/config_resolver.go                        | 39 +++++++++++++++++++++++-
 config/config_resolver_test.go                   | 25 +++++++++++++++
 config/testdata/config/resolver/application.yaml | 36 ++++++++++++++++++++++
 3 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/config/config_resolver.go b/config/config_resolver.go
index 0e4eefd4a..95b4c60e8 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 000000000..97b496cd3
--- /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 000000000..91714ddff
--- /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

Reply via email to