-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.go
47 lines (38 loc) · 848 Bytes
/
host.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
package gdq
import (
"encoding/json"
)
type host struct {
Handle string `json:"handle"`
Runs []uint `json:"runs"`
}
type hostResp struct {
Fields struct {
StartRun uint `json:"start_run"`
EndRun uint `json:"end_run"`
Handle string `json:"name"`
} `json:"fields"`
}
func (h *host) UnmarshalJSON(b []byte) error {
var v hostResp
if err := json.Unmarshal(b, &v); err != nil {
return err
}
h.Handle = v.Fields.Handle
if v.Fields.StartRun > v.Fields.EndRun {
start := v.Fields.StartRun
end := v.Fields.EndRun
v.Fields.StartRun = end
v.Fields.EndRun = start
}
if v.Fields.StartRun == v.Fields.EndRun {
h.Runs = []uint{v.Fields.StartRun}
} else {
seq := make([]uint, v.Fields.EndRun-v.Fields.StartRun+1)
for i := range seq {
seq[i] = v.Fields.StartRun + uint(i)
}
h.Runs = seq
}
return nil
}