-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcallback.go
More file actions
75 lines (61 loc) · 1.43 KB
/
callback.go
File metadata and controls
75 lines (61 loc) · 1.43 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
package callback
import (
"context"
"encoding/json"
"io"
"github.com/luno/workflow"
)
type Status int
var (
StatusUnknown Status = 0
StatusStarted Status = 1
StatusReadTheDocs Status = 2
StatusFollowedTheExample Status = 3
StatusCreatedAFunExample Status = 4
)
func (s Status) String() string {
switch s {
case StatusStarted:
return "Started"
case StatusReadTheDocs:
return "Read the docs"
case StatusFollowedTheExample:
return "Followed the example"
case StatusCreatedAFunExample:
return "Created a fun example"
default:
return "Unknown"
}
}
type Example struct {
EmailConfirmed bool
}
type EmailConfirmationResponse struct {
Confirmed bool
}
type Deps struct {
EventStreamer workflow.EventStreamer
RecordStore workflow.RecordStore
RoleScheduler workflow.RoleScheduler
}
func ExampleWorkflow(d Deps) *workflow.Workflow[Example, Status] {
b := workflow.NewBuilder[Example, Status]("callback example")
b.AddCallback(StatusStarted, func(ctx context.Context, r *workflow.Run[Example, Status], reader io.Reader) (Status, error) {
b, err := io.ReadAll(reader)
if err != nil {
return 0, err
}
var e EmailConfirmationResponse
err = json.Unmarshal(b, &e)
if err != nil {
return 0, err
}
r.Object.EmailConfirmed = e.Confirmed
return StatusFollowedTheExample, nil
}, StatusFollowedTheExample)
return b.Build(
d.EventStreamer,
d.RecordStore,
d.RoleScheduler,
)
}