github.com/prebid/prebid-server/v2@v2.18.0/privacy/rule_condition.go (about)

     1  package privacy
     2  
     3  // noClausesDefinedResult represents the default return when there is no matching criteria specified.
     4  const noClausesDefinedResult = true
     5  
     6  type ConditionRule struct {
     7  	result        ActivityResult
     8  	componentName []string
     9  	componentType []string
    10  	gppSID        []int8
    11  }
    12  
    13  func (r ConditionRule) Evaluate(target Component, request ActivityRequest) ActivityResult {
    14  	if matched := evaluateComponentName(target, r.componentName); !matched {
    15  		return ActivityAbstain
    16  	}
    17  
    18  	if matched := evaluateComponentType(target, r.componentType); !matched {
    19  		return ActivityAbstain
    20  	}
    21  
    22  	if matched := evaluateGPPSID(r.gppSID, request); !matched {
    23  		return ActivityAbstain
    24  	}
    25  
    26  	return r.result
    27  }
    28  
    29  func evaluateComponentName(target Component, componentNames []string) bool {
    30  	// no clauses are considered a match
    31  	if len(componentNames) == 0 {
    32  		return noClausesDefinedResult
    33  	}
    34  
    35  	for _, n := range componentNames {
    36  		if target.MatchesName(n) {
    37  			return true
    38  		}
    39  	}
    40  
    41  	return false
    42  }
    43  
    44  func evaluateComponentType(target Component, componentTypes []string) bool {
    45  	if len(componentTypes) == 0 {
    46  		return noClausesDefinedResult
    47  	}
    48  
    49  	// if there are clauses, at least one needs to match
    50  	for _, t := range componentTypes {
    51  		if target.MatchesType(t) {
    52  			return true
    53  		}
    54  	}
    55  
    56  	return false
    57  }
    58  
    59  func evaluateGPPSID(sid []int8, request ActivityRequest) bool {
    60  	if len(sid) == 0 {
    61  		return noClausesDefinedResult
    62  	}
    63  
    64  	for _, x := range getGPPSID(request) {
    65  		for _, y := range sid {
    66  			if x == y {
    67  				return true
    68  			}
    69  		}
    70  	}
    71  	return false
    72  }
    73  
    74  func getGPPSID(request ActivityRequest) []int8 {
    75  	if request.IsPolicies() {
    76  		return request.policies.GPPSID
    77  	}
    78  
    79  	if request.IsBidRequest() && request.bidRequest.Regs != nil {
    80  		return request.bidRequest.Regs.GPPSID
    81  	}
    82  
    83  	return nil
    84  }