forked from mc2soft/mpd
-
Notifications
You must be signed in to change notification settings - Fork 9
/
conditional_unit.go
50 lines (41 loc) · 1.06 KB
/
conditional_unit.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
package mpd
import (
"encoding/xml"
"fmt"
"strconv"
)
// ConditionalUint (ConditionalUintType) defined in XSD as a union of unsignedInt and boolean.
type ConditionalUint struct {
u *uint64
b *bool
}
// MarshalXMLAttr encodes ConditionalUint.
func (c ConditionalUint) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if c.u != nil {
return xml.Attr{Name: name, Value: strconv.FormatUint(*c.u, 10)}, nil
}
if c.b != nil {
return xml.Attr{Name: name, Value: strconv.FormatBool(*c.b)}, nil
}
// both are nil - no attribute, client will threat it like "false"
return xml.Attr{}, nil
}
// UnmarshalXMLAttr decodes ConditionalUint.
func (c *ConditionalUint) UnmarshalXMLAttr(attr xml.Attr) error {
u, err := strconv.ParseUint(attr.Value, 10, 64)
if err == nil {
c.u = &u
return nil
}
b, err := strconv.ParseBool(attr.Value)
if err == nil {
c.b = &b
return nil
}
return fmt.Errorf("ConditionalUint: can't UnmarshalXMLAttr %#v", attr)
}
// check interfaces
var (
_ xml.MarshalerAttr = ConditionalUint{}
_ xml.UnmarshalerAttr = &ConditionalUint{}
)