Add Array support to functions

This commit is contained in:
K35
2022-01-02 20:51:45 +00:00
parent a8f49d117d
commit db21c1c42e
7 changed files with 143 additions and 40 deletions

View File

@ -15,7 +15,7 @@ const conversion = {
boolean: "boolean",
number: "number",
string: "string",
void: "void"
void: "void",
};
function toJSType(type: string): string {
@ -196,7 +196,6 @@ export class TypescriptTarget extends CompileTarget {
this.getTemplate("ts_service_client.ts")
);
let lines: string[] = [];
const a: lineAppender = (i, t) => {
if (!Array.isArray(t)) {
@ -238,8 +237,10 @@ export class TypescriptTarget extends CompileTarget {
for (const fnc of def.functions) {
const params = fnc.inputs
.map((e) => `${e.name}: ${toJSType(e.type)}`)
.join(",");
.map(
(e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`
)
.join(", ");
//TODO: Prio 2 : Add optional parameters to this and the declaration file
if (!fnc.return) {
a(1, `${fnc.name}(${params}): void {`);
@ -250,7 +251,9 @@ export class TypescriptTarget extends CompileTarget {
a(2, `});`);
a(1, `}`);
} else {
const retType = fnc.return ? toJSType(fnc.return) : "void";
const retType = fnc.return
? toJSType(fnc.return.type) + (fnc.return.array ? "[]" : "")
: "void";
a(1, `${fnc.name}(${params}): Promise<${retType}> {`);
a(2, `return new Promise<${retType}>((ok, err) => {`);
a(3, `this._provider.sendMessage({`);
@ -262,10 +265,19 @@ export class TypescriptTarget extends CompileTarget {
a(4, `ok, err`);
a(3, `});`);
a(2, `}).then(result => {`);
a(
3,
`if(!verify_${fnc.return}(result)) throw new Error("Invalid result data!");`
);
if (fnc.return.array) {
a(2, `for(const elm of result) {`);
a(
3,
`if(!verify_${fnc.return.type}(elm)) throw new Error("Invalid result data!");`
);
a(2, `}`);
} else {
a(
3,
`if(!verify_${fnc.return.type}(result)) throw new Error("Invalid result data!");`
);
}
a(3, `return result;`);
a(2, `});`);
a(1, `}`);
@ -332,11 +344,15 @@ export class TypescriptTarget extends CompileTarget {
for (const fnc of def.functions) {
const params = [
...fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type)}`),
...fnc.inputs.map(
(e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`
),
`ctx: T`,
].join(", ");
const retVal = fnc.return
? `Promise<${toJSType(fnc.return)}>`
? `Promise<${
toJSType(fnc.return.type) + (fnc.return.array ? "[]" : "")
}>`
: `void`;
a(1, `abstract ${fnc.name}(${params}): ${retVal};`);
@ -353,15 +369,32 @@ export class TypescriptTarget extends CompileTarget {
a(2, `}`);
a(2, ``);
for(let i = 0; i < fnc.inputs.length; i++) {
a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`);
a(2, `if(!verify_${fnc.inputs[i].type}(p[${i}])) throw new Error("Parameter verification failed!")`);
a(2, `}`);
for (let i = 0; i < fnc.inputs.length; i++) {
a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`);
if (fnc.inputs[i].array) {
a(2, `for(const elm of p[${i}]) {`);
a(
3,
`if(!verify_${fnc.inputs[i].type}(elm)) throw new Error("Parameter verification failed!")`
);
a(2, `}`);
} else {
a(
2,
`if(!verify_${fnc.inputs[i].type}(p[${i}])) throw new Error("Parameter verification failed!")`
);
}
a(2, `}`);
}
a(2, ``);
a(2, `p.push(ctx);`);
a(2, `return this.${fnc.name}.call(this, ...p)${fnc.return == "void" ? ".then(res => undefined)" : ""};`);
a(
2,
`return this.${fnc.name}.call(this, ...p)${
fnc.return?.type == "void" ? ".then(res => undefined)" : ""
};`
);
a(1, `}`);
a(0, ``);
}