1+ /** @typedef {import('../attribute').Attribute} Attribute */
2+ const { AttributeType } = require('../attribute');
3+ const { LanguageMeta } = require("./language");
4+
5+ class Dart extends LanguageMeta {
6+ getType(attribute) {
7+ let type = "";
8+ switch (attribute.type) {
9+ case AttributeType.STRING:
10+ case AttributeType.EMAIL:
11+ case AttributeType.DATETIME:
12+ type = "String";
13+ if (attribute.format === AttributeType.ENUM) {
14+ type = LanguageMeta.toPascalCase(attribute.key);
15+ }
16+ break;
17+ case AttributeType.INTEGER:
18+ type = "int";
19+ break;
20+ case AttributeType.FLOAT:
21+ type = "double";
22+ break;
23+ case AttributeType.BOOLEAN:
24+ type = "bool";
25+ break;
26+ case AttributeType.RELATIONSHIP:
27+ type = LanguageMeta.toPascalCase(attribute.relatedCollection);
28+ if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
29+ type = `List< ${type}>`;
30+ }
31+ break;
32+ default:
33+ throw new Error(`Unknown attribute type: ${attribute.type}`);
34+ }
35+ if (attribute.array) {
36+ type = `List< ${type}>`;
37+ }
38+ if (!attribute.required) {
39+ type += "?";
40+ }
41+ return type;
42+ }
43+
44+ getTemplate() {
45+ return `<% for (const attribute of collection.attributes) { -%>
46+ <% if (attribute.type === ' relationship' ) { -%>
47+ import '<% - attribute.relatedCollection.toLowerCase() %> .dart';
48+
49+ <% } -%>
50+ <% } -%>
51+ <% for (const attribute of collection.attributes) { -%>
52+ <% if (attribute.format === ' enum' ) { -%>
53+ enum <% - toPascalCase(attribute.key) %> {
54+ <% for (const element of attribute.elements) { -%>
55+ <% - element %> ,
56+ <% } -%>
57+ }
58+
59+ <% } -%>
60+ <% } -%>
61+ class <%= toPascalCase(collection.name) %> {
62+ <% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
63+ <% - getType(attribute) %> <%= toCamelCase(attribute.key) %> ;
64+ <% } -%>
65+
66+ <%= toPascalCase(collection.name) %> ({
67+ <% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
68+ <% if (attribute.required) { %> required <% } %> this.<%= toCamelCase(attribute.key) %> ,
69+ <% } -%>
70+ });
71+
72+ factory <%= toPascalCase(collection.name) %> .fromMap(Map<String , dynamic > map) {
73+ return <%= toPascalCase(collection.name) %> (
74+ <% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
75+ <%= toCamelCase(attribute.key) %> : <% if (attribute.type === ' string' || attribute.type === ' email' || attribute.type === ' datetime' ) { -%>
76+ <% if (attribute.format === ' enum' ) { -%>
77+ <% if (attribute.array) { -%>
78+ (map['<%= attribute.key %> '] as List<dynamic >?)?.map((e) => <% - toPascalCase(attribute.key) %> .values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
79+ <% } else { -%>
80+ <% if (!attribute.required) { -%>
81+ map['<%= attribute.key %> '] != null ? <% - toPascalCase(attribute.key) %> .values.where((e) => e.name == map['<%= attribute.key %> ']).firstOrNull : null<% } else { -%>
82+ <% - toPascalCase(attribute.key) %> .values.firstWhere((e) => e.name == map['<%= attribute.key %> '])<% } -%>
83+ <% } -%>
84+ <% } else { -%>
85+ <% if (attribute.array) { -%>
86+ List<String >.from(map['<%= attribute.key %> '] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
87+ <% } else { -%>
88+ map['<%= attribute.key %> ']<% if (!attribute.required) { %> ?<% } %> .toString()<% if (!attribute.required) { %> ?? null<% } -%>
89+ <% } -%>
90+ <% } -%>
91+ <% } else if (attribute.type === ' integer' ) { -%>
92+ <% if (attribute.array) { -%>
93+ List<int >.from(map['<%= attribute.key %> '] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
94+ <% } else { -%>
95+ map['<%= attribute.key %> ']<% if (!attribute.required) { %> ?? null<% } -%>
96+ <% } -%>
97+ <% } else if (attribute.type === ' float' ) { -%>
98+ <% if (attribute.array) { -%>
99+ List<double >.from(map['<%= attribute.key %> '] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
100+ <% } else { -%>
101+ map['<%= attribute.key %> ']<% if (!attribute.required) { %> ?? null<% } -%>
102+ <% } -%>
103+ <% } else if (attribute.type === ' boolean' ) { -%>
104+ <% if (attribute.array) { -%>
105+ List<bool >.from(map['<%= attribute.key %> '] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
106+ <% } else { -%>
107+ map['<%= attribute.key %> ']<% if (!attribute.required) { %> ?? null<% } -%>
108+ <% } -%>
109+ <% } else if (attribute.type === ' relationship' ) { -%>
110+ <% if ((attribute.relationType === ' oneToMany' && attribute.side === ' parent' ) || (attribute.relationType === ' manyToOne' && attribute.side === ' child' ) || attribute.relationType === ' manyToMany' ) { -%>
111+ (map['<%= attribute.key %> '] as List<dynamic >?)?.map((e) => <% - toPascalCase(attribute.relatedCollection) %> .fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
112+ <% } else { -%>
113+ <% if (!attribute.required) { -%>
114+ map['<%= attribute.key %> '] != null ? <% - toPascalCase(attribute.relatedCollection) %> .fromMap(map['<%= attribute.key %> ']) : null<% } else { -%>
115+ <% - toPascalCase(attribute.relatedCollection) %> .fromMap(map['<%= attribute.key %> '])<% } -%>
116+ <% } -%>
117+ <% } -%> ,
118+ <% } -%>
119+ );
120+ }
121+
122+ Map<String , dynamic > toMap() {
123+ return {
124+ <% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
125+ "<%= attribute.key %> ": <% if (attribute.type === ' relationship' ) { -%>
126+ <% if ((attribute.relationType === ' oneToMany' && attribute.side === ' parent' ) || (attribute.relationType === ' manyToOne' && attribute.side === ' child' ) || attribute.relationType === ' manyToMany' ) { -%>
127+ <%= toCamelCase(attribute.key) %><% if (!attribute.required) { %> ?<% } %> .map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%>
128+ <% } else { -%>
129+ <%= toCamelCase(attribute.key) %><% if (!attribute.required) { %> ?<% } %> .toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
130+ <% } -%>
131+ <% } else if (attribute.format === ' enum' ) { -%>
132+ <% if (attribute.array) { -%>
133+ <%= toCamelCase(attribute.key) %><% if (!attribute.required) { %> ?<% } %> .map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%>
134+ <% } else { -%>
135+ <%= toCamelCase(attribute.key) %><% if (!attribute.required) { %> ?<% } %> .name<% if (!attribute.required) { %> ?? null<% } -%>
136+ <% } -%>
137+ <% } else { -%>
138+ <%= toCamelCase(attribute.key) -%>
139+ <% } -%> ,
140+ <% } -%>
141+ };
142+ }
143+ }
144+ `;
145+ }
146+
147+ getFileName(collection) {
148+ return LanguageMeta.toSnakeCase(collection.name) + ".dart";
149+ }
150+ }
151+
152+ module.exports = { Dart };
0 commit comments