From 9c12cfbec33e8fba11b11f535f3b73f8840a0654 Mon Sep 17 00:00:00 2001 From: Vibhore Bhatnagar Date: Wed, 10 Apr 2024 12:08:16 -0400 Subject: [PATCH] Adds custom type to handle incoming bool/int fields for lgpd --- numbers.go | 25 +++++++++++++++++++++++++ openrtb.go | 8 ++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/numbers.go b/numbers.go index 515f6ff..46ee57b 100644 --- a/numbers.go +++ b/numbers.go @@ -2,6 +2,7 @@ package openrtb import ( "encoding/json" + "errors" "strconv" ) @@ -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 +} diff --git a/openrtb.go b/openrtb.go index d44666d..64049e0 100644 --- a/openrtb.go +++ b/openrtb.go @@ -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