-
Notifications
You must be signed in to change notification settings - Fork 758
/
Copy pathbidderfilter.go
66 lines (54 loc) · 2.11 KB
/
bidderfilter.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
package usersync
import (
"strings"
)
// BidderFilter determines if a bidder has permission to perform a user sync activity.
type BidderFilter interface {
// Allowed returns true if the filter determines the bidder has permission and false if either
// the bidder does not have permission or if the filter has an invalid mode.
Allowed(bidder string) bool
}
// BidderFilterMode represents the inclusion mode of a BidderFilter.
type BidderFilterMode int
const (
BidderFilterModeInclude BidderFilterMode = iota
BidderFilterModeExclude
)
// SpecificBidderFilter implements the BidderFilter which applies the same mode for a list of bidders.
type SpecificBidderFilter struct {
biddersLookup map[string]struct{}
mode BidderFilterMode
}
// Allowed returns true if the bidder is specified and the mode is include or if the bidder is not specified
// and the mode is exclude and returns false in the opposite cases or when the mode is invalid.
func (f SpecificBidderFilter) Allowed(bidder string) bool {
_, exists := f.biddersLookup[bidder]
switch f.mode {
case BidderFilterModeInclude:
return exists
case BidderFilterModeExclude:
return !exists
default:
return false
}
}
// NewSpecificBidderFilter returns a new instance of the NewSpecificBidderFilter filter.
func NewSpecificBidderFilter(bidders []string, mode BidderFilterMode) BidderFilter {
biddersLookup := make(map[string]struct{}, len(bidders))
for _, bidder := range bidders {
biddersLookup[strings.ToLower(bidder)] = struct{}{}
}
return SpecificBidderFilter{biddersLookup: biddersLookup, mode: mode}
}
// UniformBidderFilter implements the BidderFilter interface which applies the same mode for all bidders.
type UniformBidderFilter struct {
mode BidderFilterMode
}
// Allowed returns true if the mode is include and false if the mode is either exclude or invalid.
func (f UniformBidderFilter) Allowed(bidder string) bool {
return f.mode == BidderFilterModeInclude
}
// NewUniformBidderFilter returns a new instance of the UniformBidderFilter filter.
func NewUniformBidderFilter(mode BidderFilterMode) BidderFilter {
return UniformBidderFilter{mode: mode}
}