Add alias flag for allowing bytes

This commit is contained in:
Fabian Stamm 2022-04-12 20:56:46 +00:00
parent 97ce0ea9b5
commit 339d3006d6
5 changed files with 127 additions and 46 deletions

42
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/examples/CSharp/Example/CSharp_Example.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/examples/CSharp/Example/CSharp_Example.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/examples/CSharp/Example/CSharp_Example.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}

View File

@ -10046,7 +10046,7 @@ function get_ir(parsed) {
]); ]);
} else if (statement.type == "define") { } else if (statement.type == "define") {
options[statement.key] = statement.value; options[statement.key] = statement.value;
if (statement.key == "use_messagepack" && statement.value == "true") { if ((statement.key == "use_messagepack" || statement.key == "allow_bytes") && statement.value == "true") {
builtin.push("bytes"); builtin.push("bytes");
} }
} else { } else {
@ -10260,7 +10260,6 @@ var TypescriptTarget = class extends CompileTarget {
const params = fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`).join(", "); const params = fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`).join(", ");
if (!fnc.return) { if (!fnc.return) {
a(1, `${fnc.name}(${params}): void {`); a(1, `${fnc.name}(${params}): void {`);
1;
a(2, `this._provider.sendMessage({`); a(2, `this._provider.sendMessage({`);
a(3, `jsonrpc: "2.0",`); a(3, `jsonrpc: "2.0",`);
a(3, `method: "${def.name}.${fnc.name}",`); a(3, `method: "${def.name}.${fnc.name}",`);

12
meta.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "jsonrpc",
"version": "0.0.1",
"description": "",
"author": "Fabian Stamm <dev@fabianstamm.de>",
"contributors": [],
"files": [
"**/*.ts",
"**/*.js",
"README.md"
]
}

View File

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

114
src/ir.ts
View File

@ -36,17 +36,17 @@ export interface EnumDefinition {
export type ServiceFunctionDecorators = { export type ServiceFunctionDecorators = {
description: string; description: string;
parameters: { parameters: {
name:string; name: string;
description: string; description: string;
}[]; }[];
returns: string; returns: string;
} };
export interface ServiceFunctionParamsDefinition { export interface ServiceFunctionParamsDefinition {
name: string; name: string;
inputs: { type: string; name: string, array: boolean }[]; inputs: { type: string; name: string; array: boolean }[];
return: { type: string, array: boolean } | undefined; return: { type: string; array: boolean } | undefined;
decorators: ServiceFunctionDecorators decorators: ServiceFunctionDecorators;
} }
export type ServiceFunctionDefinition = ServiceFunctionParamsDefinition; export type ServiceFunctionDefinition = ServiceFunctionParamsDefinition;
@ -62,8 +62,8 @@ export type Step = [
]; ];
export type IR = { export type IR = {
options: { [key:string]: string}, options: { [key: string]: string };
steps: Step[] steps: Step[];
}; };
export default function get_ir(parsed: Parsed): IR { export default function get_ir(parsed: Parsed): IR {
@ -101,7 +101,10 @@ export default function get_ir(parsed: Parsed): IR {
} }
if (defined.indexOf(field.fieldtype) < 0) { if (defined.indexOf(field.fieldtype) < 0) {
if (builtin.indexOf(field.fieldtype) < 0 && field.fieldtype !== statement.name) { if (
builtin.indexOf(field.fieldtype) < 0 &&
field.fieldtype !== statement.name
) {
throw new IRError( throw new IRError(
field, field,
`Type ${field.fieldtype} is not defined!` `Type ${field.fieldtype} is not defined!`
@ -112,11 +115,7 @@ export default function get_ir(parsed: Parsed): IR {
depends.push(field.fieldtype); depends.push(field.fieldtype);
} }
if ( if (field.map && field.map !== "number" && field.map !== "string") {
field.map &&
field.map !== "number" &&
field.map !== "string"
) {
throw new IRError( throw new IRError(
field, field,
`Type ${field.map} is not valid as map key!` `Type ${field.map} is not valid as map key!`
@ -200,7 +199,10 @@ export default function get_ir(parsed: Parsed): IR {
if (!depends.some((a) => a === fnc.return_type.type)) if (!depends.some((a) => a === fnc.return_type.type))
depends.push(fnc.return_type.type); depends.push(fnc.return_type.type);
} else { } else {
if (fnc.return_type.type !== "void" && builtin.indexOf(fnc.return_type.type) < 0) { if (
fnc.return_type.type !== "void" &&
builtin.indexOf(fnc.return_type.type) < 0
) {
throw new IRError( throw new IRError(
fnc, fnc,
`Type ${fnc.return_type.type} is not defined` `Type ${fnc.return_type.type} is not defined`
@ -225,51 +227,73 @@ export default function get_ir(parsed: Parsed): IR {
let decorators = {} as ServiceFunctionDecorators; let decorators = {} as ServiceFunctionDecorators;
fnc.decorators.forEach((values, key)=>{ fnc.decorators.forEach((values, key) => {
for(const val of values) { for (const val of values) {
switch(key) { switch (key) {
case "Description": case "Description":
if(decorators.description) if (decorators.description)
throw new IRError(fnc, `Decorator 'Description' can only be used once!`); throw new IRError(
if(val.length != 1) fnc,
throw new IRError(fnc, `Decorator 'Description' requires exactly one parameter!`); `Decorator 'Description' can only be used once!`
);
if (val.length != 1)
throw new IRError(
fnc,
`Decorator 'Description' requires exactly one parameter!`
);
decorators.description = val[0]; decorators.description = val[0];
break; break;
case "Returns": case "Returns":
if(decorators.returns) if (decorators.returns)
throw new IRError(fnc, `Decorator 'Returns' can only be used once!`); throw new IRError(
if(val.length != 1) fnc,
throw new IRError(fnc, `Decorator 'Returns' requires exactly one parameter!`); `Decorator 'Returns' can only be used once!`
decorators.returns = val[0]; );
break; if (val.length != 1)
throw new IRError(
fnc,
`Decorator 'Returns' requires exactly one parameter!`
);
decorators.returns = val[0];
break;
case "Param": case "Param":
if(!decorators.parameters) if (!decorators.parameters) decorators.parameters = [];
decorators.parameters = []; if (val.length != 2)
if(val.length != 2) throw new IRError(
throw new IRError(fnc, `Decorator 'Param' requires exactly two parameters!`); fnc,
`Decorator 'Param' requires exactly two parameters!`
);
const [name, description] = val; const [name, description] = val;
if(!fnc.inputs.find(e=>e.name == name)) if (!fnc.inputs.find((e) => e.name == name))
throw new IRError(fnc, `Decorator 'Param' requires the first param to equal the name of a function parameter!`); throw new IRError(
if(decorators.parameters.find(e=>e.name == name)) fnc,
throw new IRError(fnc, `Decorator 'Param' has already been set for the parameter ${name}!`); `Decorator 'Param' requires the first param to equal the name of a function parameter!`
);
if (decorators.parameters.find((e) => e.name == name))
throw new IRError(
fnc,
`Decorator 'Param' has already been set for the parameter ${name}!`
);
decorators.parameters.push({ decorators.parameters.push({
name, name,
description, description,
}) });
break; break;
default: default:
throw new IRError(fnc, `Decorator ${key} is not a valid decorator!`); throw new IRError(
fnc,
`Decorator ${key} is not a valid decorator!`
);
} }
} }
}) });
return { return {
name: fnc.name, name: fnc.name,
inputs: fnc.inputs, inputs: fnc.inputs,
return: fnc.return_type, return: fnc.return_type,
decorators decorators,
} as ServiceFunctionDefinition; } as ServiceFunctionDefinition;
}); });
@ -281,9 +305,13 @@ export default function get_ir(parsed: Parsed): IR {
functions, functions,
} as ServiceDefinition, } as ServiceDefinition,
]); ]);
} else if(statement.type == "define") { } else if (statement.type == "define") {
options[statement.key] = statement.value; options[statement.key] = statement.value;
if(statement.key == "use_messagepack" && statement.value == "true") { if (
(statement.key == "use_messagepack" ||
statement.key == "allow_bytes") &&
statement.value == "true"
) {
builtin.push("bytes"); builtin.push("bytes");
} }
} else { } else {
@ -293,6 +321,6 @@ export default function get_ir(parsed: Parsed): IR {
return { return {
options, options,
steps steps,
}; };
} }