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

fix: trim cron expression string #112

Merged
merged 1 commit into from
Feb 21, 2024
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
10 changes: 9 additions & 1 deletion quartz/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package quartz

import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -47,6 +48,7 @@ func NewCronTriggerWithLoc(expression string, location *time.Location) (*CronTri
return nil, illegalArgumentError("location is nil")
}

expression = trimCronExpression(expression)
fields, err := parseCronExpression(expression)
if err != nil {
return nil, err
Expand Down Expand Up @@ -128,7 +130,7 @@ var (
// where the <year> field is optional.
// See the cron expression format table in the readme file for supported special characters.
func ValidateCronExpression(expression string) error {
_, err := parseCronExpression(expression)
_, err := parseCronExpression(trimCronExpression(expression))
return err
}

Expand All @@ -154,6 +156,12 @@ func parseCronExpression(expression string) ([]*cronField, error) {
return buildCronField(tokens)
}

var whitespacePattern = regexp.MustCompile(`\s+`)

func trimCronExpression(expression string) string {
return strings.TrimSpace(whitespacePattern.ReplaceAllString(expression, " "))
}

func buildCronField(tokens []string) ([]*cronField, error) {
var err error
fields := make([]*cronField, 7)
Expand Down
16 changes: 15 additions & 1 deletion quartz/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,25 @@ func TestCronExpressionDescription(t *testing.T) {
assert.Equal(t, cronTrigger.Description(), fmt.Sprintf("CronTrigger::%s::UTC", expression))
}

func TestValidateCronExpression(t *testing.T) {
func TestCronValidateExpression(t *testing.T) {
assert.IsNil(t, quartz.ValidateCronExpression("@monthly"))
assert.NotEqual(t, quartz.ValidateCronExpression(""), nil)
}

func TestCronTrimExpression(t *testing.T) {
expression := " 0 0 10 * * Sun * "
assert.IsNil(t, quartz.ValidateCronExpression(expression))
trigger, err := quartz.NewCronTrigger(expression)
assert.IsNil(t, err)
assert.Equal(t, trigger.Description(), "CronTrigger::0 0 10 * * Sun *::UTC")

expression = " \t\n 0 0 10 * * Sun \n* \r\n "
assert.IsNil(t, quartz.ValidateCronExpression(expression))
trigger, err = quartz.NewCronTrigger(expression)
assert.IsNil(t, err)
assert.Equal(t, trigger.Description(), "CronTrigger::0 0 10 * * Sun *::UTC")
}

var readDateLayout = "Mon Jan 2 15:04:05 2006"

func iterate(prev int64, cronTrigger *quartz.CronTrigger, iterations int) (string, error) {
Expand Down
Loading