Skip to content

Commit

Permalink
Omit empty Creative>CreativeExtensions and InLine>Extensions (#28)
Browse files Browse the repository at this point in the history
* Use a pointer for CreativeExtensions for correct omitempty behavior.

* Made Inline>Extensions respect omitempty.
  • Loading branch information
jussi-kalliokoski authored and rs committed Apr 17, 2018
1 parent 1ef8013 commit e5c78ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions vast.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type InLine struct {
// custom element should be nested under <Extensions> to help separate custom
// XML elements from VAST elements. The following example includes a custom
// xml element within the Extensions element.
Extensions []Extension `xml:"Extensions>Extension,omitempty"`
Extensions *[]Extension `xml:"Extensions>Extension,omitempty"`
}

// Impression is a URI that directs the video player to a tracking resource file that
Expand Down Expand Up @@ -167,7 +167,7 @@ type Creative struct {
// of VAST.
// The nested <CreativeExtension> includes an attribute for type, which
// specifies the MIME type needed to execute the extension.
CreativeExtensions []Extension `xml:"CreativeExtensions>CreativeExtension,omitempty"`
CreativeExtensions *[]Extension `xml:"CreativeExtensions>CreativeExtension,omitempty"`
}

// CompanionAds contains companions creatives
Expand Down
29 changes: 16 additions & 13 deletions vast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ func TestCreativeExtensions(t *testing.T) {
assert.Equal(t, "abc123", ad.ID)
if assert.NotNil(t, ad.InLine) {
if assert.Len(t, ad.InLine.Creatives, 1) {
if assert.Len(t, ad.InLine.Creatives[0].CreativeExtensions, 4) {
exts := *ad.InLine.Creatives[0].CreativeExtensions
if assert.Len(t, exts, 4) {
var ext Extension
// asserting first extension
ext = ad.InLine.Creatives[0].CreativeExtensions[0]
ext = exts[0]
assert.Equal(t, "geo", ext.Type)
assert.Empty(t, ext.CustomTracking)
assert.Equal(t, "\n <Country>US</Country>\n <Bandwidth>3</Bandwidth>\n <BandwidthKbps>1680</BandwidthKbps>\n ", string(ext.Data))
// asserting second extension
ext = ad.InLine.Creatives[0].CreativeExtensions[1]
ext = exts[1]
assert.Equal(t, "activeview", ext.Type)
if assert.Len(t, ext.CustomTracking, 2) {
// first tracker
Expand All @@ -63,12 +64,12 @@ func TestCreativeExtensions(t *testing.T) {
}
assert.Empty(t, string(ext.Data))
// asserting third extension
ext = ad.InLine.Creatives[0].CreativeExtensions[2]
ext = exts[2]
assert.Equal(t, "DFP", ext.Type)
assert.Empty(t, ext.CustomTracking)
assert.Equal(t, "\n <SkippableAdType>Generic</SkippableAdType>\n ", string(ext.Data))
// asserting fourth extension
ext = ad.InLine.Creatives[0].CreativeExtensions[3]
ext = exts[3]
assert.Equal(t, "metrics", ext.Type)
assert.Empty(t, ext.CustomTracking)
assert.Equal(t, "\n <FeEventId>MubmWKCWLs_tiQPYiYrwBw</FeEventId>\n <AdEventId>CIGpsPCTkdMCFdN-Ygod-xkCKQ</AdEventId>\n ", string(ext.Data))
Expand All @@ -90,15 +91,16 @@ func TestInlineExtensions(t *testing.T) {
assert.Equal(t, "708365173", ad.ID)
if assert.NotNil(t, ad.InLine) {
if assert.NotNil(t, ad.InLine.Extensions) {
if assert.Len(t, ad.InLine.Extensions, 4) {
exts := *ad.InLine.Extensions
if assert.Len(t, exts, 4) {
var ext Extension
// asserting first extension
ext = ad.InLine.Extensions[0]
ext = exts[0]
assert.Equal(t, "geo", ext.Type)
assert.Empty(t, ext.CustomTracking)
assert.Equal(t, "\n <Country>US</Country>\n <Bandwidth>3</Bandwidth>\n <BandwidthKbps>1680</BandwidthKbps>\n ", string(ext.Data))
// asserting second extension
ext = ad.InLine.Extensions[1]
ext = exts[1]
assert.Equal(t, "activeview", ext.Type)
if assert.Len(t, ext.CustomTracking, 2) {
// first tracker
Expand All @@ -110,12 +112,12 @@ func TestInlineExtensions(t *testing.T) {
}
assert.Empty(t, string(ext.Data))
// asserting third extension
ext = ad.InLine.Extensions[2]
ext = exts[2]
assert.Equal(t, "DFP", ext.Type)
assert.Equal(t, "\n <SkippableAdType>Generic</SkippableAdType>\n ", string(ext.Data))
assert.Empty(t, ext.CustomTracking)
// asserting fourth extension
ext = ad.InLine.Extensions[3]
ext = exts[3]
assert.Equal(t, "metrics", ext.Type)
assert.Equal(t, "\n <FeEventId>MubmWKCWLs_tiQPYiYrwBw</FeEventId>\n <AdEventId>CIGpsPCTkdMCFdN-Ygod-xkCKQ</AdEventId>\n ", string(ext.Data))
assert.Empty(t, ext.CustomTracking)
Expand Down Expand Up @@ -519,11 +521,12 @@ func TestSpotXVpaid(t *testing.T) {
}

}
if assert.Len(t, inline.Extensions, 2) {
ext1 := inline.Extensions[0]
exts := *inline.Extensions
if assert.Len(t, exts, 2) {
ext1 := exts[0]
assert.Equal(t, "LR-Pricing", ext1.Type)
assert.Equal(t, "<Price model=\"CPM\" currency=\"USD\" source=\"spotxchange\"><![CDATA[3.06]]></Price>", strings.TrimSpace(string(ext1.Data)))
ext2 := inline.Extensions[1]
ext2 := exts[1]
assert.Equal(t, "SpotX-Count", ext2.Type)
assert.Equal(t, "<total_available><![CDATA[1]]></total_available>", strings.TrimSpace(string(ext2.Data)))
}
Expand Down

0 comments on commit e5c78ea

Please sign in to comment.