Bug fixed and improvements

This commit is contained in:
K35 2022-01-03 17:11:53 +00:00
parent e1e4ce0740
commit aa983c9960
4 changed files with 128 additions and 180 deletions

View File

@ -7531,8 +7531,8 @@ var TypescriptTarget = class extends CompileTarget {
`; `;
} }
generateImports(a, def) { generateImports(a, def) {
a(0, this.generateImport(`{ VerificationError, verify_number, verify_string, verify_boolean, verify_void, strip_number, strip_string, strip_boolean, strip_void }`, `./ts_base`)); a(0, this.generateImport(`{ VerificationError, apply_number, apply_string, apply_boolean, apply_void }`, `./ts_base`));
a(0, def.depends.map((dep) => this.generateImport(`${dep}, { verify_${dep}, strip_${dep} }`, "./" + dep))); a(0, def.depends.map((dep) => this.generateImport(`${dep}, { apply_${dep} }`, "./" + dep)));
} }
getFileName(typename) { getFileName(typename) {
return typename + ".ts"; return typename + ".ts";
@ -7572,68 +7572,35 @@ var TypescriptTarget = class extends CompileTarget {
a(1, `}`); a(1, `}`);
a(0, ``); a(0, ``);
a(0, ``); a(0, ``);
a(1, `static verify(data: ${def.name}) {`); a(1, `static apply(data: ${def.name}) {`);
a(2, `verify_${def.name}(data);`); a(2, `apply_${def.name}(data);`);
a(1, `}`); a(1, `}`);
a(0, `}`); a(0, `}`);
a(0, ``); a(0, ``);
a(0, `export function strip_${def.name}(data: ${def.name}): ${def.name} {`); a(0, `export function apply_${def.name}(data: ${def.name}): ${def.name} {`);
{ {
a(1, `if(typeof data !== "object") throw new VerificationError("${def.name}", undefined, data);`);
a(1, `let res = {} as any;`); a(1, `let res = {} as any;`);
def.fields.forEach((field) => { def.fields.forEach((field) => {
a(1, `if(data["${field.name}"] !== null && data["${field.name}"] !== undefined) {`);
if (field.array) { if (field.array) {
a(1, `res["${field.name}"] = data["${field.name}"].map(elm=>`); a(2, `if(!Array.isArray(data["${field.name}"])) throw new VerificationError("array", "${field.name}", data["${field.name}"]);`);
a(2, `strip_${field.type}(elm)`); a(2, `res["${field.name}"] = data["${field.name}"].map(elm=>`);
a(1, `)`); a(3, `apply_${field.type}(elm)`);
a(2, `)`);
} else if (field.map) { } else if (field.map) {
a(1, `res["${field.name}"] = {}`); a(2, `if(typeof data["${field.name}"] !== "object") throw new VerificationError("map", "${field.name}", data["${field.name}"]);`);
a(1, `Object.entries(data["${field.name}"]).forEach(([key, val]) => res["${field.name}"][key] = strip_${field.type}(val))`); a(2, `res["${field.name}"] = {}`);
a(2, `Object.entries(data["${field.name}"]).forEach(([key, val]) => res["${field.name}"][key] = apply_${field.type}(val))`);
} else { } else {
a(1, `res["${field.name}"] = strip_${field.type}(data["${field.name}"])`); a(2, `res["${field.name}"] = apply_${field.type}(data["${field.name}"])`);
} }
a(1, `}`);
}); });
a(1, `return res;`); a(1, `return res;`);
} }
a(0, `}`); a(0, `}`);
a(0, ``); a(0, ``);
a(0, `export function verify_${def.name}(data: ${def.name}) {`);
{
def.fields.forEach((field) => {
a(1, `if(data["${field.name}"] !== null && data["${field.name}"] !== undefined ) {`);
const verifyType = (varName, off = 0) => {
switch (field.type) {
case "string":
a(2 + off, `if(typeof ${varName} !== "string") throw new VerificationError("string", "${field.name}", ${varName});`);
break;
case "number":
a(2 + off, `if(typeof ${varName} !== "number") throw new VerificationError("number", "${field.name}", ${varName});`);
break;
case "boolean":
a(2 + off, `if(typeof ${varName} !== "boolean") throw new VerificationError("boolean", "${field.name}", ${varName});`);
break;
default:
a(2 + off, `verify_${field.type}(${varName});`);
}
};
if (field.array) {
a(2, `if(!Array.isArray(data["${field.name}"])) throw new VerificationError("array", "${field.name}", data["${field.name}"]);`);
a(2, `for(const elm of data["${field.name}"]) {`);
verifyType("elm", 1);
a(2, `}`);
} else if (field.map) {
a(2, `if(typeof data["${field.name}"] !== "object") throw new VerificationError("object", "${field.name}", data["${field.name}"]);`);
a(2, `for(const key in data["${field.name}"]) {`);
verifyType(`data["${field.name}"][key]`, 1);
a(2, `}`);
} else {
verifyType(`data["${field.name}"]`);
}
a(1, "}");
a(0, ``);
});
a(1, `return data`);
}
a(0, `}`);
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n")); this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
} }
generateEnum(def) { generateEnum(def) {
@ -7653,13 +7620,10 @@ var TypescriptTarget = class extends CompileTarget {
a(0, ``); a(0, ``);
a(0, `export default ${def.name}`); a(0, `export default ${def.name}`);
a(0, ``); a(0, ``);
a(0, `export function strip_${def.name} (data: ${def.name}): ${def.name} {`); a(0, `export function apply_${def.name} (data: ${def.name}): ${def.name} {`);
a(1, `return data;`); a(1, `data = Number(data);`);
a(0, `}`);
a(0, ``);
a(0, `export function verify_${def.name} (data: ${def.name}) {`);
a(1, `if(${def.name}[data] == undefined) throw new VerificationError("${def.name}", undefined, data);`); a(1, `if(${def.name}[data] == undefined) throw new VerificationError("${def.name}", undefined, data);`);
a(1, `return data`); a(1, `return data;`);
a(0, `}`); a(0, `}`);
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n")); this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
} }
@ -7709,10 +7673,10 @@ var TypescriptTarget = class extends CompileTarget {
a(2, `}).then(result => {`); a(2, `}).then(result => {`);
if (fnc.return.array) { if (fnc.return.array) {
a(2, `for(const elm of result) {`); a(2, `for(const elm of result) {`);
a(3, `verify_${fnc.return.type}(elm);`); a(3, `apply_${fnc.return.type}(elm);`);
a(2, `}`); a(2, `}`);
} else { } else {
a(3, `verify_${fnc.return.type}(result);`); a(3, `apply_${fnc.return.type}(result);`);
} }
a(3, `return result;`); a(3, `return result;`);
a(2, `});`); a(2, `});`);
@ -7771,16 +7735,16 @@ var TypescriptTarget = class extends CompileTarget {
a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`); a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`);
if (fnc.inputs[i].array) { if (fnc.inputs[i].array) {
a(2, `for(const elm of p[${i}]) {`); a(2, `for(const elm of p[${i}]) {`);
a(3, `verify_${fnc.inputs[i].type}(elm)`); a(3, `apply_${fnc.inputs[i].type}(elm)`);
a(2, `}`); a(2, `}`);
} else { } else {
a(2, `verify_${fnc.inputs[i].type}(p[${i}])`); a(2, `apply_${fnc.inputs[i].type}(p[${i}])`);
} }
a(2, `}`); a(2, `}`);
} }
a(2, ``); a(2, ``);
a(2, `p.push(ctx);`); a(2, `p.push(ctx);`);
a(2, `return this.${fnc.name}.call(this, ...p)` + (fnc.return ? `.then(${((_a = fnc.return) == null ? void 0 : _a.array) ? `res => res.map(e => verify_${fnc.return.type}(strip_${fnc.return.type}(e)))` : `res => verify_${fnc.return.type}(strip_${fnc.return.type}(res))`});` : "")); a(2, `return this.${fnc.name}.call(this, ...p)` + (fnc.return ? `.then(${((_a = fnc.return) == null ? void 0 : _a.array) ? `res => res.map(e => apply_${fnc.return.type}(e))` : `res => apply_${fnc.return.type}(res)`});` : ""));
a(1, `}`); a(1, `}`);
a(0, ``); a(0, ``);
} }
@ -7818,14 +7782,14 @@ var TypescriptTarget = class extends CompileTarget {
steps.forEach(([type, def]) => { steps.forEach(([type, def]) => {
switch (type) { switch (type) {
case "type": case "type":
a(0, this.generateImport(`${def.name}, { verify_${def.name}, strip_${def.name} }`, "./" + def.name)); a(0, this.generateImport(`${def.name}, { apply_${def.name} }`, "./" + def.name));
a(0, `export { verify_${def.name}, strip_${def.name} }`); a(0, `export { apply_${def.name} }`);
a(0, `export type { ${def.name} }`); a(0, `export type { ${def.name} }`);
a(0, ``); a(0, ``);
break; break;
case "enum": case "enum":
a(0, this.generateImport(`${def.name}, { verify_${def.name}, strip_${def.name} }`, "./" + def.name)); a(0, this.generateImport(`${def.name}, { apply_${def.name} }`, "./" + def.name));
a(0, `export { ${def.name}, verify_${def.name}, strip_${def.name} }`); a(0, `export { ${def.name}, apply_${def.name} }`);
a(0, ``); a(0, ``);
break; break;
case "service": case "service":

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/jrpcgen", "name": "@hibas123/jrpcgen",
"version": "1.0.10", "version": "1.0.13",
"main": "lib/index.js", "main": "lib/index.js",
"license": "MIT", "license": "MIT",
"packageManager": "yarn@3.1.1", "packageManager": "yarn@3.1.1",

View File

@ -44,7 +44,7 @@ export class TypescriptTarget extends CompileTarget {
a( a(
0, 0,
this.generateImport( this.generateImport(
`{ VerificationError, verify_number, verify_string, verify_boolean, verify_void, strip_number, strip_string, strip_boolean, strip_void }`, `{ VerificationError, apply_number, apply_string, apply_boolean, apply_void }`,
`./ts_base` `./ts_base`
) )
); );
@ -52,7 +52,7 @@ export class TypescriptTarget extends CompileTarget {
0, 0,
def.depends.map((dep) => def.depends.map((dep) =>
this.generateImport( this.generateImport(
`${dep}, { verify_${dep}, strip_${dep} }`, `${dep}, { apply_${dep} }`,
"./" + dep "./" + dep
) )
) )
@ -115,8 +115,8 @@ export class TypescriptTarget extends CompileTarget {
a(0, ``); a(0, ``);
a(1, `static verify(data: ${def.name}) {`); a(1, `static apply(data: ${def.name}) {`);
a(2, `verify_${def.name}(data);`); a(2, `apply_${def.name}(data);`);
a(1, `}`); a(1, `}`);
a(0, `}`); a(0, `}`);
@ -124,27 +124,32 @@ export class TypescriptTarget extends CompileTarget {
a( a(
0, 0,
`export function strip_${def.name}(data: ${def.name}): ${def.name} {` `export function apply_${def.name}(data: ${def.name}): ${def.name} {`
); );
{ {
a(1, `if(typeof data !== "object") throw new VerificationError("${def.name}", undefined, data);`)
a(1, `let res = {} as any;`); a(1, `let res = {} as any;`);
def.fields.forEach((field) => { def.fields.forEach((field) => {
a(1, `if(data["${field.name}"] !== null && data["${field.name}"] !== undefined) {`)
if (field.array) { if (field.array) {
a(1, `res["${field.name}"] = data["${field.name}"].map(elm=>`); a(2, `if(!Array.isArray(data["${field.name}"])) throw new VerificationError("array", "${field.name}", data["${field.name}"]);`)
a(2, `strip_${field.type}(elm)`); a(2, `res["${field.name}"] = data["${field.name}"].map(elm=>`);
a(1, `)`); a(3, `apply_${field.type}(elm)`);
a(2, `)`);
} else if (field.map) { } else if (field.map) {
a(1, `res["${field.name}"] = {}`); a(2, `if(typeof data["${field.name}"] !== "object") throw new VerificationError("map", "${field.name}", data["${field.name}"]);`)
a(2, `res["${field.name}"] = {}`);
a( a(
1, 2,
`Object.entries(data["${field.name}"]).forEach(([key, val]) => res["${field.name}"][key] = strip_${field.type}(val))` `Object.entries(data["${field.name}"]).forEach(([key, val]) => res["${field.name}"][key] = apply_${field.type}(val))`
); );
} else { } else {
a( a(
1, 2,
`res["${field.name}"] = strip_${field.type}(data["${field.name}"])` `res["${field.name}"] = apply_${field.type}(data["${field.name}"])`
); );
} }
a(1, `}`);
}); });
a(1, `return res;`); a(1, `return res;`);
} }
@ -152,67 +157,67 @@ export class TypescriptTarget extends CompileTarget {
a(0, ``); a(0, ``);
a(0, `export function verify_${def.name}(data: ${def.name}) {`); // a(0, `export function verify_${def.name}(data: ${def.name}) {`);
{ // {
def.fields.forEach((field) => { // def.fields.forEach((field) => {
a( // a(
1, // 1,
`if(data["${field.name}"] !== null && data["${field.name}"] !== undefined ) {` // `if(data["${field.name}"] !== null && data["${field.name}"] !== undefined ) {`
); // );
const verifyType = (varName: string, off = 0) => { // const verifyType = (varName: string, off = 0) => {
switch (field.type) { // switch (field.type) {
case "string": // case "string":
a( // a(
2 + off, // 2 + off,
`if(typeof ${varName} !== "string") throw new VerificationError("string", "${field.name}", ${varName});` // `if(typeof ${varName} !== "string") throw new VerificationError("string", "${field.name}", ${varName});`
); // );
break; // break;
case "number": // case "number":
a( // a(
2 + off, // 2 + off,
`if(typeof ${varName} !== "number") throw new VerificationError("number", "${field.name}", ${varName});` // `if(typeof ${varName} !== "number") throw new VerificationError("number", "${field.name}", ${varName});`
); // );
break; // break;
case "boolean": // case "boolean":
a( // a(
2 + off, // 2 + off,
`if(typeof ${varName} !== "boolean") throw new VerificationError("boolean", "${field.name}", ${varName});` // `if(typeof ${varName} !== "boolean") throw new VerificationError("boolean", "${field.name}", ${varName});`
); // );
break; // break;
default: // default:
a( // a(
2 + off, // 2 + off,
`verify_${field.type}(${varName});` // `verify_${field.type}(${varName});`
); // );
} // }
}; // };
if (field.array) { // if (field.array) {
a( // a(
2, // 2,
`if(!Array.isArray(data["${field.name}"])) throw new VerificationError("array", "${field.name}", data["${field.name}"]);` // ``
); // );
a(2, `for(const elm of data["${field.name}"]) {`); // a(2, `for(const elm of data["${field.name}"]) {`);
verifyType("elm", 1); // verifyType("elm", 1);
a(2, `}`); // a(2, `}`);
} else if (field.map) { // } else if (field.map) {
a( // a(
2, // 2,
`if(typeof data["${field.name}"] !== "object") throw new VerificationError("object", "${field.name}", data["${field.name}"]);` // `if(typeof data["${field.name}"] !== "object") throw new VerificationError("object", "${field.name}", data["${field.name}"]);`
); // );
a(2, `for(const key in data["${field.name}"]) {`); // a(2, `for(const key in data["${field.name}"]) {`);
verifyType(`data["${field.name}"][key]`, 1); // verifyType(`data["${field.name}"][key]`, 1);
a(2, `}`); // a(2, `}`);
} else { // } else {
verifyType(`data["${field.name}"]`); // verifyType(`data["${field.name}"]`);
} // }
a(1, "}"); // a(1, "}");
a(0, ``); // a(0, ``);
}); // });
a(1, `return data`); // a(1, `return data`);
} // }
a(0, `}`); // a(0, `}`);
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n")); this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
} }
@ -241,17 +246,14 @@ export class TypescriptTarget extends CompileTarget {
a( a(
0, 0,
`export function strip_${def.name} (data: ${def.name}): ${def.name} {` `export function apply_${def.name} (data: ${def.name}): ${def.name} {`
); );
a(1, `return data;`); a(1, `data = Number(data);`);
a(0, `}`);
a(0, ``);
a(0, `export function verify_${def.name} (data: ${def.name}) {`);
a( a(
1, 1,
`if(${def.name}[data] == undefined) throw new VerificationError("${def.name}", undefined, data);` `if(${def.name}[data] == undefined) throw new VerificationError("${def.name}", undefined, data);`
); );
a(1, `return data`); a(1, `return data;`);
a(0, `}`); a(0, `}`);
this.writeFormattedFile(this.getFileName(def.name), lines.join("\n")); this.writeFormattedFile(this.getFileName(def.name), lines.join("\n"));
@ -333,10 +335,10 @@ export class TypescriptTarget extends CompileTarget {
a(2, `}).then(result => {`); a(2, `}).then(result => {`);
if (fnc.return.array) { if (fnc.return.array) {
a(2, `for(const elm of result) {`); a(2, `for(const elm of result) {`);
a(3, `verify_${fnc.return.type}(elm);`); a(3, `apply_${fnc.return.type}(elm);`);
a(2, `}`); a(2, `}`);
} else { } else {
a(3, `verify_${fnc.return.type}(result);`); a(3, `apply_${fnc.return.type}(result);`);
} }
a(3, `return result;`); a(3, `return result;`);
a(2, `});`); a(2, `});`);
@ -426,10 +428,10 @@ export class TypescriptTarget extends CompileTarget {
a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`); a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`);
if (fnc.inputs[i].array) { if (fnc.inputs[i].array) {
a(2, `for(const elm of p[${i}]) {`); a(2, `for(const elm of p[${i}]) {`);
a(3, `verify_${fnc.inputs[i].type}(elm)`); a(3, `apply_${fnc.inputs[i].type}(elm)`);
a(2, `}`); a(2, `}`);
} else { } else {
a(2, `verify_${fnc.inputs[i].type}(p[${i}])`); a(2, `apply_${fnc.inputs[i].type}(p[${i}])`);
} }
a(2, `}`); a(2, `}`);
} }
@ -443,8 +445,8 @@ export class TypescriptTarget extends CompileTarget {
(fnc.return (fnc.return
? `.then(${ ? `.then(${
fnc.return?.array fnc.return?.array
? `res => res.map(e => verify_${fnc.return.type}(strip_${fnc.return.type}(e)))` ? `res => res.map(e => apply_${fnc.return.type}(e))`
: `res => verify_${fnc.return.type}(strip_${fnc.return.type}(res))` : `res => apply_${fnc.return.type}(res)`
});` });`
: "") : "")
); );
@ -502,12 +504,12 @@ export class TypescriptTarget extends CompileTarget {
a( a(
0, 0,
this.generateImport( this.generateImport(
`${def.name}, { verify_${def.name}, strip_${def.name} }`, `${def.name}, { apply_${def.name} }`,
"./" + def.name "./" + def.name
) )
); );
a(0, `export { verify_${def.name}, strip_${def.name} }`); a(0, `export { apply_${def.name} }`);
a(0, `export type { ${def.name} }`); a(0, `export type { ${def.name} }`);
a(0, ``); a(0, ``);
break; break;
@ -515,13 +517,13 @@ export class TypescriptTarget extends CompileTarget {
a( a(
0, 0,
this.generateImport( this.generateImport(
`${def.name}, { verify_${def.name}, strip_${def.name} }`, `${def.name}, { apply_${def.name} }`,
"./" + def.name "./" + def.name
) )
); );
a( a(
0, 0,
`export { ${def.name}, verify_${def.name}, strip_${def.name} }` `export { ${def.name}, apply_${def.name} }`
); );
a(0, ``); a(0, ``);
break; break;

View File

@ -4,40 +4,22 @@ export class VerificationError extends Error {
public readonly field?: string, public readonly field?: string,
public readonly value?: any public readonly value?: any
) { ) {
super("Parameter verification failed!"); super("Parameter verification failed! " +(type ? "Expected " + type + "! " :"") + (field ? "At: " + field + "! " : ""));
} }
} }
export function verify_number(data: any) { export function apply_number(data: any) {
if (typeof data !== "number") throw new VerificationError("number", undefined, data); data = Number(data);
if(Number.isNaN(data)) throw new VerificationError("number", undefined, data);
return data; return data;
} }
export function strip_number(data: any) { export function apply_string(data: any) {
return data; return String(data);
} }
export function verify_string(data: any) { export function apply_boolean(data: any) {
if (typeof data !== "string") throw new VerificationError("string", undefined, data); return Boolean(data);
return data;
} }
export function strip_string(data: any) { export function apply_void(data: any) {}
return data;
}
export function verify_boolean(data: any) {
if (typeof data !== "boolean") throw new VerificationError("boolean", undefined, data);
return data;
}
export function strip_boolean(data: any) {
return data;
}
//TODO: Should it return data? it is kinda undefined actually...
export function verify_void(data: any) {}
export function strip_void(data: any) {
return undefined;
}