github.com/prebid/prebid-server/v2@v2.18.0/gdpr/signal.go (about)

     1  package gdpr
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/prebid/prebid-server/v2/errortypes"
     7  )
     8  
     9  type Signal int
    10  
    11  const (
    12  	SignalAmbiguous Signal = -1
    13  	SignalNo        Signal = 0
    14  	SignalYes       Signal = 1
    15  )
    16  
    17  var gdprSignalError = &errortypes.BadInput{Message: "GDPR signal should be integer 0 or 1"}
    18  
    19  // StrSignalParse returns a parsed GDPR signal or a parse error.
    20  func StrSignalParse(signal string) (Signal, error) {
    21  	if signal == "" {
    22  		return SignalAmbiguous, nil
    23  	}
    24  
    25  	i, err := strconv.Atoi(signal)
    26  
    27  	if err != nil {
    28  		return SignalAmbiguous, gdprSignalError
    29  	}
    30  
    31  	return IntSignalParse(i)
    32  }
    33  
    34  // IntSignalParse checks parameter i is not out of bounds and returns a GDPR signal error
    35  func IntSignalParse(i int) (Signal, error) {
    36  	if i != 0 && i != 1 {
    37  		return SignalAmbiguous, gdprSignalError
    38  	}
    39  
    40  	return Signal(i), nil
    41  }
    42  
    43  // SignalNormalize normalizes a GDPR signal to ensure it's always either SignalYes or SignalNo.
    44  func SignalNormalize(signal Signal, gdprDefaultValue string) Signal {
    45  	if signal != SignalAmbiguous {
    46  		return signal
    47  	}
    48  
    49  	if gdprDefaultValue == "0" {
    50  		return SignalNo
    51  	}
    52  
    53  	return SignalYes
    54  }