Skip to content

Commit 9909be4

Browse files
committed
fix: type generation for dart
1 parent e1c4feb commit 9909be4

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

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

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
11
/** @typedef {import('../attribute').Attribute} Attribute */
22
const { AttributeType } = require('../attribute');
33
const { LanguageMeta } = require("./language");
4+
const fs = require('fs');
5+
const path = require('path');
46

57
class Dart extends LanguageMeta {
8+
getPackageName() {
9+
const pubspecPath = path.join(this.getCurrentDirectory(), 'pubspec.yaml');
10+
if (fs.existsSync(pubspecPath)) {
11+
const pubspecContent = fs.readFileSync(pubspecPath, 'utf8');
12+
const lines = pubspecContent.split('\n');
13+
14+
const dependenciesIndex = lines.findIndex(line => line.trim() === 'dependencies:');
15+
16+
if (dependenciesIndex !== -1) {
17+
const indent = lines[dependenciesIndex].search(/\S|$/);
18+
const dependencies = [];
19+
for (let i = dependenciesIndex + 1; i < lines.length; i++) {
20+
const line = lines[i];
21+
if (line.trim() === '') continue;
22+
23+
const lineIndent = line.search(/\S|$/);
24+
if (lineIndent <= indent && line.trim() !== '') {
25+
break;
26+
}
27+
28+
dependencies.push(line.trim());
29+
}
30+
31+
if (dependencies.some(dep => dep.startsWith('dart_appwrite:'))) {
32+
return 'dart_appwrite';
33+
}
34+
if (dependencies.some(dep => dep.startsWith('appwrite:'))) {
35+
return 'appwrite';
36+
}
37+
}
38+
}
39+
40+
return 'appwrite';
41+
}
42+
643
getType(attribute) {
744
let type = "";
845
switch (attribute.type) {
@@ -46,16 +83,15 @@ class Dart extends LanguageMeta {
4683
}
4784

4885
getTemplate() {
49-
return `<% for (const attribute of collection.attributes) { -%>
86+
return `import 'package:${this.getPackageName()}/models.dart';
87+
<% for (const attribute of collection.attributes) { -%>
5088
<% if (attribute.type === 'relationship') { -%>
5189
import '<%- attribute.relatedCollection.toLowerCase() %>.dart';
5290

5391
<% } -%>
5492
<% } -%>
55-
/**
56-
* This file is auto-generated by the Appwrite CLI.
57-
* You can regenerate it by running \`appwrite types -l dart ${this.getCurrentDirectory()}\`.
58-
*/
93+
/// This file is auto-generated by the Appwrite CLI.
94+
/// You can regenerate it by running \`appwrite types -l dart ${this.getCurrentDirectory()}\`.
5995

6096
<% for (const attribute of collection.attributes) { -%>
6197
<% if (attribute.format === 'enum') { -%>
@@ -67,19 +103,33 @@ enum <%- toPascalCase(attribute.key) %> {
67103

68104
<% } -%>
69105
<% } -%>
70-
class <%= toPascalCase(collection.name) %> {
106+
class <%= toPascalCase(collection.name) %> extends Document {
71107
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
72108
<%- getType(attribute) %> <%= toCamelCase(attribute.key) %>;
73109
<% } -%>
74110

75111
<%= toPascalCase(collection.name) %>({
112+
required super.$id,
113+
required super.$collectionId,
114+
required super.$databaseId,
115+
required super.$createdAt,
116+
required super.$updatedAt,
117+
required super.$permissions,
118+
required super.data,
76119
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
77120
<% if (attribute.required) { %>required <% } %>this.<%= toCamelCase(attribute.key) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
78121
<% } -%>
79122
});
80123

81124
factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
82125
return <%= toPascalCase(collection.name) %>(
126+
$id: map['\\$id'].toString(),
127+
$collectionId: map['\\$collectionId'].toString(),
128+
$databaseId: map['\\$databaseId'].toString(),
129+
$createdAt: map['\\$createdAt'].toString(),
130+
$updatedAt: map['\\$updatedAt'].toString(),
131+
$permissions: List<String>.from(map['\\$permissions'] ?? []),
132+
data: map,
83133
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
84134
<%= toCamelCase(attribute.key) %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%>
85135
<% if (attribute.format === 'enum') { -%>
@@ -130,6 +180,12 @@ map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollecti
130180

131181
Map<String, dynamic> toMap() {
132182
return {
183+
"\\$id": $id,
184+
"\\$collectionId": $collectionId,
185+
"\\$databaseId": $databaseId,
186+
"\\$createdAt": $createdAt,
187+
"\\$updatedAt": $updatedAt,
188+
"\\$permissions": $permissions,
133189
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
134190
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
135191
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>

0 commit comments

Comments
 (0)