-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.go
53 lines (42 loc) · 1.07 KB
/
callbacks.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
package callbacks
import (
"context"
"encoding/json"
"io"
"github.com/andrewwormald/workflow"
"github.com/andrewwormald/workflow/examples"
)
type Example struct {
EmailConfirmed bool
}
type EmailConfirmationResponse struct {
Confirmed bool
}
type Deps struct {
EventStreamer workflow.EventStreamer
RecordStore workflow.RecordStore
TimeoutStore workflow.TimeoutStore
RoleScheduler workflow.RoleScheduler
}
func ExampleWorkflow(d Deps) *workflow.Workflow[Example, examples.Status] {
b := workflow.NewBuilder[Example, examples.Status]("callback example")
b.AddCallback(examples.StatusStarted, func(ctx context.Context, r *workflow.Record[Example, examples.Status], reader io.Reader) (bool, error) {
b, err := io.ReadAll(reader)
if err != nil {
return false, err
}
var e EmailConfirmationResponse
err = json.Unmarshal(b, &e)
if err != nil {
return false, err
}
r.Object.EmailConfirmed = e.Confirmed
return true, nil
}, examples.StatusFollowedTheExample)
return b.Build(
d.EventStreamer,
d.RecordStore,
d.TimeoutStore,
d.RoleScheduler,
)
}