Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle incoming bool/int fields for lgpd #38

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading