Repository: go-kratos/kratos
Pull request #1332 (2021-08-13 18:57:42)

fix(config): Support colon as default value in config.yaml

#### Description (what this PR does / why we need it):
This PR fixes a bug where the default variable of a config option was trimmed when a colon was found


#### Which issue(s) this PR fixes:

fixes #

#### Other special notes for reviewer:


codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#1332](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (9dae96c) into [main](https://codecov.io/gh/go-kratos/kratos/commit/4b6ab21ae5ef5f845926d179c094cbf00f2a6f79?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (4b6ab21) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/go-kratos/kratos/pull/1332/graphs/tree.svg?width=650&height=150&src=pr&token=XwnmBTz78x&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)

```diff
@@           Coverage Diff           @@
##             main    #1332   +/-   ##
=======================================
  Coverage   63.05%   63.05%           
=======================================
  Files          61       61           
  Lines        2823     2823           
=======================================
  Hits         1780     1780           
  Misses        895      895           
  Partials      148      148           
```


| [Impacted Files](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [config/options.go](https://codecov.io/gh/go-kratos/kratos/pull/1332/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-Y29uZmlnL29wdGlvbnMuZ28=) | `89.09% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [4b6ab21...9dae96c](https://codecov.io/gh/go-kratos/kratos/pull/1332?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/config/options.go b/config/options.go
index 3574e974..a96d8939 100644
--- a/config/options.go
+++ b/config/options.go
@@ -84,7 +84,7 @@ func defaultDecoder(src *KeyValue, target map[string]interface{}) error {
 // placeholder format in ${key:default} or $key.
 func defaultResolver(input map[string]interface{}) error {
 	mapper := func(name string) string {
-		args := strings.Split(strings.TrimSpace(name), ":")
+		args := strings.SplitN(strings.TrimSpace(name), ":", 2)
 		if v, has := readValue(input, args[0]); has {
 			s, _ := v.String()
 			return s


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index 7bdacd8b..e18bb685 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -183,6 +183,7 @@ func TestDefaultResolver(t *testing.T) {
 				"enable":   "${ENABLE:false}",
 				"rate":     "${RATE}",
 				"empty":    "${EMPTY:foobar}",
+				"url":      "${URL:http://example.com}",
 				"array": []interface{}{
 					"${PORT}",
 					map[string]interface{}{"foobar": "${NOTEXIST:8081}"},
@@ -237,6 +238,11 @@ func TestDefaultResolver(t *testing.T) {
 			path:   "foo.bar.empty",
 			expect: "",
 		},
+		{
+			name:   "test url with default",
+			path:   "foo.bar.url",
+			expect: "http://example.com",
+		},
 		{
 			name:   "test array",
 			path:   "foo.bar.array",