Skip to content

Commit edf8e83

Browse files
committed
Add godoc to exported fields, fix golint warning
1 parent 470ac64 commit edf8e83

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

parser.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ import (
88
"time"
99
)
1010

11+
// Configuration options for creating a parser. Most options specify which
12+
// fields should be included, while others enable features. If a field is not
13+
// included the parser will assume a default value. These options do not change
14+
// the order fields are parse in.
1115
type ParseOption int
1216

1317
const (
14-
Second ParseOption = 1 << iota
15-
Minute
16-
Hour
17-
Dom
18-
Month
19-
Dow
20-
DowOptional
21-
Descriptor
18+
Second ParseOption = 1 << iota // Seconds field, default 0
19+
Minute // Minutes field, default 0
20+
Hour // Hours field, default 0
21+
Dom // Day of month field, default *
22+
Month // Month field, default *
23+
Dow // Day of week field, default *
24+
DowOptional // Optional day of week field, default *
25+
Descriptor // Allow descriptors such as @monthly, @weekly, etc.
2226
)
2327

2428
var places = []ParseOption{
@@ -39,11 +43,26 @@ var defaults = []string{
3943
"*",
4044
}
4145

46+
// A custom Parser that can be configured.
4247
type Parser struct {
4348
options ParseOption
4449
optionals int
4550
}
4651

52+
// Creates a custom Parser with custom options.
53+
//
54+
// // Standard parser without descriptors
55+
// specParser := NewParser(Minute | Hour | Dom | Month | Dow)
56+
// sched, err := specParser.Parse("0 0 15 */3 *")
57+
//
58+
// // Same as above, just excludes time fields
59+
// subsParser := NewParser(Dom | Month | Dow)
60+
// sched, err := specParser.Parse("15 */3 *")
61+
//
62+
// // Same as above, just makes Dow optional
63+
// subsParser := NewParser(Dom | Month | DowOptional)
64+
// sched, err := specParser.Parse("15 */3")
65+
//
4766
func NewParser(options ParseOption) Parser {
4867
optionals := 0
4968
if options&DowOptional > 0 {
@@ -53,6 +72,9 @@ func NewParser(options ParseOption) Parser {
5372
return Parser{options, optionals}
5473
}
5574

75+
// Parse returns a new crontab schedule representing the given spec.
76+
// It returns a descriptive error if the spec is not valid.
77+
// It accepts crontab specs and features configured by NewParser.
5678
func (p Parser) Parse(spec string) (Schedule, error) {
5779
if spec[0] == '@' && p.options&Descriptor > 0 {
5880
return parseDescriptor(spec)
@@ -74,9 +96,8 @@ func (p Parser) Parse(spec string) (Schedule, error) {
7496
if count := len(fields); count < min || count > max {
7597
if min == max {
7698
return nil, fmt.Errorf("Expected exactly %d fields, found %d: %s", min, count, spec)
77-
} else {
78-
return nil, fmt.Errorf("Expected %d to %d fields, found %d: %s", min, max, count, spec)
7999
}
100+
return nil, fmt.Errorf("Expected %d to %d fields, found %d: %s", min, max, count, spec)
80101
}
81102

82103
// Fill in missing fields

0 commit comments

Comments
 (0)