Skip to content

Commit

Permalink
refactor(cron): make delimiter runes constant variables (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Mar 18, 2024
1 parent 60094f6 commit eb4b074
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
40 changes: 16 additions & 24 deletions quartz/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,17 @@ func NewCronTriggerWithLoc(expression string, location *time.Location) (*CronTri
if location == nil {
return nil, illegalArgumentError("location is nil")
}

expression = trimCronExpression(expression)
fields, err := parseCronExpression(expression)
if err != nil {
return nil, err
}

lastDefined := -1
for i, field := range fields {
if len(field.values) > 0 {
lastDefined = i
}
}

// full wildcard expression
if lastDefined == -1 {
fields[0].values, _ = fillRangeValues(0, 59)
Expand Down Expand Up @@ -165,37 +162,38 @@ func trimCronExpression(expression string) string {
func buildCronField(tokens []string) ([]*cronField, error) {
var err error
fields := make([]*cronField, 7)
// second field
fields[0], err = parseField(tokens[0], 0, 59)
if err != nil {
return nil, err
}

// minute field
fields[1], err = parseField(tokens[1], 0, 59)
if err != nil {
return nil, err
}

// hour field
fields[2], err = parseField(tokens[2], 0, 23)
if err != nil {
return nil, err
}

// day-of-month field
fields[3], err = parseField(tokens[3], 1, 31)
if err != nil {
return nil, err
}

// month field
fields[4], err = parseField(tokens[4], 1, 12, months)
if err != nil {
return nil, err
}

// day-of-week field
fields[5], err = parseField(tokens[5], 1, 7, days)
if err != nil {
return nil, err
}
fields[5].add(-1)

// year field
fields[6], err = parseField(tokens[6], 1970, 1970*2)
if err != nil {
return nil, err
Expand All @@ -209,12 +207,10 @@ func parseField(field string, min, max int, translate ...[]string) (*cronField,
if len(translate) > 0 {
glossary = translate[0]
}

// any value
if field == "*" || field == "?" {
return &cronField{[]int{}}, nil
}

// simple value
i, err := strconv.Atoi(field)
if err == nil {
Expand All @@ -223,22 +219,18 @@ func parseField(field string, min, max int, translate ...[]string) (*cronField,
}
return nil, invalidCronFieldError("simple", field)
}

// list values
if strings.Contains(field, ",") {
if strings.ContainsRune(field, listRune) {
return parseListField(field, min, max, glossary)
}

// step values
if strings.Contains(field, "/") {
if strings.ContainsRune(field, stepRune) {
return parseStepField(field, min, max, glossary)
}

// range values
if strings.Contains(field, "-") {
if strings.ContainsRune(field, rangeRune) {
return parseRangeField(field, min, max, glossary)
}

// simple literal value
if glossary != nil {
intVal, err := translateLiteral(glossary, field)
Expand All @@ -251,11 +243,11 @@ func parseField(field string, min, max int, translate ...[]string) (*cronField,
return nil, invalidCronFieldError("literal", field)
}

return nil, cronParseError("parse error")
return nil, cronParseError(fmt.Sprintf("invalid field %s", field))
}

func parseListField(field string, min, max int, glossary []string) (*cronField, error) {
t := strings.Split(field, ",")
t := strings.Split(field, string(listRune))
values, stepValues := extractStepValues(t)
values, rangeValues := extractRangeValues(values)
listValues, err := translateLiterals(glossary, values)
Expand All @@ -282,7 +274,7 @@ func parseListField(field string, min, max int, glossary []string) (*cronField,
}

func parseRangeField(field string, min, max int, glossary []string) (*cronField, error) {
t := strings.Split(field, "-")
t := strings.Split(field, string(rangeRune))
if len(t) != 2 {
return nil, invalidCronFieldError("range", field)
}
Expand All @@ -306,7 +298,7 @@ func parseRangeField(field string, min, max int, glossary []string) (*cronField,
}

func parseStepField(field string, min, max int, glossary []string) (*cronField, error) {
t := strings.Split(field, "/")
t := strings.Split(field, string(stepRune))
if len(t) != 2 {
return nil, invalidCronFieldError("step", field)
}
Expand All @@ -318,8 +310,8 @@ func parseStepField(field string, min, max int, glossary []string) (*cronField,
switch {
case t[0] == "*":
from = min
case strings.Contains(t[0], "-"):
trange := strings.Split(t[0], "-")
case strings.ContainsRune(t[0], rangeRune):
trange := strings.Split(t[0], string(rangeRune))
if len(trange) != 2 {
return nil, invalidCronFieldError("step", field)
}
Expand Down
12 changes: 8 additions & 4 deletions quartz/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"time"
)

const (
listRune = ','
stepRune = '/'
rangeRune = '-'
)

// Sep is the serialization delimiter; the default is a double colon.
var Sep = "::"

Expand All @@ -26,7 +32,7 @@ func extractRangeValues(parsed []string) ([]string, []string) {
values := make([]string, 0, len(parsed))
rangeValues := make([]string, 0)
for _, v := range parsed {
if strings.Contains(v, "-") { // range value
if strings.ContainsRune(v, rangeRune) { // range value
rangeValues = append(rangeValues, v)
} else {
values = append(values, v)
Expand All @@ -39,7 +45,7 @@ func extractStepValues(parsed []string) ([]string, []string) {
values := make([]string, 0, len(parsed))
stepValues := make([]string, 0)
for _, v := range parsed {
if strings.Contains(v, "/") { // step value
if strings.ContainsRune(v, stepRune) { // step value
stepValues = append(stepValues, v)
} else {
values = append(values, v)
Expand All @@ -54,7 +60,6 @@ func fillRangeValues(from, to int) ([]int, error) {
}
length := (to - from) + 1
rangeValues := make([]int, length)

for i, j := from, 0; i <= to; i, j = i+1, j+1 {
rangeValues[j] = i
}
Expand All @@ -67,7 +72,6 @@ func fillStepValues(from, step, max int) ([]int, error) {
}
length := ((max - from) / step) + 1
stepValues := make([]int, length)

for i, j := from, 0; i <= max; i, j = i+step, j+1 {
stepValues[j] = i
}
Expand Down

0 comments on commit eb4b074

Please sign in to comment.