/
search.go
56 lines (45 loc) · 1.18 KB
/
search.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 timezone
import (
"context"
"slices"
"strings"
"github.com/target/goalert/permission"
)
// SearchOptions allow filtering and paginating the list of timezones.
type SearchOptions struct {
Search string `json:"s,omitempty"`
After SearchCursor `json:"a,omitempty"`
// Omit specifies a list of timezone names to exclude from the results.
Omit []string `json:"o,omitempty"`
Limit int `json:"-"`
}
// SearchCursor is used to indicate a position in a paginated list.
type SearchCursor struct {
Name string `json:"n,omitempty"`
}
func (store *Store) Search(ctx context.Context, opts *SearchOptions) ([]string, error) {
err := permission.LimitCheckAny(ctx, permission.User)
if err != nil {
return nil, err
}
if opts == nil {
opts = &SearchOptions{}
}
results := make([]string, 0, len(zones))
for _, zone := range zones {
if opts.Search != "" && !strings.Contains(strings.ToLower(zone), strings.ToLower(opts.Search)) {
continue
}
if slices.Contains(opts.Omit, zone) {
continue
}
if opts.After.Name != "" && zone <= opts.After.Name {
continue
}
results = append(results, zone)
if len(results) >= opts.Limit {
break
}
}
return results, nil
}