Skip to content

Commit c4db909

Browse files
committed
test: map alias with json parsing
closes #308
1 parent e631edf commit c4db909

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

env_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package env
22

33
import (
44
"encoding/base64"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"net/http"
@@ -2053,3 +2054,26 @@ func TestIssue234(t *testing.T) {
20532054
isEqual(t, "kek", cfg.Foo.Str)
20542055
isEqual(t, "lel", cfg.Bar.Str)
20552056
}
2057+
2058+
type Issue308 struct {
2059+
Inner Issue308Map `env:"A_MAP"`
2060+
}
2061+
2062+
type Issue308Map map[string][]string
2063+
2064+
func (rc *Issue308Map) UnmarshalText(b []byte) error {
2065+
m := map[string][]string{}
2066+
if err := json.Unmarshal(b, &m); err != nil {
2067+
return err
2068+
}
2069+
*rc = Issue308Map(m)
2070+
return nil
2071+
}
2072+
2073+
func TestIssue308(t *testing.T) {
2074+
t.Setenv("A_MAP", `{"FOO":["BAR", "ZAZ"]}`)
2075+
2076+
cfg := Issue308{}
2077+
isNoErr(t, Parse(&cfg))
2078+
isEqual(t, Issue308Map{"FOO": []string{"BAR", "ZAZ"}}, cfg.Inner)
2079+
}

0 commit comments

Comments
 (0)