Skip to content

Commit 114d0cd

Browse files
Adds block example
1 parent f2e2eef commit 114d0cd

File tree

4 files changed

+23
-109
lines changed

4 files changed

+23
-109
lines changed

cmd/samples/blocks/blocks

32.4 MB
Binary file not shown.

cmd/samples/blocks/blocks_workflow.go

Lines changed: 18 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"time"
55

66
"go.uber.org/cadence/workflow"
7+
"go.uber.org/cadence/x/blocks"
78
"go.uber.org/zap"
89
)
910

@@ -16,39 +17,6 @@ const ApplicationName = "blocksGroup"
1617

1718
const blocksWorkflowName = "blocksWorkflow"
1819

19-
// Query response structures
20-
type LunchQueryResponse struct {
21-
CadenceResponseType string `json:"cadenceResponseType"`
22-
Format string `json:"format"`
23-
Blocks []Block `json:"blocks"`
24-
}
25-
26-
type Block struct {
27-
Type string `json:"type"`
28-
Format string `json:"format,omitempty"`
29-
ComponentOptions *ComponentOptions `json:"componentOptions,omitempty"`
30-
Elements []Element `json:"elements,omitempty"`
31-
}
32-
33-
type Element struct {
34-
Type string `json:"type"`
35-
ComponentOptions *ComponentOptions `json:"componentOptions,omitempty"`
36-
Action *Action `json:"action,omitempty"`
37-
}
38-
39-
type ComponentOptions struct {
40-
Type string `json:"type,omitempty"`
41-
Text string `json:"text,omitempty"`
42-
}
43-
44-
type Action struct {
45-
Type string `json:"type"`
46-
SignalName string `json:"signal_name,omitempty"`
47-
SignalValue interface{} `json:"signal_value,omitempty"`
48-
WorkflowID string `json:"workflow_id,omitempty"`
49-
RunID string `json:"run_id,omitempty"`
50-
}
51-
5220
// This is an example of using the 'blocks' query response in a cadence query, in this example,
5321
// to select the lunch option.
5422
func blocksWorkflow(ctx workflow.Context, name string) error {
@@ -63,81 +31,11 @@ func blocksWorkflow(ctx workflow.Context, name string) error {
6331

6432
votes := []map[string]string{}
6533

66-
workflow.SetQueryHandler(ctx, "options", func() (LunchQueryResponse, error) {
34+
workflow.SetQueryHandler(ctx, "options", func() (blocks.QueryResponse, error) {
6735
logger := workflow.GetLogger(ctx)
6836
logger.Info("Responding to 'options' query")
6937

70-
return LunchQueryResponse{
71-
CadenceResponseType: "formattedData",
72-
Format: "blocks",
73-
Blocks: []Block{
74-
{
75-
Type: "section",
76-
Format: "text/markdown",
77-
ComponentOptions: &ComponentOptions{
78-
Text: "## Lunch options\nWe're voting on where to order lunch today. Select the option you want to vote for.",
79-
},
80-
},
81-
{
82-
Type: "divider",
83-
},
84-
{
85-
Type: "section",
86-
Format: "text/markdown",
87-
ComponentOptions: &ComponentOptions{
88-
Text: makeVoteTable(votes),
89-
},
90-
},
91-
{
92-
Type: "section",
93-
Format: "text/markdown",
94-
ComponentOptions: &ComponentOptions{
95-
Text: makeMenu(),
96-
},
97-
},
98-
{
99-
Type: "actions",
100-
Elements: []Element{
101-
{
102-
Type: "button",
103-
ComponentOptions: &ComponentOptions{
104-
Type: "plain_text",
105-
Text: "Farmhouse",
106-
},
107-
Action: &Action{
108-
Type: "signal",
109-
SignalName: "lunch_order",
110-
SignalValue: map[string]string{"location": "farmhouse - red thai curry", "requests": "spicy"},
111-
},
112-
},
113-
{
114-
Type: "button",
115-
ComponentOptions: &ComponentOptions{
116-
Type: "plain_text",
117-
Text: "Ethiopian",
118-
},
119-
Action: &Action{
120-
Type: "signal",
121-
SignalName: "no_lunch_order_walk_in_person",
122-
WorkflowID: "in-person-order-workflow",
123-
},
124-
},
125-
{
126-
Type: "button",
127-
ComponentOptions: &ComponentOptions{
128-
Type: "plain_text",
129-
Text: "Ler Ros",
130-
},
131-
Action: &Action{
132-
Type: "signal",
133-
SignalName: "lunch_order",
134-
SignalValue: map[string]string{"location": "Ler Ros", "meal": "tofo Bahn Mi"},
135-
},
136-
},
137-
},
138-
},
139-
},
140-
}, nil
38+
return makeResponse(votes), nil
14139
})
14240

14341
votesChan := workflow.GetSignalChannel(ctx, "lunch_order")
@@ -162,6 +60,21 @@ func blocksWorkflow(ctx workflow.Context, name string) error {
16260
return nil
16361
}
16462

63+
// makeResponse creates the lunch query response payload based on the current votes
64+
func makeResponse(votes []map[string]string) blocks.QueryResponse {
65+
return blocks.New(
66+
blocks.NewMarkdownSection("## Lunch options\nWe're voting on where to order lunch today. Select the option you want to vote for."),
67+
blocks.NewDivider(),
68+
blocks.NewMarkdownSection(makeVoteTable(votes)),
69+
blocks.NewMarkdownSection(makeMenu()),
70+
blocks.NewSignalActions(
71+
blocks.NewSignalButton("Farmhouse", "lunch_order", map[string]string{"location": "farmhouse - red thai curry", "requests": "spicy"}),
72+
blocks.NewSignalButtonWithExternalWorkflow("Ethiopian", "no_lunch_order_walk_in_person", nil, "in-person-order-workflow", ""),
73+
blocks.NewSignalButton("Ler Ros", "lunch_order", map[string]string{"location": "Ler Ros", "meal": "tofo Bahn Mi"}),
74+
),
75+
)
76+
}
77+
16578
func makeMenu() string {
16679

16780
options := []struct {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ require (
1414
github.com/uber-go/tally v3.4.3+incompatible
1515
github.com/uber/cadence-idl v0.0.0-20250616185004-cc6f52f87bc6
1616
github.com/uber/jaeger-client-go v2.30.0+incompatible
17-
go.uber.org/cadence v1.3.1-rc.8
17+
go.uber.org/cadence v1.3.1-rc.11
1818
go.uber.org/yarpc v1.60.0
1919
go.uber.org/zap v1.23.0
2020
gopkg.in/yaml.v2 v2.4.0
21+
gopkg.in/yaml.v3 v3.0.1
2122
)
2223

2324
require (
@@ -70,6 +71,5 @@ require (
7071
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce // indirect
7172
google.golang.org/grpc v1.28.0 // indirect
7273
google.golang.org/protobuf v1.31.0 // indirect
73-
gopkg.in/yaml.v3 v3.0.1 // indirect
7474
honnef.co/go/tools v0.3.2 // indirect
7575
)

go.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
9494
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9595
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
9696
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
97+
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
9798
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
9899
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
99100
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -239,8 +240,8 @@ go.uber.org/atomic v1.5.1/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
239240
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
240241
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
241242
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
242-
go.uber.org/cadence v1.3.1-rc.8 h1:9m1h7KZsPqJsNW87iVd3Mgi85PojCPyaUeIdzTjVu3Y=
243-
go.uber.org/cadence v1.3.1-rc.8/go.mod h1:/Lv4o2eahjro+LKjXmuYm20wg5+84t+h2pu0xm7gUfM=
243+
go.uber.org/cadence v1.3.1-rc.11 h1:+AVmN+w1zV01BGjjK2S/PGsddo1Q5UkJt2B9ore/FWA=
244+
go.uber.org/cadence v1.3.1-rc.11/go.mod h1:Yf2WaRFj6TtqrUbGRWxLU/8Vou3WPn0M2VIdfEIlEjE=
244245
go.uber.org/dig v1.8.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
245246
go.uber.org/dig v1.10.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
246247
go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=

0 commit comments

Comments
 (0)