-
Notifications
You must be signed in to change notification settings - Fork 667
/
status.go
101 lines (92 loc) · 1.9 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
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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"errors"
)
var (
errUnknownStatus = errors.New("unknown status")
)
// Status ...
type Status uint32
// List of possible status values
// [Unknown] Zero value, means the status is not known
// [Preferred] means the operation is known and preferred, but hasn't been decided yet
// [Created] means the operation occurred, but isn't managed locally
// [Validating] means the operation was accepted and is managed locally
const (
Unknown Status = iota
Preferred
Created
Validating
Committed
Aborted
Processing
Dropped
)
// MarshalJSON ...
func (s Status) MarshalJSON() ([]byte, error) {
if err := s.Valid(); err != nil {
return nil, err
}
return []byte("\"" + s.String() + "\""), nil
}
// UnmarshalJSON ...
func (s *Status) UnmarshalJSON(b []byte) error {
str := string(b)
if str == "null" {
return nil
}
switch str {
case "\"Unknown\"":
*s = Unknown
case "\"Preferred\"":
*s = Preferred
case "\"Created\"":
*s = Created
case "\"Validating\"":
*s = Validating
case "\"Committed\"":
*s = Committed
case "\"Aborted\"":
*s = Aborted
case "\"Processing\"":
*s = Processing
case "\"Dropped\"":
*s = Dropped
default:
return errUnknownStatus
}
return nil
}
// Valid returns nil if the status is a valid status.
func (s Status) Valid() error {
switch s {
case Unknown, Preferred, Created, Validating, Committed, Aborted, Processing, Dropped:
return nil
default:
return errUnknownStatus
}
}
func (s Status) String() string {
switch s {
case Unknown:
return "Unknown"
case Preferred:
return "Preferred"
case Created:
return "Created"
case Validating:
return "Validating"
case Committed:
return "Committed"
case Aborted:
return "Aborted"
case Processing:
return "Processing"
case Dropped:
return "Dropped"
default:
return "Invalid status"
}
}