diff --git a/README.md b/README.md index a039194..4d88809 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # openrtb -[![GoDoc](https://godoc.org/gopkg.in/mxmCherry/openrtb.v2?status.svg)](https://godoc.org/gopkg.in/mxmCherry/openrtb.v2) +[![GoDoc](https://godoc.org/gopkg.in/mxmCherry/openrtb.v3?status.svg)](https://godoc.org/gopkg.in/mxmCherry/openrtb.v3) [OpenRTB](//github.com/openrtb/OpenRTB) [v2.3.1](//github.com/openrtb/OpenRTB/blob/master/OpenRTB-API-Specification-Version-2-3-1-FINAL.pdf) types for Go programming language (golang) @@ -9,11 +9,11 @@ # Using ```bash -go get -u "gopkg.in/mxmCherry/openrtb.v2" +go get -u "gopkg.in/mxmCherry/openrtb.v3" ``` ```go -import "gopkg.in/mxmCherry/openrtb.v2" +import "gopkg.in/mxmCherry/openrtb.v3" ``` # Goals diff --git a/app.go b/app.go index 11b1cc0..af13077 100644 --- a/app.go +++ b/app.go @@ -128,5 +128,5 @@ type App struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/banner.go b/banner.go index 068991e..00be11c 100644 --- a/banner.go +++ b/banner.go @@ -151,5 +151,5 @@ type Banner struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/bid.go b/bid.go index 2cfd2cb..48ff4a8 100644 --- a/bid.go +++ b/bid.go @@ -152,5 +152,5 @@ type Bid struct { // object // Description: // Placeholder for bidder-specific extensions to OpenRTB - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/bid_request.go b/bid_request.go index 063c025..0071fb9 100644 --- a/bid_request.go +++ b/bid_request.go @@ -150,5 +150,5 @@ type BidRequest struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/bid_response.go b/bid_response.go index 550a1da..c4aa526 100644 --- a/bid_response.go +++ b/bid_response.go @@ -70,5 +70,5 @@ type BidResponse struct { // object // Description: // Placeholder for bidder-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/content.go b/content.go index fbf1cfe..db6782f 100644 --- a/content.go +++ b/content.go @@ -176,5 +176,5 @@ type Content struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/data.go b/data.go index b4f07d1..a852cab 100644 --- a/data.go +++ b/data.go @@ -39,5 +39,5 @@ type Data struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/deal.go b/deal.go index 5cb2da0..38162a1 100644 --- a/deal.go +++ b/deal.go @@ -66,5 +66,5 @@ type Deal struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/device.go b/device.go index c314a3c..55241c3 100644 --- a/device.go +++ b/device.go @@ -242,5 +242,5 @@ type Device struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/ext.go b/ext.go deleted file mode 100644 index 44a399a..0000000 --- a/ext.go +++ /dev/null @@ -1,4 +0,0 @@ -package openrtb - -// Placeholder for exchange-specific extensions to OpenRTB. -type Ext interface{} diff --git a/geo.go b/geo.go index c599095..280195a 100644 --- a/geo.go +++ b/geo.go @@ -100,5 +100,5 @@ type Geo struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/imp.go b/imp.go index 8995585..d9f2fd0 100644 --- a/imp.go +++ b/imp.go @@ -128,5 +128,5 @@ type Imp struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/native.go b/native.go index ddb122f..382802a 100644 --- a/native.go +++ b/native.go @@ -57,5 +57,5 @@ type Native struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/pmp.go b/pmp.go index 7d4b2f2..77f9a52 100644 --- a/pmp.go +++ b/pmp.go @@ -30,5 +30,5 @@ type PMP struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/producer.go b/producer.go index 92a2cf1..6ca8a94 100644 --- a/producer.go +++ b/producer.go @@ -48,5 +48,5 @@ type Producer struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/publisher.go b/publisher.go index 32cf850..c69ff32 100644 --- a/publisher.go +++ b/publisher.go @@ -45,5 +45,5 @@ type Publisher struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/raw_json.go b/raw_json.go new file mode 100644 index 0000000..09bd555 --- /dev/null +++ b/raw_json.go @@ -0,0 +1,25 @@ +package openrtb + +import "errors" + +// RawJSON is a raw encoded JSON value. +// It implements encoding/json.Marshaler and encoding/json.Unmarshaler and can +// be used to delay JSON decoding or precompute a JSON encoding. +// +// Basically, it's just a copy of encoding/json.RawMessage type, +// but with more convenient non-pointer encoding. +type RawJSON []byte + +// MarshalJSON returns m as the JSON encoding of m. +func (m RawJSON) MarshalJSON() ([]byte, error) { + return m, nil +} + +// UnmarshalJSON sets *m to a copy of data. +func (m *RawJSON) UnmarshalJSON(data []byte) error { + if m == nil { + return errors.New("openrtb.RawJSON: UnmarshalJSON on nil pointer") + } + *m = append((*m)[0:0], data...) + return nil +} diff --git a/raw_json_test.go b/raw_json_test.go new file mode 100644 index 0000000..b06b24e --- /dev/null +++ b/raw_json_test.go @@ -0,0 +1,57 @@ +package openrtb_test + +import ( + "encoding/json" + + "github.com/mxmCherry/openrtb" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("RawJSON", func() { + + var _ json.Marshaler = (openrtb.RawJSON)(nil) + var _ json.Unmarshaler = (*openrtb.RawJSON)(nil) + + It("should encode JSON", func() { + subject := openrtb.RawJSON(`true`) + + actual, err := subject.MarshalJSON() + Expect(err).NotTo(HaveOccurred()) + Expect(actual).To(Equal([]byte(`true`))) + }) + + It("should decode JSON", func() { + subject := openrtb.RawJSON(nil) + + err := subject.UnmarshalJSON([]byte(`true`)) + Expect(err).NotTo(HaveOccurred()) + Expect(subject).To(Equal(openrtb.RawJSON(`true`))) + }) + + It("should decode JSON when embedded into struct", func() { + wrapper := struct { + Raw openrtb.RawJSON `json:"raw"` + }{ + Raw: nil, + } + + err := json.Unmarshal([]byte(`{"raw":true}`), &wrapper) + Expect(err).NotTo(HaveOccurred()) + Expect(wrapper.Raw).To(Equal(openrtb.RawJSON(`true`))) + }) + + It("should encode JSON when embedded into struct", func() { + wrapper := struct { + Raw openrtb.RawJSON `json:"raw"` + }{ + Raw: openrtb.RawJSON(`true`), + } + + actual, err := json.Marshal(wrapper) + Expect(err).NotTo(HaveOccurred()) + Expect(actual).To(MatchJSON(`{"raw":true}`)) + }) + +}) diff --git a/regs.go b/regs.go index dd934fd..7840436 100644 --- a/regs.go +++ b/regs.go @@ -22,5 +22,5 @@ type Regs struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/seat_bid.go b/seat_bid.go index 87025de..d1d44cb 100644 --- a/seat_bid.go +++ b/seat_bid.go @@ -40,5 +40,5 @@ type SeatBid struct { // object // Description: // Placeholder for bidder-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/segment.go b/segment.go index d7bed66..6ab18f6 100644 --- a/segment.go +++ b/segment.go @@ -37,5 +37,5 @@ type Segment struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/site.go b/site.go index 0dd13e1..13387c2 100644 --- a/site.go +++ b/site.go @@ -127,5 +127,5 @@ type Site struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/user.go b/user.go index a8119c7..f267513 100644 --- a/user.go +++ b/user.go @@ -100,5 +100,5 @@ type User struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` } diff --git a/video.go b/video.go index b6533b5..04082ec 100644 --- a/video.go +++ b/video.go @@ -212,5 +212,5 @@ type Video struct { // object // Description: // Placeholder for exchange-specific extensions to OpenRTB. - Ext Ext `json:"ext,omitempty"` + Ext RawJSON `json:"ext,omitempty"` }