Skip to content

Commit

Permalink
feat!: ParseWithOptions: add the ability to override default opt.Func…
Browse files Browse the repository at this point in the history
…Map keys (#272)

BREAKING CHANGE: behavior changed and now default parsers can be overwritten.

Co-authored-by: danielvanderwel <d.d.van.der.wel@gmai.com>
  • Loading branch information
Daniel538 and danielvanderwel committed Aug 7, 2023
1 parent 1289c93 commit 62b4ae1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func customOptions(opt Options) Options {
opt.FuncMap = map[reflect.Type]ParserFunc{}
}
for k, v := range defOpts.FuncMap {
opt.FuncMap[k] = v
if _, exists := opt.FuncMap[k]; !exists {
opt.FuncMap[k] = v
}
}
return opt
}
Expand Down
20 changes: 20 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1875,3 +1875,23 @@ func isNil(object interface{}) bool {
}
return false
}

func TestParseWithOptionsOverride(t *testing.T) {
type config struct {
Interval time.Duration `env:"INTERVAL"`
}

t.Setenv("INTERVAL", "1")

var cfg config

isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
reflect.TypeOf(time.Nanosecond): func(value string) (interface{}, error) {
intervalI, err := strconv.Atoi(value)
if err != nil {
return nil, err
}
return time.Duration(intervalI), nil
},
}}))
}

0 comments on commit 62b4ae1

Please sign in to comment.