Skip to content

Commit d4dc6d7

Browse files
committed
chore: work in progress dart
1 parent 734fbc2 commit d4dc6d7

File tree

5 files changed

+81
-35
lines changed

5 files changed

+81
-35
lines changed

src/SDK/Language/CLI.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,6 @@ public function getFiles(): array
186186
'destination' => 'lib/type-generation/attribute.js',
187187
'template' => 'cli/lib/type-generation/attribute.js.twig',
188188
],
189-
[
190-
'scope' => 'default',
191-
'destination' => 'lib/type-generation/collection.js',
192-
'template' => 'cli/lib/type-generation/collection.js.twig',
193-
],
194189
[
195190
'scope' => 'default',
196191
'destination' => 'lib/type-generation/languages/language.js',
@@ -206,11 +201,6 @@ public function getFiles(): array
206201
'destination' => 'lib/type-generation/languages/typescript.js',
207202
'template' => 'cli/lib/type-generation/languages/typescript.js.twig',
208203
],
209-
[
210-
'scope' => 'default',
211-
'destination' => 'lib/type-generation/languages/python.js',
212-
'template' => 'cli/lib/type-generation/languages/python.js.twig',
213-
],
214204
[
215205
'scope' => 'default',
216206
'destination' => 'lib/type-generation/languages/kotlin.js',
@@ -226,6 +216,11 @@ public function getFiles(): array
226216
'destination' => 'lib/type-generation/languages/java.js',
227217
'template' => 'cli/lib/type-generation/languages/java.js.twig',
228218
],
219+
[
220+
'scope' => 'default',
221+
'destination' => 'lib/type-generation/languages/dart.js',
222+
'template' => 'cli/lib/type-generation/languages/dart.js.twig',
223+
],
229224
[
230225
'scope' => 'default',
231226
'destination' => 'lib/questions.js',

templates/cli/lib/commands/types.js.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const { Python } = require("../type-generation/languages/python");
1111
const { Kotlin } = require("../type-generation/languages/kotlin");
1212
const { Swift } = require("../type-generation/languages/swift");
1313
const { Java } = require("../type-generation/languages/java");
14+
const { Dart } = require("../type-generation/languages/dart");
1415

1516
/**
1617
* @param {string} language
@@ -28,6 +29,8 @@ function createLanguageMeta(language) {
2829
return new Swift();
2930
case "java":
3031
return new Java();
32+
case "dart":
33+
return new Dart();
3134
default:
3235
throw new Error(`Language '${language}' is not supported`);
3336
}
@@ -50,7 +53,7 @@ const typesLanguageOption = new Option(
5053
"-l, --language <language>",
5154
"The language of the types"
5255
)
53-
.choices(["ts", "php", "python", "kotlin", "swift", "java"])
56+
.choices(["ts", "php", "kotlin", "swift", "java", "dart"])
5457
.default("auto");
5558

5659
const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {

templates/cli/lib/type-generation/attribute.js.twig

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ const AttributeType = {
1111
RELATIONSHIP: "relationship",
1212
};
1313

14-
/**
15-
* @typedef {Object} Attribute
16-
* @property {string} key - The unique identifier of the attribute.
17-
* @property {"string"|"integer"|"float"|"boolean"|"datetime"|"email"|"ip"|"url"|"enum"|"relationship"} type - The type of the attribute.
18-
* @property {string} status - The status of the attribute.
19-
* @property {boolean} required - The required status of the attribute.
20-
* @property {boolean} array - The array status of the attribute.
21-
* @property {number} size - The size of the attribute.
22-
* @property {string} default - The default value of the attribute.
23-
*/
24-
2514
module.exports = {
2615
AttributeType,
2716
};

templates/cli/lib/type-generation/collection.js.twig

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
case AttributeType.ENUM:
13+
type = "String";
14+
break;
15+
case AttributeType.INTEGER:
16+
type = "int";
17+
break;
18+
case AttributeType.FLOAT:
19+
type = "double";
20+
break;
21+
case AttributeType.BOOLEAN:
22+
type = "bool";
23+
break;
24+
case AttributeType.RELATIONSHIP:
25+
type = "Map<String, dynamic>";
26+
break;
27+
default:
28+
throw new Error(`Unknown attribute type: ${attribute.type}`);
29+
}
30+
if (attribute.array) {
31+
type = `List<${type}>`;
32+
}
33+
if (!attribute.required) {
34+
type += "?";
35+
}
36+
return type;
37+
}
38+
39+
getTemplate() {
40+
return `import 'package:appwrite/models.dart';
41+
42+
class <%- toPascalCase(collection.name) %> extends Document {
43+
<% for (const attribute of collection.attributes) { -%>
44+
final <%- getType(attribute) %> <%- toCamelCase(attribute.key) %>;
45+
<% } -%>
46+
47+
<%- toPascalCase(collection.name) %>({
48+
required String $id,
49+
required String $collectionId,
50+
required String $databaseId,
51+
required DateTime $createdAt,
52+
required DateTime $updatedAt,
53+
<% for (const attribute of collection.attributes) { -%>
54+
required this.<%- toCamelCase(attribute.key) %>,
55+
<% } -%>
56+
}) : super(
57+
$id: $id,
58+
$collectionId: $collectionId,
59+
$databaseId: $databaseId,
60+
$createdAt: $createdAt,
61+
$updatedAt: $updatedAt,
62+
);
63+
}
64+
`;
65+
}
66+
67+
getFileName(collection) {
68+
return LanguageMeta.toSnakeCase(collection.name) + ".dart";
69+
}
70+
}
71+
72+
module.exports = { Dart };

0 commit comments

Comments
 (0)