|
1 | 1 | /** @typedef {import('../attribute').Attribute} Attribute */
|
2 | 2 | const { AttributeType } = require('../attribute');
|
3 | 3 | const { LanguageMeta } = require("./language");
|
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
4 | 6 |
|
5 | 7 | 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 | + |
6 | 43 | getType(attribute) {
|
7 | 44 | let type = "";
|
8 | 45 | switch (attribute.type) {
|
@@ -46,42 +83,55 @@ class Dart extends LanguageMeta {
|
46 | 83 | }
|
47 | 84 |
|
48 | 85 | getTemplate() {
|
49 |
| - return `<% for (const attribute of collection.attributes) { -%> |
| 86 | + return `import 'package:${this.getPackageName()}/models.dart'; |
| 87 | +<% for (const attribute of collection.attributes) { -%> |
50 | 88 | <% if (attribute.type === 'relationship') { -%>
|
51 | 89 | import '<%- attribute.relatedCollection.toLowerCase() %>.dart';
|
52 | 90 |
|
53 | 91 | <% } -%>
|
54 | 92 | <% } -%>
|
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()}\`. |
59 | 95 |
|
60 | 96 | <% for (const attribute of collection.attributes) { -%>
|
61 | 97 | <% if (attribute.format === 'enum') { -%>
|
62 | 98 | enum <%- toPascalCase(attribute.key) %> {
|
63 | 99 | <% for (const [index, element] of Object.entries(attribute.elements)) { -%>
|
64 |
| - <%- toSnakeCase(element) %><% if (index < attribute.elements.length - 1) { %>,<% } %> |
| 100 | + <%- strict ? toCamelCase(element) : element %><% if (index < attribute.elements.length - 1) { %>,<% } %> |
65 | 101 | <% } -%>
|
66 | 102 | }
|
67 | 103 |
|
68 | 104 | <% } -%>
|
69 | 105 | <% } -%>
|
70 |
| -class <%= toPascalCase(collection.name) %> { |
| 106 | +class <%= toPascalCase(collection.name) %> extends Document { |
71 | 107 | <% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
|
72 |
| - <%- getType(attribute) %> <%= toCamelCase(attribute.key) %>; |
| 108 | + <%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>; |
73 | 109 | <% } -%>
|
74 | 110 |
|
75 | 111 | <%= 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, |
76 | 119 | <% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
|
77 |
| - <% if (attribute.required) { %>required <% } %>this.<%= toCamelCase(attribute.key) %><% if (index < collection.attributes.length - 1) { %>,<% } %> |
| 120 | + <% if (attribute.required) { %>required <% } %>this.<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (index < collection.attributes.length - 1) { %>,<% } %> |
78 | 121 | <% } -%>
|
79 | 122 | });
|
80 | 123 |
|
81 | 124 | factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
|
82 | 125 | 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, |
83 | 133 | <% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
|
84 |
| - <%= toCamelCase(attribute.key) %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%> |
| 134 | + <%= strict ? toCamelCase(attribute.key) : attribute.key %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%> |
85 | 135 | <% if (attribute.format === 'enum') { -%>
|
86 | 136 | <% if (attribute.array) { -%>
|
87 | 137 | (map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
|
@@ -130,21 +180,27 @@ map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollecti
|
130 | 180 |
|
131 | 181 | Map<String, dynamic> toMap() {
|
132 | 182 | return {
|
| 183 | + "\\$id": $id, |
| 184 | + "\\$collectionId": $collectionId, |
| 185 | + "\\$databaseId": $databaseId, |
| 186 | + "\\$createdAt": $createdAt, |
| 187 | + "\\$updatedAt": $updatedAt, |
| 188 | + "\\$permissions": $permissions, |
133 | 189 | <% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
|
134 | 190 | "<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
|
135 | 191 | <% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
|
136 |
| -<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%> |
| 192 | +<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%> |
137 | 193 | <% } else { -%>
|
138 |
| -<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%> |
| 194 | +<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%> |
139 | 195 | <% } -%>
|
140 | 196 | <% } else if (attribute.format === 'enum') { -%>
|
141 | 197 | <% if (attribute.array) { -%>
|
142 |
| -<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%> |
| 198 | +<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%> |
143 | 199 | <% } else { -%>
|
144 |
| -<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%> |
| 200 | +<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%> |
145 | 201 | <% } -%>
|
146 | 202 | <% } else { -%>
|
147 |
| -<%= toCamelCase(attribute.key) -%> |
| 203 | +<%= strict ? toCamelCase(attribute.key) : attribute.key -%> |
148 | 204 | <% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
|
149 | 205 | <% } -%>
|
150 | 206 | };
|
|
0 commit comments