This repository has been 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 pathresponse_type.go
114 lines (100 loc) · 2.93 KB
/
response_type.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cap
import (
"encoding/json"
"encoding/xml"
"errors"
"strings"
)
const (
ResponseTypeUnknown = iota
ResponseTypeShelter
ResponseTypeEvacuate
ResponseTypePrepare
ResponseTypeExecute
ResponseTypeAvoid
ResponseTypeMonitor
ResponseTypeAssess
ResponseTypeAllClear
ResponseTypeNone
)
type ResponseType int
// UnmarshalString unmarshals the string into a ResponseType value.
func (responseType *ResponseType) UnmarshalString(str string) error {
str = strings.ToLower(str)
if str == "shelter" {
*responseType = ResponseTypeShelter
} else if str == "evacuate" {
*responseType = ResponseTypeEvacuate
} else if str == "prepare" {
*responseType = ResponseTypePrepare
} else if str == "execute" {
*responseType = ResponseTypeExecute
} else if str == "avoid" {
*responseType = ResponseTypeAvoid
} else if str == "monitor" {
*responseType = ResponseTypeMonitor
} else if str == "assess" {
*responseType = ResponseTypeAssess
} else if str == "allclear" || str == "all clear" {
*responseType = ResponseTypeAllClear
} else if str == "none" {
*responseType = ResponseTypeNone
} else {
return errors.New("Unknown ResponseType value: " + str)
}
return nil
}
// UnmarshalXML unmarshals the XML into a ResponseType value.
func (responseType *ResponseType) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
return responseType.UnmarshalString(str)
}
// UnmarshalJSON unmarshals the JSON into a ResponseType value.
func (responseType *ResponseType) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err
}
return responseType.UnmarshalString(str)
}
// MarshalJSON returns the string version of the response type.
func (responseType *ResponseType) MarshalJSON() ([]byte, error) {
return json.Marshal(responseType.String())
}
// MarshalXML returns the string version of the response type.
func (responseType *ResponseType) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
str := responseType.String()
// Some values differ in XML
if str == "All Clear" {
str = "AllClear"
}
return e.EncodeElement(str, start)
}
// String returns a ResponseType as a string
func (responseType ResponseType) String() string {
if responseType == ResponseTypeShelter {
return "Shelter"
} else if responseType == ResponseTypeEvacuate {
return "Evacuate"
} else if responseType == ResponseTypePrepare {
return "Prepare"
} else if responseType == ResponseTypeExecute {
return "Execute"
} else if responseType == ResponseTypeAvoid {
return "Avoid"
} else if responseType == ResponseTypeMonitor {
return "Monitor"
} else if responseType == ResponseTypeAssess {
return "Assess"
} else if responseType == ResponseTypeAllClear {
return "All Clear"
} else if responseType == ResponseTypeNone {
return "None"
} else if responseType == ResponseTypeUnknown {
return "Unknown"
}
return ""
}