|
| 1 | +// Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. |
| 2 | +// |
| 3 | +// WSO2 LLC. licenses this file to you under the Apache License, |
| 4 | +// Version 2.0 (the "License"); you may not use this file except |
| 5 | +// in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, |
| 11 | +// software distributed under the License is distributed on an |
| 12 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | +// KIND, either express or implied. See the License for the |
| 14 | +// specific language governing permissions and limitations |
| 15 | +// under the License. |
| 16 | + |
| 17 | +import ballerina/workflow; |
| 18 | + |
| 19 | +type Input record {| string id; |}; |
| 20 | + |
| 21 | +type ApprovalDecision record {| boolean approved; string approverId; |}; |
| 22 | + |
| 23 | +type ComplianceDecision record {| boolean compliant; string reviewerId; |}; |
| 24 | + |
| 25 | +// Valid: full wait captured WITHOUT `check` as a `T|error` union (no forced check). |
| 26 | +// Enabled by the dependent type parameter `typedesc<anydata|error>`. |
| 27 | +@workflow:Workflow |
| 28 | +function awaitUnionNoCheck( |
| 29 | + workflow:Context ctx, |
| 30 | + Input input, |
| 31 | + record {| |
| 32 | + future<ApprovalDecision> approval; |
| 33 | + future<ComplianceDecision> compliance; |
| 34 | + |} events |
| 35 | +) returns string|error { |
| 36 | + [ApprovalDecision, ComplianceDecision]|error result = ctx->await([events.approval, events.compliance]); |
| 37 | + if result is error { |
| 38 | + return result; |
| 39 | + } |
| 40 | + [ApprovalDecision, ComplianceDecision] [appr, comp] = result; |
| 41 | + return appr.approved && comp.compliant ? "DONE" : "REJECTED"; |
| 42 | +} |
| 43 | + |
| 44 | +// Valid: full wait with a tuple-binding pattern + check (direct destructuring). |
| 45 | +@workflow:Workflow |
| 46 | +function awaitBindingPattern( |
| 47 | + workflow:Context ctx, |
| 48 | + Input input, |
| 49 | + record {| |
| 50 | + future<ApprovalDecision> approval; |
| 51 | + future<ComplianceDecision> compliance; |
| 52 | + |} events |
| 53 | +) returns string|error { |
| 54 | + [ApprovalDecision, ComplianceDecision] [appr, comp] = |
| 55 | + check ctx->await([events.approval, events.compliance]); |
| 56 | + return appr.approverId + ":" + comp.reviewerId; |
| 57 | +} |
| 58 | + |
| 59 | +// Valid: per-position error tuple — each slot is a value or an error. Enabled by the |
| 60 | +// widened constraint `typedesc<anydata|error|(anydata|error)[]>` and validated per position |
| 61 | +// by the compiler plugin. |
| 62 | +@workflow:Workflow |
| 63 | +function awaitPerPositionError( |
| 64 | + workflow:Context ctx, |
| 65 | + Input input, |
| 66 | + record {| |
| 67 | + future<ApprovalDecision> approval; |
| 68 | + future<ComplianceDecision> compliance; |
| 69 | + |} events |
| 70 | +) returns string|error { |
| 71 | + [ApprovalDecision|error, ComplianceDecision|error] [appr, comp] = |
| 72 | + check ctx->await([events.approval, events.compliance]); |
| 73 | + if appr is error { |
| 74 | + return appr; |
| 75 | + } |
| 76 | + if comp is error { |
| 77 | + return comp; |
| 78 | + } |
| 79 | + return appr.approved && comp.compliant ? "DONE" : "REJECTED"; |
| 80 | +} |
| 81 | + |
| 82 | +// Valid: single wait. |
| 83 | +@workflow:Workflow |
| 84 | +function awaitSingle( |
| 85 | + workflow:Context ctx, |
| 86 | + Input input, |
| 87 | + record {| |
| 88 | + future<ApprovalDecision> approval; |
| 89 | + |} events |
| 90 | +) returns string|error { |
| 91 | + [ApprovalDecision] [appr] = check ctx->await([events.approval]); |
| 92 | + return appr.approverId; |
| 93 | +} |
| 94 | + |
| 95 | +// Valid: partial (alternate) wait, nilable members, binding pattern. |
| 96 | +@workflow:Workflow |
| 97 | +function awaitPartial( |
| 98 | + workflow:Context ctx, |
| 99 | + Input input, |
| 100 | + record {| |
| 101 | + future<ApprovalDecision> approverA; |
| 102 | + future<ApprovalDecision> approverB; |
| 103 | + |} events |
| 104 | +) returns string|error { |
| 105 | + [ApprovalDecision?, ApprovalDecision?] [a, b] = |
| 106 | + check ctx->await([events.approverA, events.approverB], 1); |
| 107 | + if a is ApprovalDecision { |
| 108 | + return a.approverId; |
| 109 | + } |
| 110 | + if b is ApprovalDecision { |
| 111 | + return b.approverId; |
| 112 | + } |
| 113 | + return "NONE"; |
| 114 | +} |
| 115 | + |
| 116 | +// Valid: partial wait captured as a union without check. |
| 117 | +@workflow:Workflow |
| 118 | +function awaitPartialUnionNoCheck( |
| 119 | + workflow:Context ctx, |
| 120 | + Input input, |
| 121 | + record {| |
| 122 | + future<ApprovalDecision> approverA; |
| 123 | + future<ApprovalDecision> approverB; |
| 124 | + |} events |
| 125 | +) returns string|error { |
| 126 | + [ApprovalDecision?, ApprovalDecision?]|error result = |
| 127 | + ctx->await([events.approverA, events.approverB], 1, timeout = {seconds: 5}); |
| 128 | + if result is error { |
| 129 | + return "TIMED_OUT"; |
| 130 | + } |
| 131 | + return "OK"; |
| 132 | +} |
0 commit comments