Format some code

This commit is contained in:
Fabian Stamm 2022-12-11 22:45:56 +01:00
parent c6af431292
commit f301b583cb
2 changed files with 79 additions and 28 deletions

View File

@ -11,3 +11,5 @@ indent_size = 2
indent_size = 2 indent_size = 2
[*.zig] [*.zig]
indent_size = 4 indent_size = 4
[*.dart]
indent_size = 2

View File

@ -26,7 +26,9 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
} }
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!"
);
} }
} }
@ -35,7 +37,7 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
} }
generateImports(a: lineAppender, def: TypeDefinition | ServiceDefinition) { generateImports(a: lineAppender, def: TypeDefinition | ServiceDefinition) {
a(0, `import "./base.dart";`) a(0, `import "./base.dart";`);
def.depends.forEach((dep) => { def.depends.forEach((dep) => {
a(0, this.getImport(dep)); a(0, this.getImport(dep));
}); });
@ -50,7 +52,7 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
} }
generateType(definition: TypeDefinition): void { generateType(definition: TypeDefinition): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender(2);
this.generateImports(a, definition); this.generateImports(a, definition);
@ -62,7 +64,8 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
} else if (field.map) { } else if (field.map) {
a( a(
1, 1,
`Map<${toDartType(field.map)},${toDartType(field.type)}>? ${field.name `Map<${toDartType(field.map)},${toDartType(field.type)}>? ${
field.name
};` };`
); );
} else { } else {
@ -85,7 +88,10 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
if (field.array) { if (field.array) {
a(3, `this.${field.name} = [];`); a(3, `this.${field.name} = [];`);
a(3, `(json["${field.name}"] as List<dynamic>).forEach((e) => {`); a(3, `(json["${field.name}"] as List<dynamic>).forEach((e) => {`);
a(4, `this.${field.name}!.add(${this.getTypeParse(field.type, "e")})`); a(
4,
`this.${field.name}!.add(${this.getTypeParse(field.type, "e")})`
);
a(3, `});`); a(3, `});`);
} else if (field.map) { } else if (field.map) {
a(3, `this.${field.name} = {};`); a(3, `this.${field.name} = {};`);
@ -95,12 +101,21 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
field.map field.map
)},dynamic>).forEach((key, value) => {` )},dynamic>).forEach((key, value) => {`
); );
a(4, `this.${field.name}![key] = ${this.getTypeParse(field.type, "value")}`); a(
4,
`this.${field.name}![key] = ${this.getTypeParse(
field.type,
"value"
)}`
);
a(3, `});`); a(3, `});`);
} else { } else {
a( a(
3, 3,
`this.${field.name} = ${this.getTypeParse(field.type, `json["${field.name}"]`)};` `this.${field.name} = ${this.getTypeParse(
field.type,
`json["${field.name}"]`
)};`
); );
} }
a(2, `} else {`); a(2, `} else {`);
@ -142,7 +157,7 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
} }
generateEnum(definition: EnumDefinition): void { generateEnum(definition: EnumDefinition): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender(2);
a(0, `enum ${definition.name} {`); a(0, `enum ${definition.name} {`);
for (const entry of definition.values) { for (const entry of definition.values) {
@ -179,7 +194,7 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
} }
generateServiceClient(definition: ServiceDefinition): void { generateServiceClient(definition: ServiceDefinition): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender(2);
this.generateImports(a, definition); this.generateImports(a, definition);
a(0, `import "./service_client.dart";`); a(0, `import "./service_client.dart";`);
@ -187,30 +202,57 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
a(0, `class ${definition.name}Client extends Service {`); a(0, `class ${definition.name}Client extends Service {`);
a(0, ``); a(0, ``);
a(1, `${definition.name}Client(ServiceProvider provider):super(provider, "${definition.name}");`); a(
1,
`${definition.name}Client(ServiceProvider provider):super(provider, "${definition.name}");`
);
a(0, ``); a(0, ``);
for (const func of definition.functions) { for (const func of definition.functions) {
const args = func.inputs.map(inp => const args = func.inputs
(inp.array ? `List<${toDartType(inp.type)}>` : toDartType(inp.type)) + " " + inp.name .map(
).join(", "); (inp) =>
(inp.array
? `List<${toDartType(inp.type)}>`
: toDartType(inp.type)) +
" " +
inp.name
)
.join(", ");
const asParams = func.inputs.map(e => e.name).join(", "); const asParams = func.inputs.map((e) => e.name).join(", ");
if (!func.return) { if (!func.return) {
a(1, `void ${func.name}(${args}) {`) a(1, `void ${func.name}(${args}) {`);
a(2, `provider.sendNotification("${definition.name}.${func.name}", [${asParams}]);`); a(
2,
`provider.sendNotification("${definition.name}.${func.name}", [${asParams}]);`
);
a(1, `}`); a(1, `}`);
} else { } else {
const baseReturnType = func.return.type != "void" ? (toDartType(func.return.type) + "?") : toDartType(func.return.type); const baseReturnType =
const returnType = func.return.array ? `List<${baseReturnType}>` : baseReturnType; func.return.type != "void"
? toDartType(func.return.type) + "?"
: toDartType(func.return.type);
const returnType = func.return.array
? `List<${baseReturnType}>`
: baseReturnType;
a(1, `Future<${returnType}> ${func.name}(${args}) async {`); a(1, `Future<${returnType}> ${func.name}(${args}) async {`);
a(2, `var res = await this.provider.sendRequest("${definition.name}.${func.name}", [${asParams}]);`); a(
2,
`var res = await this.provider.sendRequest("${definition.name}.${func.name}", [${asParams}]);`
);
if (func.return.type !== "void") { if (func.return.type !== "void") {
if(func.return.array) { if (func.return.array) {
a(2, `return res.map((entry) =>${this.getTypeParse(func.return.type, "entry")}).toList();`); a(
2,
`return res.map((entry) =>${this.getTypeParse(
func.return.type,
"entry"
)}).toList();`
);
} else { } else {
a(2, `return ${this.getTypeParse(func.return.type, "res")};`); a(2, `return ${this.getTypeParse(func.return.type, "res")};`);
} }
@ -234,19 +276,20 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
generateService(definition: ServiceDefinition): void { generateService(definition: ServiceDefinition): void {
this.generateServiceClient(definition); this.generateServiceClient(definition);
this.writeFile("lib/src/service_client.dart", this.getTemplate("Dart/service_client.dart")) this.writeFile(
"lib/src/service_client.dart",
this.getTemplate("Dart/service_client.dart")
);
} }
finalize(steps: Step[]): void { finalize(steps: Step[]): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender(2);
a(0, `library ${this.options.dart_library_name};`) a(0, `library ${this.options.dart_library_name};`);
a(0, ``); a(0, ``);
let hasService = false; let hasService = false;
steps.forEach(([type, def]) => { steps.forEach(([type, def]) => {
switch (type) { switch (type) {
case "type": case "type":
@ -269,11 +312,17 @@ export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
}); });
if (hasService) { if (hasService) {
a(0, `export 'src/service_client.dart';`) a(0, `export 'src/service_client.dart';`);
} }
this.writeFile(`lib/${this.options.dart_library_name}.dart`, getResult()); this.writeFile(`lib/${this.options.dart_library_name}.dart`, getResult());
this.writeFile(`pubspec.yaml`, this.getTemplate("Dart/pubspec.yaml").replace("__NAME__", this.options.dart_library_name)); this.writeFile(
`pubspec.yaml`,
this.getTemplate("Dart/pubspec.yaml").replace(
"__NAME__",
this.options.dart_library_name
)
);
this.writeFile(`lib/src/base.dart`, this.getTemplate("Dart/base.dart")); this.writeFile(`lib/src/base.dart`, this.getTemplate("Dart/base.dart"));
} }
} }