-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsaga.go
85 lines (72 loc) · 2.07 KB
/
saga.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
package saga
import (
"context"
"errors"
"fmt"
"reflect"
)
func NewSaga(name string) *Saga {
return &Saga{
Name: name,
}
}
type StepOptions struct {
}
type Step struct {
Name string
Func interface{}
CompensateFunc interface{}
Options *StepOptions
}
type Result struct {
ExecutionError error
CompensateErrors []error
}
type Saga struct {
Name string
steps []*Step
}
func (saga *Saga) AddStep(step *Step) error {
if err := checkStep(step); err != nil {
return err
}
saga.steps = append(saga.steps, step)
return nil
}
func checkStep(step *Step) error {
funcType := reflect.TypeOf(step.Func)
if funcType.Kind() != reflect.Func {
return fmt.Errorf("func field is not a func, but %s", funcType.Kind())
}
compensateType := reflect.TypeOf(step.CompensateFunc)
if compensateType.Kind() != reflect.Func {
return fmt.Errorf("func field is not a func, but %s", compensateType.Kind())
}
if funcType.NumIn() != 1 || funcType.In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() {
return errors.New("func must have strictly one parameter context.Context")
}
if funcType.NumOut() == 0 {
return errors.New("func must have at least one out value of type error")
}
if !funcType.Out(funcType.NumOut() - 1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
return errors.New("last out parameter of func must be of type error")
}
if compensateType.NumIn() == 0 {
return errors.New("compensate must have at least one parameter context.Context")
}
if compensateType.In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() {
return errors.New("first parameter of a compensate must be of type context.Context")
}
if compensateType.NumOut() != 1 {
return errors.New("compensate must must return single value of type error")
}
if compensateType.NumIn() != funcType.NumOut() {
return errors.New("compensate in params not matched to func return values")
}
for i := 0; i < compensateType.NumIn()-1; i++ {
if compensateType.In(i+1) != funcType.Out(i) {
return fmt.Errorf("param %d not matched in func and compensate", i)
}
}
return nil
}