-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
179 lines (142 loc) · 3.5 KB
/
encoding.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package eureka
import (
"encoding/xml"
"fmt"
"io"
"sort"
"time"
)
var dataCenterTypes = []string{
"MyOwn",
"Amazon",
}
func (dct DataCenterType) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if int(dct) >= len(dataCenterTypes) {
return fmt.Errorf("Unknown datacenter type code: %d", dct)
}
return e.EncodeElement(dataCenterTypes[dct], start)
}
func (dct *DataCenterType) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
for i, n := range dataCenterTypes {
if n == str {
*dct = DataCenterType(i)
return nil
}
}
return fmt.Errorf("Unknown datacenter type: %s", str)
}
func (m Metadata) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start); err != nil {
return err
}
var keys []string
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
if err := e.EncodeElement(m[key], xml.StartElement{Name: xml.Name{Local: key}}); err != nil {
return err
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func (m *Metadata) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
aux := make(map[string]string)
for {
token, err := d.Token()
if err == io.EOF {
break
}
if start, ok := token.(xml.StartElement); ok {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
aux[start.Name.Local] = str
}
}
*m = aux
return nil
}
type portXML struct {
Value Port `xml:",chardata"`
Enabled bool `xml:"enabled,attr"`
}
func (p Port) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(portXML{p, p != 0}, start)
}
func (p *Port) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var aux portXML
if err := d.DecodeElement(&aux, &start); err != nil {
return err
}
*p = aux.Value
return nil
}
func (d Duration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(int64(time.Duration(d).Seconds()), start)
}
func (d *Duration) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
var seconds int64
if err := decoder.DecodeElement(&seconds, &start); err != nil {
return err
}
*d = Duration(time.Duration(seconds) * time.Second)
return nil
}
func (t Time) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
epoch := int64(time.Time(t).UnixNano() / int64(time.Millisecond))
return e.EncodeElement(epoch, start)
}
func (t *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var epoch int64
if err := d.DecodeElement(&epoch, &start); err != nil {
return err
}
*t = Time(time.Unix(0, epoch*int64(time.Millisecond)))
return nil
}
var statusNames = []string{
"UP",
"DOWN",
"STARTING",
"OUT_OF_SERVICE",
"UNKNOWN",
}
func ParseStatus(name string) (status Status, err error) {
for i, n := range statusNames {
if n == name {
status = Status(i)
return
}
}
err = fmt.Errorf("Unknown status '%s'", name)
return
}
func (s Status) String() string {
if int(s) >= len(statusNames) {
s = StatusUnknown
}
return statusNames[s]
}
func (s Status) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(s.String(), start)
}
func (s *Status) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
if str == "" {
*s = StatusUnknown
return nil
}
var err error
*s, err = ParseStatus(str)
return err
}