-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.go
More file actions
79 lines (63 loc) · 1.78 KB
/
validation.go
File metadata and controls
79 lines (63 loc) · 1.78 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
package workflow
import (
"context"
"fmt"
)
// StepValidator provides validation for workflow steps
type StepValidator[T any] struct{}
// ValidateStep validates a step for common issues
func (v StepValidator[T]) ValidateStep(step Step[T]) error {
if step == nil {
return fmt.Errorf("step cannot be nil")
}
// Test string representation
if step.String() == "" {
return fmt.Errorf("step must provide a non-empty string representation")
}
return nil
}
// ValidatePipeline validates an entire pipeline
func (v StepValidator[T]) ValidatePipeline(pipeline *Pipeline[T]) error {
if pipeline == nil {
return fmt.Errorf("pipeline cannot be nil")
}
for i, step := range pipeline.Steps {
if err := v.ValidateStep(step); err != nil {
return fmt.Errorf("step %d validation failed: %w", i, err)
}
}
return nil
}
// SafeRun provides a safe way to run steps with validation
func SafeRun[T any](ctx context.Context, step Step[T], data *T) (*T, error) {
if step == nil {
return nil, fmt.Errorf("cannot run nil step")
}
if data == nil {
return nil, fmt.Errorf("cannot run step with nil data")
}
// Check context
if ctx == nil {
ctx = context.Background()
}
return step.Run(ctx, data)
}
// DeepCopyInterface defines an interface for types that can deep copy themselves
type DeepCopyInterface[T any] interface {
DeepCopy() *T
}
// SafeCopy provides safe copying for parallel execution.
// It will create a deep copy of the original data if the type implements [DeepCopyInterface].
func SafeCopy[T any](original *T) *T {
if original == nil {
return nil
}
// Check if the type implements DeepCopy
if copyable, ok := any(original).(DeepCopyInterface[T]); ok {
return copyable.DeepCopy()
}
// Fall back to shallow copy (existing behavior)
cp := new(T)
*cp = *original
return cp
}