Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
Use RawJSON type for Ext instead of interface{}
Browse files Browse the repository at this point in the history
As proposed in: #8
  • Loading branch information
mxmCherry committed Jan 19, 2017
1 parent 0e64a5f commit 5b614ff
Show file tree
Hide file tree
Showing 25 changed files with 106 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion bid_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion bid_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
4 changes: 0 additions & 4 deletions ext.go

This file was deleted.

2 changes: 1 addition & 1 deletion geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion pmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
25 changes: 25 additions & 0 deletions raw_json.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions raw_json_test.go
Original file line number Diff line number Diff line change
@@ -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}`))
})

})
2 changes: 1 addition & 1 deletion regs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion seat_bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 5b614ff

Please sign in to comment.