Skip to content

chore: type generation fixes + improvements #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
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
27 changes: 3 additions & 24 deletions templates/cli/lib/type-generation/languages/dart.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ class Dart extends LanguageMeta {
}

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

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

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

<%= toPascalCase(collection.name) %>({
required super.$id,
required super.$collectionId,
required super.$databaseId,
required super.$createdAt,
required super.$updatedAt,
required super.$permissions,
required super.data,
<% 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) { %>,<% } %>
<% } -%>
});

factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
return <%= toPascalCase(collection.name) %>(
$id: map['\\$id'].toString(),
$collectionId: map['\\$collectionId'].toString(),
$databaseId: map['\\$databaseId'].toString(),
$createdAt: map['\\$createdAt'].toString(),
$updatedAt: map['\\$updatedAt'].toString(),
$permissions: List<String>.from(map['\\$permissions'] ?? []),
data: map,
<% 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') { -%>
Expand Down Expand Up @@ -180,12 +165,6 @@ map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollecti

Map<String, dynamic> toMap() {
return {
"\\$id": $id,
"\\$collectionId": $collectionId,
"\\$databaseId": $databaseId,
"\\$createdAt": $createdAt,
"\\$updatedAt": $updatedAt,
"\\$permissions": $permissions,
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
Expand Down
72 changes: 36 additions & 36 deletions templates/cli/lib/type-generation/languages/java.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Java extends LanguageMeta {
* You can regenerate it by running \`appwrite types -l java ${this.getCurrentDirectory()}\`.
*/

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

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

<% } -%>
<% } -%>
<% for (const attribute of collection.attributes) { -%>
private <%- getType(attribute) %> <%- strict ? toCamelCase(attribute.key) : attribute.key %>;
private <%- getType(attribute) %> <%- strict ? toCamelCase(attribute.key) : attribute.key %>;
<% } -%>

public <%- toPascalCase(collection.name) %>() {
}
public <%- toPascalCase(collection.name) %>() {
}

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

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

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

<% } -%>
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
<%- toPascalCase(collection.name) %> that = (<%- toPascalCase(collection.name) %>) obj;
return <% collection.attributes.forEach((attr, index) => { %>Objects.equals(<%= toCamelCase(attr.key) %>, that.<%= toCamelCase(attr.key) %>)<% if (index < collection.attributes.length - 1) { %> &&
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
<%- toPascalCase(collection.name) %> that = (<%- toPascalCase(collection.name) %>) obj;
return <% collection.attributes.forEach((attr, index) => { %>Objects.equals(<%= toCamelCase(attr.key) %>, that.<%= toCamelCase(attr.key) %>)<% if (index < collection.attributes.length - 1) { %> &&
<% } }); %>;
}
}

@Override
public int hashCode() {
return Objects.hash(<%= collection.attributes.map(attr => toCamelCase(attr.key)).join(', ') %>);
}
@Override
public int hashCode() {
return Objects.hash(<%= collection.attributes.map(attr => toCamelCase(attr.key)).join(', ') %>);
}

@Override
public String toString() {
return "<%- toPascalCase(collection.name) %>{" +
<% for (const attribute of collection.attributes) { -%>
"<%= strict ? toCamelCase(attribute.key) : attribute.key %>=" + <%= strict ? toCamelCase(attribute.key) : attribute.key %> +
@Override
public String toString() {
return "<%- toPascalCase(collection.name) %>{" +
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
"<%= strict ? toCamelCase(attribute.key) : attribute.key %>=" + <%= strict ? toCamelCase(attribute.key) : attribute.key %> +<% if (index < collection.attributes.length - 1) { %> ", " +<% } %>
<% } -%>
'}';
}
'}';
}
}
`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class JavaScript extends LanguageMeta {
type += "[]";
}
if (!attribute.required && attribute.default === null) {
type += "|null";
type += " | null";
}
return type;
}
Expand Down
4 changes: 2 additions & 2 deletions templates/cli/lib/type-generation/languages/kotlin.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ import <%- toPascalCase(attribute.relatedCollection) %>
<% if (attribute.format === 'enum') { -%>
enum class <%- toPascalCase(attribute.key) %> {
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
<%- strict ? toUpperSnakeCase(element) : element %><%- index < attribute.elements.length - 1 ? ',' : '' %>
<%- strict ? toUpperSnakeCase(element) : element %><%- index < attribute.elements.length - 1 ? ',' : '' %>
<% } -%>
}

<% } -%>
<% } -%>
data class <%- toPascalCase(collection.name) %>(
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
val <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
val <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
<% } -%>
)`;
}
Expand Down
28 changes: 14 additions & 14 deletions templates/cli/lib/type-generation/languages/php.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,39 @@ use Appwrite\\Models\\<%- toPascalCase(attribute.relatedCollection) %>;
<% if (attribute.format === 'enum') { -%>
enum <%- toPascalCase(attribute.key) %>: string {
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
case <%- strict ? toUpperSnakeCase(element) : element %> = '<%- element %>';
case <%- strict ? toUpperSnakeCase(element) : element %> = '<%- element %>';
<% } -%>
}

<% } -%>
<% } -%>
class <%- toPascalCase(collection.name) %> {
<% for (const attribute of collection.attributes ){ -%>
private <%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
private <%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
<% } -%>

public function __construct(
public function __construct(
<% for (const attribute of collection.attributes ){ -%>
<% if (attribute.required) { -%>
<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %><% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %><% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
<% } else { -%>
?<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %> = null<% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
?<%- getType(attribute).replace('|null', '') %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %> = null<% if (collection.attributes.indexOf(attribute) < collection.attributes.length - 1) { %>,<% } %>
<% } -%>
<% } -%>
) {
) {
<% for (const attribute of collection.attributes ){ -%>
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
<% } -%>
}
}

<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
public function get<%- toPascalCase(attribute.key) %>(): <%- getType(attribute) %> {
return $this-><%- strict ? toCamelCase(attribute.key) : attribute.key %>;
}
public function get<%- toPascalCase(attribute.key) %>(): <%- getType(attribute) %> {
return $this-><%- strict ? toCamelCase(attribute.key) : attribute.key %>;
}

public function set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>): void {
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
}
public function set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>): void {
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
}
<% if (index < collection.attributes.length - 1) { %>
<% } -%>
<% } -%>
Expand Down
Loading