-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support oRTB 2.6 qty field in impressions object (#111)
- Loading branch information
1 parent
5563255
commit 5547ba6
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,8 @@ | |
"at": 2 | ||
} | ||
] | ||
}, | ||
"qty": { | ||
"multiplier": 1.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"multiplier": 3.14, | ||
"sourcetype": 2 | ||
} |