Skip to content

Commit

Permalink
fix: race condition in ParseWithFuncs (#223)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed May 12, 2022
1 parent 863e59d commit 302a103
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 180 deletions.
8 changes: 5 additions & 3 deletions env.go
Expand Up @@ -72,8 +72,10 @@ var (
return float32(f), err
},
}
)

defaultTypeParsers = map[reflect.Type]ParserFunc{
func defaultTypeParsers() map[reflect.Type]ParserFunc {
return map[reflect.Type]ParserFunc{
reflect.TypeOf(url.URL{}): func(v string) (interface{}, error) {
u, err := url.Parse(v)
if err != nil {
Expand All @@ -89,7 +91,7 @@ var (
return s, err
},
}
)
}

// ParserFunc defines the signature of a function that can be used within `CustomParsers`.
type ParserFunc func(v string) (interface{}, error)
Expand Down Expand Up @@ -187,7 +189,7 @@ func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...
if ref.Kind() != reflect.Struct {
return ErrNotAStructPtr
}
parsers := defaultTypeParsers
parsers := defaultTypeParsers()
for k, v := range funcMap {
parsers[k] = v
}
Expand Down

0 comments on commit 302a103

Please sign in to comment.