-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathfilter.go
More file actions
173 lines (143 loc) · 3.49 KB
/
filter.go
File metadata and controls
173 lines (143 loc) · 3.49 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package workflow
import (
"slices"
"strconv"
"strings"
"time"
)
const multiValueDelimiter = ","
func MakeFilter(filters ...RecordFilter) *recordFilters {
var rf recordFilters
for _, f := range filters {
f(&rf)
}
return &rf
}
type recordFilters struct {
byForeignID Filter
byStatus Filter
byRunState Filter
byCreatedAtAfter FilterTime
byCreatedAtBefore FilterTime
}
func (r recordFilters) ByForeignID() Filter {
return r.byForeignID
}
func (r recordFilters) ByStatus() Filter {
return r.byStatus
}
func (r recordFilters) ByRunState() Filter {
return r.byRunState
}
func (r recordFilters) ByCreatedAtAfter() FilterTime {
return r.byCreatedAtAfter
}
func (r recordFilters) ByCreatedAtBefore() FilterTime {
return r.byCreatedAtBefore
}
func makeFilterValue(value string, isMultiMatch bool) Filter {
return Filter{
Enabled: true,
IsMultiMatch: isMultiMatch,
value: value,
}
}
type Filter struct {
Enabled bool
IsMultiMatch bool
value string
}
func (f Filter) Matches(findValue string) bool {
if !f.IsMultiMatch {
return f.value == findValue
}
return slices.Contains(f.MultiValues(), findValue)
}
func (f Filter) MultiValues() []string {
return strings.Split(f.value, multiValueDelimiter)
}
func (f Filter) Value() string {
return f.value
}
func makeFilterTime(compare int, value time.Time) FilterTime {
return FilterTime{
Enabled: true,
compare: compare,
value: value,
}
}
type FilterTime struct {
Enabled bool
compare int
value time.Time
}
func (f FilterTime) Matches(findValue time.Time) bool {
if f.value.Compare(findValue) == f.compare {
return true
}
return false
}
func (f FilterTime) Value() time.Time {
return f.value
}
type RecordFilter func(filters *recordFilters)
func FilterByForeignID(foreignIDs ...string) RecordFilter {
return func(filters *recordFilters) {
if len(foreignIDs) == 1 {
filters.byForeignID = makeFilterValue(foreignIDs[0], false)
return
}
var val strings.Builder
for i, foreignID := range foreignIDs {
if i != 0 {
val.WriteString(multiValueDelimiter)
}
val.WriteString(foreignID)
}
filters.byForeignID = makeFilterValue(val.String(), true)
}
}
func FilterByStatus[statusType ~int | ~int8 | ~int16 | ~int32 | ~int64](statuses ...statusType) RecordFilter {
return func(filters *recordFilters) {
if len(statuses) == 1 {
i := strconv.FormatInt(int64(statuses[0]), 10)
filters.byStatus = makeFilterValue(i, false)
return
}
var val strings.Builder
for i, status := range statuses {
if i != 0 {
val.WriteString(multiValueDelimiter)
}
val.WriteString(strconv.FormatInt(int64(status), 10))
}
filters.byStatus = makeFilterValue(val.String(), true)
}
}
func FilterByRunState(runStates ...RunState) RecordFilter {
return func(filters *recordFilters) {
if len(runStates) == 1 {
i := strconv.FormatInt(int64(runStates[0]), 10)
filters.byRunState = makeFilterValue(i, false)
return
}
var val strings.Builder
for i, rs := range runStates {
if i != 0 {
val.WriteString(multiValueDelimiter)
}
val.WriteString(strconv.FormatInt(int64(rs), 10))
}
filters.byRunState = makeFilterValue(val.String(), true)
}
}
func FilterByCreatedAtAfter(after time.Time) RecordFilter {
return func(filters *recordFilters) {
filters.byCreatedAtAfter = makeFilterTime(-1, after)
}
}
func FilterByCreatedAtBefore(before time.Time) RecordFilter {
return func(filters *recordFilters) {
filters.byCreatedAtBefore = makeFilterTime(1, before)
}
}