Skip to content

Commit

Permalink
Adds custom type to handle incoming bool/int fields for lgpd
Browse files Browse the repository at this point in the history
  • Loading branch information
vibhore-bhatnagar committed Apr 10, 2024
1 parent 67ef78a commit 9c12cfb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openrtb

import (
"encoding/json"
"errors"
"strconv"
)

Expand Down Expand Up @@ -51,3 +52,27 @@ func (n *StringOrNumber) UnmarshalJSON(data []byte) error {
}
return nil
}

// BoolOrNumber attemps to fix OpenRTB incompatibilities where a field is expected as bool but the spec expects int values. This was not seen till now, but some mediation partners will follow the spec closely.
type BoolOrNumber bool

// UnmarshalJSON implements json.Unmarshaler
func (b *BoolOrNumber) UnmarshalJSON(data []byte) error {
var val interface{}
if err := json.Unmarshal(data, &val); err != nil {
return err
}

switch v := val.(type) {
case bool:
*b = BoolOrNumber(v)
case float64:
// When unmarshaling JSON into an interface value, Unmarshal stores JSON numbers in the interface value float64
*b = BoolOrNumber(v != 0)
default:

return errors.New("BoolOrInt: invalid type")
}

return nil
}
8 changes: 4 additions & 4 deletions openrtb.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,10 +841,10 @@ type ChannelEntity struct {

// RegExtension Extension object for Regulations
type RegExtension struct {
GDPR int `json:"gdpr,omitempty"`
LGPD bool `json:"lgpd,omitempty"`
PIPL bool `json:"pipl,omitempty"`
USPrivacy string `json:"us_privacy,omitempty"`
GDPR int `json:"gdpr,omitempty"`
LGPD BoolOrNumber `json:"lgpd,omitempty"`
PIPL BoolOrNumber `json:"pipl,omitempty"`
USPrivacy string `json:"us_privacy,omitempty"`
}

// UserExtension Extension object for User
Expand Down

0 comments on commit 9c12cfb

Please sign in to comment.