Skip to content

Commit fd7e4e0

Browse files
authored
Merge pull request #51 from antmendoza/support-creation-function-object
Support the creation of function object
2 parents 5346c8f + 976dc1d commit fd7e4e0

File tree

5 files changed

+129
-27
lines changed

5 files changed

+129
-27
lines changed

spec/applicantrequest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"functions": [
88
{
99
"name": "sendRejectionEmailFunction",
10-
"operation": "http://myapis.org/applicationapi.json#emailRejection"
10+
"operation": "http://myapis.org/applicationapi.json#emailRejection",
11+
"type": "rest"
1112
}
1213
],
1314
"states":[
@@ -51,4 +52,4 @@
5152
"end": true
5253
}
5354
]
54-
}
55+
}

spec/function-builder.spec.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 {FunctionBuilder} from '../src';
19+
20+
21+
describe("FunctionBuilder", () => {
22+
23+
24+
it("should throws an error if mandatory fields are not set ", () => {
25+
26+
expect(() => new FunctionBuilder().build()).toThrowError();
27+
28+
expect(() => new FunctionBuilder()
29+
.withName("")
30+
.withOperation("")
31+
.build())
32+
.toThrowError();
33+
34+
expect(() => new FunctionBuilder()
35+
.withOperation("http://myapis.org/applicationapi.json#emailRejection")
36+
.build())
37+
.toThrowError();
38+
39+
expect(() => new FunctionBuilder()
40+
.withName("functionName")
41+
.build())
42+
.toThrowError();
43+
});
44+
45+
46+
it("should generate a populated object with default type value", () => {
47+
expect(new FunctionBuilder()
48+
.withName("functionName")
49+
.withOperation("http://myapis.org/applicationapi.json#emailRejection")
50+
.build()).toEqual(
51+
{
52+
name: "functionName",
53+
operation: "http://myapis.org/applicationapi.json#emailRejection",
54+
type: "rest",
55+
},
56+
);
57+
58+
});
59+
60+
it("should generate a populated object with default type value", () => {
61+
expect(new FunctionBuilder()
62+
.withName("functionName")
63+
.withOperation("file#serviceName#method")
64+
.withType("rpc")
65+
.build()).toEqual(
66+
{
67+
name: "functionName",
68+
operation: "file#serviceName#method",
69+
type: "rpc",
70+
},
71+
);
72+
73+
});
74+
75+
});
76+
77+

spec/workflow.builder.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ describe("workflow builder", () => {
111111

112112
const workflowValidator = new WorkflowValidator({});
113113
spyOn(workflowValidator, 'isValid')
114-
.and.returnValue(false).and.callThrough();
114+
.and.returnValue(false);
115115
spyOn(workflowValidator, 'validate')
116-
.and.returnValue(new ValidationErrors([new ValidationError("any error")]))
117-
.and.callThrough();
116+
.and.returnValue(new ValidationErrors([new ValidationError("any error")]));
118117

119118
const validatorFactory = new ValidatorFactory();
120119
spyOn(validatorFactory, 'workflowValidator')

src/model/function.builder.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,48 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {FunctionType} from "./types";
17+
import {Function, FunctionType} from "./types";
1818

1919
export class FunctionBuilder {
20-
// @ts-ignore
21-
private model: FunctionType = {};
22-
23-
withName(value: string): FunctionBuilder {
24-
this.model.name = value;
25-
return this;
26-
}
27-
28-
withOperation(value: string): FunctionBuilder {
29-
this.model.operation = value;
30-
return this;
31-
}
32-
33-
build() {
34-
//TODO validate
35-
return this.model;
36-
}
20+
21+
// @ts-ignore
22+
private model: Function = {
23+
type: "rest",
24+
};
25+
26+
withName(value: string): FunctionBuilder {
27+
this.model.name = value;
28+
return this;
29+
}
30+
31+
withOperation(value: string): FunctionBuilder {
32+
this.model.operation = value;
33+
return this;
34+
}
35+
36+
withType(value: FunctionType): any {
37+
this.model.type = value;
38+
return this;
39+
}
40+
41+
build(): Function {
42+
43+
44+
const errors: string[] = [];
45+
46+
if (!this.model.name) {
47+
errors.push("Field name can not be undefined or empty");
48+
}
49+
50+
if (!this.model.operation) {
51+
errors.push("Field operation can not be undefined or empty");
52+
}
53+
54+
if (errors.length > 0) {
55+
throw new Error(errors.map(e => e).join("; "));
56+
}
57+
58+
return this.model;
59+
}
60+
3761
}

src/model/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ export type ActionType = {
109109
export type ActionsType = ActionType[];
110110

111111

112-
export type FunctionType = {
112+
export type FunctionType = "rest" | "rpc" | "expression";
113+
export type Function = {
113114
/**
114115
* Unique function name
115116
*/
@@ -121,13 +122,13 @@ export type FunctionType = {
121122
/**
122123
* Defines the function type. Is either `rest`, `rpc` or `expression`. Default is `rest`
123124
*/
124-
type?: "rest" | "rpc" | "expression";
125+
type?: FunctionType;
125126
};
126127

127128
export type FunctionsDef = | string
128129
| [
129-
FunctionType,
130-
...FunctionType[]
130+
Function,
131+
...Function[]
131132
];
132133
export type StatesType = [
133134
(

0 commit comments

Comments
 (0)