github.com/prebid/prebid-server/v2@v2.18.0/usersync/bidderfilter.go (about)

     1  package usersync
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // BidderFilter determines if a bidder has permission to perform a user sync activity.
     8  type BidderFilter interface {
     9  	// Allowed returns true if the filter determines the bidder has permission and false if either
    10  	// the bidder does not have permission or if the filter has an invalid mode.
    11  	Allowed(bidder string) bool
    12  }
    13  
    14  // BidderFilterMode represents the inclusion mode of a BidderFilter.
    15  type BidderFilterMode int
    16  
    17  const (
    18  	BidderFilterModeInclude BidderFilterMode = iota
    19  	BidderFilterModeExclude
    20  )
    21  
    22  // SpecificBidderFilter implements the BidderFilter which applies the same mode for a list of bidders.
    23  type SpecificBidderFilter struct {
    24  	biddersLookup map[string]struct{}
    25  	mode          BidderFilterMode
    26  }
    27  
    28  // Allowed returns true if the bidder is specified and the mode is include or if the bidder is not specified
    29  // and the mode is exclude and returns false in the opposite cases or when the mode is invalid.
    30  func (f SpecificBidderFilter) Allowed(bidder string) bool {
    31  	_, exists := f.biddersLookup[bidder]
    32  
    33  	switch f.mode {
    34  	case BidderFilterModeInclude:
    35  		return exists
    36  	case BidderFilterModeExclude:
    37  		return !exists
    38  	default:
    39  		return false
    40  	}
    41  }
    42  
    43  // NewSpecificBidderFilter returns a new instance of the NewSpecificBidderFilter filter.
    44  func NewSpecificBidderFilter(bidders []string, mode BidderFilterMode) BidderFilter {
    45  	biddersLookup := make(map[string]struct{}, len(bidders))
    46  	for _, bidder := range bidders {
    47  		biddersLookup[strings.ToLower(bidder)] = struct{}{}
    48  	}
    49  
    50  	return SpecificBidderFilter{biddersLookup: biddersLookup, mode: mode}
    51  }
    52  
    53  // UniformBidderFilter implements the BidderFilter interface which applies the same mode for all bidders.
    54  type UniformBidderFilter struct {
    55  	mode BidderFilterMode
    56  }
    57  
    58  // Allowed returns true if the mode is include and false if the mode is either exclude or invalid.
    59  func (f UniformBidderFilter) Allowed(bidder string) bool {
    60  	return f.mode == BidderFilterModeInclude
    61  }
    62  
    63  // NewUniformBidderFilter returns a new instance of the UniformBidderFilter filter.
    64  func NewUniformBidderFilter(mode BidderFilterMode) BidderFilter {
    65  	return UniformBidderFilter{mode: mode}
    66  }