Skip to content

Commit 81256c0

Browse files
committed
Split into separate object files. Extract validations & defaults into public methods.
1 parent 143e00c commit 81256c0

17 files changed

+569
-503
lines changed

app.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package openrtb
2+
3+
// An "app" object should be included if the ad supported content is part of a mobile application
4+
// (as opposed to a mobile website). A bid request must not contain both an "app" object and a
5+
// "site" object.
6+
type App struct {
7+
Id *string // App ID on the exchange
8+
Name *string // App name
9+
Domain *string
10+
Cat []string // Array of IAB content categories
11+
Sectioncat []string // Array of IAB content categories for subsection
12+
Pagecat []string // Array of IAB content categories for page
13+
Ver *string // App version
14+
Bundle *string // App bundle or package name
15+
Privacypolicy *int // Default: 1 ("1": site has a privacy policy)
16+
Paid *int // "1": Paid, "2": Free
17+
Publisher *Publisher
18+
// Content Content
19+
Keywords []string
20+
Storeurl *string // App store URL for an installed app
21+
Ext Extensions
22+
}
23+
24+
// Returns the privacy policy status, with default fallback
25+
func (a *App) IsPrivacyPolicy() bool {
26+
if a.Privacypolicy != nil {
27+
return *a.Privacypolicy == 1
28+
}
29+
return false
30+
}
31+
32+
// Returns the paid status, with default fallback
33+
func (a *App) IsPaid() bool {
34+
if a.Paid != nil {
35+
return *a.Paid == 1
36+
}
37+
return false
38+
}
39+
40+
// Applies defaults
41+
func (a *App) WithDefaults() *App {
42+
if a.Privacypolicy == nil {
43+
a.Privacypolicy = new(int)
44+
*a.Privacypolicy = 0
45+
}
46+
if a.Paid == nil {
47+
a.Paid = new(int)
48+
*a.Paid = 0
49+
}
50+
return a
51+
}

app_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package openrtb
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestApp_IsPrivacyPolicy(t *testing.T) {
9+
a := &App{}
10+
assert.Equal(t, a.IsPrivacyPolicy(), false)
11+
}
12+
13+
func TestApp_IsPaid(t *testing.T) {
14+
a := &App{}
15+
assert.Equal(t, a.IsPaid(), false)
16+
}
17+
18+
func TestApp_WithDefaults(t *testing.T) {
19+
assert.Equal(t, "pending", "TODO")
20+
}

banner.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package openrtb
2+
3+
// The "banner" object must be included directly in the impression object if the impression offered
4+
// for auction is display or rich media, or it may be optionally embedded in the video object to
5+
// describe the companion banners available for the linear or non-linear video ad. The banner
6+
// object may include a unique identifier; this can be useful if these IDs can be leveraged in the
7+
// VAST response to dictate placement of the companion creatives when multiple companion ad
8+
// opportunities of the same size are available on a page.
9+
type Banner struct {
10+
W *int // Width
11+
H *int // Height
12+
Id *string // A unique identifier
13+
Pos *int // Ad Position
14+
Btype []int // Blocked creative types
15+
Battr []int // Blocked creative attributes
16+
Mimes []string // Whitelist of content MIME types supported
17+
Topframe *int // Default: 0 ("1": Delivered in top frame, "0": Elsewhere)
18+
Expdir []int // Specify properties for an expandable ad
19+
Api []int // List of supported API frameworks
20+
Ext Extensions
21+
}
22+
23+
// Returns topframe status, with default fallback
24+
func (b *Banner) IsTopFrame() bool {
25+
if b.Topframe != nil {
26+
return *b.Topframe == 1
27+
}
28+
return false
29+
}
30+
31+
// Returns the position, with default fallback
32+
func (b *Banner) Position() int {
33+
if b.Pos != nil {
34+
return *b.Pos
35+
}
36+
return AD_POS_UNKNOWN
37+
}
38+
39+
// Applies defaults
40+
func (b *Banner) WithDefaults() *Banner {
41+
if b.Topframe == nil {
42+
b.Topframe = new(int)
43+
*b.Topframe = 0
44+
}
45+
if b.Pos == nil {
46+
b.Pos = new(int)
47+
*b.Pos = AD_POS_UNKNOWN
48+
}
49+
return b
50+
}

banner_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package openrtb
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestBanner_IsTopFrame(t *testing.T) {
9+
b := &Banner{}
10+
assert.Equal(t, b.IsTopFrame(), false)
11+
}
12+
13+
func TestBanner_Position(t *testing.T) {
14+
b := &Banner{}
15+
assert.Equal(t, b.Position(), 0)
16+
}
17+
18+
func TestBanner_WithDefaults(t *testing.T) {
19+
assert.Equal(t, "pending", "TODO")
20+
}

device.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package openrtb
2+
3+
// The "device" object provides information pertaining to the device including its hardware,
4+
// platform, location, and carrier. This device can refer to a mobile handset, a desktop computer,
5+
// set top box or other digital device.
6+
type Device struct {
7+
Dnt *int // "1": Do not track
8+
Ua *string // User agent
9+
Ip *string // IPv4
10+
Geo *Geo
11+
Didsha1 *string // SHA1 hashed device ID
12+
Didmd5 *string // MD5 hashed device ID
13+
Dpidsha1 *string // SHA1 hashed platform device ID
14+
Dpidmd5 *string // MD5 hashed platform device ID
15+
Ipv6 *string // IPv6
16+
Carrier *string // Carrier or ISP derived from the IP address
17+
Language *string // Browser language
18+
Make *string // Device make
19+
Model *string // Device model
20+
Os *string // Device OS
21+
Osv *string // Device OS version
22+
Js *int // Javascript status ("0": Disabled, "1": Enabled)
23+
Connectiontype *int
24+
Devicetype *int
25+
Flashver *string // Flash version
26+
Ext map[string]string
27+
}
28+
29+
// Returns the DNT status, with default fallback
30+
func (d *Device) IsDnt() bool {
31+
if d.Dnt != nil {
32+
return *d.Dnt == 1
33+
}
34+
return false
35+
}
36+
37+
// Returns the JS status, with default fallback
38+
func (d *Device) IsJs() bool {
39+
if d.Js != nil {
40+
return *d.Js == 1
41+
}
42+
return false
43+
}
44+
45+
// Returns the connection type, with default fallback
46+
func (d *Device) ConnectionType() int {
47+
if d.Connectiontype != nil {
48+
return *d.Connectiontype
49+
}
50+
return CONN_TYPE_UNKNOWN
51+
}
52+
53+
// Returns the connection type, with default fallback
54+
func (d *Device) DeviceType() int {
55+
if d.Devicetype != nil {
56+
return *d.Devicetype
57+
}
58+
return DEVICE_TYPE_UNKNOWN
59+
}
60+
61+
// Applies defaults
62+
func (d *Device) WithDefaults() *Device {
63+
if d.Dnt == nil {
64+
d.Dnt = new(int)
65+
*d.Dnt = 0
66+
}
67+
if d.Js == nil {
68+
d.Js = new(int)
69+
*d.Js = 0
70+
}
71+
if d.Connectiontype == nil {
72+
d.Connectiontype = new(int)
73+
*d.Connectiontype = CONN_TYPE_UNKNOWN
74+
}
75+
if d.Devicetype == nil {
76+
d.Devicetype = new(int)
77+
*d.Devicetype = DEVICE_TYPE_UNKNOWN
78+
}
79+
return d
80+
}

device_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package openrtb
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestDevice_IsDnt(t *testing.T) {
9+
d := &Device{}
10+
assert.Equal(t, d.IsDnt(), false)
11+
}
12+
13+
func TestDevice_IsJs(t *testing.T) {
14+
d := &Device{}
15+
assert.Equal(t, d.IsJs(), false)
16+
}
17+
18+
func TestDevice_ConnectionType(t *testing.T) {
19+
d := &Device{}
20+
assert.Equal(t, d.ConnectionType(), 0)
21+
}
22+
23+
func TestDevice_DeviceType(t *testing.T) {
24+
d := &Device{}
25+
assert.Equal(t, d.DeviceType(), 0)
26+
}
27+
28+
func TestDevice_WithDefaults(t *testing.T) {
29+
assert.Equal(t, "pending", "TODO")
30+
}

impression.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package openrtb
2+
3+
// The "imp" object describes the ad position or impression being auctioned. A single bid request
4+
// can include multiple "imp" objects, a use case for which might be an exchange that supports
5+
// selling all ad positions on a given page as a bundle. Each "imp" object has a required ID so that
6+
// bids can reference them individually. An exchange can also conduct private auctions by
7+
// restricting involvement to specific subsets of seats within bidders.
8+
type Impression struct {
9+
Id *string // A unique identifier for this impression
10+
Banner *Banner
11+
Video *Video
12+
Displaymanager *string // Name of ad mediation partner, SDK technology, etc
13+
Displaymanagerver *string // Version of the above
14+
Instl *int // Interstitial, Default: 0 ("1": Interstitial, "0": Something else)
15+
Tagid *string // Identifier for specific ad placement or ad tag
16+
Bidfloor *float32 // Bid floor for this impression in CPM
17+
Bidfloorcur *string // Currency of bid floor
18+
Iframebuster []string // Array of names for supportediframe busters.
19+
Ext Extensions
20+
}
21+
22+
// Validates the `imp` object
23+
func (imp *Impression) Valid() (bool, error) {
24+
25+
if imp.Id == nil {
26+
return false, errValidationImpId
27+
} else if imp.Banner != nil && imp.Video != nil {
28+
return false, errValidationImpBaV
29+
} else if imp.Video != nil {
30+
if ok, err := imp.Video.Valid(); !ok {
31+
return ok, err
32+
}
33+
} else {
34+
return false, errValidationImpBoV
35+
}
36+
37+
return true, nil
38+
}
39+
40+
// Returns the `imp` object returning defaults
41+
func (imp *Impression) WithDefaults() *Impression {
42+
if imp.Instl == nil {
43+
imp.Instl = new(int)
44+
*imp.Instl = 0
45+
}
46+
47+
if imp.Bidfloor == nil {
48+
imp.Bidfloor = new(float32)
49+
*imp.Bidfloor = 0
50+
}
51+
52+
if imp.Bidfloorcur == nil {
53+
imp.Bidfloorcur = new(string)
54+
*imp.Bidfloorcur = "USD"
55+
}
56+
57+
if imp.Banner != nil {
58+
imp.Banner.WithDefaults()
59+
}
60+
61+
if imp.Video != nil {
62+
imp.Video.WithDefaults()
63+
}
64+
65+
return imp
66+
}

impression_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package openrtb
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestImpression_Valid(t *testing.T) {
9+
assert.Equal(t, "pending", "TODO")
10+
}
11+
12+
func TestImpression_WithDefaults(t *testing.T) {
13+
assert.Equal(t, "pending", "TODO")
14+
}

0 commit comments

Comments
 (0)