Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve placeholder #2014

Merged
merged 5 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion config/config_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
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"

"github.com/pkg/errors"
Expand Down Expand Up @@ -66,5 +73,39 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf {
if err != nil {
panic(err)
}
return k
return resolvePlaceholder(k)
}

const (
PlaceholderPrefix = "${"
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
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 {
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
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, " ")
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
if !strings.HasPrefix(s, PlaceholderPrefix) || !strings.HasSuffix(s, PlaceholderSuffix) {
return ""
}
return strings.Trim(s[len(PlaceholderPrefix):len(s)-len(PlaceholderSuffix)], " ")
}
29 changes: 29 additions & 0 deletions config/config_resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"testing"
)

import (
"github.com/knadh/koanf"

"github.com/stretchr/testify/assert"
)

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)

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