Skip to content

Commit 0b01a39

Browse files
authored
Merge pull request #1110 from appwrite/pla-3213
chore: type generation fixes + improvements
2 parents 02a7e0d + bc38be7 commit 0b01a39

File tree

8 files changed

+109
-130
lines changed

8 files changed

+109
-130
lines changed

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ class Dart extends LanguageMeta {
8383
}
8484

8585
getTemplate() {
86-
return `import 'package:${this.getPackageName()}/models.dart';
87-
<% for (const attribute of collection.attributes) { -%>
86+
return `<% for (const attribute of collection.attributes) { -%>
8887
<% if (attribute.type === 'relationship') { -%>
89-
import '<%- attribute.relatedCollection.toLowerCase() %>.dart';
88+
import '<%- toSnakeCase(attribute.relatedCollection) %>.dart';
9089

9190
<% } -%>
9291
<% } -%>
@@ -103,33 +102,19 @@ enum <%- toPascalCase(attribute.key) %> {
103102

104103
<% } -%>
105104
<% } -%>
106-
class <%= toPascalCase(collection.name) %> extends Document {
105+
class <%= toPascalCase(collection.name) %> {
107106
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
108107
<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
109108
<% } -%>
110109

111110
<%= 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,
119111
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
120112
<% if (attribute.required) { %>required <% } %>this.<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (index < collection.attributes.length - 1) { %>,<% } %>
121113
<% } -%>
122114
});
123115

124116
factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
125117
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,
133118
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
134119
<%= strict ? toCamelCase(attribute.key) : attribute.key %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%>
135120
<% if (attribute.format === 'enum') { -%>
@@ -180,12 +165,6 @@ map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollecti
180165

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

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Java extends LanguageMeta {
5050
* You can regenerate it by running \`appwrite types -l java ${this.getCurrentDirectory()}\`.
5151
*/
5252

53-
import java.util.*;
53+
import java.util.Objects;
5454
<% for (const attribute of collection.attributes) { -%>
5555
<% if (attribute.type === 'relationship') { -%>
5656
import io.appwrite.models.<%- toPascalCase(attribute.relatedCollection) %>;
@@ -61,63 +61,63 @@ public class <%- toPascalCase(collection.name) %> {
6161
<% for (const attribute of collection.attributes) { -%>
6262
<% if (attribute.format === 'enum') { -%>
6363

64-
public enum <%- toPascalCase(attribute.key) %> {
64+
public enum <%- toPascalCase(attribute.key) %> {
6565
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
66-
<%- strict ? toSnakeCase(element) : element %><%- index < attribute.elements.length - 1 ? ',' : ';' %>
66+
<%- strict ? toUpperSnakeCase(element) : element %><%- index < attribute.elements.length - 1 ? ',' : ';' %>
6767
<% } -%>
68-
}
68+
}
6969

7070
<% } -%>
7171
<% } -%>
7272
<% for (const attribute of collection.attributes) { -%>
73-
private <%- getType(attribute) %> <%- strict ? toCamelCase(attribute.key) : attribute.key %>;
73+
private <%- getType(attribute) %> <%- strict ? toCamelCase(attribute.key) : attribute.key %>;
7474
<% } -%>
7575

76-
public <%- toPascalCase(collection.name) %>() {
77-
}
76+
public <%- toPascalCase(collection.name) %>() {
77+
}
7878

79-
public <%- toPascalCase(collection.name) %>(
79+
public <%- toPascalCase(collection.name) %>(
8080
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
81-
<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %><%- index < collection.attributes.length - 1 ? ',' : '' %>
81+
<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %><%- index < collection.attributes.length - 1 ? ',' : '' %>
8282
<% } -%>
83-
) {
83+
) {
8484
<% for (const attribute of collection.attributes) { -%>
85-
this.<%= strict ? toCamelCase(attribute.key) : attribute.key %> = <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
85+
this.<%= strict ? toCamelCase(attribute.key) : attribute.key %> = <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
8686
<% } -%>
87-
}
87+
}
8888

8989
<% for (const attribute of collection.attributes) { -%>
90-
public <%- getType(attribute) %> get<%- toPascalCase(attribute.key) %>() {
91-
return <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
92-
}
90+
public <%- getType(attribute) %> get<%- toPascalCase(attribute.key) %>() {
91+
return <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
92+
}
9393

94-
public void set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>) {
95-
this.<%= strict ? toCamelCase(attribute.key) : attribute.key %> = <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
96-
}
94+
public void set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>) {
95+
this.<%= strict ? toCamelCase(attribute.key) : attribute.key %> = <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
96+
}
9797

9898
<% } -%>
99-
@Override
100-
public boolean equals(Object obj) {
101-
if (this == obj) return true;
102-
if (obj == null || getClass() != obj.getClass()) return false;
103-
<%- toPascalCase(collection.name) %> that = (<%- toPascalCase(collection.name) %>) obj;
104-
return <% collection.attributes.forEach((attr, index) => { %>Objects.equals(<%= toCamelCase(attr.key) %>, that.<%= toCamelCase(attr.key) %>)<% if (index < collection.attributes.length - 1) { %> &&
99+
@Override
100+
public boolean equals(Object obj) {
101+
if (this == obj) return true;
102+
if (obj == null || getClass() != obj.getClass()) return false;
103+
<%- toPascalCase(collection.name) %> that = (<%- toPascalCase(collection.name) %>) obj;
104+
return <% collection.attributes.forEach((attr, index) => { %>Objects.equals(<%= toCamelCase(attr.key) %>, that.<%= toCamelCase(attr.key) %>)<% if (index < collection.attributes.length - 1) { %> &&
105105
<% } }); %>;
106-
}
106+
}
107107

108-
@Override
109-
public int hashCode() {
110-
return Objects.hash(<%= collection.attributes.map(attr => toCamelCase(attr.key)).join(', ') %>);
111-
}
108+
@Override
109+
public int hashCode() {
110+
return Objects.hash(<%= collection.attributes.map(attr => toCamelCase(attr.key)).join(', ') %>);
111+
}
112112

113-
@Override
114-
public String toString() {
115-
return "<%- toPascalCase(collection.name) %>{" +
116-
<% for (const attribute of collection.attributes) { -%>
117-
"<%= strict ? toCamelCase(attribute.key) : attribute.key %>=" + <%= strict ? toCamelCase(attribute.key) : attribute.key %> +
113+
@Override
114+
public String toString() {
115+
return "<%- toPascalCase(collection.name) %>{" +
116+
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
117+
"<%= strict ? toCamelCase(attribute.key) : attribute.key %>=" + <%= strict ? toCamelCase(attribute.key) : attribute.key %> +<% if (index < collection.attributes.length - 1) { %> ", " +<% } %>
118118
<% } -%>
119-
'}';
120-
}
119+
'}';
120+
}
121121
}
122122
`;
123123
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class JavaScript extends LanguageMeta {
4141
type += "[]";
4242
}
4343
if (!attribute.required && attribute.default === null) {
44-
type += "|null";
44+
type += " | null";
4545
}
4646
return type;
4747
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ import <%- toPascalCase(attribute.relatedCollection) %>
6363
<% if (attribute.format === 'enum') { -%>
6464
enum class <%- toPascalCase(attribute.key) %> {
6565
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
66-
<%- strict ? toUpperSnakeCase(element) : element %><%- index < attribute.elements.length - 1 ? ',' : '' %>
66+
<%- strict ? toUpperSnakeCase(element) : element %><%- index < attribute.elements.length - 1 ? ',' : '' %>
6767
<% } -%>
6868
}
6969

7070
<% } -%>
7171
<% } -%>
7272
data class <%- toPascalCase(collection.name) %>(
7373
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
74-
val <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
74+
val <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
7575
<% } -%>
7676
)`;
7777
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,39 +62,39 @@ use Appwrite\\Models\\<%- toPascalCase(attribute.relatedCollection) %>;
6262
<% if (attribute.format === 'enum') { -%>
6363
enum <%- toPascalCase(attribute.key) %>: string {
6464
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
65-
case <%- strict ? toUpperSnakeCase(element) : element %> = '<%- element %>';
65+
case <%- strict ? toUpperSnakeCase(element) : element %> = '<%- element %>';
6666
<% } -%>
6767
}
6868
6969
<% } -%>
7070
<% } -%>
7171
class <%- toPascalCase(collection.name) %> {
7272
<% for (const attribute of collection.attributes ){ -%>
73-
private <%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
73+
private <%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
7474
<% } -%>
7575
76-
public function __construct(
76+
public function __construct(
7777
<% for (const attribute of collection.attributes ){ -%>
7878
<% if (attribute.required) { -%>
79-
<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %><% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
79+
<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %><% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
8080
<% } else { -%>
81-
?<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %> = null<% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
81+
?<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %> = null<% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
8282
<% } -%>
8383
<% } -%>
84-
) {
84+
) {
8585
<% for (const attribute of collection.attributes ){ -%>
86-
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
86+
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
8787
<% } -%>
88-
}
88+
}
8989
9090
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
91-
public function get<%- toPascalCase(attribute.key) %>(): <%- getType(attribute) %> {
92-
return $this-><%- strict ? toCamelCase(attribute.key) : attribute.key %>;
93-
}
91+
public function get<%- toPascalCase(attribute.key) %>(): <%- getType(attribute) %> {
92+
return $this-><%- strict ? toCamelCase(attribute.key) : attribute.key %>;
93+
}
9494
95-
public function set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>): void {
96-
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
97-
}
95+
public function set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>): void {
96+
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
97+
}
9898
<% if (index < collection.attributes.length - 1) { %>
9999
<% } -%>
100100
<% } -%>

0 commit comments

Comments
 (0)