Skip to content

Commit

Permalink
test: map alias with json parsing
Browse files Browse the repository at this point in the history
closes #308
  • Loading branch information
caarlos0 committed May 17, 2024
1 parent e631edf commit c4db909
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -2053,3 +2054,26 @@ func TestIssue234(t *testing.T) {
isEqual(t, "kek", cfg.Foo.Str)
isEqual(t, "lel", cfg.Bar.Str)
}

type Issue308 struct {
Inner Issue308Map `env:"A_MAP"`
}

type Issue308Map map[string][]string

func (rc *Issue308Map) UnmarshalText(b []byte) error {
m := map[string][]string{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
*rc = Issue308Map(m)
return nil
}

func TestIssue308(t *testing.T) {
t.Setenv("A_MAP", `{"FOO":["BAR", "ZAZ"]}`)

cfg := Issue308{}
isNoErr(t, Parse(&cfg))
isEqual(t, Issue308Map{"FOO": []string{"BAR", "ZAZ"}}, cfg.Inner)
}

0 comments on commit c4db909

Please sign in to comment.