-
Notifications
You must be signed in to change notification settings - Fork 21
/
status.go
64 lines (56 loc) · 1.57 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
package v2
import (
"bytes"
"encoding/json"
)
// Status represents an bundle status
type Status int
// Possible statuses
const (
Unknown Status = iota // No information about this bundle
Started // Diagnostics is preparing
InProgress // Diagnostics in progress
Done // Diagnostics finished and the file is ready to be downloaded
Canceled // Diagnostics has been canceled
Deleted // Diagnostics was finished but was deleted
Failed // Diagnostics could not be downloaded
)
func (s Status) String() string {
return toString[s]
}
var toString = map[Status]string{
Unknown: "Unknown",
Started: "Started",
InProgress: "InProgress",
Done: "Done",
Canceled: "Canceled",
Deleted: "Deleted",
Failed: "Failed",
}
var toID = map[string]Status{
"Unknown": Unknown,
"Started": Started,
"InProgress": InProgress,
"Done": Done,
"Canceled": Canceled,
"Deleted": Deleted,
"Failed": Failed,
}
// MarshalJSON marshals the enum as a quoted json string
func (s Status) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(toString[s])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
// UnmarshalJSON unmashals a quoted json string to the enum value
func (s *Status) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
// Note that if the string cannot be found then it will be set to the zero value, 'Created' in this case.
*s = toID[j]
return nil
}