From bddf2fa78e5ccae228d72922e77e746ee879937a Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 1 Dec 2022 14:51:10 -0500 Subject: [PATCH] Allow for space delimiters (#80) --- envconfig.go | 3 ++- envconfig_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/envconfig.go b/envconfig.go index bba1189..a28fd11 100644 --- a/envconfig.go +++ b/envconfig.go @@ -76,6 +76,7 @@ import ( "strconv" "strings" "time" + "unicode" ) const ( @@ -452,7 +453,7 @@ func keyAndOpts(tag string) (string, *options, error) { LOOP: for i, o := range tagOpts { - o = strings.TrimSpace(o) + o = strings.TrimLeftFunc(o, unicode.IsSpace) switch { case o == optOverwrite: opts.Overwrite = true diff --git a/envconfig_test.go b/envconfig_test.go index f0a7bf8..d62508a 100644 --- a/envconfig_test.go +++ b/envconfig_test.go @@ -2391,6 +2391,36 @@ func TestProcessWith(t *testing.T) { "URL": "https://foo.bar", }), }, + { + // https://github.com/sethvargo/go-envconfig/issues/79 + name: "space_delimiter", + input: &struct { + Field map[string]string `env:"FIELD,delimiter= "` + }{}, + exp: &struct { + Field map[string]string `env:"FIELD,delimiter= "` + }{ + Field: map[string]string{"foo": "1,2", "bar": "3,4", "zip": "zap:zoo,3"}, + }, + lookuper: MapLookuper(map[string]string{ + "FIELD": "foo:1,2 bar:3,4 zip:zap:zoo,3", + }), + }, + { + // https://github.com/sethvargo/go-envconfig/issues/79 + name: "space_separator", + input: &struct { + Field map[string]string `env:"FIELD,separator= "` + }{}, + exp: &struct { + Field map[string]string `env:"FIELD,separator= "` + }{ + Field: map[string]string{"foo": "bar", "zip:zap": "zoo:zil"}, + }, + lookuper: MapLookuper(map[string]string{ + "FIELD": "foo bar,zip:zap zoo:zil", + }), + }, } for _, tc := range cases {