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

feat: support toLower and toUpper in regex replace #433

Merged
merged 1 commit into from
Apr 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion regex/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package regex

import (
"regexp"
"strings"

"k8s.io/apimachinery/pkg/util/validation/field"
)
Expand All @@ -31,6 +32,12 @@ type Replace struct {
// Replacement is the value after replacement
// +optional
Replacement string `json:"replacement,omitempty"`

// ToLower is a flag to convert the string to lowercase
ToLower bool `json:"toLower,omitempty"`

// ToUpper is a flag to convert the string to uppercase
ToUpper bool `json:"toUpper,omitempty"`
}

// Replaces is a list of Replace
Expand All @@ -42,7 +49,14 @@ func (r *Replace) ReplaceAllString(s string) string {
return s
}
re := regexp.MustCompile(r.Regex)
return re.ReplaceAllString(s, r.Replacement)
s = re.ReplaceAllString(s, r.Replacement)
if r.ToLower {
s = strings.ToLower(s)
}
if r.ToUpper {
s = strings.ToUpper(s)
}
return s
}

// ReplaceAllString replace all string
Expand All @@ -66,6 +80,10 @@ func (r *Replace) Validate(fld *field.Path) (errs field.ErrorList) {
errs = append(errs, field.Invalid(fld.Child("regex"), r.Regex, err.Error()))
}

if r.ToLower && r.ToUpper {
errs = append(errs, field.Invalid(fld, r.ToLower, "toLower and toUpper cannot be set at the same time"))
}

return
}

Expand Down
13 changes: 9 additions & 4 deletions regex/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = Describe("Test.Replaces.ReplaceAllString", func() {
// (?U) indicates that the regex is non-greedy regex.
{Regex: `^(?U)(.*)(.{0,30})$`, Replacement: "${2}"},
// remove the starting non [0-9a-zA-Z] characters
{Regex: `^[^0-9a-zA-Z]*`, Replacement: ""},
{Regex: `^[^0-9a-zA-Z]*`, Replacement: "", ToLower: true},
}

DescribeTable("ReplaceAllString",
Expand All @@ -56,8 +56,8 @@ var _ = Describe("Test.Replaces.ReplaceAllString", func() {
Entry("ending contains / and _", nameAsTagReplaces, "original_/-", "original"),
Entry("starting contains / and _", nameAsTagReplaces, "_/-original", "original"),
Entry("length is 1", nameAsTagReplaces, "a", "a"),
Entry("length greater than 30", nameAsTagReplaces, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
Entry("length greater than 30", nameAsTagReplaces, "0123456789abcdefghijklmnopqrstuvwxyz/-_+ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
Entry("length greater than 30", nameAsTagReplaces, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "wxyzabcdefghijklmnopqrstuvwxyz"),
Entry("length greater than 30", nameAsTagReplaces, "0123456789abcdefghijklmnopqrstuvwxyz/-_+ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),
)
})

Expand Down Expand Up @@ -107,8 +107,13 @@ var _ = Describe("Test.Replaces.Validate", func() {
Message: "Invalid value: \"^.0,128[.*{$\": error parsing regexp: missing closing ]: `[.*{$`",
Field: "prefix[1].regex",
},
metav1.StatusCause{
Type: "FieldValueInvalid",
Message: "Invalid value: true: toLower and toUpper cannot be set at the same time",
Field: "prefix[2]",
},
))
Expect(statusErr.ErrStatus.Details.Causes).To(HaveLen(2))
Expect(statusErr.ErrStatus.Details.Causes).To(HaveLen(3))
})
})

Expand Down
4 changes: 4 additions & 0 deletions regex/testdata/replaces_validation.InvalidData.original.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- regex: "[abcd$"

- regex: "^.0,128[.*{$"

- regex: "^[a-z0-9A-Z_]{1,128}$"
toLower: true
toUpper: true
7 changes: 7 additions & 0 deletions regex/testdata/replaces_validation.Valid.original.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
- regex: ^(.{0,128})(.*)$
replacement: ${1}

- regex: ^(.{0,128})(.*)$
replacement: ${1}
toLower: true

- regex: ^(.{0,128})(.*)$
replacement: ${1}
toUpper: true