Skip to content

Commit

Permalink
Enhance regex patterns
Browse files Browse the repository at this point in the history
- Used non-capturing groups when the info is not relevant
- Simplified some expressions

Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Oct 27, 2020
1 parent 180a3c5 commit 1759529
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/internal/validation/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
// "k8s.io/apimachinery" is needed, re-consider whether to add the dependency.

const (
dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
dns1123LabelFmt string = "[a-z0-9](?:[-a-z0-9]*[a-z0-9])?"
dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
dns1035LabelFmt string = "[a-z](?:[-a-z0-9]*[a-z0-9])?"
)

type dnsValidationConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/validation/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// projectVersionFmt defines the project version format from a project config.
const projectVersionFmt string = "[1-9][0-9]*(-(alpha|beta))?"
const projectVersionFmt string = "[1-9][0-9]*(?:-(?:alpha|beta))?"

var projectVersionRe = regexp.MustCompile("^" + projectVersionFmt + "$")

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/resource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

const (
versionPattern = "^v\\d+(alpha\\d+|beta\\d+)?$"
versionPattern = "^v\\d+(?:alpha\\d+|beta\\d+)?$"
groupRequired = "group cannot be empty"
versionRequired = "version cannot be empty"
kindRequired = "kind cannot be empty"
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/internal/util/go_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func fetchAndCheckGoVersion() error {
// checkGoVersion should only ever check if the Go version >= 1.13, since the kubebuilder binary only cares
// that the go binary supports go modules which were stabilized in that version (i.e. in go 1.13) by default
func checkGoVersion(verStr string) error {
goVerRegex := `^go?([0-9]+)\.([0-9]+)([\.0-9A-Za-z\-]+)?$`
goVerRegex := `^go?([0-9]+)\.([0-9]+)[\.0-9A-Za-z\-]*$`
m := regexp.MustCompile(goVerRegex).FindStringSubmatch(verStr)
if m == nil {
return fmt.Errorf("invalid version string")
Expand Down
12 changes: 6 additions & 6 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
)

// verRe defines the string format of a version.
var verRe = regexp.MustCompile("^(v)?([1-9][0-9]*)(-alpha|-beta)?$")
var verRe = regexp.MustCompile("^v?([1-9][0-9]*)(?:-(alpha|beta))?$")

// Version is a plugin version containing a non-zero integer and an optional stage value
// that if present identifies a version as not stable to some degree.
Expand Down Expand Up @@ -75,15 +75,15 @@ func ParseVersion(version string) (v Version, err error) {
return v, errors.New("plugin version is empty")
}

// A valid version string will have 4 submatches, each of which may be empty: the full string, "v",
// the integer, and the stage suffix. Invalid version strings do not have 4 submatches.
// A valid version string will have 3 submatches, each of which may be empty: the full string,
// the integer, and the stage suffix. Invalid version strings do not have 3 submatches.
submatches := verRe.FindStringSubmatch(version)
if len(submatches) != 4 {
if len(submatches) != 3 {
return v, fmt.Errorf("version format must match %s", verRe.String())
}

// Parse version number.
versionNumStr := submatches[2]
versionNumStr := submatches[1]
if versionNumStr == "" {
return v, errors.New("version must contain an integer")
}
Expand All @@ -92,7 +92,7 @@ func ParseVersion(version string) (v Version, err error) {
}

// Parse stage suffix, if any.
v.Stage = strings.TrimPrefix(submatches[3], "-")
v.Stage = submatches[2]

return v, v.Validate()
}
Expand Down

0 comments on commit 1759529

Please sign in to comment.