-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
180 lines (159 loc) · 4.59 KB
/
Copy pathstate.go
File metadata and controls
180 lines (159 loc) · 4.59 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package state
import (
"context"
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var ErrAmbigousMatch = fmt.Errorf("ambigous match conditions")
type MatchCondition struct {
Type string
Status metav1.ConditionStatus
}
type State[T, R any] struct {
Name string
F func(context.Context, T) (string, R, error)
Match []MatchCondition
}
type Builder[T, R any] struct {
states []State[T, R]
transitions map[[2]string]func(*[]metav1.Condition) error
}
type Machine[T, R any] struct {
states []State[T, R]
transitions map[[2]string]func(*[]metav1.Condition) error
}
func (b *Builder[T, R]) AddState(name string, f func(context.Context, T) (string, R, error), match ...MatchCondition) *Builder[T, R] {
b.states = append(b.states, State[T, R]{
Name: name,
F: f,
Match: match,
})
return b
}
func (b *Builder[T, R]) AddTransition(from, to string, f func(*[]metav1.Condition) error) *Builder[T, R] {
if b.transitions == nil {
b.transitions = make(map[[2]string]func(*[]metav1.Condition) error)
}
b.transitions[[2]string{from, to}] = f
return b
}
func (b *Builder[T, R]) Build() (*Machine[T, R], error) {
ambigousStates := make([][2]string, 0)
for i := range b.states {
for j := i + 1; j < len(b.states); j++ {
if ambiguousMatch(b.states[i].Match, b.states[j].Match) {
ambigousStates = append(ambigousStates, [2]string{b.states[i].Name, b.states[j].Name})
}
}
}
if len(ambigousStates) > 0 {
return nil, fmt.Errorf("%w: %v", ErrAmbigousMatch, ambigousStates)
}
return &Machine[T, R]{
states: b.states,
transitions: b.transitions,
}, nil
}
// Graphviz returns a Graphviz representation of the state machine. This is useful for debugging and visualization purposes.
func (b *Builder[T, R]) Graphviz() (string, error) {
s := strings.Builder{}
s.WriteString("digraph G {\n")
for _, state := range b.states {
lbl := strings.Builder{}
lbl.WriteString(state.Name)
if len(state.Match) > 0 {
lbl.WriteString("\\n")
}
for _, m := range state.Match {
lbl.WriteString(fmt.Sprintf("\\n%s=%s", m.Type, m.Status))
}
s.WriteString(fmt.Sprintf(" %s [label=\"%s\"];\n", state.Name, lbl.String()))
}
for transition := range b.transitions {
s.WriteString(fmt.Sprintf(" %s -> %s;\n", transition[0], transition[1]))
}
s.WriteString("}\n")
return s.String(), nil
}
func (m *Machine[T, R]) Run(ctx context.Context, cond *[]metav1.Condition, input T) (R, error) {
state, ok := findStateByMatch(m.states, cond)
if !ok {
var zero R
return zero, fmt.Errorf("no matching state found")
}
newState, r, err := state.F(ctx, input)
if newState != "" {
trans, ok := m.transitions[[2]string{state.Name, newState}]
if !ok {
var zero R
return zero, fmt.Errorf("invalid transition from '%s' to '%s'", state.Name, newState)
}
newStateObj, ok := findStateByName(m.states, newState)
if !ok {
var zero R
return zero, fmt.Errorf("no matching state found for new state '%s'", newState)
}
if err := trans(cond); err != nil {
var zero R
return zero, fmt.Errorf("transition from '%s' to '%s' failed: %w", state.Name, newState, err)
}
if !matches(newStateObj.Match, cond) {
var zero R
return zero, fmt.Errorf("after transition from '%s' to '%s', conditions do not match the new state", state.Name, newState)
}
}
return r, err
}
func findStateByName[T, R any](states []State[T, R], name string) (State[T, R], bool) {
for _, state := range states {
if state.Name == name {
return state, true
}
}
var zero State[T, R]
return zero, false
}
func findStateByMatch[T, R any](states []State[T, R], cond *[]metav1.Condition) (State[T, R], bool) {
for _, state := range states {
if matches(state.Match, cond) {
return state, true
}
}
var zero State[T, R]
return zero, false
}
func matches(match []MatchCondition, cond *[]metav1.Condition) bool {
for _, m := range match {
found := false
for _, c := range *cond {
if c.Type == m.Type && c.Status == m.Status {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func ambiguousMatch(a, b []MatchCondition) bool {
am := make(map[string]metav1.ConditionStatus, len(a))
bm := make(map[string]metav1.ConditionStatus, len(b))
for _, cond := range a {
am[cond.Type] = cond.Status
}
for _, cond := range b {
bm[cond.Type] = cond.Status
}
// Check if there's any contradiction (same Type with different Status)
for t, status := range am {
if bStatus, ok := bm[t]; ok && bStatus != status {
// Contradiction found - conditions can't both be true
return false
}
}
// No contradiction - conditions could both be true simultaneously
return true
}