fix(config): replace text with "${}" only (#1375)

* fix(config): replace text with "${}" only
pull/1377/head
包子 3 years ago committed by GitHub
parent eaf0ceab0c
commit 925e55a04d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      config/config_test.go
  2. 2
      config/env/env_test.go
  3. 22
      config/options.go

@ -116,7 +116,6 @@ func TestConfig(t *testing.T) {
httpAddr = "0.0.0.0"
httpTimeout = 0.5
grpcPort = 10080
enableSSL = true
endpoint1 = "www.aaa.com"
databaseDriver = "mysql"
)
@ -160,7 +159,7 @@ func TestConfig(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, httpAddr, testConf.Server.Http.Addr)
assert.Equal(t, httpTimeout, testConf.Server.Http.Timeout)
assert.Equal(t, enableSSL, testConf.Server.Http.EnableSSL)
assert.Equal(t, true, testConf.Server.Http.EnableSSL)
assert.Equal(t, grpcPort, testConf.Server.GRpc.Port)
assert.Equal(t, endpoint1, testConf.Endpoints[0])
assert.Equal(t, 2, len(testConf.Endpoints))
@ -170,7 +169,6 @@ func TestDefaultResolver(t *testing.T) {
var (
portString = "8080"
countInt = 10
enableBool = true
rateFloat = 0.9
)
@ -226,7 +224,7 @@ func TestDefaultResolver(t *testing.T) {
{
name: "test bool with default",
path: "foo.bar.enable",
expect: enableBool,
expect: true,
},
{
name: "test float without default",
@ -253,16 +251,6 @@ func TestDefaultResolver(t *testing.T) {
path: "foo.bar.value1",
expect: "foobar",
},
{
name: "test $value",
path: "foo.bar.value2",
expect: portString,
},
{
name: "test $value:default",
path: "foo.bar.value3",
expect: portString + ":default",
},
}
for _, test := range tests {

@ -16,7 +16,7 @@ const _testJSON = `
{
"test":{
"server":{
"name":"$SERVICE_NAME",
"name":"${SERVICE_NAME}",
"addr":"${ADDR:127.0.0.1}",
"port":"${PORT:8080}"
}

@ -2,7 +2,7 @@ package config
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/go-kratos/kratos/v2/encoding"
@ -81,7 +81,7 @@ func defaultDecoder(src *KeyValue, target map[string]interface{}) error {
}
// defaultResolver resolve placeholder in map value,
// placeholder format in ${key:default} or $key.
// placeholder format in ${key:default}.
func defaultResolver(input map[string]interface{}) error {
mapper := func(name string) string {
args := strings.SplitN(strings.TrimSpace(name), ":", 2)
@ -99,7 +99,7 @@ func defaultResolver(input map[string]interface{}) error {
for k, v := range sub {
switch vt := v.(type) {
case string:
sub[k] = os.Expand(vt, mapper)
sub[k] = expand(vt, mapper)
case map[string]interface{}:
if err := resolve(vt); err != nil {
return err
@ -108,7 +108,7 @@ func defaultResolver(input map[string]interface{}) error {
for i, iface := range vt {
switch it := iface.(type) {
case string:
vt[i] = os.Expand(it, mapper)
vt[i] = expand(it, mapper)
case map[string]interface{}:
if err := resolve(it); err != nil {
return err
@ -122,3 +122,17 @@ func defaultResolver(input map[string]interface{}) error {
}
return resolve(input)
}
func expand(s string, mapping func(string) string) string {
r, err := regexp.Compile(`\${(.*?)}`)
if err != nil {
return s
}
re := r.FindAllStringSubmatch(s, -1)
for _, i := range re {
if len(i) == 2 {
s = strings.ReplaceAll(s, i[0], mapping(i[1]))
}
}
return s
}

Loading…
Cancel
Save