-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_expression.go
125 lines (112 loc) · 3.22 KB
/
list_expression.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package schedule
import (
"sort"
"time"
)
// ListExpression is the struct used to create cron list expressions.
type ListExpression struct {
values []int
}
// List is an expression used to iterate the provided list of int parameters (*inclusive*).
func List(values []int) *ListExpression {
if len(values) < 1 {
panic("schedule: invalid ListExpression expression")
}
sort.Ints(values)
return &ListExpression{
values: values,
}
}
// ListMilliseconds uses the regular list logic, ensuring valid millisecond parameters.
func ListMilliseconds(values ...int) *ListExpression {
for _, v := range values {
validateMillisecond(v)
}
return List(values)
}
// ListSeconds uses the regular list logic, ensuring valid second parameters.
func ListSeconds(values ...int) *ListExpression {
for _, v := range values {
validateSecond(v)
}
return List(values)
}
// ListMinutes uses the regular list logic, ensuring valid minute parameters.
func ListMinutes(values ...int) *ListExpression {
for _, v := range values {
validateMinute(v)
}
return List(values)
}
// ListHours uses the regular list logic, ensuring valid hour parameters.
func ListHours(values ...int) *ListExpression {
for _, v := range values {
validateHour(v)
}
return List(values)
}
// ListDays uses the regular list logic, ensuring valid day parameters.
func ListDays(values ...int) *ListExpression {
for _, v := range values {
validateDay(v)
}
return List(values)
}
// ListWeekdays uses the regular list logic, ensuring valid time.Weekday parameters.
func ListWeekdays(values ...time.Weekday) *ListExpression {
converted := make([]int, len(values))
for i, v := range values {
vI := int(v)
validateWeekday(vI)
converted[i] = vI
}
return List(converted)
}
// ListMonths uses the regular list logic, ensuring valid time.Month parameters.
func ListMonths(values ...time.Month) *ListExpression {
converted := make([]int, len(values))
for i, v := range values {
vI := int(v)
validateMonth(vI)
converted[i] = vI
}
return List(converted)
}
// ListYears uses the regular list logic, ensuring valid year parameters.
func ListYears(values ...int) *ListExpression {
for _, v := range values {
validateYear(v)
}
return List(values)
}
// Next allows retrieval of the next value from this expression.
// Expressions are stateless, the determination of their next value is based on input.
// Given a valid expression value, the parameter inc is used to specify if it should be included in the output.
// Given the last value of the expression or above, the inc parameter is ignored.
// It returns the next value according to provided parameters and a boolean indicating if it is the last value.
func (exp *ListExpression) Next(from int, inc bool) (int, bool) {
lastI := len(exp.values) - 1
lastV := exp.values[lastI]
if from < lastV {
for i, v := range exp.values {
if from < v || (inc && from == v) {
return v, i == lastI
}
}
}
return lastV, true
}
// Contains verifies if the provided value belongs to this expression
func (exp *ListExpression) Contains(val int) bool {
firstV := exp.values[0]
lastV := exp.values[len(exp.values)-1]
if val < firstV || val > lastV {
return false
}
for _, v := range exp.values {
if val == v {
return true
}
}
return false
}