-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
47 lines (39 loc) · 1016 Bytes
/
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
package types
import (
"encoding/json"
"fmt"
"strconv"
)
// StatusText is an enum used to represent the status of the prophecy
type StatusText int
const (
PendingStatusText StatusText = iota
SuccessStatusText
FailedStatusText
)
var StatusTextToString = [...]string{"pending", "success", "failed"}
var StringToStatusText = map[string]StatusText{
"pending": PendingStatusText,
"success": SuccessStatusText,
"failed": FailedStatusText,
}
func (text StatusText) String() string {
return StatusTextToString[text]
}
func (text StatusText) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", text.String())), nil
}
func (text *StatusText) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
stringKey, err := strconv.Unquote(string(b))
if err != nil {
return err
}
// Note that if the string cannot be found then it will be set to the zero value, 'pending' in this case.
*text = StringToStatusText[stringKey]
return nil
}