This commit is contained in:
Fabian Stamm 2022-07-21 09:20:55 +02:00
commit 327d7dfac6

View File

@ -5,149 +5,187 @@ import { LineAppender, lineAppender } from "../utils";
import chalk from "chalk"; import chalk from "chalk";
const conversion = { const conversion = {
boolean: "bool", boolean: "bool",
int: "int", int: "int",
float: "double", float: "double",
string: "String", string: "String",
void: "void", void: "void",
bytes: "Uint8List", //TODO: Check this bytes: "Uint8List", //TODO: Check this
}; };
function toDartType(type: string): string { function toDartType(type: string): string {
return (conversion as any)[type] || type; return (conversion as any)[type] || type;
} }
export class DartTarget extends CompileTarget<{ dart_library_name: string }> { export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
name: string = "dart"; name: string = "dart";
start(): void { start(): void {
if (this.options.allow_bytes == true) { if (this.options.allow_bytes == true) {
throw new Error("Dart has no support for 'bytes' yet!"); throw new Error("Dart has no support for 'bytes' yet!");
} }
if (!this.options.dart_library_name) { if (!this.options.dart_library_name) {
throw new Error("Setting dart_library_name is required for DART target!"); throw new Error("Setting dart_library_name is required for DART target!");
} }
} }
getImport(name: string) { getImport(name: string) {
return `import "./${name}.dart";`; return `import "./${name}.dart";`;
} }
generateImports(a: lineAppender, def: TypeDefinition | ServiceDefinition) { generateImports(a: lineAppender, def: TypeDefinition | ServiceDefinition) {
def.depends.forEach((dep) => {
a(0, this.getImport(dep));
});
}
def.depends.forEach((dep) => { generateType(definition: TypeDefinition): void {
a(0, this.getImport(dep)); const { a, getResult } = LineAppender();
});
}
generateType(definition: TypeDefinition): void { this.generateImports(a, definition);
const { a, getResult } = LineAppender();
this.generateImports(a, definition); a(0, ``);
a(0, `class ${definition.name} {`);
for (const field of definition.fields) {
if (field.array) {
a(1, `List<${toDartType(field.type)}>? ${field.name};`);
} else if (field.map) {
a(
1,
`Map<${toDartType(field.map)},${toDartType(field.type)}>? ${
field.name
};`
);
} else {
a(1, `${toDartType(field.type)}? ${field.name};`);
}
}
a(0, ``); a(0, ``);
a(0, `class ${definition.name} {`); a(
for (const field of definition.fields) { 1,
if (field.array) { `${definition.name}({${definition.fields
a(1, `List<${toDartType(field.type)}>? ${field.name};`); .map((e) => `this.${e.name}`)
} else if (field.map) { .join(", ")}});`
a(1, `Map<${toDartType(field.map)},${toDartType(field.type)}>? ${field.name};`); );
a(0, ``);
a(1, `${definition.name}.fromJson(Map<String, dynamic> json) {`);
for (const field of definition.fields) {
a(2, `if(json.containsKey("${field.name}")) {`);
const parseField = (value: string) => {
if (conversion[field.type]) {
return value;
} else { } else {
a(1, `${toDartType(field.type)}? ${field.name};`); return `${field.type}.fromJson(${value})`;
} }
} };
a(0, ``); if (field.array) {
a(1, `${definition.name}({${definition.fields.map(e => `this.${e.name}`).join(",")}});`); a(3, `this.${field.name} = [];`);
a(0, ``); a(3, `(json["${field.name}"] as List<dynamic>).forEach((e) => {`);
a(1, `${definition.name}.fromJson(Map<String, dynamic> json) {`); a(4, `this.${field.name}!.add(${parseField("e")})`);
for (const field of definition.fields) { a(3, `});`);
a(2, `if(json.containsKey("${field.name}")) {`); } else if (field.map) {
a(3, `this.${field.name} = {};`);
a(
3,
`(json["${field.name}"] as Map<${toDartType(
field.map
)},dynamic>).forEach((key, value) => {`
);
a(4, `this.${field.name}![key] = ${parseField("value")}`);
a(3, `});`);
} else {
a(
3,
`this.${field.name} = ${parseField(`json["${field.name}"]`)};`
);
}
a(2, `} else {`);
a(3, `this.${field.name} = null;`);
a(2, `}`);
a(0, ``);
}
const parseField = (value: string) => { a(1, `}`);
if (conversion[field.type]) {
return value;
} else {
return `${field.type}.fromJson(${value})`;
}
}
a(1, `Map<String, dynamic> toJson() {`);
a(2, `Map<String, dynamic> res = {};`);
for (const field of definition.fields) {
if (conversion[field.type]) {
a(2, `res["${field.name}"] = this.${field.name};`);
} else {
if (field.array) { if (field.array) {
a(3, `this.${field.name} = [];`); a(
a(3, `(json["${field.name}"] as List<dynamic>).forEach((e) => {`) 2,
a(4, `this.${field.name}!.add(${parseField("e")})`); `res["${field.name}"] = this.${field.name}?.map((entry) => entry.toJson()).toList();`
a(3, `});`) );
} else if (field.map) { } else if (field.map) {
a(3, `this.${field.name} = {};`); // dict.map((key, value) => MapEntry(key, value.toString()));
a(3, `(json["${field.name}"] as Map<${toDartType(field.map)},dynamic>).forEach((key, value) => {`) a(
a(4, `this.${field.name}![key] = ${parseField("value")}`); 2,
a(3, `});`) `res["${field.name}"] = this.${field.name}?.map((key, value) => MapEntry(key, value.toJson()));`
);
} else { } else {
a(3, `this.${field.name} = ${parseField(`json["${field.name}"]`)};`); a(2, `res["${field.name}"] = this.${field.name};`);
} }
a(2, `} else {`); }
a(3, `this.${field.name} = null;`); }
a(2, `}`); a(2, `return res;`);
a(0, ``); a(1, `}`);
}
a(1, `}`); a(0, `}`);
a(0, `}`);
this.writeFile(`lib/src/${definition.name}.dart`, getResult()); this.writeFile(`lib/src/${definition.name}.dart`, getResult());
} }
generateEnum(definition: EnumDefinition): void { generateEnum(definition: EnumDefinition): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender();
a(0, `enum ${definition.name} {`); a(0, `enum ${definition.name} {`);
for (const entry of definition.values) { for (const entry of definition.values) {
const isLast = definition.values[definition.values.length - 1] == entry; const isLast =
a(1, `${entry.name}(${entry.value})${isLast ? ";" : ","}`); definition.values[definition.values.length - 1] == entry;
a(1, `${entry.name}(${entry.value})${isLast ? ";" : ","}`);
}
a(0, ``);
a(1, `final int val;`);
a(1, `const ${definition.name}(int valT) : val= valT;`);
a(1, `static ${definition.name}? fromJson(int val) {`);
a(2, `switch(val){`);
for (const entry of definition.values) {
a(3, `case ${entry.value}:`);
a(4, `return ${definition.name}.${entry.name};`);
}
a(3, `default:`);
a(4, `return null;`);
a(2, `}`);
a(1, `}`);
} a(0, ``);
a(0, ``);
a(1, `final int val;`);
a(1, `const ${definition.name}(int valT) : val= valT;`);
a(1, `static ${definition.name}? fromJson(int val) {`);
a(2, `switch(val){`);
for (const entry of definition.values) {
a(3, `case ${entry.value}:`);
a(4, `return ${definition.name}.${entry.name};`);
}
a(3, `default:`);
a(4, `return null;`);
a(2, `}`);
a(1, `}`);
a(0, `}`);
a(0, ``); a(1, `int toJson() {`);
a(2, `return this.val;`);
a(1, `}`);
// a(0, `extension ${definition.name}Ext on ${definition.name} {`); a(0, ``);
// a(1, `int get val {`); a(0, `}`);
// a(2, `switch(this) {`);
// for(const entry of definition.values) {
// a(3, `case ${definition.name}.${entry.name}:`);
// a(4, `return ${entry.value};`);
// }
// a(3, `default:`);
// a(4, `return -double.maxFinite.toInt();`);
// a(2, `}`);
// a(1, `}`);
// a(0, `}`);
this.writeFile(`lib/src/${definition.name}.dart`, getResult()); a(0, ``);
}
generateService(definition: ServiceDefinition): void { this.writeFile(`lib/src/${definition.name}.dart`, getResult());
console.log( }
chalk.yellow("[DART] WARNING:"),
"DART support for services is not yet there. Service generation is skipped!"
);
}
finalize(steps: Step[]): void { generateService(definition: ServiceDefinition): void {
console.log(
chalk.yellow("[DART] WARNING:"),
"DART support for services is not yet there. Service generation is skipped!"
);
}
finalize(steps: Step[]): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender();
a(0, `library ${this.options.dart_library_name};`) a(0, `library ${this.options.dart_library_name};`)