github.com/prebid/prebid-server/v2@v2.18.0/hooks/hookstage/bidderrequest_mutations.go (about)

     1  package hookstage
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     7  
     8  	"github.com/prebid/openrtb/v20/adcom1"
     9  )
    10  
    11  func (c *ChangeSet[T]) BidderRequest() ChangeSetBidderRequest[T] {
    12  	return ChangeSetBidderRequest[T]{changeSet: c}
    13  }
    14  
    15  type ChangeSetBidderRequest[T any] struct {
    16  	changeSet *ChangeSet[T]
    17  }
    18  
    19  func (c ChangeSetBidderRequest[T]) BAdv() ChangeSetBAdv[T] {
    20  	return ChangeSetBAdv[T]{changeSetBidderRequest: c}
    21  }
    22  
    23  func (c ChangeSetBidderRequest[T]) BCat() ChangeSetBCat[T] {
    24  	return ChangeSetBCat[T]{changeSetBidderRequest: c}
    25  }
    26  
    27  func (c ChangeSetBidderRequest[T]) CatTax() ChangeSetCatTax[T] {
    28  	return ChangeSetCatTax[T]{changeSetBidderRequest: c}
    29  }
    30  
    31  func (c ChangeSetBidderRequest[T]) BApp() ChangeSetBApp[T] {
    32  	return ChangeSetBApp[T]{changeSetBidderRequest: c}
    33  }
    34  
    35  func (c ChangeSetBidderRequest[T]) castPayload(p T) (*openrtb_ext.RequestWrapper, error) {
    36  	if payload, ok := any(p).(BidderRequestPayload); ok {
    37  		if payload.Request == nil || payload.Request.BidRequest == nil {
    38  			return nil, errors.New("payload contains a nil bid request")
    39  		}
    40  		return payload.Request, nil
    41  	}
    42  	return nil, errors.New("failed to cast BidderRequestPayload")
    43  }
    44  
    45  type ChangeSetBAdv[T any] struct {
    46  	changeSetBidderRequest ChangeSetBidderRequest[T]
    47  }
    48  
    49  func (c ChangeSetBAdv[T]) Update(badv []string) {
    50  	c.changeSetBidderRequest.changeSet.AddMutation(func(p T) (T, error) {
    51  		bidRequest, err := c.changeSetBidderRequest.castPayload(p)
    52  		if err == nil {
    53  			bidRequest.BAdv = badv
    54  		}
    55  		return p, err
    56  	}, MutationUpdate, "bidrequest", "badv")
    57  }
    58  
    59  type ChangeSetBCat[T any] struct {
    60  	changeSetBidderRequest ChangeSetBidderRequest[T]
    61  }
    62  
    63  func (c ChangeSetBCat[T]) Update(bcat []string) {
    64  	c.changeSetBidderRequest.changeSet.AddMutation(func(p T) (T, error) {
    65  		bidRequest, err := c.changeSetBidderRequest.castPayload(p)
    66  		if err == nil {
    67  			bidRequest.BCat = bcat
    68  		}
    69  		return p, err
    70  	}, MutationUpdate, "bidrequest", "bcat")
    71  }
    72  
    73  type ChangeSetCatTax[T any] struct {
    74  	changeSetBidderRequest ChangeSetBidderRequest[T]
    75  }
    76  
    77  func (c ChangeSetCatTax[T]) Update(cattax adcom1.CategoryTaxonomy) {
    78  	c.changeSetBidderRequest.changeSet.AddMutation(func(p T) (T, error) {
    79  		bidRequest, err := c.changeSetBidderRequest.castPayload(p)
    80  		if err == nil {
    81  			bidRequest.CatTax = cattax
    82  		}
    83  		return p, err
    84  	}, MutationUpdate, "bidrequest", "cattax")
    85  }
    86  
    87  type ChangeSetBApp[T any] struct {
    88  	changeSetBidderRequest ChangeSetBidderRequest[T]
    89  }
    90  
    91  func (c ChangeSetBApp[T]) Update(bapp []string) {
    92  	c.changeSetBidderRequest.changeSet.AddMutation(func(p T) (T, error) {
    93  		bidRequest, err := c.changeSetBidderRequest.castPayload(p)
    94  		if err == nil {
    95  			bidRequest.BApp = bapp
    96  		}
    97  		return p, err
    98  	}, MutationUpdate, "bidrequest", "bapp")
    99  }