forked from tellytv/telly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
59 lines (51 loc) · 1.6 KB
/
types.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
54
55
56
57
58
59
package models
import (
"encoding/json"
"encoding/xml"
"fmt"
)
// ConvertibleBoolean is a helper type to allow JSON documents using 0/1 or "true" and "false" be converted to bool.
type ConvertibleBoolean bool
// MarshalJSON returns a 0 or 1 depending on bool state.
func (bit ConvertibleBoolean) MarshalJSON() ([]byte, error) {
var bitSetVar int8
if bit {
bitSetVar = 1
}
return json.Marshal(bitSetVar)
}
// UnmarshalJSON converts a 0, 1, true or false into a bool
func (bit *ConvertibleBoolean) UnmarshalJSON(data []byte) error {
asString := string(data)
if asString == "1" || asString == "true" {
*bit = true
} else if asString == "0" || asString == "false" {
*bit = false
} else {
return fmt.Errorf("Boolean unmarshal error: invalid input %s", asString)
}
return nil
}
// MarshalXML used to determine if the element is present or not. see https://stackoverflow.com/a/46516243
func (bit *ConvertibleBoolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
var bitSetVar int8
if *bit {
bitSetVar = 1
}
return e.EncodeElement(bitSetVar, start)
}
// UnmarshalXML used to determine if the element is present or not. see https://stackoverflow.com/a/46516243
func (bit *ConvertibleBoolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var asString string
if decodeErr := d.DecodeElement(&asString, &start); decodeErr != nil {
return decodeErr
}
if asString == "1" || asString == "true" {
*bit = true
} else if asString == "0" || asString == "false" {
*bit = false
} else {
return fmt.Errorf("Boolean unmarshal error: invalid input %s", asString)
}
return nil
}