Skip to content

Commit 7a74cc6

Browse files
committed
feat: fix relationship attribute mapping
1 parent ea25a56 commit 7a74cc6

File tree

7 files changed

+58
-14
lines changed

7 files changed

+58
-14
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class Dart extends LanguageMeta {
2323
break;
2424
case AttributeType.RELATIONSHIP:
2525
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
26+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
27+
type = `List<${type}>`;
28+
}
2629
break;
2730
default:
2831
throw new Error(`Unknown attribute type: ${attribute.type}`);
@@ -37,9 +40,15 @@ class Dart extends LanguageMeta {
3740
}
3841

3942
getTemplate() {
40-
return `class <%= toPascalCase(collection.name) %> {
43+
return `<% for (const attribute of collection.attributes) { -%>
44+
<% if (attribute.type === 'relationship') { -%>
45+
import '<%- attribute.relatedCollection.toLowerCase() %>.dart';
46+
47+
<% } -%>
48+
<% } -%>
49+
class <%= toPascalCase(collection.name) %> {
4150
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
42-
<%= getType(attribute) %> <%= toCamelCase(attribute.key) %>;
51+
<%- getType(attribute) %> <%= toCamelCase(attribute.key) %>;
4352
<% } -%>
4453

4554
<%= toPascalCase(collection.name) %>({

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class Java extends LanguageMeta {
2222
type = "Boolean";
2323
break;
2424
case AttributeType.RELATIONSHIP:
25-
type = "Map<String, Object>";
25+
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
26+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
27+
type = "List<" + type + ">";
28+
}
2629
break;
2730
default:
2831
throw new Error(`Unknown attribute type: ${attribute.type}`);
@@ -37,18 +40,23 @@ class Java extends LanguageMeta {
3740
return `package io.appwrite.models;
3841

3942
import java.util.*;
43+
<% for (const attribute of collection.attributes) { -%>
44+
<% if (attribute.type === 'relationship') { -%>
45+
import <%- toPascalCase(attribute.relatedCollection) %>;
4046

47+
<% } -%>
48+
<% } -%>
4149
public class <%- toPascalCase(collection.name) %> {
4250
<% for (const attribute of collection.attributes) { -%>
43-
private <%= getType(attribute) %> <%= toCamelCase(attribute.key) %>;
51+
private <%- getType(attribute) %> <%= toCamelCase(attribute.key) %>;
4452
<% } -%>
4553

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

4957
public <%- toPascalCase(collection.name) %>(
5058
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
51-
<%= getType(attribute) %> <%= toCamelCase(attribute.key) %><%- index < collection.attributes.length - 1 ? ',' : '' %>
59+
<%- getType(attribute) %> <%= toCamelCase(attribute.key) %><%- index < collection.attributes.length - 1 ? ',' : '' %>
5260
<% } -%>
5361
) {
5462
<% for (const attribute of collection.attributes) { -%>
@@ -57,11 +65,11 @@ public class <%- toPascalCase(collection.name) %> {
5765
}
5866

5967
<% for (const attribute of collection.attributes) { -%>
60-
public <%= getType(attribute) %> get<%- toPascalCase(attribute.key) %>() {
68+
public <%- getType(attribute) %> get<%- toPascalCase(attribute.key) %>() {
6169
return <%= toCamelCase(attribute.key) %>;
6270
}
6371

64-
public void set<%- toPascalCase(attribute.key) %>(<%= getType(attribute) %> <%= toCamelCase(attribute.key) %>) {
72+
public void set<%- toPascalCase(attribute.key) %>(<%- getType(attribute) %> <%= toCamelCase(attribute.key) %>) {
6573
this.<%= toCamelCase(attribute.key) %> = <%= toCamelCase(attribute.key) %>;
6674
}
6775

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class JavaScript extends LanguageMeta {
2727
type = "boolean";
2828
break;
2929
case AttributeType.RELATIONSHIP:
30-
type = "any";
30+
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
31+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
32+
type = `Array<${type}>`;
33+
}
3134
break;
3235
default:
3336
throw new Error(`Unknown attribute type: ${attribute.type}`);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class Kotlin extends LanguageMeta {
2222
type = "Boolean";
2323
break;
2424
case AttributeType.RELATIONSHIP:
25-
type = "Map<String, Any>";
25+
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
26+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
27+
type = `List<${type}>`;
28+
}
2629
break;
2730
default:
2831
throw new Error(`Unknown attribute type: ${attribute.type}`);
@@ -38,7 +41,13 @@ class Kotlin extends LanguageMeta {
3841

3942
getTemplate() {
4043
return `package io.appwrite.models
41-
44+
45+
<% for (const attribute of collection.attributes) { -%>
46+
<% if (attribute.type === 'relationship') { -%>
47+
import <%- toPascalCase(attribute.relatedCollection) %>
48+
49+
<% } -%>
50+
<% } -%>
4251
data class <%- toPascalCase(collection.name) %>(
4352
<% for (const attribute of collection.attributes) { -%>
4453
val <%- toCamelCase(attribute.key) %>: <%- getType(attribute) %>,

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class PHP extends LanguageMeta {
2525
type = "bool";
2626
break;
2727
case AttributeType.RELATIONSHIP:
28-
type = "mixed";
28+
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
29+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
30+
type = "array";
31+
}
2932
break;
3033
default:
3134
throw new Error(`Unknown attribute type: ${attribute.type}`);
@@ -40,6 +43,12 @@ class PHP extends LanguageMeta {
4043
return `<?php
4144
namespace Appwrite\\Models;
4245
46+
<% for (const attribute of collection.attributes) { -%>
47+
<% if (attribute.type === 'relationship' && !(attribute.relationType === 'manyToMany') && !(attribute.relationType === 'oneToMany' && attribute.side === 'parent')) { -%>
48+
use Appwrite\\Models\\<%- toPascalCase(attribute.relatedCollection) %>;
49+
50+
<% } -%>
51+
<% } -%>
4352
class <%- toPascalCase(collection.name) %> {
4453
<% for (const attribute of collection.attributes ){ -%>
4554
private <%- getType(attribute) %> $<%- toCamelCase(attribute.key) %>;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class Swift extends LanguageMeta {
2222
type = "Bool";
2323
break;
2424
case AttributeType.RELATIONSHIP:
25-
return "Map<String, Any>";
25+
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
26+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
27+
type = `[${type}]`;
28+
}
2629
break;
2730
default:
2831
throw new Error(`Unknown attribute type: ${attribute.type}`);
@@ -101,7 +104,7 @@ public class <%- toPascalCase(collection.name) %>: Codable {
101104
return <%- toPascalCase(collection.name) %>(
102105
<% for (const attribute of collection.attributes) { -%>
103106
<% if (attribute.type === 'relationship') { -%>
104-
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> Map<String, Any><% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
107+
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> <%- toPascalCase(attribute.relatedCollection) %><% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>
105108
<% } else if (attribute.array) { -%>
106109
<% if (attribute.type === 'string') { -%>
107110
<%- toCamelCase(attribute.key) %>: map["<%- attribute.key %>"] as<% if (!attribute.required) { %>?<% } else { %>!<% } %> [String]<% if (attribute !== collection.attributes[collection.attributes.length - 1]) { %>,<% } %>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class TypeScript extends LanguageMeta {
2727
type = "boolean";
2828
break;
2929
case AttributeType.RELATIONSHIP:
30-
type = "any";
30+
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
31+
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || attribute.relationType === 'manyToMany') {
32+
type = `${type}[]`;
33+
}
3134
break;
3235
default:
3336
throw new Error(`Unknown attribute type: ${attribute.type}`);

0 commit comments

Comments
 (0)