-
Notifications
You must be signed in to change notification settings - Fork 27
/
extension.go
53 lines (46 loc) · 1.67 KB
/
extension.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package vast
import "encoding/xml"
// Extension represent arbitrary XML provided by the platform to extend the
// VAST response or by custom trackers.
type Extension struct {
Type string `xml:"type,attr,omitempty"`
CustomTracking []Tracking `xml:"CustomTracking>Tracking,omitempty" json:",omitempty"`
Data string `xml:",innerxml" json:",omitempty"`
}
// the extension type as a middleware in the encoding process.
type extension Extension
type extensionNoCT struct {
Type string `xml:"type,attr,omitempty"`
Data string `xml:",innerxml" json:",omitempty"`
}
// MarshalXML implements xml.Marshaler interface.
func (e Extension) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
// create a temporary element from a wrapper Extension, copy what we need to
// it and return it's encoding.
var e2 interface{}
// if we have custom trackers, we should ignore the data, if not, then we
// should consider only the data.
if len(e.CustomTracking) > 0 {
e2 = extension{Type: e.Type, CustomTracking: e.CustomTracking}
} else {
e2 = extensionNoCT{Type: e.Type, Data: e.Data}
}
return enc.EncodeElement(e2, start)
}
// UnmarshalXML implements xml.Unmarshaler interface.
func (e *Extension) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
// decode the extension into a temporary element from a wrapper Extension,
// copy what we need over.
var e2 extension
if err := dec.DecodeElement(&e2, &start); err != nil {
return err
}
// copy the type and the customTracking
e.Type = e2.Type
e.CustomTracking = e2.CustomTracking
// copy the data only of customTracking is empty
if len(e.CustomTracking) == 0 {
e.Data = e2.Data
}
return nil
}