From 925e55a04d4d639187faa6e33bdd5d9bffdf4f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=AD=90?= Date: Wed, 25 Aug 2021 23:52:25 +0800 Subject: [PATCH] fix(config): replace text with "${}" only (#1375) * fix(config): replace text with "${}" only --- config/config_test.go | 16 ++-------------- config/env/env_test.go | 2 +- config/options.go | 22 ++++++++++++++++++---- internal/httputil/http_test.go | 10 +++++----- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index e18bb685b..314335cb8 100644 --- a/config/config_test.go +++ b/config/config_test.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 { diff --git a/config/env/env_test.go b/config/env/env_test.go index 910e6a179..a7d49ef9b 100644 --- a/config/env/env_test.go +++ b/config/env/env_test.go @@ -16,7 +16,7 @@ const _testJSON = ` { "test":{ "server":{ - "name":"$SERVICE_NAME", + "name":"${SERVICE_NAME}", "addr":"${ADDR:127.0.0.1}", "port":"${PORT:8080}" } diff --git a/config/options.go b/config/options.go index a96d8939e..c86dd755c 100644 --- a/config/options.go +++ b/config/options.go @@ -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 +} diff --git a/internal/httputil/http_test.go b/internal/httputil/http_test.go index f6b7ba3ea..11b934a37 100644 --- a/internal/httputil/http_test.go +++ b/internal/httputil/http_test.go @@ -97,12 +97,12 @@ func TestStatusFromGRPCCode(t *testing.T) { func TestContentType(t *testing.T) { tests := []struct { - name string + name string subtype string - want string + want string }{ - {"kratos","kratos","application/kratos"}, - {"json","json","application/json"}, + {"kratos", "kratos", "application/kratos"}, + {"json", "json", "application/json"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -111,4 +111,4 @@ func TestContentType(t *testing.T) { } }) } -} \ No newline at end of file +}