-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfind.go
53 lines (46 loc) · 1.17 KB
/
find.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
package cryptoWallet
import (
I "github.com/xaionaro-go/cryptoWallet/interfaces"
)
// Filter is a struct that could be passed to Find() to select devices
type Filter struct {
VendorID *uint16
ProductIDs []uint16
}
// IsFit returns true if vendorID and productID matches the rules of
// the Filter struct
//
// - If `filter.VendorID` is nil then it will match to any vendorID
// and any productID
//
// - If `filter.ProductIDs` is an empty slice and `filter.VendorID` is
// not nil then it will match for any product of the defined vendor
// ID.
//
// - If the `filter` is empty then it will match for vendor ID and any
// productID
func (filter Filter) IsFit(vendorID uint16, productID uint16) bool {
if filter.VendorID == nil {
return true
}
if vendorID != *filter.VendorID {
return false
}
if len(filter.ProductIDs) == 0 {
return true
}
for _, wantedProductID := range filter.ProductIDs { // TODO: don't iterate everytime through this slice
if productID == wantedProductID {
return true
}
}
return false
}
// FindAny returns any found known wallet
func FindAny() I.Wallet {
result := Find(Filter{})
if len(result) == 0 {
return nil
}
return result[0]
}