Skip to content

Commit 9c12cfb

Browse files
Adds custom type to handle incoming bool/int fields for lgpd
1 parent 67ef78a commit 9c12cfb

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

numbers.go

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openrtb
22

33
import (
44
"encoding/json"
5+
"errors"
56
"strconv"
67
)
78

@@ -51,3 +52,27 @@ func (n *StringOrNumber) UnmarshalJSON(data []byte) error {
5152
}
5253
return nil
5354
}
55+
56+
// 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.
57+
type BoolOrNumber bool
58+
59+
// UnmarshalJSON implements json.Unmarshaler
60+
func (b *BoolOrNumber) UnmarshalJSON(data []byte) error {
61+
var val interface{}
62+
if err := json.Unmarshal(data, &val); err != nil {
63+
return err
64+
}
65+
66+
switch v := val.(type) {
67+
case bool:
68+
*b = BoolOrNumber(v)
69+
case float64:
70+
// When unmarshaling JSON into an interface value, Unmarshal stores JSON numbers in the interface value float64
71+
*b = BoolOrNumber(v != 0)
72+
default:
73+
74+
return errors.New("BoolOrInt: invalid type")
75+
}
76+
77+
return nil
78+
}

openrtb.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,10 @@ type ChannelEntity struct {
841841

842842
// RegExtension Extension object for Regulations
843843
type RegExtension struct {
844-
GDPR int `json:"gdpr,omitempty"`
845-
LGPD bool `json:"lgpd,omitempty"`
846-
PIPL bool `json:"pipl,omitempty"`
847-
USPrivacy string `json:"us_privacy,omitempty"`
844+
GDPR int `json:"gdpr,omitempty"`
845+
LGPD BoolOrNumber `json:"lgpd,omitempty"`
846+
PIPL BoolOrNumber `json:"pipl,omitempty"`
847+
USPrivacy string `json:"us_privacy,omitempty"`
848848
}
849849

850850
// UserExtension Extension object for User

0 commit comments

Comments
 (0)