Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 50 additions & 53 deletions templates/cli/lib/type-generation/languages/dart.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -83,104 +83,101 @@ class Dart extends LanguageMeta {
}

getTemplate() {
return `<% for (const attribute of collection.attributes) { -%>
<% if (attribute.type === 'relationship') { -%>
import '<%- toSnakeCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.dart';

<% } -%>
<% } -%>
/// This file is auto-generated by the Appwrite CLI.
/// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.

<% for (const attribute of collection.attributes) { -%>
<% if (attribute.format === 'enum') { -%>
return `// This file is auto-generated by the Appwrite CLI.
// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
<% const __relatedImportsSeen = new Set();
const sortedAttributes = collection.attributes.slice().sort((a, b) => {
if (a.required === b.required) return 0;
return a.required ? -1 : 1;
}); -%>
<% const __attrs = sortedAttributes; -%>
<% for (const attribute of __attrs) { -%>
<% if (attribute.type === '${AttributeType.RELATIONSHIP}') { -%>
<% const related = collections.find(c => c.$id === attribute.relatedCollection); -%>
<% if (related && !__relatedImportsSeen.has(toSnakeCase(related.name))) { -%>
import '<%- toSnakeCase(related.name) %>.dart';
<% __relatedImportsSeen.add(toSnakeCase(related.name)); -%>
<% } -%>
<% } -%>
<% } -%>

<% for (const attribute of __attrs) { -%>
<% if (attribute.format === '${AttributeType.ENUM}') { -%>
enum <%- toPascalCase(attribute.key) %> {
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
<%- strict ? toCamelCase(element) : element %><% if (index < attribute.elements.length - 1) { %>,<% } %>
<%- strict ? toCamelCase(element) : element %><% if (index < attribute.elements.length - 1) { -%>,<% } %>
<% } -%>
}

<% } -%>
<% } -%>
class <%= toPascalCase(collection.name) %> {
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
<%- getType(attribute, collections) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
<% } -%>

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

factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
return <%= toPascalCase(collection.name) %>(
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%>
<% if (attribute.format === 'enum') { -%>
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %>: <% if (attribute.type === '${AttributeType.STRING}' || attribute.type === '${AttributeType.EMAIL}' || attribute.type === '${AttributeType.DATETIME}') { -%>
<% if (attribute.format === '${AttributeType.ENUM}') { -%>
<% if (attribute.array) { -%>
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% } else { -%>
<% if (!attribute.required) { -%>
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.key) %>.values.where((e) => e.name == map['<%= attribute.key %>']).firstOrNull : null<% } else { -%>
<%- toPascalCase(attribute.key) %>.values.firstWhere((e) => e.name == map['<%= attribute.key %>'])<% } -%>
<% } -%>
<% } else { -%>
<% if (attribute.array) { -%>
List<String>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% if (!attribute.required) { %> ?? null<% } -%>
List<String>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% } -%>
<% } -%>
<% } -%>
<% } else if (attribute.type === 'integer') { -%>
<% } else if (attribute.type === '${AttributeType.INTEGER}') { -%>
<% if (attribute.array) { -%>
List<int>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else if (attribute.type === 'float') { -%>
List<int>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
map['<%= attribute.key %>']<% } -%>
<% } else if (attribute.type === '${AttributeType.FLOAT}') { -%>
<% if (attribute.array) { -%>
List<double>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else if (attribute.type === 'boolean') { -%>
List<double>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
map['<%= attribute.key %>']<% } -%>
<% } else if (attribute.type === '${AttributeType.BOOLEAN}') { -%>
<% if (attribute.array) { -%>
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else if (attribute.type === 'relationship') { -%>
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% } else { -%>
map['<%= attribute.key %>']<% } -%>
<% } else if (attribute.type === '${AttributeType.RELATIONSHIP}') { -%>
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(e)).toList()
<% } else { -%>
<% if (!attribute.required) { -%>
map['<%= attribute.key %>'] != null ? <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%>
<%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(map['<%= attribute.key %>'])<% } -%>
<% } -%>
<% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
<% } -%><% if (index < __attrs.length - 1) { -%>,<% } %>
<% } -%>
);
}

Map<String, dynamic> toMap() {
return {
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
<% for (const [index, attribute] of Object.entries(__attrs)) { -%>
'<%= attribute.key %>': <% if (attribute.type === '${AttributeType.RELATIONSHIP}') { -%>
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()
<% } else { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
<% } -%>
<% } else if (attribute.format === 'enum') { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.toMap()<% } -%>
<% } else if (attribute.format === '${AttributeType.ENUM}') { -%>
<% if (attribute.array) { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% } else { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.name<% } -%>
<% } else { -%>
<%= strict ? toCamelCase(attribute.key) : attribute.key -%>
<% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
<% } -%><% if (index < __attrs.length - 1) { -%>,<% } %>
<% } -%>
};
}
Expand Down