From 70d56b6986edbb57065dd9918db6d46fffabce41 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sat, 9 May 2020 08:39:40 -0400 Subject: [PATCH] Allow colons in values for map fields A common use case for this is putting a URL in a map value. --- envconfig.go | 2 +- envconfig_test.go | 9 +++++++++ testdata/custom.txt | 1 + testdata/default_list.txt | 5 +++++ testdata/default_table.txt | 1 + testdata/fault.txt | 1 + 6 files changed, 18 insertions(+), 1 deletion(-) diff --git a/envconfig.go b/envconfig.go index 3f16108..0d01e70 100644 --- a/envconfig.go +++ b/envconfig.go @@ -321,7 +321,7 @@ func processField(value string, field reflect.Value) error { if len(strings.TrimSpace(value)) != 0 { pairs := strings.Split(value, ",") for _, pair := range pairs { - kvpair := strings.Split(pair, ":") + kvpair := strings.SplitN(pair, ":", 2) if len(kvpair) != 2 { return fmt.Errorf("invalid map item: %q", pair) } diff --git a/envconfig_test.go b/envconfig_test.go index 2ca765d..103c02c 100644 --- a/envconfig_test.go +++ b/envconfig_test.go @@ -70,6 +70,7 @@ type Specification struct { MapField map[string]string `default:"one:two,three:four"` UrlValue CustomURL UrlPointer *CustomURL + MapFieldWithColon map[string]string } type Embedded struct { @@ -111,6 +112,7 @@ func TestProcess(t *testing.T) { os.Setenv("ENV_CONFIG_MULTI_WORD_ACR_WITH_AUTO_SPLIT", "25") os.Setenv("ENV_CONFIG_URLVALUE", "https://github.com/kelseyhightower/envconfig") os.Setenv("ENV_CONFIG_URLPOINTER", "https://github.com/kelseyhightower/envconfig") + os.Setenv("ENV_CONFIG_MAPFIELDWITHCOLON", "name:envconfig,url:https://github.com/kelseyhightower/envconfig") err := Process("env_config", &s) if err != nil { t.Error(err.Error()) @@ -217,6 +219,13 @@ func TestProcess(t *testing.T) { if *s.UrlPointer.Value != *u { t.Errorf("expected %q, got %q", u, s.UrlPointer.Value.String()) } + + if s.MapFieldWithColon["name"] != "envconfig" { + t.Errorf("expected %q, got %q", "envconfig", s.MapFieldWithColon["name"]) + } + if s.MapFieldWithColon["url"] != "https://github.com/kelseyhightower/envconfig" { + t.Errorf("expected %q, got %q", "https://github.com/kelseyhightower/envconfig", s.MapFieldWithColon["url"]) + } } func TestParseErrorBool(t *testing.T) { diff --git a/testdata/custom.txt b/testdata/custom.txt index 04d2f5d..5dd2c97 100644 --- a/testdata/custom.txt +++ b/testdata/custom.txt @@ -34,3 +34,4 @@ ENV_CONFIG_DATETIME= ENV_CONFIG_MAPFIELD= ENV_CONFIG_URLVALUE= ENV_CONFIG_URLPOINTER= +ENV_CONFIG_MAPFIELDWITHCOLON= diff --git a/testdata/default_list.txt b/testdata/default_list.txt index fb0eced..2242406 100644 --- a/testdata/default_list.txt +++ b/testdata/default_list.txt @@ -181,3 +181,8 @@ ENV_CONFIG_URLPOINTER ..[type]........CustomURL ..[default]..... ..[required].... +ENV_CONFIG_MAPFIELDWITHCOLON +..[description]. +..[type]........Comma-separated.list.of.String:String.pairs +..[default]..... +..[required].... diff --git a/testdata/default_table.txt b/testdata/default_table.txt index 65c9b44..d0a4280 100644 --- a/testdata/default_table.txt +++ b/testdata/default_table.txt @@ -38,3 +38,4 @@ ENV_CONFIG_DATETIME..............................Time........................... ENV_CONFIG_MAPFIELD..............................Comma-separated.list.of.String:String.pairs.....one:two,three:four................ ENV_CONFIG_URLVALUE..............................CustomURL......................................................................... ENV_CONFIG_URLPOINTER............................CustomURL......................................................................... +ENV_CONFIG_MAPFIELDWITHCOLON.....................Comma-separated.list.of.String:String.pairs....................................... diff --git a/testdata/fault.txt b/testdata/fault.txt index b525ff1..4d74596 100644 --- a/testdata/fault.txt +++ b/testdata/fault.txt @@ -34,3 +34,4 @@ {.Key} {.Key} {.Key} +{.Key}