Skip to content

Commit eb8bd87

Browse files
committed
fix: type generation for java with class based approach
1 parent a75de52 commit eb8bd87

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

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

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,82 @@ class Java extends LanguageMeta {
1313
type = "String";
1414
break;
1515
case AttributeType.INTEGER:
16-
type = "Int";
16+
type = "Integer";
1717
break;
1818
case AttributeType.FLOAT:
19-
type = "Float";
19+
type = "Double";
2020
break;
2121
case AttributeType.BOOLEAN:
2222
type = "Boolean";
2323
break;
2424
case AttributeType.RELATIONSHIP:
25-
type = "Map<String, Any>";
25+
type = "Map<String, Object>";
2626
break;
2727
default:
2828
throw new Error(`Unknown attribute type: ${attribute.type}`);
2929
}
3030
if (attribute.array) {
3131
type = "List<" + type + ">";
3232
}
33-
if (attribute.required) {
34-
type += "?";
35-
}
3633
return type;
3734
}
3835

3936
getTemplate() {
4037
return `package io.appwrite.models;
4138

4239
import java.util.*;
43-
44-
public record <%- toPascalCase(collection.name) %>(
40+
41+
public class <%- toPascalCase(collection.name) %> {
42+
<% for (const attribute of collection.attributes) { -%>
43+
private <%= getType(attribute) %> <%= toCamelCase(attribute.key) %>;
44+
<% } -%>
45+
46+
public <%- toPascalCase(collection.name) %>() {
47+
}
48+
49+
public <%- toPascalCase(collection.name) %>(
50+
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
51+
<%= getType(attribute) %> <%= toCamelCase(attribute.key) %><%- index < collection.attributes.length - 1 ? ',' : '' %>
52+
<% } -%>
53+
) {
54+
<% for (const attribute of collection.attributes) { -%>
55+
this.<%= toCamelCase(attribute.key) %> = <%= toCamelCase(attribute.key) %>;
56+
<% } -%>
57+
}
58+
59+
<% for (const attribute of collection.attributes) { -%>
60+
public <%= getType(attribute) %> get<%- toPascalCase(attribute.key) %>() {
61+
return <%= toCamelCase(attribute.key) %>;
62+
}
63+
64+
public void set<%- toPascalCase(attribute.key) %>(<%= getType(attribute) %> <%= toCamelCase(attribute.key) %>) {
65+
this.<%= toCamelCase(attribute.key) %> = <%= toCamelCase(attribute.key) %>;
66+
}
67+
68+
<% } -%>
69+
@Override
70+
public boolean equals(Object obj) {
71+
if (this == obj) return true;
72+
if (obj == null || getClass() != obj.getClass()) return false;
73+
<%- toPascalCase(collection.name) %> that = (<%- toPascalCase(collection.name) %>) obj;
74+
return Objects.equals(<%= collection.attributes.map(attr => toCamelCase(attr.key)).join(', ') %><%- collection.attributes.length > 1 ? ', that.' + collection.attributes.map(attr => toCamelCase(attr.key)).join(', that.') : collection.attributes.length === 1 ? ', that.' + toCamelCase(collection.attributes[0].key) : '' %>);
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return Objects.hash(<%= collection.attributes.map(attr => toCamelCase(attr.key)).join(', ') %>);
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "<%- toPascalCase(collection.name) %>{" +
4585
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
46-
<%= getType(attribute) %> <%= toCamelCase(attribute.key) %><%- index < collection.attributes.length - 1 ? ',' : '' %>
86+
"<%= toCamelCase(attribute.key) %>=" + <%= toCamelCase(attribute.key) %> +
4787
<% } -%>
48-
) { }`;
88+
'}';
89+
}
90+
}
91+
`;
4992
}
5093

5194
getFileName(collection) {

0 commit comments

Comments
 (0)