Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition in ParseWithFuncs #223

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions env.go
Original file line number Diff line number Diff line change
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