Skip to content

Commit 0637a99

Browse files
committed
added builder to produceEventDef
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent 5c2125d commit 0637a99

File tree

4 files changed

+155
-124
lines changed

4 files changed

+155
-124
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
18+
import {EventName, ProduceEventDef} from '../src';
19+
20+
21+
export class ProduceEventDefBuilder {
22+
// @ts-ignore
23+
private readonly model: ProduceEventDef = {};
24+
25+
26+
withEventRef(value: EventName): ProduceEventDefBuilder {
27+
this.model.eventRef = value;
28+
return this;
29+
}
30+
31+
withData(value: object): ProduceEventDefBuilder {
32+
this.model.data = value;
33+
return this;
34+
35+
}
36+
37+
withContextAttributes(value: object): any {
38+
this.model.contextAttributes = value;
39+
return this;
40+
}
41+
42+
build(): ProduceEventDef {
43+
if (!this.model.eventRef) {
44+
throw new Error("Field eventRef can not be undefined");
45+
}
46+
return this.model;
47+
}
48+
49+
}
50+
51+
describe("ProduceEventDefBuilder", () => {
52+
53+
it("should throws an error if mandatory fields are not set ", () => {
54+
55+
expect(() => new ProduceEventDefBuilder()
56+
.build()).toThrowError();
57+
58+
expect(() => new ProduceEventDefBuilder()
59+
.withEventRef("").build())
60+
.toThrowError();
61+
});
62+
63+
it("should build a valid ProduceEventDef object ", () => {
64+
65+
expect(new ProduceEventDefBuilder()
66+
.withEventRef("eventName")
67+
.withData({kData: "kdValue"})
68+
.withContextAttributes({
69+
kContext: "kcValue",
70+
})
71+
.build())
72+
.toEqual({
73+
eventRef: "eventName",
74+
data: {kData: "kdValue"},
75+
contextAttributes: {
76+
kContext: "kcValue",
77+
},
78+
});
79+
});
80+
81+
82+
});
83+
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 {EventName, ProduceEventDef} from '../src';
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: object): 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: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ export type EndType = | boolean
185185
*/
186186
data?:
187187
| string
188-
| {
189-
[k: string]: unknown;
190-
};
188+
| object;
191189
/**
192190
* Add additional event extension context attributes
193191
*/
@@ -359,3 +357,20 @@ export type CronDef =
359357
validUntil?: string;
360358
};
361359
export type Interval = string;
360+
export type ProduceEventDef = {
361+
/**
362+
* References a name of a defined event
363+
*/
364+
eventRef: EventName;
365+
/**
366+
* 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.
367+
*/
368+
data?:
369+
| string
370+
| object;
371+
/**
372+
* Add additional event extension context attributes
373+
*/
374+
contextAttributes?: object;
375+
};
376+
export type ProduceEventsDef = ProduceEventDef[];

src/model/workflow.ts

Lines changed: 7 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
DataConditionsType,
2121
DefaultTransitionType,
2222
EndType, EventsDef,
23-
FunctionsDef, Interval, MetadataType, RepeatType, RetriesDef, StartScheduledType,
23+
FunctionsDef, Interval, MetadataType, ProduceEventsDef, RepeatType, RetriesDef, StartScheduledType,
2424
StateDataFilterType,
2525
StatesType,
2626
} from "./types";
@@ -128,26 +128,7 @@ export type Transition =
128128
/**
129129
* Array of events to be produced before the transition happens
130130
*/
131-
produceEvents?: {
132-
/**
133-
* References a name of a defined event
134-
*/
135-
eventRef: string;
136-
/**
137-
* 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.
138-
*/
139-
data?:
140-
| string
141-
| {
142-
[k: string]: unknown;
143-
};
144-
/**
145-
* Add additional event extension context attributes
146-
*/
147-
contextAttributes?: {
148-
[k: string]: string;
149-
};
150-
}[];
131+
produceEvents?: ProduceEventsDef;
151132
/**
152133
* If set to true, triggers workflow compensation when before this transition is taken. Default is false
153134
*/
@@ -163,26 +144,7 @@ export type End =
163144
/**
164145
* Defines events that should be produced
165146
*/
166-
produceEvents?: {
167-
/**
168-
* References a name of a defined event
169-
*/
170-
eventRef: string;
171-
/**
172-
* 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.
173-
*/
174-
data?:
175-
| string
176-
| {
177-
[k: string]: unknown;
178-
};
179-
/**
180-
* Add additional event extension context attributes
181-
*/
182-
contextAttributes?: {
183-
[k: string]: string;
184-
};
185-
}[];
147+
produceEvents?: ProduceEventsDef;
186148
/**
187149
* If set to true, triggers workflow compensation. Default is false
188150
*/
@@ -256,26 +218,7 @@ export interface DelayState {
256218
/**
257219
* Array of events to be produced before the transition happens
258220
*/
259-
produceEvents?: {
260-
/**
261-
* References a name of a defined event
262-
*/
263-
eventRef: string;
264-
/**
265-
* 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.
266-
*/
267-
data?:
268-
| string
269-
| {
270-
[k: string]: unknown;
271-
};
272-
/**
273-
* Add additional event extension context attributes
274-
*/
275-
contextAttributes?: {
276-
[k: string]: string;
277-
};
278-
}[];
221+
produceEvents?: ProduceEventsDef;
279222
/**
280223
* If set to true, triggers workflow compensation when before this transition is taken. Default is false
281224
*/
@@ -353,26 +296,7 @@ export interface OperationState {
353296
/**
354297
* Array of events to be produced before the transition happens
355298
*/
356-
produceEvents?: {
357-
/**
358-
* References a name of a defined event
359-
*/
360-
eventRef: string;
361-
/**
362-
* 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.
363-
*/
364-
data?:
365-
| string
366-
| {
367-
[k: string]: unknown;
368-
};
369-
/**
370-
* Add additional event extension context attributes
371-
*/
372-
contextAttributes?: {
373-
[k: string]: string;
374-
};
375-
}[];
299+
produceEvents?: ProduceEventsDef;
376300
/**
377301
* If set to true, triggers workflow compensation when before this transition is taken. Default is false
378302
*/
@@ -460,26 +384,7 @@ export interface ParallelState {
460384
/**
461385
* Array of events to be produced before the transition happens
462386
*/
463-
produceEvents?: {
464-
/**
465-
* References a name of a defined event
466-
*/
467-
eventRef: string;
468-
/**
469-
* 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.
470-
*/
471-
data?:
472-
| string
473-
| {
474-
[k: string]: unknown;
475-
};
476-
/**
477-
* Add additional event extension context attributes
478-
*/
479-
contextAttributes?: {
480-
[k: string]: string;
481-
};
482-
}[];
387+
produceEvents?:ProduceEventsDef;
483388
/**
484389
* If set to true, triggers workflow compensation when before this transition is taken. Default is false
485390
*/
@@ -713,26 +618,7 @@ export interface Transitioneventcondition {
713618
/**
714619
* Array of events to be produced before the transition happens
715620
*/
716-
produceEvents?: {
717-
/**
718-
* References a name of a defined event
719-
*/
720-
eventRef: string;
721-
/**
722-
* 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.
723-
*/
724-
data?:
725-
| string
726-
| {
727-
[k: string]: unknown;
728-
};
729-
/**
730-
* Add additional event extension context attributes
731-
*/
732-
contextAttributes?: {
733-
[k: string]: string;
734-
};
735-
}[];
621+
produceEvents?: ProduceEventsDef;
736622
/**
737623
* If set to true, triggers workflow compensation when before this transition is taken. Default is false
738624
*/

0 commit comments

Comments
 (0)