Skip to content

Commit

Permalink
add lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Sep 4, 2024
1 parent c07447d commit a3092bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
34 changes: 34 additions & 0 deletions pkg/rules/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package rules

import (
"regexp"
"sync"
)

// nolint: lll
// Define all regular expressions here:
var (
validUUIDRegexp = lazyRegexCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
asciiRegexp = lazyRegexCompile(`^[\x00-\x7F]*$`)
)

// lazyRegexCompile returns a function that compiles the regular expression
// once, when the function is called for the first time.
// If the function is never called, the regular expression is never compiled,
// thus saving on performance.
//
// All regular expression literals should be compiled using this function.
//
// Credits: https://github.com/go-playground/validator/commit/2e1df48b5ab876bdd461bdccc51d109389e7572f
func lazyRegexCompile(str string) func() *regexp.Regexp {
var (
regex *regexp.Regexp
once sync.Once
)
return func() *regexp.Regexp {
once.Do(func() {
regex = regexp.MustCompile(str)
})
return regex
}
}
9 changes: 2 additions & 7 deletions pkg/rules/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,19 @@ func StringDNSLabel() govy.RuleSet[string] {
).WithErrorCode(ErrorCodeStringDNSLabel)
}

var validUUIDRegexp = regexp.
MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")

// StringUUID ensures property's value is a valid UUID string.
func StringUUID() govy.Rule[string] {
return StringMatchRegexp(validUUIDRegexp,
return StringMatchRegexp(validUUIDRegexp(),
"00000000-0000-0000-0000-000000000000",
"e190c630-8873-11ee-b9d1-0242ac120002",
"79258D24-01A7-47E5-ACBB-7E762DE52298").
WithDetails("expected RFC-4122 compliant UUID string").
WithErrorCode(ErrorCodeStringUUID)
}

var asciiRegexp = regexp.MustCompile("^[\x00-\x7F]*$")

// StringASCII ensures property's value contains only ASCII characters.
func StringASCII() govy.Rule[string] {
return StringMatchRegexp(asciiRegexp).WithErrorCode(ErrorCodeStringASCII)
return StringMatchRegexp(asciiRegexp()).WithErrorCode(ErrorCodeStringASCII)
}

// StringURL ensures property's value is a valid URL as defined by [url.Parse] function.
Expand Down

0 comments on commit a3092bf

Please sign in to comment.