Skip to content

Commit 1482996

Browse files
committed
add sendcloudevent example
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent 3f11239 commit 1482996

14 files changed

+242
-43
lines changed

spec/examples/carauctionbids.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
{
1919
"name": "CarBidEvent",
2020
"type": "carBidMadeType",
21-
"source": "carBidEventSource"
21+
"source": "carBidEventSource",
22+
"kind": "consumed"
2223
}
2324
],
2425
"states": [

spec/examples/checkcarvitals.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
{
3333
"name": "CarTurnedOnEvent",
3434
"type": "car.events",
35-
"source": "my/car/start"
35+
"source": "my/car/start",
36+
"kind": "consumed"
3637
},
3738
{
3839
"name": "CarTurnedOffEvent",
3940
"type": "car.events",
40-
"source": "my/car/start"
41+
"source": "my/car/start",
42+
"kind": "consumed"
4143
}
4244
]
4345
}

spec/examples/sendcloudevent.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"id": "sendcloudeventonprovision",
3+
"version": "1.0",
4+
"name": "Send CloudEvent on provision completion",
5+
"start": "ProvisionOrdersState",
6+
"events": [
7+
{
8+
"name": "provisioningCompleteEvent",
9+
"type": "provisionCompleteType",
10+
"kind": "produced"
11+
}
12+
],
13+
"functions": [
14+
{
15+
"name": "provisionOrderFunction",
16+
"operation": "http://myapis.org/provisioning.json#doProvision",
17+
"type": "rest"
18+
}
19+
],
20+
"states": [
21+
{
22+
"name": "ProvisionOrdersState",
23+
"type": "foreach",
24+
"inputCollection": "${ .orders }",
25+
"iterationParam": "singleorder",
26+
"outputCollection": "${ .provisionedOrders }",
27+
"actions": [
28+
{
29+
"functionRef": {
30+
"refName": "provisionOrderFunction",
31+
"arguments": {
32+
"order": "${ .singleorder }"
33+
}
34+
}
35+
}
36+
],
37+
"end": {
38+
"produceEvents": [
39+
{
40+
"eventRef": "provisioningCompleteEvent",
41+
"data": "${ .provisionedOrders }"
42+
}
43+
]
44+
}
45+
}
46+
]
47+
}

spec/examples/sendcloudevent.spec.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import {WorkflowBuilder} from "../../src";
18+
import * as fs from "fs";
19+
import {
20+
ActionBuilder, EndBuilder,
21+
EventBuilder,
22+
EventsBuilder,
23+
ForEachStateBuilder,
24+
FunctionDefBuilder,
25+
FunctionRefBuilder,
26+
ProduceEventDefBuilder,
27+
} from '../../src';
28+
29+
30+
describe("sendcloudevent workflow example", () => {
31+
32+
33+
it('should generate Workflow object', function () {
34+
35+
const workflow = new WorkflowBuilder()
36+
.withId("sendcloudeventonprovision")
37+
.withVersion("1.0")
38+
.withName("Send CloudEvent on provision completion")
39+
.withStart("ProvisionOrdersState")
40+
.withEvents(
41+
new EventsBuilder()
42+
.withEvents([
43+
new EventBuilder()
44+
.withName("provisioningCompleteEvent")
45+
.withType("provisionCompleteType")
46+
.withKind("produced")
47+
.build(),
48+
49+
]).build(),
50+
)
51+
.withFunctions([
52+
new FunctionDefBuilder()
53+
.withName("provisionOrderFunction")
54+
.withOperation("http://myapis.org/provisioning.json#doProvision")
55+
.build(),
56+
])
57+
.withStates([
58+
new ForEachStateBuilder()
59+
.withName("ProvisionOrdersState")
60+
.withInputCollection("${ .orders }")
61+
.withIterationParam("singleorder")
62+
.withOutputCollection("${ .provisionedOrders }")
63+
.withActions([
64+
new ActionBuilder()
65+
.withFunctionRef(
66+
new FunctionRefBuilder()
67+
.withRefName("provisionOrderFunction")
68+
.withArguments({
69+
"order": "${ .singleorder }",
70+
})
71+
.build(),
72+
)
73+
.build(),
74+
])
75+
.withEnd(
76+
new EndBuilder()
77+
.withProduceEvents([
78+
new ProduceEventDefBuilder()
79+
.withEventRef("provisioningCompleteEvent")
80+
.withData("${ .provisionedOrders }")
81+
.build(),
82+
])
83+
.build(),
84+
)
85+
.build(),
86+
])
87+
.build();
88+
89+
90+
91+
92+
const expected = JSON.parse(fs.readFileSync("./spec/examples/sendcloudevent.json")
93+
.toLocaleString()) as any;
94+
expect(workflow).toEqual(expected);
95+
96+
});
97+
98+
99+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {CronDefBuilder} from "./model/cron-def.builder";
2323
export {DelayStateBuilder} from "./model/delay-state.builder";
2424
export {DatabasedSwitchBuilder} from "./model/databased-switch.builder";
2525
export {DefaultTransitionTypeBuilder} from "./model/default-transition-type.builder";
26+
export {EndBuilder} from "./model/end.builder";
2627
export {EventBuilder} from "./model/event.builder";
2728
export {EventBasedSwitchBuilder} from "./model/event-based-switch.builder";
2829
export {EventRefBuilder} from "./model/event-ref.builder";

src/model/end.builder.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import {End, ProduceEventsDef, Terminate} from './types';
18+
19+
export class EndBuilder {
20+
private terminate: Terminate = false;
21+
private produceEvents: ProduceEventsDef;
22+
private compensate: boolean;
23+
24+
withProduceEvents(value: ProduceEventsDef): EndBuilder {
25+
this.produceEvents = value;
26+
return this;
27+
}
28+
29+
withCompensate(value: boolean): EndBuilder {
30+
this.compensate = value;
31+
return this;
32+
}
33+
34+
build(): End {
35+
36+
let isObject: boolean = false;
37+
const result: End = {};
38+
39+
40+
if (this.produceEvents) {
41+
result.produceEvents = this.produceEvents;
42+
isObject = true;
43+
}
44+
if (this.compensate) {
45+
result.compensate = this.compensate;
46+
isObject = true;
47+
}
48+
if (isObject) {
49+
return result;
50+
}
51+
52+
return this.terminate;
53+
}
54+
55+
}

src/model/event.builder.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {EventDef, EventName} from './types';
17+
import {EventDef, EventKind, EventName} from './types';
1818

1919
export class EventBuilder {
20-
private model: EventDef = {};
20+
private model: EventDef = {
21+
kind: "consumed"
22+
};
2123

2224
withName(value: EventName): EventBuilder {
2325
this.model.name = value;
@@ -35,9 +37,15 @@ export class EventBuilder {
3537
}
3638

3739

40+
withKind(value: EventKind): any {
41+
this.model.kind = value;
42+
return this;
43+
}
44+
3845
build(): EventDef {
3946
//TODO validate
4047
return this.model;
4148
}
4249

50+
4351
}

src/model/for-each-state.builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {Actions, ForEachState, StateDataFilter} from '../index';
17+
import {Actions, End, ForEachState, StateDataFilter} from '../index';
1818

1919
export class ForEachStateBuilder {
2020

@@ -53,7 +53,7 @@ export class ForEachStateBuilder {
5353
return this;
5454
}
5555

56-
withEnd(value: boolean): any {
56+
withEnd(value: End): any {
5757
this.model.end = value;
5858
return this;
5959
}

src/model/inject-state.builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717
import {InjectState} from "./workflow";
18+
import {End} from './types';
1819

1920
export class InjectStateBuilder {
2021
private model: InjectState = {
@@ -35,7 +36,7 @@ export class InjectStateBuilder {
3536

3637
}
3738

38-
withEnd(value: boolean) {
39+
withEnd(value: End): InjectStateBuilder {
3940
this.model.end = value;
4041
return this;
4142

src/model/operation-state.builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717
import {OperationState, Transition} from "./workflow";
18-
import {ActionModeType, Actions} from "./types";
18+
import {ActionModeType, Actions, End} from "./types";
1919

2020
export class OperationStateBuilder {
2121

@@ -36,7 +36,7 @@ export class OperationStateBuilder {
3636

3737
}
3838

39-
withEnd(value: boolean): OperationStateBuilder {
39+
withEnd(value: End): OperationStateBuilder {
4040
this.model.end = value;
4141
return this;
4242
}

0 commit comments

Comments
 (0)