-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_enum.go
140 lines (128 loc) · 3.02 KB
/
state_enum.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Code generated by "go-enumerator"; DO NOT EDIT.
package app
import (
"encoding"
"fmt"
)
// String implements [fmt.Stringer]. If !s.Defined(), then a generated string is returned based on s's value.
func (s state) String() string {
switch s {
case invalid:
return "invalid"
case running:
return "running"
case stopping:
return "stopping"
case terminated:
return "terminated"
}
return fmt.Sprintf("state(%d)", s)
}
// Bytes returns a byte-level representation of String(). If !s.Defined(), then a generated string is returned based on s's value.
func (s state) Bytes() []byte {
switch s {
case invalid:
return []byte{'i', 'n', 'v', 'a', 'l', 'i', 'd'}
case running:
return []byte{'r', 'u', 'n', 'n', 'i', 'n', 'g'}
case stopping:
return []byte{'s', 't', 'o', 'p', 'p', 'i', 'n', 'g'}
case terminated:
return []byte{'t', 'e', 'r', 'm', 'i', 'n', 'a', 't', 'e', 'd'}
}
return []byte(fmt.Sprintf("state(%d)", s))
}
// Defined returns true if s holds a defined value.
func (s state) Defined() bool {
switch s {
case 0, 1, 2, 3:
return true
default:
return false
}
}
// Scan implements [fmt.Scanner]. Use [fmt.Scan] to parse strings into state values
func (s *state) Scan(scanState fmt.ScanState, verb rune) error {
token, err := scanState.Token(true, nil)
if err != nil {
return err
}
switch string(token) {
case "invalid":
*s = invalid
case "running":
*s = running
case "stopping":
*s = stopping
case "terminated":
*s = terminated
default:
return fmt.Errorf("unknown state value: %s", token)
}
return nil
}
// Next returns the next defined state. If s is not defined, then Next returns the first defined value.
// Next() can be used to loop through all values of an enum.
//
// s := state(0)
// for {
// fmt.Println(s)
// s = s.Next()
// if s == state(0) {
// break
// }
// }
//
// The exact order that values are returned when looping should not be relied upon.
func (s state) Next() state {
switch s {
case invalid:
return running
case running:
return stopping
case stopping:
return terminated
case terminated:
return invalid
default:
return invalid
}
}
func _() {
var x [1]struct{}
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the go-enumerator command to generate them again.
_ = x[invalid-0]
_ = x[running-1]
_ = x[stopping-2]
_ = x[terminated-3]
}
// MarshalText implements [encoding.TextMarshaler]
func (s state) MarshalText() ([]byte, error) {
return s.Bytes(), nil
}
// UnmarshalText implements [encoding.TextUnmarshaler]
func (s *state) UnmarshalText(x []byte) error {
switch string(x) {
case "invalid":
*s = invalid
return nil
case "running":
*s = running
return nil
case "stopping":
*s = stopping
return nil
case "terminated":
*s = terminated
return nil
default:
return fmt.Errorf("failed to parse value %v into %T", x, *s)
}
}
var (
_ fmt.Stringer = state(0)
_ fmt.Scanner = new(state)
_ encoding.TextMarshaler = state(0)
_ encoding.TextUnmarshaler = new(state)
)