jahshd
This commit is contained in:
parent
68af1ff442
commit
e23dd6a6fb
@ -31,7 +31,6 @@ export class DartTarget extends CompileTarget<{}> {
|
||||
}
|
||||
|
||||
generateImports(a: lineAppender, def: TypeDefinition | ServiceDefinition) {
|
||||
|
||||
def.depends.forEach((dep) => {
|
||||
a(0, this.getImport(dep));
|
||||
});
|
||||
@ -48,14 +47,24 @@ export class DartTarget extends CompileTarget<{}> {
|
||||
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};`);
|
||||
a(
|
||||
1,
|
||||
`Map<${toDartType(field.map)},${toDartType(field.type)}>? ${
|
||||
field.name
|
||||
};`
|
||||
);
|
||||
} else {
|
||||
a(1, `${toDartType(field.type)}? ${field.name};`);
|
||||
}
|
||||
}
|
||||
|
||||
a(0, ``);
|
||||
a(1, `${definition.name}(${definition.fields.map(e => `this.${e.name}`).join(",")});`);
|
||||
a(
|
||||
1,
|
||||
`${definition.name}(${definition.fields
|
||||
.map((e) => `this.${e.name}`)
|
||||
.join(", ")});`
|
||||
);
|
||||
a(0, ``);
|
||||
a(1, `${definition.name}.fromJson(Map<String, dynamic> json) {`);
|
||||
for (const field of definition.fields) {
|
||||
@ -67,20 +76,28 @@ export class DartTarget extends CompileTarget<{}> {
|
||||
} else {
|
||||
return `${field.type}.fromJson(${value})`;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (field.array) {
|
||||
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(${parseField("e")})`);
|
||||
a(3, `});`)
|
||||
a(3, `});`);
|
||||
} else if (field.map) {
|
||||
a(3, `this.${field.name} = {};`);
|
||||
a(3, `(json["${field.name}"] as Map<${toDartType(field.map)},dynamic>).forEach((key, value) => {`)
|
||||
a(
|
||||
3,
|
||||
`(json["${field.name}"] as Map<${toDartType(
|
||||
field.map
|
||||
)},dynamic>).forEach((key, value) => {`
|
||||
);
|
||||
a(4, `this.${field.name}![key] = ${parseField("value")}`);
|
||||
a(3, `});`)
|
||||
a(3, `});`);
|
||||
} else {
|
||||
a(3, `this.${field.name} = ${parseField(`json["${field.name}"]`)};`);
|
||||
a(
|
||||
3,
|
||||
`this.${field.name} = ${parseField(`json["${field.name}"]`)};`
|
||||
);
|
||||
}
|
||||
a(2, `} else {`);
|
||||
a(3, `this.${field.name} = null;`);
|
||||
@ -89,6 +106,32 @@ export class DartTarget extends CompileTarget<{}> {
|
||||
}
|
||||
|
||||
a(1, `}`);
|
||||
|
||||
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) {
|
||||
a(
|
||||
2,
|
||||
`res["${field.name}"] = this.${field.name}?.map((entry) => entry.toJson()).toList();`
|
||||
);
|
||||
} else if (field.map) {
|
||||
// dict.map((key, value) => MapEntry(key, value.toString()));
|
||||
a(
|
||||
2,
|
||||
`res["${field.name}"] = this.${field.name}?.map((key, value) => MapEntry(key, value.toJson()));`
|
||||
);
|
||||
} else {
|
||||
a(2, `res["${field.name}"] = this.${field.name};`);
|
||||
}
|
||||
}
|
||||
}
|
||||
a(2, `return res;`);
|
||||
a(1, `}`);
|
||||
|
||||
a(0, `}`);
|
||||
|
||||
this.writeFile(`${definition.name}.dart`, getResult());
|
||||
@ -99,9 +142,9 @@ export class DartTarget extends CompileTarget<{}> {
|
||||
|
||||
a(0, `enum ${definition.name} {`);
|
||||
for (const entry of definition.values) {
|
||||
const isLast = definition.values[definition.values.length - 1] == entry;
|
||||
const isLast =
|
||||
definition.values[definition.values.length - 1] == entry;
|
||||
a(1, `${entry.name}(${entry.value})${isLast ? ";" : ","}`);
|
||||
|
||||
}
|
||||
a(0, ``);
|
||||
a(1, `final int val;`);
|
||||
@ -114,23 +157,19 @@ export class DartTarget extends CompileTarget<{}> {
|
||||
}
|
||||
a(3, `default:`);
|
||||
a(4, `return null;`);
|
||||
a(2, `}`);
|
||||
a(1, `}`);
|
||||
a(0, `}`);
|
||||
|
||||
a(0, ``);
|
||||
|
||||
// a(0, `extension ${definition.name}Ext on ${definition.name} {`);
|
||||
// a(1, `int get val {`);
|
||||
// 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, `}`);
|
||||
a(1, `int toJson() {`);
|
||||
a(2, `return this.val;`);
|
||||
a(1, `}`);
|
||||
|
||||
a(0, ``);
|
||||
a(0, `}`);
|
||||
|
||||
a(0, ``);
|
||||
|
||||
this.writeFile(`${definition.name}.dart`, getResult());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user