Skip to content

Commit 62b4ae1

Browse files
Daniel538danielvanderwel
andauthored
feat!: ParseWithOptions: add the ability to override default opt.FuncMap keys (#272)
BREAKING CHANGE: behavior changed and now default parsers can be overwritten. Co-authored-by: danielvanderwel <[email protected]>
1 parent 1289c93 commit 62b4ae1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

env.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ func customOptions(opt Options) Options {
144144
opt.FuncMap = map[reflect.Type]ParserFunc{}
145145
}
146146
for k, v := range defOpts.FuncMap {
147-
opt.FuncMap[k] = v
147+
if _, exists := opt.FuncMap[k]; !exists {
148+
opt.FuncMap[k] = v
149+
}
148150
}
149151
return opt
150152
}

env_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,3 +1875,23 @@ func isNil(object interface{}) bool {
18751875
}
18761876
return false
18771877
}
1878+
1879+
func TestParseWithOptionsOverride(t *testing.T) {
1880+
type config struct {
1881+
Interval time.Duration `env:"INTERVAL"`
1882+
}
1883+
1884+
t.Setenv("INTERVAL", "1")
1885+
1886+
var cfg config
1887+
1888+
isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
1889+
reflect.TypeOf(time.Nanosecond): func(value string) (interface{}, error) {
1890+
intervalI, err := strconv.Atoi(value)
1891+
if err != nil {
1892+
return nil, err
1893+
}
1894+
return time.Duration(intervalI), nil
1895+
},
1896+
}}))
1897+
}

0 commit comments

Comments
 (0)