Skip to content

Commit a0e60c8

Browse files
committed
refactor(query): drop blocks package, use Cadence Web markdown format
- Remove go.uber.org/cadence/x/blocks from all three query samples - Return simple formattedData JSON (cadenceResponseType, format: text/markdown, data) - Inline response struct in each workflow for self-contained samples - Ignore new_samples/query/query executable build artifact in .gitignore Signed-off-by: “Kevin” <kevlar_ksb@yahoo.com>
1 parent b2d75c0 commit a0e60c8

4 files changed

Lines changed: 44 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ test.log
1414
vendor/
1515
# Executables produced by cadence-samples repo
1616
bin/
17+
# Binary from go build in new_samples/query
18+
new_samples/query/query
1719
docker-compose.yml
1820

1921
# Credentials

new_samples/query/lunch_vote_workflow.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import (
66
"time"
77

88
"go.uber.org/cadence/workflow"
9-
"go.uber.org/cadence/x/blocks"
109
"go.uber.org/zap"
1110
)
1211

12+
// lunchVoteFormattedResponse is the JSON shape Cadence Web expects for markdown query results (formattedData, text/markdown, data).
13+
type lunchVoteFormattedResponse struct {
14+
CadenceResponseType string `json:"cadenceResponseType"`
15+
Format string `json:"format"`
16+
Data string `json:"data"`
17+
}
18+
1319
// LunchVoteWorkflow demonstrates using MarkDoc query responses for interactive voting.
1420
// Users can vote for lunch options via signal buttons rendered in the query response.
1521
func LunchVoteWorkflow(ctx workflow.Context) error {
@@ -18,7 +24,7 @@ func LunchVoteWorkflow(ctx workflow.Context) error {
1824

1925
votes := []map[string]string{}
2026

21-
workflow.SetQueryHandler(ctx, "options", func() (blocks.QueryResponse, error) {
27+
workflow.SetQueryHandler(ctx, "options", func() (lunchVoteFormattedResponse, error) {
2228
logger := workflow.GetLogger(ctx)
2329
logger.Info("Responding to 'options' query")
2430

@@ -47,7 +53,7 @@ func LunchVoteWorkflow(ctx workflow.Context) error {
4753
}
4854

4955
// makeLunchVoteResponse creates the MarkDoc query response for lunch voting
50-
func makeLunchVoteResponse(ctx workflow.Context, votes []map[string]string) blocks.QueryResponse {
56+
func makeLunchVoteResponse(ctx workflow.Context, votes []map[string]string) lunchVoteFormattedResponse {
5157
type P map[string]interface{}
5258

5359
markdownTemplate, err := template.New("").Parse(`
@@ -116,7 +122,11 @@ We're voting on where to order lunch today. Select the option you want to vote f
116122
panic("Failed to execute template: " + err.Error())
117123
}
118124

119-
return blocks.New(blocks.NewMarkdownSection(markdown.String()))
125+
return lunchVoteFormattedResponse{
126+
CadenceResponseType: "formattedData",
127+
Format: "text/markdown",
128+
Data: markdown.String(),
129+
}
120130
}
121131

122132
// makeLunchVoteTable generates a markdown table of current votes

new_samples/query/markdown_query.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ import (
99

1010
"go.uber.org/cadence/activity"
1111
"go.uber.org/cadence/workflow"
12-
"go.uber.org/cadence/x/blocks"
1312
"go.uber.org/zap"
1413
)
1514

1615
const (
1716
CompleteSignalChan = "complete"
1817
)
1918

19+
// markdownFormattedResponse is the JSON shape Cadence Web expects for markdown query results (formattedData, text/markdown, data).
20+
type markdownFormattedResponse struct {
21+
CadenceResponseType string `json:"cadenceResponseType"`
22+
Format string `json:"format"`
23+
Data string `json:"data"`
24+
}
25+
2026
func MarkdownQueryWorkflow(ctx workflow.Context) error {
2127
ao := workflow.ActivityOptions{
2228
ScheduleToStartTimeout: time.Minute * 60,
@@ -26,7 +32,7 @@ func MarkdownQueryWorkflow(ctx workflow.Context) error {
2632
logger := workflow.GetLogger(ctx)
2733
logger.Info("MarkdownQueryWorkflow started")
2834

29-
workflow.SetQueryHandler(ctx, "Signal", func() (blocks.QueryResponse, error) {
35+
workflow.SetQueryHandler(ctx, "Signal", func() (markdownFormattedResponse, error) {
3036
logger := workflow.GetLogger(ctx)
3137
logger.Info("Responding to 'Signal' query")
3238

@@ -57,7 +63,7 @@ func MarkdownQueryWorkflow(ctx workflow.Context) error {
5763
}
5864
}
5965

60-
func makeMarkdownQueryResponse(ctx workflow.Context) blocks.QueryResponse {
66+
func makeMarkdownQueryResponse(ctx workflow.Context) markdownFormattedResponse {
6167
type P map[string]interface{}
6268

6369
markdownTemplate, err := template.New("").Parse(`
@@ -114,7 +120,11 @@ func makeMarkdownQueryResponse(ctx workflow.Context) blocks.QueryResponse {
114120
panic("Failed to execute template: " + err.Error())
115121
}
116122

117-
return blocks.New(blocks.NewMarkdownSection(markdown.String()))
123+
return markdownFormattedResponse{
124+
CadenceResponseType: "formattedData",
125+
Format: "text/markdown",
126+
Data: markdown.String(),
127+
}
118128
}
119129

120130
func MarkdownQueryActivity(ctx context.Context, complete bool) (string, error) {

new_samples/query/order_fulfillment_workflow.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import (
77
"time"
88

99
"go.uber.org/cadence/workflow"
10-
"go.uber.org/cadence/x/blocks"
1110
"go.uber.org/zap"
1211
)
1312

13+
// orderDashboardFormattedResponse is the JSON shape Cadence Web expects for markdown query results (formattedData, text/markdown, data).
14+
type orderDashboardFormattedResponse struct {
15+
CadenceResponseType string `json:"cadenceResponseType"`
16+
Format string `json:"format"`
17+
Data string `json:"data"`
18+
}
19+
1420
// Order represents an e-commerce order being fulfilled
1521
type Order struct {
1622
OrderID string
@@ -114,7 +120,7 @@ func OrderFulfillmentWorkflow(ctx workflow.Context) error {
114120
}
115121

116122
// Register query handler for the ops dashboard
117-
workflow.SetQueryHandler(ctx, "dashboard", func() (blocks.QueryResponse, error) {
123+
workflow.SetQueryHandler(ctx, "dashboard", func() (orderDashboardFormattedResponse, error) {
118124
logger.Info("Responding to 'dashboard' query")
119125
return makeOrderDashboard(ctx, order, actionLog), nil
120126
})
@@ -259,7 +265,7 @@ func getOperator(operator string) string {
259265
return operator
260266
}
261267

262-
func makeOrderDashboard(ctx workflow.Context, order Order, actionLog []ActionLogEntry) blocks.QueryResponse {
268+
func makeOrderDashboard(ctx workflow.Context, order Order, actionLog []ActionLogEntry) orderDashboardFormattedResponse {
263269
type P map[string]interface{}
264270

265271
markdownTemplate, err := template.New("").Parse(`
@@ -330,7 +336,11 @@ func makeOrderDashboard(ctx workflow.Context, order Order, actionLog []ActionLog
330336
panic("Failed to execute template: " + err.Error())
331337
}
332338

333-
return blocks.New(blocks.NewMarkdownSection(markdown.String()))
339+
return orderDashboardFormattedResponse{
340+
CadenceResponseType: "formattedData",
341+
Format: "text/markdown",
342+
Data: markdown.String(),
343+
}
334344
}
335345

336346
func getStatusBadge(status string) string {

0 commit comments

Comments
 (0)