forked from gorhill/cronexpr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronexpr_format.go
56 lines (44 loc) · 1.51 KB
/
cronexpr_format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cronexpr
import (
"errors"
)
// CronFormat is an enum for different cron expression formats.
type CronFormat string
const (
// The standard Cron format, see https://en.wikipedia.org/wiki/Cron#CRON_expression.
// Uses the default implementation from https://github.com/gorhill/cronexpr, with some additional
// relaxed rules.
CronFormatStandard CronFormat = "standard"
// Uses the Quartz scheduler format.
// See http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html#format.
CronFormatQuartz CronFormat = "quartz"
)
var ErrUnknownFormat = errors.New("unknown CronFormat")
// formattedExpression wraps an Expression acts as a middleware injector for different CronFormats.
type formattedExpression struct {
*Expression
handler expressionHandler
}
// expressionHandler supports delegation of field parsing to custom handlers.
// *Expression should always implement this interface, which provides the default implementation.
type expressionHandler interface {
dowFieldHandler(s string) error
}
var _ expressionHandler = &Expression{}
func newFormattedExpression(format CronFormat) (*formattedExpression, error) {
e := &formattedExpression{
Expression: &Expression{},
}
switch format {
case CronFormatStandard:
e.handler = e.Expression
case CronFormatQuartz:
e.handler = &quartzExpression{Expression: e.Expression}
default:
return nil, ErrUnknownFormat
}
return e, nil
}
func (e *formattedExpression) dowFieldHandler(s string) error {
return e.handler.dowFieldHandler(s)
}