Skip to content

Commit 07d2e96

Browse files
committed
feat: strict mode in type generation
1 parent 35473b1 commit 07d2e96

File tree

8 files changed

+88
-63
lines changed

8 files changed

+88
-63
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ const typesLanguageOption = new Option(
5858
.choices(["auto", "ts", "js", "php", "kotlin", "swift", "java", "dart"])
5959
.default("auto");
6060

61-
const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
61+
const typesStrictOption = new Option(
62+
"-s, --strict",
63+
"Enable strict mode to automatically convert field names to follow language conventions"
64+
)
65+
.default(false);
66+
67+
const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict}) => {
6268
if (language === "auto") {
6369
language = detectLanguage();
6470
log(`Detected language: ${language}`);
6571
}
6672

73+
if (strict) {
74+
log(`Strict mode enabled: Field names will be converted to follow ${language} conventions`);
75+
}
76+
6777
const meta = createLanguageMeta(language);
6878

6979
const rawOutputPath = rawOutputDirectory;
@@ -106,6 +116,7 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
106116
if (meta.isSingleFile()) {
107117
const content = templater({
108118
collections,
119+
strict,
109120
...templateHelpers,
110121
getType: meta.getType
111122
});
@@ -118,6 +129,7 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
118129
for (const collection of collections) {
119130
const content = templater({
120131
collection,
132+
strict,
121133
...templateHelpers,
122134
getType: meta.getType
123135
});
@@ -136,6 +148,7 @@ const types = new Command("types")
136148
.description("Generate types for your Appwrite project")
137149
.addArgument(typesOutputArgument)
138150
.addOption(typesLanguageOption)
151+
.addOption(typesStrictOption)
139152
.action(actionRunner(typesCommand));
140153

141154
module.exports = { types };

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ import '<%- attribute.relatedCollection.toLowerCase() %>.dart';
9797
<% if (attribute.format === 'enum') { -%>
9898
enum <%- toPascalCase(attribute.key) %> {
9999
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
100-
<%- toCamelCase(element) %><% if (index < attribute.elements.length - 1) { %>,<% } %>
100+
<%- strict ? toCamelCase(element) : element %><% if (index < attribute.elements.length - 1) { %>,<% } %>
101101
<% } -%>
102102
}
103103

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

111111
<%= toPascalCase(collection.name) %>({
@@ -117,7 +117,7 @@ class <%= toPascalCase(collection.name) %> extends Document {
117117
required super.$permissions,
118118
required super.data,
119119
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
120-
<% 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) { %>,<% } %>
121121
<% } -%>
122122
});
123123

@@ -131,7 +131,7 @@ class <%= toPascalCase(collection.name) %> extends Document {
131131
$permissions: List<String>.from(map['\\$permissions'] ?? []),
132132
data: map,
133133
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
134-
<%= 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') { -%>
135135
<% if (attribute.format === 'enum') { -%>
136136
<% if (attribute.array) { -%>
137137
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
@@ -189,18 +189,18 @@ map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollecti
189189
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
190190
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
191191
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
192-
<%= 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) { %> ?? []<% } -%>
193193
<% } else { -%>
194-
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
194+
<%= strict ? toCamelCase(attribute.key) : attribute.key %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
195195
<% } -%>
196196
<% } else if (attribute.format === 'enum') { -%>
197197
<% if (attribute.array) { -%>
198-
<%= 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) { %> ?? []<% } -%>
199199
<% } else { -%>
200-
<%= 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<% } -%>
201201
<% } -%>
202202
<% } else { -%>
203-
<%= toCamelCase(attribute.key) -%>
203+
<%= strict ? toCamelCase(attribute.key) : attribute.key -%>
204204
<% } -%><% if (index < collection.attributes.length - 1) { %>,<% } %>
205205
<% } -%>
206206
};

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,36 @@ public class <%- toPascalCase(collection.name) %> {
6363

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

7070
<% } -%>
7171
<% } -%>
7272
<% for (const attribute of collection.attributes) { -%>
73-
private <%- getType(attribute) %> <%- toCamelCase(attribute.key) %>;
73+
private <%- getType(attribute) %> <%- 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) %> <%= toCamelCase(attribute.key) %><%- index < collection.attributes.length - 1 ? ',' : '' %>
81+
<%- getType(attribute) %> <%= strict ? toCamelCase(attribute.key) : attribute.key %><%- index < collection.attributes.length - 1 ? ',' : '' %>
8282
<% } -%>
8383
) {
8484
<% for (const attribute of collection.attributes) { -%>
85-
this.<%= toCamelCase(attribute.key) %> = <%= toCamelCase(attribute.key) %>;
85+
this.<%= strict ? toCamelCase(attribute.key) : attribute.key %> = <%= strict ? toCamelCase(attribute.key) : attribute.key %>;
8686
<% } -%>
8787
}
8888

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

94-
public void set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> <%= toCamelCase(attribute.key) %>) {
95-
this.<%= toCamelCase(attribute.key) %> = <%= toCamelCase(attribute.key) %>;
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 %>;
9696
}
9797

9898
<% } -%>
@@ -114,7 +114,7 @@ public class <%- toPascalCase(collection.name) %> {
114114
public String toString() {
115115
return "<%- toPascalCase(collection.name) %>{" +
116116
<% for (const attribute of collection.attributes) { -%>
117-
"<%= toCamelCase(attribute.key) %>=" + <%= toCamelCase(attribute.key) %> +
117+
"<%= strict ? toCamelCase(attribute.key) : attribute.key %>=" + <%= strict ? toCamelCase(attribute.key) : attribute.key %> +
118118
<% } -%>
119119
'}';
120120
}

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class JavaScript extends LanguageMeta {
1616
case AttributeType.___URL:
1717
type = "string";
1818
if (attribute.format === AttributeType.ENUM) {
19-
type = `"${attribute.elements.join('"|"')}"`;
19+
type = LanguageMeta.toPascalCase(attribute.key);
2020
}
2121
break;
2222
case AttributeType.INTEGER:
@@ -31,7 +31,7 @@ class JavaScript extends LanguageMeta {
3131
case AttributeType.RELATIONSHIP:
3232
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
3333
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
34-
type = `Array<${type}>`;
34+
type = `${type}[]`;
3535
}
3636
break;
3737
default:
@@ -65,20 +65,32 @@ class JavaScript extends LanguageMeta {
6565
}
6666

6767
getTemplate() {
68-
return `/**
69-
* @typedef {import('${this._getAppwriteDependency()}').Models.Document} Document
70-
*/
71-
68+
return `
7269
// This file is auto-generated by the Appwrite CLI.
7370
// You can regenerate it by running \`appwrite types -l js ${this.getCurrentDirectory()}\`.
7471

75-
<% for (const collection of collections) { %>
7672
/**
77-
* @typedef {Object} <%- toPascalCase(collection.name) %>
73+
* @typedef {import('${this._getAppwriteDependency()}').Models.Document} Document
74+
*/
75+
76+
<% for (const collection of collections) { -%>
77+
<% for (const attribute of collection.attributes) { -%>
78+
<% if (attribute.format === 'enum') { -%>
79+
/**
80+
* @typedef {"<%- attribute.elements.join('"|"') %>"} <%- toPascalCase(attribute.key) %>
81+
*/
82+
83+
<% } -%>
84+
<% } -%>
85+
<% } -%>
86+
<% for (const collection of collections) { %>/**
87+
* @typedef {Document & {
7888
<% for (const attribute of collection.attributes) { -%>
79-
* @property {<%- getType(attribute) %>} <%- toCamelCase(attribute.key) %>
89+
* <%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %>;
8090
<% } -%>
91+
* }} <%- toPascalCase(collection.name) %>
8192
*/
93+
8294
<% } %>`;
8395
}
8496

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-
<%- toUpperSnakeCase(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 <%- toCamelCase(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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,38 @@ 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 <%- toUpperSnakeCase(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) %> $<%- toCamelCase(attribute.key) %>;
73+
private <%- getType(attribute) %> $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
7474
<% } -%>
7575
7676
public function __construct(
7777
<% for (const attribute of collection.attributes ){ -%>
7878
<% if (attribute.required) { -%>
79-
<%- getType(attribute).replace('|null', '') %> $<%- toCamelCase(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', '') %> $<%- toCamelCase(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
<% } -%>
8484
) {
8585
<% for (const attribute of collection.attributes ){ -%>
86-
$this-><%- toCamelCase(attribute.key) %> = $<%- toCamelCase(attribute.key) %>;
86+
$this-><%- strict ? toCamelCase(attribute.key) : attribute.key %> = $<%- strict ? toCamelCase(attribute.key) : attribute.key %>;
8787
<% } -%>
8888
}
8989
9090
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
9191
public function get<%- toPascalCase(attribute.key) %>(): <%- getType(attribute) %> {
92-
return $this-><%- toCamelCase(attribute.key) %>;
92+
return $this-><%- strict ? toCamelCase(attribute.key) : attribute.key %>;
9393
}
9494
95-
public function set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> $<%- toCamelCase(attribute.key) %>): void {
96-
$this-><%- toCamelCase(attribute.key) %> = $<%- toCamelCase(attribute.key) %>;
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 %>;
9797
}
9898
<% if (index < collection.attributes.length - 1) { %>
9999
<% } -%>

0 commit comments

Comments
 (0)