-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync.OnceValue was introduced in go1.21. Currently validator's go.mod states minimum go version to be go1.18. This commit moves the lazy regex initialization into its own file lazy.go and provides backwards compatibility using buildtags and the file lazy_compat.go. Signed-off-by: Kimmo Lehto <[email protected]>
- Loading branch information
Showing
3 changed files
with
33 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:build go1.21 | ||
|
||
package validator | ||
|
||
import ( | ||
"regexp" | ||
"sync" | ||
) | ||
|
||
func lazyRegexCompile(str string) func() *regexp.Regexp { | ||
return sync.OnceValue(func() *regexp.Regexp { | ||
return regexp.MustCompile(str) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !go1.21 | ||
|
||
package validator | ||
|
||
import ( | ||
"regexp" | ||
"sync" | ||
) | ||
|
||
func lazyRegexCompile(str string) func() *regexp.Regexp { | ||
var regex *regexp.Regexp | ||
var once sync.Once | ||
return func() *regexp.Regexp { | ||
once.Do(func() { | ||
regex = regexp.MustCompile(str) | ||
}) | ||
return regex | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters