Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Expose ParseStrict to error on too many fields #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions cronexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,22 @@ func MustParse(cronLine string) *Expression {
/******************************************************************************/

// Parse returns a new Expression pointer. An error is returned if a malformed
// cron expression is supplied.
// cron expression is supplied. ParseStrict does the same thing, but unlike
// Parse, it will return an error if the provided cron line has too many
// tokens.
// See <https://github.com/gorhill/cronexpr#implementation> for documentation
// about what is a well-formed cron expression from this library's point of
// view.

func Parse(cronLine string) (*Expression, error) {
return parse(cronLine, false)
}

func ParseStrict(cronLine string) (*Expression, error) {
return parse(cronLine, true)
}

func parse(cronLine string, strict bool) (*Expression, error) {

// Maybe one of the built-in aliases is being used
cron := cronNormalizer.Replace(cronLine)
Expand All @@ -76,8 +87,14 @@ func Parse(cronLine string) (*Expression, error) {
if fieldCount < 5 {
return nil, fmt.Errorf("missing field(s)")
}
// ignore fields beyond 7th

if fieldCount > 7 {
// In strict mode, error out
if strict {
return nil, fmt.Errorf("too many fields")
}

// In non-strict mode, ignore fields beyond 7th
fieldCount = 7
}

Expand Down
7 changes: 7 additions & 0 deletions cronexpr/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/krallin/cronexpr/cronexpr

go 1.14

replace github.com/krallin/cronexpr => ../

require github.com/krallin/cronexpr v0.0.0-00010101000000-000000000000
Empty file added cronexpr/go.sum
Empty file.
2 changes: 1 addition & 1 deletion cronexpr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"os"
"time"

"github.com/gorhill/cronexpr"
"github.com/krallin/cronexpr"
)

/******************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions cronexpr_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
directive.first = desc.atoi(snormal[pairs[2]:pairs[3]])
directive.last = desc.atoi(snormal[pairs[4]:pairs[5]])
directive.step = 1
if directive.last < directive.first {
return nil, fmt.Errorf("invalid interval %s (normalized to %d-%d)", snormal, directive.first, directive.last)
}
directives = append(directives, &directive)
continue
}
Expand Down
29 changes: 29 additions & 0 deletions cronexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,35 @@ func TestInterval_Interval60Issue(t *testing.T) {
}
}

// Issue: https://github.com/aptible/supercronic/issues/63
func TestRange_OutOfOrder(t *testing.T) {
_, err := Parse("45 4 * * 6-7")
if err == nil {
t.Errorf("parsing with range 6-7 should return err")
}

_, err = Parse("45 4 * * 6-5")
if err == nil {
t.Errorf("parsing with range 6-5 should return err")
}
}

/******************************************************************************/

func TestTooManyFields(t *testing.T) {
cronLine := "* * * * * * * foobar"

_, err := ParseStrict(cronLine)
if err == nil {
t.Errorf("ParseStrict with too many fields should return err ")
}

_, err = Parse(cronLine)
if err != nil {
t.Errorf("Parse with too many fields should not return err ")
}
}

/******************************************************************************/

var benchmarkExpressions = []string{
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/krallin/cronexpr

go 1.14