Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support oRTB 2.6 qty field in impressions object #111

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions impression.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ type Impression struct {
BidFloor float64 `json:"bidfloor,omitempty"` // Bid floor for this impression in CPM
BidFloorCurrency string `json:"bidfloorcur,omitempty"` // Currency of bid floor
Secure NumberOrString `json:"secure,omitempty"` // Flag to indicate whether the impression requires secure HTTPS URL creative assets and markup.
Exp int `json:"exp,omitempty"` // Advisory as to the number of seconds that may elapse between the auction and the actual impression.
IFrameBusters []string `json:"iframebuster,omitempty"` // Array of names for supportediframe busters.
Quantity *Quantity `json:"qty,omitempty"` // Includes the impression multiplier, and describes its source.
Exp int `json:"exp,omitempty"` // Advisory as to the number of seconds that may elapse between the auction and the actual impression.
IFrameBusters []string `json:"iframebuster,omitempty"` // Array of names for supportediframe busters.
Ext json.RawMessage `json:"ext,omitempty"`
}

Expand Down Expand Up @@ -66,6 +67,11 @@ func (imp *Impression) Validate() error {
return err
}
}
if imp.Quantity != nil {
if err := imp.Quantity.Validate(); err != nil {
return err
}
}

return nil
}
3 changes: 3 additions & 0 deletions impression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func TestImpression(t *testing.T) {
},
},
},
Quantity: &Quantity{
Multiplier: 1.0,
},
}
if got := subject; !reflect.DeepEqual(exp, got) {
t.Errorf("expected %+v, got %+v", exp, got)
Expand Down
37 changes: 37 additions & 0 deletions quantity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package openrtb

import (
"encoding/json"
"errors"
)

var (
ErrMissingMultiplier = errors.New("openrtb: qty.multiplier is required")
ErrMissingMeasurementVendor = errors.New("openrtb: qty.vendor is required when qty.sourcetype is 1 (Measurement Vendor)")
)

type MeasurementSourceType int

const (
MeasurementSourceTypeUnknown MeasurementSourceType = 0
MeasurementSourceTypeMeasurementVendor MeasurementSourceType = 1
MeasurementSourceTypePublisher MeasurementSourceType = 2
MeasurementSourceTypeExchange MeasurementSourceType = 3
)

type Quantity struct {
Multiplier float64 `json:"multiplier"`
SourceType MeasurementSourceType `json:"sourcetype,omitempty"`
Vendor string `json:"vendor,omitempty"`
Ext *json.RawMessage `json:"ext,omitempty"`
}

func (qty *Quantity) Validate() error {
if qty.Multiplier == 0 {
return ErrMissingMultiplier
}
if qty.SourceType == MeasurementSourceTypeMeasurementVendor && qty.Vendor == "" {
return ErrMissingMeasurementVendor
}
return nil
}
55 changes: 55 additions & 0 deletions quantity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package openrtb_test

import (
"reflect"
"testing"

. "github.com/bsm/openrtb/v3"
)

func TestQuantity(t *testing.T) {
var subject *Quantity
if err := fixture("quantity", &subject); err != nil {
t.Fatalf("expected no error, got %+v", err)
}

exp := &Quantity{
Multiplier: 3.14,
SourceType: MeasurementSourceTypePublisher,
}

if !reflect.DeepEqual(exp, subject) {
t.Fatalf("expected %+v, got %+v", exp, subject)
}
}

func TestQuantity_Validate(t *testing.T) {
subject := Quantity{}
if got := subject.Validate(); got != ErrMissingMultiplier {
t.Fatalf("expected %+v, got %+v", ErrMissingMultiplier, got)
}

// If MeasurementSourceType is MeasurementSourceTypeMeasurementVendor, the
// Vendor field should not be empty.
subject = Quantity{
Multiplier: 1.0,
SourceType: MeasurementSourceTypeMeasurementVendor,
}
if got := subject.Validate(); got != ErrMissingMeasurementVendor {
t.Fatalf("expected %+v, got %+v", ErrMissingMeasurementVendor, got)
}

subject.Vendor = "TestVendor" // Should fix the invalid Quantity
if got := subject.Validate(); got != nil {
t.Fatalf("expected no error, got %+v", got)
}

// All other value from MeasurementSourceType can be used without Vendor
subject = Quantity{
Multiplier: 2.0,
SourceType: MeasurementSourceTypeUnknown,
}
if got := subject.Validate(); got != nil {
t.Fatalf("expected nil, got %+v", got)
}
}
3 changes: 3 additions & 0 deletions testdata/impression.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"at": 2
}
]
},
"qty": {
"multiplier": 1.0
}
}
4 changes: 4 additions & 0 deletions testdata/quantity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"multiplier": 3.14,
"sourcetype": 2
}