This repository was archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
87 lines (75 loc) · 1.9 KB
/
status.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cap
import (
"encoding/json"
"encoding/xml"
"errors"
"strings"
)
const (
StatusUnknown = iota
StatusActual
StatusExcercise
StatusSystem
StatusTest
StatusDraft
)
type Status int
// UnmarshalString unmarshals the string into a Status value.
func (status *Status) UnmarshalString(str string) error {
str = strings.ToLower(str)
if str == "actual" {
*status = StatusActual
} else if str == "excercise" {
*status = StatusExcercise
} else if str == "system" {
*status = StatusSystem
} else if str == "test" {
*status = StatusTest
} else if str == "draft" {
*status = StatusDraft
} else {
return errors.New("Unknown Status value: " + str)
}
return nil
}
// UnmarshalXML unmarshals the XML into a Status value.
func (status *Status) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
return status.UnmarshalString(str)
}
// UnmarshalJSON unmarshals the JSON into a Status value.
func (status *Status) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err
}
return status.UnmarshalString(str)
}
// MarshalJSON returns the string version of the status.
func (status *Status) MarshalJSON() ([]byte, error) {
return json.Marshal(status.String())
}
// MarshalXML returns the string version of the status.
func (status *Status) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(status.String(), start)
}
// String returns a Status as a string
func (status Status) String() string {
if status == StatusActual {
return "Actual"
} else if status == StatusExcercise {
return "Excercise"
} else if status == StatusSystem {
return "System"
} else if status == StatusTest {
return "Test"
} else if status == StatusDraft {
return "Draft"
} else if status == StatusUnknown {
return "Unknown"
}
return ""
}