File size: 3,244 Bytes
1e92f2d |
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 |
package nfydest
import (
"context"
"slices"
"sort"
"strings"
)
type FieldSearcher interface {
SearchField(ctx context.Context, fieldID string, options SearchOptions) (*SearchResult, error)
FieldLabel(ctx context.Context, fieldID, value string) (string, error)
}
type SearchResult struct {
HasNextPage bool
Cursor string
Values []FieldValue
}
type FieldValue struct {
Value string
Label string
IsFavorite bool
}
type SearchOptions struct {
Search string
Omit []string
Cursor string
Limit int
}
type Fieldable interface {
AsField() FieldValue
}
type Cursorable interface {
Cursor() (string, error)
Fieldable
}
type OptionFrommer interface {
FromNotifyOptions(context.Context, SearchOptions) error
}
func SearchByCursorFunc[OptionType any, POptionType interface {
*OptionType
OptionFrommer
}, Result Cursorable](ctx context.Context, opts SearchOptions, searchFn func(context.Context, *OptionType) ([]Result, error)) (*SearchResult, error) {
if opts.Limit <= 0 {
opts.Limit = 15
}
origLimit := opts.Limit
opts.Limit++ // Fetch one more to determine if there is a next page.
var searchOpts OptionType
p := POptionType(&searchOpts)
err := p.FromNotifyOptions(ctx, opts)
if err != nil {
return nil, err
}
results, err := searchFn(ctx, &searchOpts)
if err != nil {
return nil, err
}
var res SearchResult
if len(results) > origLimit {
res.HasNextPage = true
res.Cursor, err = results[origLimit].Cursor()
if err != nil {
return nil, err
}
results = results[:origLimit]
}
for _, r := range results {
res.Values = append(res.Values, r.AsField())
}
return &res, nil
}
// SearchByListFunc allows returning a SearchResult from a function that returns a list of Fieldable items.
func SearchByListFunc[T Fieldable](ctx context.Context, searchOpts SearchOptions, listFn func(context.Context) ([]T, error)) (*SearchResult, error) {
if searchOpts.Limit <= 0 {
searchOpts.Limit = 15 // Default limit.
}
_items, err := listFn(ctx)
if err != nil {
return nil, err
}
items := make([]FieldValue, 0, len(_items))
for _, item := range _items {
items = append(items, item.AsField())
}
// Sort by name, case-insensitive, then sensitive.
sort.Slice(items, func(i, j int) bool {
iLabel, jLabel := strings.ToLower(items[i].Label), strings.ToLower(items[j].Label)
if iLabel != jLabel {
return iLabel < jLabel
}
return items[i].Label < items[j].Label
})
// No DB search, so we manually filter for the cursor and search strings.
searchOpts.Search = strings.ToLower(searchOpts.Search)
filtered := items[:0]
for _, item := range items {
lowerName := strings.ToLower(item.Label)
if !strings.Contains(lowerName, searchOpts.Search) {
continue
}
if searchOpts.Cursor != "" && item.Label <= searchOpts.Cursor {
continue
}
if slices.Contains(searchOpts.Omit, item.Value) {
continue
}
filtered = append(filtered, item)
}
items = filtered
hasNextPage := len(items) > searchOpts.Limit
if hasNextPage {
items = items[:searchOpts.Limit]
}
var cursor string
if len(items) > 0 {
cursor = items[len(items)-1].Label
}
return &SearchResult{
HasNextPage: hasNextPage,
Cursor: cursor,
Values: items,
}, nil
}
|