Skip to content

Commit 7ac7096

Browse files
Merge pull request #1112 from appwrite/fix-type-generation-comment-path
fix: type generation templates to include relative path and strict flag
2 parents d5ada35 + ddf66fb commit 7ac7096

File tree

8 files changed

+92
-89
lines changed

8 files changed

+92
-89
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require("path");
44
const { LanguageMeta, detectLanguage } = require("../type-generation/languages/language");
55
const { Command, Option, Argument } = require("commander");
66
const { localConfig } = require("../config");
7-
const { success, log, actionRunner } = require("../parser");
7+
const { success, log, warn, actionRunner } = require("../parser");
88
const { PHP } = require("../type-generation/languages/php");
99
const { TypeScript } = require("../type-generation/languages/typescript");
1010
const { Kotlin } = require("../type-generation/languages/kotlin");
@@ -71,7 +71,7 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
7171
}
7272

7373
if (strict) {
74-
log(`Strict mode enabled: Field names will be converted to follow ${language} conventions`);
74+
warn(`Strict mode enabled: Field names will be converted to follow ${language} conventions`);
7575
}
7676

7777
const meta = createLanguageMeta(language);
@@ -118,7 +118,7 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
118118
collections,
119119
strict,
120120
...templateHelpers,
121-
getType: meta.getType
121+
getType: meta.getType,
122122
});
123123

124124
const destination = singleFileDestination || path.join(outputDirectory, meta.getFileName());
@@ -128,10 +128,11 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
128128
} else {
129129
for (const collection of collections) {
130130
const content = templater({
131+
collections,
131132
collection,
132133
strict,
133134
...templateHelpers,
134-
getType: meta.getType
135+
getType: meta.getType,
135136
});
136137

137138
const destination = path.join(outputDirectory, meta.getFileName(collection));

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const path = require('path');
66

77
class Dart extends LanguageMeta {
88
getPackageName() {
9-
const pubspecPath = path.join(this.getCurrentDirectory(), 'pubspec.yaml');
9+
const pubspecPath = path.join(process.cwd(), 'pubspec.yaml');
1010
if (fs.existsSync(pubspecPath)) {
1111
const pubspecContent = fs.readFileSync(pubspecPath, 'utf8');
1212
const lines = pubspecContent.split('\n');
@@ -40,7 +40,7 @@ class Dart extends LanguageMeta {
4040
return 'appwrite';
4141
}
4242

43-
getType(attribute) {
43+
getType(attribute, collections) {
4444
let type = "";
4545
switch (attribute.type) {
4646
case AttributeType.STRING:
@@ -61,7 +61,11 @@ class Dart extends LanguageMeta {
6161
type = "bool";
6262
break;
6363
case AttributeType.RELATIONSHIP:
64-
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
64+
const relatedCollection = collections.find(c => c.$id === attribute.relatedCollection);
65+
if (!relatedCollection) {
66+
throw new Error(`Related collection with ID '${attribute.relatedCollection}' not found.`);
67+
}
68+
type = LanguageMeta.toPascalCase(relatedCollection.name);
6569
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
6670
type = `List<${type}>`;
6771
}
@@ -78,19 +82,15 @@ class Dart extends LanguageMeta {
7882
return type;
7983
}
8084

81-
getCurrentDirectory() {
82-
return process.cwd();
83-
}
84-
8585
getTemplate() {
8686
return `<% for (const attribute of collection.attributes) { -%>
8787
<% if (attribute.type === 'relationship') { -%>
88-
import '<%- toSnakeCase(attribute.relatedCollection) %>.dart';
88+
import '<%- toSnakeCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.dart';
8989

9090
<% } -%>
9191
<% } -%>
9292
/// This file is auto-generated by the Appwrite CLI.
93-
/// You can regenerate it by running \`appwrite types -l dart ${this.getCurrentDirectory()}\`.
93+
/// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
9494

9595
<% for (const attribute of collection.attributes) { -%>
9696
<% if (attribute.format === 'enum') { -%>
@@ -104,7 +104,7 @@ enum <%- toPascalCase(attribute.key) %> {
104104
<% } -%>
105105
class <%= toPascalCase(collection.name) %> {
106106
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
107-
<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
107+
<%- getType(attribute, collections) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
108108
<% } -%>
109109

110110
<%= toPascalCase(collection.name) %>({
@@ -152,11 +152,11 @@ map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
152152
<% } -%>
153153
<% } else if (attribute.type === 'relationship') { -%>
154154
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
155-
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.relatedCollection) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
155+
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
156156
<% } else { -%>
157157
<% if (!attribute.required) { -%>
158-
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%>
159-
<%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>'])<% } -%>
158+
map['<%= attribute.key %>'] != null ? <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%>
159+
<%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>.fromMap(map['<%= attribute.key %>'])<% } -%>
160160
<% } -%>
161161
<% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
162162
<% } -%>

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { AttributeType } = require('../attribute');
33
const { LanguageMeta } = require("./language");
44

55
class Java extends LanguageMeta {
6-
getType(attribute) {
6+
getType(attribute, collections) {
77
let type = "";
88
switch (attribute.type) {
99
case AttributeType.STRING:
@@ -24,7 +24,11 @@ class Java extends LanguageMeta {
2424
type = "boolean";
2525
break;
2626
case AttributeType.RELATIONSHIP:
27-
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
27+
const relatedCollection = collections.find(c => c.$id === attribute.relatedCollection);
28+
if (!relatedCollection) {
29+
throw new Error(`Related collection with ID '${attribute.relatedCollection}' not found.`);
30+
}
31+
type = LanguageMeta.toPascalCase(relatedCollection.name);
2832
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
2933
type = "List<" + type + ">";
3034
}
@@ -38,25 +42,21 @@ class Java extends LanguageMeta {
3842
return type;
3943
}
4044

41-
getCurrentDirectory() {
42-
return process.cwd();
43-
}
44-
4545
getTemplate() {
4646
return `package io.appwrite.models;
4747

4848
/**
4949
* This file is auto-generated by the Appwrite CLI.
50-
* You can regenerate it by running \`appwrite types -l java ${this.getCurrentDirectory()}\`.
50+
* You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
5151
*/
5252

5353
import java.util.Objects;
5454
<% for (const attribute of collection.attributes) { -%>
5555
<% if (attribute.type === 'relationship') { -%>
56-
import io.appwrite.models.<%- toPascalCase(attribute.relatedCollection) %>;
57-
56+
import io.appwrite.models.<%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>;
5857
<% } -%>
5958
<% } -%>
59+
6060
public class <%- toPascalCase(collection.name) %> {
6161
<% for (const attribute of collection.attributes) { -%>
6262
<% if (attribute.format === 'enum') { -%>
@@ -70,15 +70,15 @@ public class <%- toPascalCase(collection.name) %> {
7070
<% } -%>
7171
<% } -%>
7272
<% for (const attribute of collection.attributes) { -%>
73-
private <%- getType(attribute) %> <%- strict ? toCamelCase(attribute.key) : attribute.key %>;
73+
private <%- getType(attribute, collections) %> <%- strict ? toCamelCase(attribute.key) : attribute.key %>;
7474
<% } -%>
7575

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

7979
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, collections) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %><%- index < collection.attributes.length - 1 ? ',' : '' %>
8282
<% } -%>
8383
) {
8484
<% for (const attribute of collection.attributes) { -%>
@@ -87,11 +87,11 @@ public class <%- toPascalCase(collection.name) %> {
8787
}
8888

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

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

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { AttributeType } = require('../attribute');
66
const { LanguageMeta } = require("./language");
77

88
class JavaScript extends LanguageMeta {
9-
getType(attribute) {
9+
getType(attribute, collections) {
1010
let type = ""
1111
switch (attribute.type) {
1212
case AttributeType.STRING:
@@ -29,7 +29,11 @@ class JavaScript extends LanguageMeta {
2929
type = "boolean";
3030
break;
3131
case AttributeType.RELATIONSHIP:
32-
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
32+
const relatedCollection = collections.find(c => c.$id === attribute.relatedCollection);
33+
if (!relatedCollection) {
34+
throw new Error(`Related collection with ID '${attribute.relatedCollection}' not found.`);
35+
}
36+
type = LanguageMeta.toPascalCase(relatedCollection.name);
3337
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
3438
type = `${type}[]`;
3539
}
@@ -60,19 +64,14 @@ class JavaScript extends LanguageMeta {
6064
return "appwrite";
6165
}
6266

63-
getCurrentDirectory() {
64-
return process.cwd();
65-
}
66-
6767
getTemplate() {
68-
return `
69-
// This file is auto-generated by the Appwrite CLI.
70-
// You can regenerate it by running \`appwrite types -l js ${this.getCurrentDirectory()}\`.
71-
72-
/**
68+
return `/**
7369
* @typedef {import('${this._getAppwriteDependency()}').Models.Document} Document
7470
*/
7571

72+
// This file is auto-generated by the Appwrite CLI.
73+
// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
74+
7675
<% for (const collection of collections) { -%>
7776
<% for (const attribute of collection.attributes) { -%>
7877
<% if (attribute.format === 'enum') { -%>
@@ -83,14 +82,15 @@ class JavaScript extends LanguageMeta {
8382
<% } -%>
8483
<% } -%>
8584
<% } -%>
86-
<% for (const collection of collections) { %>/**
85+
<% for (const [index, collection] of Object.entries(collections)) { %>/**
8786
* @typedef {Document & {
8887
<% for (const attribute of collection.attributes) { -%>
89-
* <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %>;
88+
* <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute, collections) %>;
9089
<% } -%>
9190
* }} <%- toPascalCase(collection.name) %>
9291
*/
93-
92+
<% if (index < collections.length - 1) { %>
93+
<% } -%>
9494
<% } %>`;
9595
}
9696

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { AttributeType } = require('../attribute');
33
const { LanguageMeta } = require("./language");
44

55
class Kotlin extends LanguageMeta {
6-
getType(attribute) {
6+
getType(attribute, collections) {
77
let type = "";
88
switch (attribute.type) {
99
case AttributeType.STRING:
@@ -24,7 +24,11 @@ class Kotlin extends LanguageMeta {
2424
type = "Boolean";
2525
break;
2626
case AttributeType.RELATIONSHIP:
27-
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
27+
const relatedCollection = collections.find(c => c.$id === attribute.relatedCollection);
28+
if (!relatedCollection) {
29+
throw new Error(`Related collection with ID '${attribute.relatedCollection}' not found.`);
30+
}
31+
type = LanguageMeta.toPascalCase(relatedCollection.name);
2832
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
2933
type = `List<${type}>`;
3034
}
@@ -41,22 +45,18 @@ class Kotlin extends LanguageMeta {
4145
return type;
4246
}
4347

44-
getCurrentDirectory() {
45-
return process.cwd();
46-
}
47-
4848
getTemplate() {
4949
return `package io.appwrite.models
5050

5151
<% for (const attribute of collection.attributes) { -%>
5252
<% if (attribute.type === 'relationship') { -%>
53-
import <%- toPascalCase(attribute.relatedCollection) %>
53+
import <%- toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name) %>
5454

5555
<% } -%>
5656
<% } -%>
5757
/**
5858
* This file is auto-generated by the Appwrite CLI.
59-
* You can regenerate it by running \`appwrite types -l kotlin ${this.getCurrentDirectory()}\`.
59+
* You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`.
6060
*/
6161

6262
<% for (const attribute of collection.attributes) { -%>
@@ -71,9 +71,10 @@ enum class <%- toPascalCase(attribute.key) %> {
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, collections) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
7575
<% } -%>
76-
)`;
76+
)
77+
`;
7778
}
7879

7980
getFileName(collection) {

0 commit comments

Comments
 (0)