/
state.go
63 lines (53 loc) · 1.19 KB
/
state.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
package issuedb
import (
"encoding/json"
"errors"
"fmt"
)
type State int
const (
IssueInProgress State = iota
ActiveCertificate
MaxState = ActiveCertificate
)
func (s State) String() string {
switch s {
case IssueInProgress:
return "issue_in_progress"
case ActiveCertificate:
return "active"
}
return fmt.Sprintf("unknown_state_%d", s)
}
func (s State) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *State) UnmarshalJSON(bs []byte) error {
var str string
if err := json.Unmarshal(bs, &str); err != nil {
return fmt.Errorf("Unmarshal issuedb.State string: %w", err)
}
switch str {
case "issue_in_progress":
*s = IssueInProgress
return nil
case "active":
*s = ActiveCertificate
return nil
default:
return fmt.Errorf("Unknown State %q", str)
}
}
func validateStateTransfer(current, next State) error {
switch current {
case IssueInProgress:
if next == ActiveCertificate {
return nil
}
return fmt.Errorf("Invalid state transfer issue_in_progress -> %v.", next)
case ActiveCertificate:
return errors.New("No valid state transfer from ActiveCertificate state.")
default:
return fmt.Errorf("Unknown current state: %v", current)
}
}