Skip to content

Commit

Permalink
refactor: reuse compiled regex
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzielenski committed Jan 25, 2024
1 parent d5ee067 commit 1be599d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 20 additions & 7 deletions pkg/generators/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func lintArrayIndices(markerComments []string, prefix string) error {

// If there was a non-prefixed marker inserted in between, then
// reset the previous line and index.
if !strings.HasPrefix(line, "+"+prefix) {
if !strings.HasPrefix(line, prefix) {
prevLine = ""
prevIndex = -1
continue
Expand Down Expand Up @@ -308,11 +308,24 @@ func lintArrayIndices(markerComments []string, prefix string) error {
// value per key (preferring explicit array indices), supports raw strings
// with concatenation, and limits the usable characters allowed in a key
// (for simpler parsing).
func extractCommentTags(marker string, lines []string) (map[string]string, error) {
allowedKeyCharacterSet := `[:_a-zA-Z0-9\[\]\-]`
valueEmpty := regexp.MustCompile(fmt.Sprintf(`^\+%s(%s*)$`, marker, allowedKeyCharacterSet))
valueAssign := regexp.MustCompile(fmt.Sprintf(`^\+%s(%s*)=(.*)$`, marker, allowedKeyCharacterSet))
valueRawString := regexp.MustCompile(fmt.Sprintf(`^\+%s(%s*)>(.*)$`, marker, allowedKeyCharacterSet))
//
// Assignments and empty values have the same syntax as from gengo. Raw strings
// have the syntax:
//
// 'marker' + "key>value"
// 'marker' + "key>value"
//
// Successive usages of the same raw string key results in concatenating each
// line with `\n` in between. It is an error to use `=` to assing to a previously
// assigned key
// (in contrast to types.ExtractCommentTags which allows array-typed
// values to be specified using `=`).
var (
allowedKeyCharacterSet = `[:_a-zA-Z0-9\[\]\-]`
valueEmpty = regexp.MustCompile(fmt.Sprintf(`^(%s*)$`, allowedKeyCharacterSet))
valueAssign = regexp.MustCompile(fmt.Sprintf(`^(%s*)=(.*)$`, allowedKeyCharacterSet))
valueRawString = regexp.MustCompile(fmt.Sprintf(`^(%s*)>(.*)$`, allowedKeyCharacterSet))
)

out := map[string]string{}
lastKey := ""
Expand Down Expand Up @@ -416,7 +429,7 @@ func parseMarkers(markerComments []string, prefix string) (map[string]any, error
return nil, err
}

markers, err := extractCommentTags("+"+prefix, markerComments)
markers, err := extractCommentTags(prefix, markerComments)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (

// This is the comment tag that carries parameters for open API generation.
const tagName = "k8s:openapi-gen"
const markerPrefix = "k8s:validation:"
const markerPrefix = "+k8s:validation:"
const tagOptional = "optional"
const tagDefault = "default"

Expand Down

0 comments on commit 1be599d

Please sign in to comment.