Skip to content

Commit 5ee1cfd

Browse files
authored
Merge pull request #58 from antmendoza/support-creation-produceeventdef
Support creation produceeventdef
2 parents 71662e9 + 064cb59 commit 5ee1cfd

File tree

5 files changed

+141
-245
lines changed

5 files changed

+141
-245
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 {ProduceEventDefBuilder} from '../src/model/produce-event-def.builder';
18+
19+
20+
describe("ProduceEventDefBuilder", () => {
21+
22+
it("should throws an error if mandatory fields are not set ", () => {
23+
24+
expect(() => new ProduceEventDefBuilder()
25+
.build()).toThrowError();
26+
27+
expect(() => new ProduceEventDefBuilder()
28+
.withEventRef("").build())
29+
.toThrowError();
30+
});
31+
32+
it("should build a valid ProduceEventDef object ", () => {
33+
34+
expect(new ProduceEventDefBuilder()
35+
.withEventRef("ConfirmationCompletedEvent")
36+
.withData("${ .payment }")
37+
.withContextAttributes({
38+
kContext: "kcValue",
39+
})
40+
.build())
41+
.toEqual({
42+
eventRef: "ConfirmationCompletedEvent",
43+
data: "${ .payment }",
44+
contextAttributes: {
45+
kContext: "kcValue",
46+
},
47+
});
48+
});
49+
50+
51+
});
52+

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {FunctionBuilder} from "./model/function.builder";
2727
export {FunctionRefImplBuilder} from "./model/function-ref-impl.builder";
2828
export {InjectStateBuilder} from "./model/inject-state.builder";
2929
export {OperationStateBuilder} from "./model/operation-state.builder";
30+
export {ProduceEventDefBuilder} from "./model/produce-event-def.builder";
3031
export {RepeatBuilder} from "./model/repeat.builder";
3132
export {ScheduleBuilder} from "./model/schedule.builder";
3233
export {SubFlowStateBuilder} from "./model/sub-flow-state.builder";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 {EventData, EventName, ProduceEventDef} from '../index';
18+
19+
export class ProduceEventDefBuilder {
20+
// @ts-ignore
21+
private readonly model: ProduceEventDef = {};
22+
23+
24+
withEventRef(value: EventName): ProduceEventDefBuilder {
25+
this.model.eventRef = value;
26+
return this;
27+
}
28+
29+
withData(value: EventData): ProduceEventDefBuilder {
30+
this.model.data = value;
31+
return this;
32+
33+
}
34+
35+
withContextAttributes(value: object): any {
36+
this.model.contextAttributes = value;
37+
return this;
38+
}
39+
40+
build(): ProduceEventDef {
41+
if (!this.model.eventRef) {
42+
throw new Error("Field eventRef can not be undefined");
43+
}
44+
return this.model;
45+
}
46+
47+
}

src/model/types.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ export type EventRefType = {
6060
/**
6161
* If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'. If object type, a custom object to become the data (payload) of the event referenced by 'triggerEventRef'.
6262
*/
63-
data?:
64-
| string
65-
| object;
63+
data?: EventData;
6664
/**
6765
* Add additional extension context attributes to the produced event
6866
*/
69-
contextAttributes?: object;
67+
contextAttributes?: ContextAttributes;
7068
};
7169

7270
export type ActionDataFilterType = {
@@ -175,26 +173,7 @@ export type EndType = | boolean
175173
/**
176174
* Defines events that should be produced
177175
*/
178-
produceEvents?: {
179-
/**
180-
* References a name of a defined event
181-
*/
182-
eventRef: string;
183-
/**
184-
* If String, expression which selects parts of the states data output to become the data of the produced event. If object a custom object to become the data of produced event.
185-
*/
186-
data?:
187-
| string
188-
| {
189-
[k: string]: unknown;
190-
};
191-
/**
192-
* Add additional event extension context attributes
193-
*/
194-
contextAttributes?: {
195-
[k: string]: string;
196-
};
197-
}[];
176+
produceEvents?: ProduceEventsDef;
198177
/**
199178
* If set to true, triggers workflow compensation. Default is false
200179
*/
@@ -359,3 +338,29 @@ export type CronDef =
359338
validUntil?: string;
360339
};
361340
export type Interval = string;
341+
342+
export type ExpressionExtractData = string;
343+
344+
export type DataObject = object;
345+
346+
export type EventData =
347+
| ExpressionExtractData
348+
| DataObject;
349+
350+
export type ContextAttributes = object;
351+
352+
export type ProduceEventDef = {
353+
/**
354+
* References a name of a defined event
355+
*/
356+
eventRef: EventName;
357+
/**
358+
* If String, expression which selects parts of the states data output to become the data of the produced event. If object a custom object to become the data of produced event.
359+
*/
360+
data?: EventData;
361+
/**
362+
* Add additional event extension context attributes
363+
*/
364+
contextAttributes?: ContextAttributes;
365+
};
366+
export type ProduceEventsDef = ProduceEventDef[];

0 commit comments

Comments
 (0)