-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinquery.go
88 lines (71 loc) · 2.23 KB
/
pinquery.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
package sips
import (
"fmt"
"strings"
"time"
)
// PinQuery provides query info for finding pinning requests. Any
// fields which are zero values, or have length zero in the case of
// slices, should be considered as though they are not present.
type PinQuery struct {
// CID is a list CIDs that are being searched for.
CID []string
// Name is the name of the desired pinning request.
Name string
// Match is the strategy to use for finding the name.
Match TextMatchingStrategy
// Status is a list of statuses used to filter the returned
// requests.
Status []RequestStatus
// Before and After indicate creation time filters.
Before, After time.Time
// Limit is the maximum number of results that should be returned.
// If the handler returns more than this, the list will be truncated
// to match the value of this field.
Limit int
// Meta is used to filter against the Meta field of the returned
// requests. Note that this is simply the result of unmarshalling
// json using the encoding/json package, so it will likely be a
// map[string]interface{}, but it might not be, either. It is up to
// the handler to deal with this as it sees fit.
Meta interface{}
}
func defaultPinQuery() PinQuery {
return PinQuery{
Match: Exact,
Limit: 10,
}
}
// TextMatchingStrategy indicates a strategy to use for matching one
// string against another.
type TextMatchingStrategy string
const (
Exact TextMatchingStrategy = "exact"
IExact TextMatchingStrategy = "iexact"
Partial TextMatchingStrategy = "partial"
IPartial TextMatchingStrategy = "ipartial"
)
func (tms TextMatchingStrategy) valid() bool {
switch tms {
case Exact, IExact, Partial, IPartial:
return true
default:
return false
}
}
// Match performs a match using the specified strategy. It will panic
// if the strategy provided is invalid.
func (tms TextMatchingStrategy) Match(haystack, needle string) bool {
switch tms {
case Exact:
return needle == haystack
case IExact:
return strings.ToLower(needle) == strings.ToLower(haystack)
case Partial:
return strings.Contains(haystack, needle)
case IPartial:
return strings.Contains(strings.ToLower(haystack), strings.ToLower(needle))
default:
panic(fmt.Errorf("invalid text matching strategy: %q", tms))
}
}