Skip to content

Commit d69be8d

Browse files
authored
Merge pull request #1197 from Fellmonkey/CLI-Dart-model-generation-fixes
2 parents bad2096 + 89bb185 commit d69be8d

File tree

1 file changed

+50
-53
lines changed

1 file changed

+50
-53
lines changed

templates/cli/lib/type-generation/languages/dart.js.twig

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -83,104 +83,101 @@ class Dart extends LanguageMeta {
8383
}
8484

8585
getTemplate() {
86-
return `<% for (const attribute of collection.attributes) { -%>
87-
<% if (attribute.type === 'relationship') { -%>
88-
import '<%- toSnakeCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.dart';
89-
90-
<% } -%>
91-
<% } -%>
92-
/// This file is auto-generated by the Appwrite CLI.
93-
/// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
94-
95-
<% for (const attribute of collection.attributes) { -%>
96-
<% if (attribute.format === 'enum') { -%>
86+
return `// This file is auto-generated by the Appwrite CLI.
87+
// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
88+
<% const __relatedImportsSeen = new Set();
89+
const sortedAttributes = collection.attributes.slice().sort((a, b) => {
90+
if (a.required === b.required) return 0;
91+
return a.required ? -1 : 1;
92+
}); -%>
93+
<% const __attrs = sortedAttributes; -%>
94+
<% for (const attribute of __attrs) { -%>
95+
<% if (attribute.type === '${AttributeType.RELATIONSHIP}') { -%>
96+
<% const related = collections.find(c => c.$id === attribute.relatedCollection); -%>
97+
<% if (related && !__relatedImportsSeen.has(toSnakeCase(related.name))) { -%>
98+
import '<%- toSnakeCase(related.name) %>.dart';
99+
<% __relatedImportsSeen.add(toSnakeCase(related.name)); -%>
100+
<% } -%>
101+
<% } -%>
102+
<% } -%>
103+
104+
<% for (const attribute of __attrs) { -%>
105+
<% if (attribute.format === '${AttributeType.ENUM}') { -%>
97106
enum <%- toPascalCase(attribute.key) %> {
98107
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
99-
<%- strict ? toCamelCase(element) : element %><% if (index < attribute.elements.length - 1) { %>,<% } %>
108+
<%- strict ? toCamelCase(element) : element %><% if (index < attribute.elements.length - 1) { -%>,<% } %>
100109
<% } -%>
101110
}
102111

103112
<% } -%>
104113
<% } -%>
105114
class <%= toPascalCase(collection.name) %> {
106-
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
115+
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
107116
<%- getType(attribute, collections) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
108117
<% } -%>
109118

110119
<%= toPascalCase(collection.name) %>({
111-
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
112-
<% if (attribute.required) { %>required <% } %>this.<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (index < collection.attributes.length - 1) { %>,<% } %>
120+
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
121+
<% if (attribute.required) { %>required <% } %>this.<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (index < __attrs.length - 1) { -%>,<% } %>
113122
<% } -%>
114123
});
115124

116125
factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
117126
return <%= toPascalCase(collection.name) %>(
118-
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
119-
<%= strict ? toCamelCase(attribute.key) : attribute.key %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%>
120-
<% if (attribute.format === 'enum') { -%>
127+
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
128+
<%= strict ? toCamelCase(attribute.key) : attribute.key %>: <% if (attribute.type === '${AttributeType.STRING}' || attribute.type === '${AttributeType.EMAIL}' || attribute.type === '${AttributeType.DATETIME}') { -%>
129+
<% if (attribute.format === '${AttributeType.ENUM}') { -%>
121130
<% if (attribute.array) { -%>
122-
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
123-
<% } else { -%>
131+
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% } else { -%>
124132
<% if (!attribute.required) { -%>
125133
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.key) %>.values.where((e) => e.name == map['<%= attribute.key %>']).firstOrNull : null<% } else { -%>
126134
<%- toPascalCase(attribute.key) %>.values.firstWhere((e) => e.name == map['<%= attribute.key %>'])<% } -%>
127135
<% } -%>
128136
<% } else { -%>
129137
<% if (attribute.array) { -%>
130-
List<String>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
131-
<% } else { -%>
132-
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% if (!attribute.required) { %> ?? null<% } -%>
138+
List<String>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
139+
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% } -%>
133140
<% } -%>
134-
<% } -%>
135-
<% } else if (attribute.type === 'integer') { -%>
141+
<% } else if (attribute.type === '${AttributeType.INTEGER}') { -%>
136142
<% if (attribute.array) { -%>
137-
List<int>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
138-
<% } else { -%>
139-
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
140-
<% } -%>
141-
<% } else if (attribute.type === 'float') { -%>
143+
List<int>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
144+
map['<%= attribute.key %>']<% } -%>
145+
<% } else if (attribute.type === '${AttributeType.FLOAT}') { -%>
142146
<% if (attribute.array) { -%>
143-
List<double>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
144-
<% } else { -%>
145-
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
146-
<% } -%>
147-
<% } else if (attribute.type === 'boolean') { -%>
147+
List<double>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
148+
map['<%= attribute.key %>']<% } -%>
149+
<% } else if (attribute.type === '${AttributeType.BOOLEAN}') { -%>
148150
<% if (attribute.array) { -%>
149-
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
150-
<% } else { -%>
151-
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
152-
<% } -%>
153-
<% } else if (attribute.type === 'relationship') { -%>
151+
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
152+
map['<%= attribute.key %>']<% } -%>
153+
<% } else if (attribute.type === '${AttributeType.RELATIONSHIP}') { -%>
154154
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
155-
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
155+
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(e)).toList()
156156
<% } else { -%>
157157
<% if (!attribute.required) { -%>
158158
map['<%= attribute.key %>'] != null ? <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%>
159159
<%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(map['<%= attribute.key %>'])<% } -%>
160160
<% } -%>
161-
<% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
161+
<% } -%><% if (index < __attrs.length - 1) { -%>,<% } %>
162162
<% } -%>
163163
);
164164
}
165165

166166
Map<String, dynamic> toMap() {
167167
return {
168-
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
169-
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
168+
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
169+
'<%= attribute.key %>': <% if (attribute.type === '${AttributeType.RELATIONSHIP}') { -%>
170170
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
171-
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%>
171+
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()
172172
<% } else { -%>
173-
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
174-
<% } -%>
175-
<% } else if (attribute.format === 'enum') { -%>
173+
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.toMap()<% } -%>
174+
<% } else if (attribute.format === '${AttributeType.ENUM}') { -%>
176175
<% if (attribute.array) { -%>
177-
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%>
178-
<% } else { -%>
179-
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%>
180-
<% } -%>
176+
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% } else { -%>
177+
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.name<% } -%>
181178
<% } else { -%>
182179
<%= strict ? toCamelCase(attribute.key) : attribute.key -%>
183-
<% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
180+
<% } -%><% if (index < __attrs.length - 1) { -%>,<% } %>
184181
<% } -%>
185182
};
186183
}

0 commit comments

Comments
 (0)