github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/imp_appnexus.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/prebid/prebid-server/v2/util/jsonutil"
     9  )
    10  
    11  // ExtImpAppnexus defines the contract for bidrequest.imp[i].ext.prebid.bidder.appnexus
    12  type ExtImpAppnexus struct {
    13  	DeprecatedPlacementId    jsonutil.StringInt     `json:"placementId"`
    14  	LegacyInvCode            string                 `json:"invCode"`
    15  	LegacyTrafficSourceCode  string                 `json:"trafficSourceCode"`
    16  	PlacementId              jsonutil.StringInt     `json:"placement_id"`
    17  	InvCode                  string                 `json:"inv_code"`
    18  	Member                   string                 `json:"member"`
    19  	Keywords                 ExtImpAppnexusKeywords `json:"keywords"`
    20  	TrafficSourceCode        string                 `json:"traffic_source_code"`
    21  	Reserve                  float64                `json:"reserve"`
    22  	Position                 string                 `json:"position"`
    23  	UsePaymentRule           *bool                  `json:"use_pmt_rule"`
    24  	DeprecatedUsePaymentRule *bool                  `json:"use_payment_rule"`
    25  	// At this time we do no processing on the private sizes, so just leaving it as a JSON blob.
    26  	PrivateSizes  json.RawMessage `json:"private_sizes"`
    27  	AdPodId       bool            `json:"generate_ad_pod_id"`
    28  	ExtInvCode    string          `json:"ext_inv_code"`
    29  	ExternalImpId string          `json:"external_imp_id"`
    30  }
    31  
    32  type ExtImpAppnexusKeywords string
    33  
    34  // extImpAppnexusKeyVal defines the contract for bidrequest.imp[i].ext.prebid.bidder.appnexus.keywords[i]
    35  type extImpAppnexusKeyVal struct {
    36  	Key    string   `json:"key,omitempty"`
    37  	Values []string `json:"value,omitempty"`
    38  }
    39  
    40  func (ks *ExtImpAppnexusKeywords) UnmarshalJSON(b []byte) error {
    41  	if len(b) == 0 {
    42  		return nil
    43  	}
    44  
    45  	switch b[0] {
    46  	case '{':
    47  		var results map[string][]string
    48  		if err := jsonutil.UnmarshalValid(b, &results); err != nil {
    49  			return err
    50  		}
    51  
    52  		var keywords strings.Builder
    53  		for key, values := range results {
    54  			if len(values) == 0 {
    55  				keywords.WriteString(fmt.Sprintf("%s,", key))
    56  			} else {
    57  				for _, val := range values {
    58  					keywords.WriteString(fmt.Sprintf("%s=%s,", key, val))
    59  				}
    60  			}
    61  		}
    62  		if len(keywords.String()) > 0 {
    63  			*ks = ExtImpAppnexusKeywords(keywords.String()[:keywords.Len()-1])
    64  		}
    65  	case '[':
    66  		var results []extImpAppnexusKeyVal
    67  		if err := jsonutil.UnmarshalValid(b, &results); err != nil {
    68  			return err
    69  		}
    70  		var kvs strings.Builder
    71  		for _, kv := range results {
    72  			if len(kv.Values) == 0 {
    73  				kvs.WriteString(fmt.Sprintf("%s,", kv.Key))
    74  			} else {
    75  				for _, val := range kv.Values {
    76  					kvs.WriteString(fmt.Sprintf("%s=%s,", kv.Key, val))
    77  				}
    78  			}
    79  		}
    80  		if len(kvs.String()) > 0 {
    81  			*ks = ExtImpAppnexusKeywords(kvs.String()[:kvs.Len()-1])
    82  		}
    83  	case '"':
    84  		var keywords string
    85  		if err := jsonutil.UnmarshalValid(b, &keywords); err != nil {
    86  			return err
    87  		}
    88  		*ks = ExtImpAppnexusKeywords(keywords)
    89  	}
    90  	return nil
    91  }
    92  
    93  func (ks *ExtImpAppnexusKeywords) String() string {
    94  	return *(*string)(ks)
    95  }