Support enums

This commit is contained in:
Fabian Stamm
2023-01-02 16:34:13 +01:00
parent 6e947bde57
commit b3b202c9f9
8 changed files with 75 additions and 48 deletions

View File

@ -83,17 +83,24 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
fn = `pub type_:`;
a(1, `#[serde(rename = "type")]`);
}
let opts = "";
let opte = "";
if (field.optional) {
opts = "Option<";
opte = ">";
}
if (field.array) {
a(1, `${fn} Vec<${toRustType(field.type)}>,`);
a(1, `${fn} ${opts}Vec<${toRustType(field.type)}>${opte},`);
} else if (field.map) {
a(
1,
`${fn} HashMap<${toRustType(
`${fn} ${opts}HashMap<${toRustType(
field.map
)}, ${toRustType(field.type)}>,`
)}, ${toRustType(field.type)}>${opte},`
);
} else {
a(1, `${fn} ${toRustType(field.type)},`);
a(1, `${fn} ${opts}${toRustType(field.type)}${opte},`);
}
}
a(0, `}`);
@ -113,7 +120,7 @@ export class RustTarget extends CompileTarget<{ rust_crate: string }> {
a(0, `#[repr(i64)]`);
a(
0,
"#[derive(Clone, Copy, Debug, Eq, PartialEq, IntEnum, Deserialize, Serialize)]"
"#[derive(Clone, Copy, Debug, Eq, PartialEq, IntEnum)]"
);
a(0, `pub enum ${definition.name} {`);
for (const field of definition.values) {

View File

@ -33,9 +33,8 @@ export class TypescriptTarget extends CompileTarget {
}
private generateImport(imports: string, path: string) {
return `import ${imports} from "${
path + (this.flavour === "esm" ? ".ts" : "")
}";\n`;
return `import ${imports} from "${path + (this.flavour === "esm" ? ".ts" : "")
}";\n`;
}
private generateImports(
@ -89,7 +88,7 @@ export class TypescriptTarget extends CompileTarget {
} else {
type = toJSType(field.type);
}
return `${field.name}?: ${type}; `;
return `${field.name}${field.optional ? "?" : ""}: ${type}; `;
})
);
@ -125,10 +124,18 @@ export class TypescriptTarget extends CompileTarget {
);
a(1, `let res = new ${def.name}() as any;`);
def.fields.forEach((field) => {
a(
1,
`if(data["${field.name}"] !== null && data["${field.name}"] !== undefined) {`
);
if (field.optional) {
a(
1,
`if(data["${field.name}"] !== null && data["${field.name}"] !== undefined) {`
);
} else {
a(
1,
`if(data["${field.name}"] === null || data["${field.name}"] === undefined) throw new VerificationError("${def.name}", "${field.name}", data["${field.name}"]);`
);
a(1, `else {`);
}
if (field.array) {
a(
2,
@ -203,9 +210,9 @@ export class TypescriptTarget extends CompileTarget {
"{ RequestObject, ResponseObject, ErrorCodes, Logging }",
"./service_base"
) +
this.generateImport(" { VerificationError }", "./ts_base") +
"\n\n" +
this.getTemplate("ts_service_client.ts")
this.generateImport(" { VerificationError }", "./ts_base") +
"\n\n" +
this.getTemplate("ts_service_client.ts")
);
const { a, getResult } = LineAppender();
@ -300,9 +307,9 @@ export class TypescriptTarget extends CompileTarget {
"{ RequestObject, ResponseObject, ErrorCodes, Logging }",
"./service_base"
) +
this.generateImport(" { VerificationError }", "./ts_base") +
"\n\n" +
this.getTemplate("ts_service_server.ts")
this.generateImport(" { VerificationError }", "./ts_base") +
"\n\n" +
this.getTemplate("ts_service_server.ts")
);
this.generateImports(a, def);
@ -335,9 +342,8 @@ export class TypescriptTarget extends CompileTarget {
`ctx: T`,
].join(", ");
const retVal = fnc.return
? `Promise<${
toJSType(fnc.return.type) + (fnc.return.array ? "[]" : "")
}>`
? `Promise<${toJSType(fnc.return.type) + (fnc.return.array ? "[]" : "")
}>`
: `void`;
a(1, `abstract ${fnc.name}(${params}): ${retVal};`);
@ -380,13 +386,12 @@ export class TypescriptTarget extends CompileTarget {
a(
2,
`return this.${fnc.name}.call(this, ...p)` + //TODO: Refactor. This line is way to compicated for anyone to understand, including me
(fnc.return
? `.then(${
fnc.return?.array
? `res => res.map(e => apply_${fnc.return.type}(e))`
: `res => apply_${fnc.return.type}(res)`
});`
: "")
(fnc.return
? `.then(${fnc.return?.array
? `res => res.map(e => apply_${fnc.return.type}(e))`
: `res => apply_${fnc.return.type}(res)`
});`
: "")
);
a(1, `}`);
a(0, ``);