Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
306 lines (244 loc) · 7.86 KB
/
Copy pathcontext.go
File metadata and controls
306 lines (244 loc) · 7.86 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package teleflow
import (
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
"time"
)
var (
ErrDataKeyEmpty = errors.New("flow: data key cannot be empty")
ErrInvalidValue = errors.New("flow: invalid value")
)
// Context exposes session data and transition operations during one step handler call.
//
// Data changes are persisted only after the handler returns a valid transition without an error and the state write succeeds.
// Setters and Delete return ErrDataKeyEmpty when given an empty key.
type Context interface {
// SetString stores a string under the key.
SetString(string, string) error
// SetBool stores a boolean under the key.
SetBool(string, bool) error
// SetInt stores an int value normalized to int64.
SetInt(string, int) error
// SetInt32 stores an int32 value normalized to int64.
SetInt32(string, int32) error
// SetInt64 stores an int64 under the key.
SetInt64(string, int64) error
// SetFloat32 stores a finite float32 value normalized to float64.
SetFloat32(string, float32) error
// SetFloat64 stores a finite float64 under the key.
SetFloat64(string, float64) error
// SetDuration stores a time.Duration as an int64 nanosecond count.
SetDuration(string, time.Duration) error
// SetTime stores a time.Time in RFC3339Nano format.
SetTime(string, time.Time) error
// Delete removes the value stored under the key.
// It returns ErrDataKeyEmpty for an empty key and succeeds when the key is absent.
Delete(string) error
// Has reports whether the key exists in the current flow data.
Has(string) bool
// GetString returns the stored string and reports whether the key exists with that type.
GetString(string) (string, bool)
// GetBool returns the stored boolean and reports whether the key exists with that type.
GetBool(string) (bool, bool)
// GetInt returns the stored integer and reports whether it exists and fits in an int.
GetInt(string) (int, bool)
// GetInt32 returns the stored integer and reports whether it exists and fits in an int32.
GetInt32(string) (int32, bool)
// GetInt64 returns the stored int64 and reports whether the key exists with that type.
GetInt64(string) (int64, bool)
// GetFloat32 returns the stored number and reports whether it exists and converts without overflow or underflow.
GetFloat32(string) (float32, bool)
// GetFloat64 returns the stored float64 and reports whether the key exists with that type.
GetFloat64(string) (float64, bool)
// GetDuration returns the stored duration and reports whether the key exists with that type.
GetDuration(string) (time.Duration, bool)
// GetTime returns the stored time and reports whether the key contains a valid RFC3339Nano value.
GetTime(string) (time.Time, bool)
// Current returns the step whose handler is running.
Current() string
// Depth returns the number of retained history entries at the start of the handler call.
Depth() int
// CanBack reports whether a previous history entry existed at the start of the handler call.
CanBack() bool
// Step returns the active step recorded in the working session state.
Step() string
// Next requests the next defined step.
// On the last step, successful handling completes the flow and removes the session state.
Next() StepResult
// Back requests the previous retained history entry.
// At the first retained entry, the flow stays on the current step.
Back() StepResult
// Go requests the named step.
// Handling returns ErrTargetStepNotFound when the flow has no step with that name.
Go(string) StepResult
// Stay requests no step change.
// Successful handling persists any data changes.
Stay() StepResult
}
// nativeContext implements Context.
type nativeContext struct {
state *runtimeState
current string
depth int
canBack bool
target string
}
func (c *nativeContext) set(k string, value any) error {
if k == "" {
return ErrDataKeyEmpty
}
encoded, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("%w: %w", ErrInvalidValue, err)
}
if c.state.Data == nil {
c.state.Data = make(map[string]json.RawMessage)
}
c.state.Data[k] = encoded
return nil
}
func (c *nativeContext) SetString(k, value string) error {
return c.set(k, value)
}
func (c *nativeContext) SetBool(k string, value bool) error {
return c.set(k, value)
}
func (c *nativeContext) SetInt(k string, value int) error {
return c.SetInt64(k, int64(value))
}
func (c *nativeContext) SetInt32(k string, value int32) error {
return c.SetInt64(k, int64(value))
}
func (c *nativeContext) SetInt64(k string, value int64) error {
return c.set(k, value)
}
func (c *nativeContext) SetFloat32(k string, value float32) error {
return c.SetFloat64(k, float64(value))
}
func (c *nativeContext) SetFloat64(k string, value float64) error {
if math.IsNaN(value) || math.IsInf(value, 0) {
return ErrInvalidValue
}
return c.set(k, value)
}
func (c *nativeContext) SetDuration(k string, value time.Duration) error {
return c.SetInt64(k, int64(value))
}
func (c *nativeContext) SetTime(k string, value time.Time) error {
return c.SetString(k, value.Format(time.RFC3339Nano))
}
func (c *nativeContext) Delete(k string) error {
if k == "" {
return ErrDataKeyEmpty
}
delete(c.state.Data, k)
return nil
}
func (c *nativeContext) Has(k string) bool {
_, ok := c.state.Data[k]
return ok
}
func (c *nativeContext) GetString(k string) (string, bool) {
var value string
ok := c.decode(k, &value)
return value, ok
}
func (c *nativeContext) GetBool(k string) (bool, bool) {
var value bool
ok := c.decode(k, &value)
return value, ok
}
func (c *nativeContext) GetInt(k string) (int, bool) {
value, ok := c.GetInt64(k)
if !ok {
return 0, false
}
if strconv.IntSize == 32 && (value < math.MinInt32 || value > math.MaxInt32) {
return 0, false
}
return int(value), true
}
func (c *nativeContext) GetInt32(k string) (int32, bool) {
value, ok := c.GetInt64(k)
if !ok || value < math.MinInt32 || value > math.MaxInt32 {
return 0, false
}
return int32(value), true
}
func (c *nativeContext) GetInt64(k string) (int64, bool) {
var value int64
ok := c.decode(k, &value)
return value, ok
}
func (c *nativeContext) GetFloat32(k string) (float32, bool) {
value, ok := c.GetFloat64(k)
if !ok || value < -math.MaxFloat32 || value > math.MaxFloat32 {
return 0, false
}
converted := float32(value)
if value != 0 && converted == 0 {
return 0, false
}
return converted, true
}
func (c *nativeContext) GetFloat64(k string) (float64, bool) {
var value float64
ok := c.decode(k, &value)
return value, ok
}
func (c *nativeContext) decode(k string, dst any) bool {
value, ok := c.state.Data[k]
if !ok {
return false
}
return json.Unmarshal(value, dst) == nil
}
func (c *nativeContext) GetDuration(k string) (time.Duration, bool) {
value, ok := c.GetInt64(k)
return time.Duration(value), ok
}
// GetTime returns the RFC3339Nano time stored under k.
func (c *nativeContext) GetTime(k string) (time.Time, bool) {
value, ok := c.GetString(k)
if !ok {
return time.Time{}, false
}
parsed, err := time.Parse(time.RFC3339Nano, value)
return parsed, err == nil
}
// Current returns the step active when this callback context was created.
func (c *nativeContext) Current() string {
return c.current
}
// Depth returns the number of entries in the bounded history.
func (c *nativeContext) Depth() int {
return c.depth
}
// CanBack reports whether the flow has a previous step.
func (c *nativeContext) CanBack() bool {
return c.canBack
}
// Step returns the active step in the runtime state.
func (c *nativeContext) Step() string {
return c.state.CurrentStep
}
// Next requests the next step.
func (c *nativeContext) Next() StepResult {
return stepNext
}
// Back requests the previous step.
func (c *nativeContext) Back() StepResult {
return stepBack
}
// Go requests the named step.
func (c *nativeContext) Go(step string) StepResult {
c.target = step
return stepGo
}
// Stay keeps the current step.
func (c *nativeContext) Stay() StepResult {
return stepStay
}