Skip to content

Commit

Permalink
Forcing ordered operation when generating regex
Browse files Browse the repository at this point in the history
Range was used to iterate over the operations as part of generating
the regex. Range iterations are random. There were cases where the
empty key caused the regex to be generated as invalid.

This change removes the range so that the order is always the same.

Closes #155
  • Loading branch information
mattfarina committed Nov 18, 2020
1 parent 60c7ae8 commit 440427c
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions constraints.go
Expand Up @@ -164,14 +164,11 @@ func init() {
"^": constraintCaret,
}

ops := make([]string, 0, len(constraintOps))
for k := range constraintOps {
ops = append(ops, regexp.QuoteMeta(k))
}
ops := `=||!=|>|<|>=|=>|<=|=<|~|~>|\^`

constraintRegex = regexp.MustCompile(fmt.Sprintf(
`^\s*(%s)\s*(%s)\s*$`,
strings.Join(ops, "|"),
ops,
cvRegex))

constraintRangeRegex = regexp.MustCompile(fmt.Sprintf(
Expand All @@ -180,12 +177,12 @@ func init() {

findConstraintRegex = regexp.MustCompile(fmt.Sprintf(
`(%s)\s*(%s)`,
strings.Join(ops, "|"),
ops,
cvRegex))

validConstraintRegex = regexp.MustCompile(fmt.Sprintf(
`^(\s*(%s)\s*(%s)\s*\,?)+$`,
strings.Join(ops, "|"),
ops,
cvRegex))
}

Expand Down

0 comments on commit 440427c

Please sign in to comment.