Skip to content

Commit

Permalink
Make internal implementations private
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilthoniel committed Dec 3, 2023
1 parent ae9f2d2 commit 34e6556
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
1 change: 1 addition & 0 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
12 changes: 7 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand All @@ -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
Expand All @@ -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]
}
64 changes: 32 additions & 32 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
}
Expand All @@ -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, "/"):
Expand All @@ -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 {
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
}

Expand All @@ -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())
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions time-unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 34e6556

Please sign in to comment.