-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep.go
More file actions
360 lines (289 loc) · 9.09 KB
/
step.go
File metadata and controls
360 lines (289 loc) · 9.09 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package gorkflow
import (
"encoding/json"
"fmt"
"reflect"
"github.com/go-playground/validator/v10"
)
// StepHandler is the user-defined function signature for step logic
type StepHandler[TIn, TOut any] func(ctx *StepContext, input TIn) (TOut, error)
// Step is a generic, type-safe step definition
type Step[TIn, TOut any] struct {
// Identity
ID string
Name string
Description string
// The actual step logic (user-defined function)
Handler StepHandler[TIn, TOut]
// Execution configuration
Config ExecutionConfig
// Validation configuration (internal)
validationConfig *validationConfig
// Type information (for runtime reflection/validation)
inputType reflect.Type
outputType reflect.Type
}
// StepExecutor is the interface the engine works with (polymorphic)
type StepExecutor interface {
// Metadata
GetID() string
GetName() string
GetDescription() string
GetConfig() ExecutionConfig
SetConfig(config ExecutionConfig)
// Type information
InputType() reflect.Type
OutputType() reflect.Type
// Execution (works with serializable data)
Execute(ctx *StepContext, input []byte) (output []byte, err error)
// Validation
ValidateInput(data []byte) error
ValidateOutput(data []byte) error
}
// NewStep creates a new type-safe step with validation enabled by default
func NewStep[TIn, TOut any](
id, name string,
handler StepHandler[TIn, TOut],
opts ...StepOption,
) *Step[TIn, TOut] {
s := &Step[TIn, TOut]{
ID: id,
Name: name,
Handler: handler,
Config: DefaultExecutionConfig,
validationConfig: defaultValidationConfig, // Validation enabled by default
inputType: reflect.TypeOf((*TIn)(nil)).Elem(),
outputType: reflect.TypeOf((*TOut)(nil)).Elem(),
}
// Apply options (can override validation config)
for _, opt := range opts {
opt.applyStep(s)
}
return s
}
// Implement StepExecutor interface
func (s *Step[TIn, TOut]) GetID() string {
return s.ID
}
func (s *Step[TIn, TOut]) GetName() string {
return s.Name
}
func (s *Step[TIn, TOut]) GetDescription() string {
return s.Description
}
func (s *Step[TIn, TOut]) GetConfig() ExecutionConfig {
return s.Config
}
func (s *Step[TIn, TOut]) SetConfig(config ExecutionConfig) {
s.Config = config
}
func (s *Step[TIn, TOut]) InputType() reflect.Type {
return s.inputType
}
func (s *Step[TIn, TOut]) OutputType() reflect.Type {
return s.outputType
}
// Execute runs the step handler with type-safe marshaling and validation
func (s *Step[TIn, TOut]) Execute(ctx *StepContext, inputBytes []byte) ([]byte, error) {
// Unmarshal and validate input
input, err := validateInputData[TIn](inputBytes, s.validationConfig)
if err != nil {
return nil, err
}
// Execute user's handler
output, err := s.Handler(ctx, input)
if err != nil {
return nil, err
}
// Validate and marshal output
outputBytes, err := validateOutputData(output, s.validationConfig)
if err != nil {
return nil, err
}
return outputBytes, nil
}
// ValidateInput validates that data can be unmarshaled to TIn and passes validation
func (s *Step[TIn, TOut]) ValidateInput(data []byte) error {
_, err := validateInputData[TIn](data, s.validationConfig)
if err != nil {
return fmt.Errorf("invalid input for step %s: %w", s.ID, err)
}
return nil
}
// ValidateOutput validates that data can be unmarshaled to TOut and passes validation
func (s *Step[TIn, TOut]) ValidateOutput(data []byte) error {
var output TOut
if err := json.Unmarshal(data, &output); err != nil {
return fmt.Errorf("invalid output for step %s: %w", s.ID, err)
}
// Validate struct if validation is enabled
if s.validationConfig != nil && s.validationConfig.validateOutput {
if err := s.validationConfig.validateStruct(output); err != nil {
return fmt.Errorf("output validation failed for step %s: %w", s.ID, err)
}
}
return nil
}
// Configuration setters (for functional options)
func (s *Step[TIn, TOut]) SetMaxRetries(max int) {
s.Config.MaxRetries = max
}
func (s *Step[TIn, TOut]) SetTimeout(seconds int) {
s.Config.TimeoutSeconds = seconds
}
func (s *Step[TIn, TOut]) SetBackoff(strategy BackoffStrategy) {
s.Config.RetryBackoff = strategy
}
func (s *Step[TIn, TOut]) SetRetryDelay(ms int) {
s.Config.RetryDelayMs = ms
}
func (s *Step[TIn, TOut]) SetContinueOnError(continueOnError bool) {
s.Config.ContinueOnError = continueOnError
}
func (s *Step[TIn, TOut]) SetCustomValidator(v *validator.Validate) {
if s.validationConfig == nil {
s.validationConfig = &validationConfig{
validateInput: true,
validateOutput: true,
}
}
s.validationConfig.validator = v
}
func (s *Step[TIn, TOut]) DisableValidation() {
s.validationConfig = nil
}
// Condition is a function that determines if a step should execute
type Condition func(ctx *StepContext) (bool, error)
// ConditionalStep wraps a step with a condition
type ConditionalStep[TIn, TOut any] struct {
Step *Step[TIn, TOut]
Condition Condition
Default *TOut
}
// Implement StepExecutor interface for ConditionalStep
func (cs *ConditionalStep[TIn, TOut]) GetID() string {
return cs.Step.GetID()
}
func (cs *ConditionalStep[TIn, TOut]) GetName() string {
return cs.Step.GetName()
}
func (cs *ConditionalStep[TIn, TOut]) GetDescription() string {
return cs.Step.GetDescription()
}
func (cs *ConditionalStep[TIn, TOut]) GetConfig() ExecutionConfig {
return cs.Step.GetConfig()
}
func (cs *ConditionalStep[TIn, TOut]) SetConfig(config ExecutionConfig) {
cs.Step.SetConfig(config)
}
func (cs *ConditionalStep[TIn, TOut]) InputType() reflect.Type {
return cs.Step.InputType()
}
func (cs *ConditionalStep[TIn, TOut]) OutputType() reflect.Type {
return cs.Step.OutputType()
}
func (cs *ConditionalStep[TIn, TOut]) Execute(ctx *StepContext, inputBytes []byte) ([]byte, error) {
// Evaluate condition
shouldRun, err := cs.Condition(ctx)
if err != nil {
return nil, fmt.Errorf("condition evaluation failed: %w", err)
}
if !shouldRun {
LogStepSkipped(ctx.Logger, ctx.RunID, ctx.StepID, "condition_not_met")
// Step skipped - return default or zero value
if cs.Default != nil {
return json.Marshal(cs.Default)
}
var zero TOut
return json.Marshal(zero)
}
// Execute the wrapped step
return cs.Step.Execute(ctx, inputBytes)
}
func (cs *ConditionalStep[TIn, TOut]) ValidateInput(data []byte) error {
return cs.Step.ValidateInput(data)
}
func (cs *ConditionalStep[TIn, TOut]) ValidateOutput(data []byte) error {
return cs.Step.ValidateOutput(data)
}
// NewConditionalStep creates a conditional step wrapper
func NewConditionalStep[TIn, TOut any](
step *Step[TIn, TOut],
condition Condition,
defaultValue *TOut,
) *ConditionalStep[TIn, TOut] {
return &ConditionalStep[TIn, TOut]{
Step: step,
Condition: condition,
Default: defaultValue,
}
}
// conditionalStepWrapper wraps any StepExecutor with conditional execution logic
// This is used by the builder API to provide builder-level conditional support
type conditionalStepWrapper struct {
step StepExecutor
condition Condition
defaultValue any
}
func (w *conditionalStepWrapper) GetID() string {
return w.step.GetID()
}
func (w *conditionalStepWrapper) GetName() string {
return w.step.GetName()
}
func (w *conditionalStepWrapper) GetDescription() string {
return w.step.GetDescription()
}
func (w *conditionalStepWrapper) GetConfig() ExecutionConfig {
return w.step.GetConfig()
}
func (w *conditionalStepWrapper) SetConfig(config ExecutionConfig) {
w.step.SetConfig(config)
}
func (w *conditionalStepWrapper) InputType() reflect.Type {
return w.step.InputType()
}
func (w *conditionalStepWrapper) OutputType() reflect.Type {
return w.step.OutputType()
}
func (w *conditionalStepWrapper) Execute(ctx *StepContext, inputBytes []byte) ([]byte, error) {
// Evaluate condition
shouldRun, err := w.condition(ctx)
if err != nil {
return nil, fmt.Errorf("condition evaluation failed: %w", err)
}
if !shouldRun {
LogStepSkipped(ctx.Logger, ctx.RunID, ctx.StepID, "condition_not_met")
// Step skipped - return default or zero value
if w.defaultValue != nil {
return json.Marshal(w.defaultValue)
}
// If input type matches output type, pass through the input
// This allows chaining conditional steps without losing the previous output
if w.step.InputType() == w.step.OutputType() {
return inputBytes, nil
}
// Return zero value for the output type
zeroVal := reflect.Zero(w.step.OutputType()).Interface()
bytes, _ := json.Marshal(zeroVal)
return bytes, ErrStepSkipped
}
// Execute the wrapped step
return w.step.Execute(ctx, inputBytes)
}
func (w *conditionalStepWrapper) ValidateInput(data []byte) error {
return w.step.ValidateInput(data)
}
func (w *conditionalStepWrapper) ValidateOutput(data []byte) error {
return w.step.ValidateOutput(data)
}
// WrapStepWithCondition wraps a StepExecutor with conditional execution logic
// This is the type-erased version used by the builder API
// For type-safe conditional steps, use NewConditionalStep directly
func WrapStepWithCondition(step StepExecutor, condition Condition, defaultValue any) StepExecutor {
return &conditionalStepWrapper{
step: step,
condition: condition,
defaultValue: defaultValue,
}
}