From 34e6556865feb0530899a00cfec9e8ce33bbe35c Mon Sep 17 00:00:00 2001 From: Gaylor Bosson Date: Sun, 3 Dec 2023 17:13:30 +0100 Subject: [PATCH] Make internal implementations private --- cron.go | 1 + error.go | 12 ++++++---- parser.go | 64 +++++++++++++++++++++++++------------------------- parser_test.go | 2 +- time-unit.go | 12 +++++----- 5 files changed, 47 insertions(+), 44 deletions(-) diff --git a/cron.go b/cron.go index 218cd3e..c5d217e 100644 --- a/cron.go +++ b/cron.go @@ -10,6 +10,7 @@ const ( maxYearAttempts = 100 ) +// TimeUnit represents a single part of a Cron expression. type TimeUnit interface { // Next returns the next iteration of a schedule and `true` when valid, // otherwise it returns a time after `next` and `false`. diff --git a/error.go b/error.go index 1a5ee4f..5ce9530 100644 --- a/error.go +++ b/error.go @@ -10,12 +10,14 @@ var ( ErrValueOutsideRange = errors.New("values are outside the supported range") ) +// TimeUnitError is an error returned when a time unit of a Cron expression is +// malformed. type TimeUnitError struct { inner error - kind TimeUnitKind + kind timeUnitKind } -func newTimeUnitErr(kind TimeUnitKind, inner error) TimeUnitError { +func newTimeUnitErr(kind timeUnitKind, inner error) TimeUnitError { return TimeUnitError{inner: inner, kind: kind} } @@ -31,10 +33,10 @@ func (e TimeUnitError) Is(err error) bool { return errors.Is(e.inner, err) } -type TimeUnitKind int +type timeUnitKind int const ( - Seconds TimeUnitKind = iota + Seconds timeUnitKind = iota Minutes Hours Days @@ -44,6 +46,6 @@ const ( var kinds = []string{"seconds", "minutes", "hours", "days", "months", "week days"} -func (k TimeUnitKind) String() string { +func (k timeUnitKind) String() string { return kinds[k] } diff --git a/parser.go b/parser.go index eafe4f8..3d5ce4d 100644 --- a/parser.go +++ b/parser.go @@ -6,22 +6,6 @@ import ( "time" ) -type Ordering int - -const ( - OrderingLess Ordering = iota - 1 - OrderingEqual - OrderingGreater -) - -type ExprField interface { - Compare(t time.Time, other int) Ordering - Value(t time.Time, other int) int - SubsetOf(min, max int) bool -} - -type ConvertFn func(string) (ExprField, error) - // Parser is a parser from Cron expressions. type Parser struct{} @@ -69,7 +53,7 @@ func (p Parser) Parse(expression string) (schedule Schedule, err error) { return } -func (Parser) parse(expr string, convFn ConvertFn, min, max int) (fields []ExprField, err error) { +func (Parser) parse(expr string, convFn converterFn, min, max int) (fields []exprField, err error) { if expr == "*" { return } @@ -79,7 +63,7 @@ func (Parser) parse(expr string, convFn ConvertFn, min, max int) (fields []ExprF } for _, u := range strings.Split(expr, ",") { - var field ExprField + var field exprField switch { case strings.Contains(u, "/"): @@ -104,7 +88,7 @@ func (Parser) parse(expr string, convFn ConvertFn, min, max int) (fields []ExprF return } -func parseRange(expr string, convFn ConvertFn) (r rangeExpr, err error) { +func parseRange(expr string, convFn converterFn) (r rangeExpr, err error) { parts := strings.Split(expr, "-") r.from, err = convFn(parts[0]) if err != nil { @@ -118,7 +102,7 @@ func parseRange(expr string, convFn ConvertFn) (r rangeExpr, err error) { return } -func parseInterval(expr string, convFn ConvertFn, min, max int) (i intervalExpr, err error) { +func parseInterval(expr string, convFn converterFn, min, max int) (i intervalExpr, err error) { parts := strings.Split(expr, "/") i.incr, err = strconv.Atoi(parts[1]) @@ -148,7 +132,7 @@ func parseInterval(expr string, convFn ConvertFn, min, max int) (i intervalExpr, } // isNotSpecified returns true if any of the field is not specified. -func isNotSpecified(fields []ExprField) bool { +func isNotSpecified(fields []exprField) bool { for _, field := range fields { if _, ok := field.(notSpecifiedExpr); ok { return true @@ -161,7 +145,7 @@ func isNotSpecified(fields []ExprField) bool { // or in other words a value not specified. type notSpecifiedExpr struct{} -func (notSpecifiedExpr) Compare(_ time.Time, other int) Ordering { +func (notSpecifiedExpr) Compare(_ time.Time, other int) ordering { return OrderingEqual } @@ -177,7 +161,7 @@ func (notSpecifiedExpr) SubsetOf(_, _ int) bool { // unitExpr is an expression field that represents a single possible value. type unitExpr int -func (u unitExpr) Compare(_ time.Time, other int) Ordering { +func (u unitExpr) Compare(_ time.Time, other int) ordering { switch { case int(u) < other: return OrderingLess @@ -199,11 +183,11 @@ func (u unitExpr) SubsetOf(min, max int) bool { // rangeExpr is an expression field that represents an inclusive range of // values. type rangeExpr struct { - from ExprField - to ExprField + from exprField + to exprField } -func (r rangeExpr) Compare(t time.Time, other int) Ordering { +func (r rangeExpr) Compare(t time.Time, other int) ordering { switch { case r.to.Compare(t, other) == OrderingLess: return OrderingLess @@ -227,7 +211,7 @@ type intervalExpr struct { incr int } -func (i intervalExpr) Compare(t time.Time, other int) Ordering { +func (i intervalExpr) Compare(t time.Time, other int) ordering { nearestAfter := i.Value(t, other) isMaxGreaterOrEqual := i.rge.to.Compare(t, nearestAfter) != OrderingLess @@ -264,7 +248,7 @@ func (i intervalExpr) SubsetOf(min, max int) bool { // day of a month. type lastDayOfMonthExpr struct{} -func (l lastDayOfMonthExpr) Compare(t time.Time, other int) Ordering { +func (l lastDayOfMonthExpr) Compare(t time.Time, other int) ordering { return unitExpr(l.Value(t, other)).Compare(t, other) } @@ -283,7 +267,7 @@ type lastWeekDayOfMonthExpr struct { weekday time.Weekday } -func (l lastWeekDayOfMonthExpr) Compare(t time.Time, other int) Ordering { +func (l lastWeekDayOfMonthExpr) Compare(t time.Time, other int) ordering { unit := unitExpr(l.Value(t, other)) return unit.Compare(t, t.Day()) } @@ -318,7 +302,7 @@ var weekdays = map[string]time.Weekday{ "sat": time.Saturday, } -func convertWeekDay(value string) (ExprField, error) { +func convertWeekDay(value string) (exprField, error) { if value == "L" { return unitExpr(time.Saturday), nil } @@ -336,14 +320,30 @@ func convertWeekDay(value string) (ExprField, error) { return convertUnit(value) } -func convertWithLastDayOfMonth(value string) (ExprField, error) { +func convertWithLastDayOfMonth(value string) (exprField, error) { if value == "L" { return lastDayOfMonthExpr{}, nil } return convertUnit(value) } -func convertUnit(value string) (ExprField, error) { +func convertUnit(value string) (exprField, error) { num, err := strconv.Atoi(value) return unitExpr(num), err } + +type ordering int + +const ( + OrderingLess ordering = iota - 1 + OrderingEqual + OrderingGreater +) + +type exprField interface { + Compare(t time.Time, other int) ordering + Value(t time.Time, other int) int + SubsetOf(min, max int) bool +} + +type converterFn func(string) (exprField, error) diff --git a/parser_test.go b/parser_test.go index aa23d22..97f330b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -101,7 +101,7 @@ func requireErrorAs(t testing.TB, err error, target any) { } } -func requireSameKind(t testing.TB, a, b TimeUnitKind) { +func requireSameKind(t testing.TB, a, b timeUnitKind) { t.Helper() if a != b { t.Fatalf("%s != %s", a, b) diff --git a/time-unit.go b/time-unit.go index 4442d93..ad6cbeb 100644 --- a/time-unit.go +++ b/time-unit.go @@ -5,7 +5,7 @@ import "time" // secTimeUnit is a time unit implementation for the seconds field in a Cron // expression. type secTimeUnit struct { - units []ExprField + units []exprField } // Next implements TimeUnit. @@ -30,7 +30,7 @@ func (s secTimeUnit) Next(next time.Time) (time.Time, bool) { // minTimeUnit is a time unit implementation for the minutes field in a Cron // expression. type minTimeUnit struct { - fields []ExprField + fields []exprField } // Next implements TimeUnit. @@ -55,7 +55,7 @@ func (m minTimeUnit) Next(next time.Time) (time.Time, bool) { // hourTimeUnit is a time unit implementation for the hours field in a Cron // expression. type hourTimeUnit struct { - fields []ExprField + fields []exprField } // Next implements TimeUnit. @@ -80,7 +80,7 @@ func (h hourTimeUnit) Next(next time.Time) (time.Time, bool) { // dayTimeUnit is a time unit implementation for the days field in a Cron // expression. type dayTimeUnit struct { - units []ExprField + units []exprField } // Next implements TimeUnit. @@ -111,7 +111,7 @@ func (d dayTimeUnit) Next(next time.Time) (time.Time, bool) { // monthTimeUnit is a time unit implementation for the months field in a Cron // expression. type monthTimeUnit struct { - units []ExprField + units []exprField } // Next implements TimeUnit. @@ -136,7 +136,7 @@ func (m monthTimeUnit) Next(next time.Time) (time.Time, bool) { // weekdayTimeUnit is a time unit implementation for the week days field in a // Cron expression. type weekdayTimeUnit struct { - units []ExprField + units []exprField } // Next implements TimeUnit.