Skip to content

Commit f9fae6e

Browse files
committed
fix extension marshaling and tests
1 parent 0d1b208 commit f9fae6e

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

extension.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package openrtb
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
)
7+
8+
// Extension is a raw encoded JSON value.
9+
// It implements Marshaler and Unmarshaler, defined in encoding/json package
10+
type Extension json.RawMessage
11+
12+
// MarshalJSON returns e as the JSON encoding of e.
13+
func (e Extension) MarshalJSON() ([]byte, error) {
14+
return json.RawMessage(e).MarshalJSON()
15+
}
16+
17+
// UnmarshalJSON sets *e to a copy of data.
18+
func (e *Extension) UnmarshalJSON(data []byte) error {
19+
if e == nil {
20+
return errors.New("openrtb.Extension: UnmarshalJSON on nil pointer")
21+
}
22+
return (*json.RawMessage)(e).UnmarshalJSON(data)
23+
}

extension_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package openrtb_test
2+
3+
import (
4+
"encoding/json"
5+
"reflect"
6+
"testing"
7+
8+
. "github.com/UnityTech/openrtb"
9+
)
10+
11+
func TestExtension(t *testing.T) {
12+
var _ json.Marshaler = (Extension)(nil)
13+
var _ json.Unmarshaler = (*Extension)(nil)
14+
15+
t.Run("should marshal JSON", func(t *testing.T) {
16+
subject := Extension(`{"foo":"bar"}`)
17+
18+
got, err := json.Marshal(subject)
19+
if err != nil {
20+
t.Fatal(err.Error())
21+
}
22+
exp := []byte(`{"foo":"bar"}`)
23+
if !reflect.DeepEqual(exp, got) {
24+
t.Errorf("expected %+v, got %+v", exp, got)
25+
}
26+
})
27+
28+
t.Run("should unmarshal JSON", func(t *testing.T) {
29+
subject := Extension([]byte{})
30+
err := json.Unmarshal([]byte(`{"foo":"bar"}`), &subject)
31+
if err != nil {
32+
t.Fatal(err.Error())
33+
}
34+
exp := Extension(`{"foo":"bar"}`)
35+
if got := subject; !reflect.DeepEqual(exp, got) {
36+
t.Errorf("expected %+v, got %+v", exp, got)
37+
}
38+
})
39+
}

openrtb.go

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package openrtb
22

3-
import "encoding/json"
4-
53
// ContentCategory as defined in section 5.1
64
type ContentCategory string
75

@@ -841,8 +839,6 @@ type ChannelEntity struct {
841839
Ext Extension `json:"ext,omitempty"`
842840
}
843841

844-
type Extension json.RawMessage
845-
846842
// RegExtension Extension object for Regulations
847843
type RegExtension struct {
848844
GDPR int `json:"gdpr,omitempty"`

testdata/content.quoted.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"len": 129,
1010
"series": "book reading",
1111
"videoquality": 2,
12-
"context": "1",
12+
"context": 1,
1313
"season": "1",
1414
"sourcerelationship": 0,
1515
"id": "eb9f13ede5fd225333971523f6042f9d"

testdata/device.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"js": 1,
2323
"connectiontype": 3,
2424
"mccmnc": "722-341",
25-
"devicetype": 1
25+
"devicetype": 1,
26+
"oaid": "1fe9a970-efbb-29e0-0bdd-f5dbbf751ab5"
2627
}

testdata/impression.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
},
3131
"qty": {
3232
"multiplier": 1.0
33-
}
33+
},
34+
"rwdd": 1
3435
}

0 commit comments

Comments
 (0)