Skip to content

Commit 57d291d

Browse files
committed
Refactor model template for generics and improve formatting
1 parent cde2a80 commit 57d291d

File tree

1 file changed

+53
-44
lines changed
  • templates/android/library/src/main/java/io/appwrite/models

1 file changed

+53
-44
lines changed
Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,71 @@
1-
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst}}>{% else %}{{property.sub_schema | caseUcfirst}}{% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map<String, Any>{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %}
21
package {{ sdk.namespace | caseDot }}.models
32

43
import com.google.gson.annotations.SerializedName
4+
import io.appwrite.extensions.jsonCast
55

66
/**
77
* {{ definition.description }}
88
*/
9-
data class {{ definition.name | caseUcfirst }}(
10-
{% for property in definition.properties %}
9+
data class {{ definition | modelType(spec) | raw }}(
10+
{%~ for property in definition.properties %}
1111
/**
1212
* {{ property.description }}
13-
*
1413
*/
1514
@SerializedName("{{ property.name | escapeKeyword | escapeDollarSign}}")
16-
{% if property.required %}val{% else%}var{% endif %} {{ property.name | escapeKeyword | removeDollarSign }}: {{_self.sub_schema(property)}}{% if not property.required %}?{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{{ "\n" }}{% endif %}
15+
{% if property.required -%} val
16+
{%- else -%} var
17+
{%- endif %} {{ property.name | escapeKeyword | removeDollarSign }}: {{ property | propertyType(spec) | raw }},
1718

18-
{% endfor %}
19-
{% if definition.additionalProperties %}
20-
val data: Map<String, Any>
21-
{% endif %}
19+
{%~ endfor %}
20+
{%~ if definition.additionalProperties %}
21+
/**
22+
* Additional properties
23+
*/
24+
@SerializedName("data")
25+
val data: T
26+
{%~ endif %}
2227
) {
23-
companion object {
24-
@Suppress("UNCHECKED_CAST")
25-
fun from(map: Map<String, Any>) = {{ definition.name | caseUcfirst }}(
26-
{% for property in definition.properties %}
27-
{{ property.name | escapeKeyword | removeDollarSign }} = {% if property.sub_schema %}{% if property.type == 'array' %}(map["{{ property.name | escapeDollarSign }}"] as List<Map<String, Any>>).map { {{property.sub_schema | caseUcfirst}}.from(map = it) }{% else %}{{property.sub_schema | caseUcfirst}}.from(map = map["{{property.name | escapeDollarSign }}"] as Map<String, Any>){% endif %}{% else %}{% if property.type == "integer" or property.type == "number" %}({% endif %}map["{{ property.name | escapeDollarSign }}"]{% if property.type == "integer" or property.type == "number" %} as{% if not property.required %}?{% endif %} Number){% endif %}{% if property.type == "integer" %}{% if not property.required %}?{% endif %}.toLong(){% elseif property.type == "number" %}{% if not property.required %}?{% endif %}.toDouble(){% else %} as{% if not property.required %}?{% endif %} {{ _self.sub_schema(property) }}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
28-
29-
{% endfor %}
30-
{% if definition.additionalProperties %}
31-
data = map
32-
{% endif %}
33-
)
34-
}
35-
3628
fun toMap(): Map<String, Any> = mapOf(
37-
{% for property in definition.properties %}
38-
"{{ property.name | escapeDollarSign }}" to {% if property.sub_schema %}{% if property.type == 'array' %}{{property.name | escapeKeyword | removeDollarSign}}.map { it.toMap() }{% else %}{{property.name | escapeKeyword | removeDollarSign}}.toMap(){% endif %}{% else %}{{property.name | escapeKeyword | removeDollarSign}}{% endif %} as Any{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
39-
40-
{% endfor %}
41-
{% if definition.additionalProperties %}
42-
"data" to data
43-
{% endif %}
29+
{%~ for property in definition.properties %}
30+
"{{ property.name | escapeDollarSign }}" to {% if property.sub_schema %}{% if property.type == 'array' %}{{property.name | escapeKeyword | removeDollarSign}}.map { it.toMap() }{% else %}{{property.name | escapeKeyword | removeDollarSign}}.toMap(){% endif %}{% else %}{{property.name | escapeKeyword | removeDollarSign}}{% endif %} as Any,
31+
{%~ endfor %}
32+
{%~ if definition.additionalProperties %}
33+
"data" to data!!.jsonCast(to = Map::class.java)
34+
{%~ endif %}
4435
)
45-
{% if definition.additionalProperties %}
4636

47-
fun <T> convertTo(fromJson: (Map<String, Any>) -> T): T {
48-
return fromJson(data)
49-
}
50-
{% endif %}
51-
{% for property in definition.properties %}
52-
{% if property.sub_schema %}
53-
{% for def in spec.definitions %}
54-
{% if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %}
37+
companion object {
38+
{%~ if definition.name | hasGenericType(spec) %}
39+
operator fun invoke(
40+
{%~ for property in definition.properties %}
41+
{{ property.name | escapeKeyword | removeDollarSign }}: {{ property | propertyType(spec, 'Map<String, Any>') | raw }},
42+
{%~ endfor %}
43+
{%~ if definition.additionalProperties %}
44+
data: Map<String, Any>
45+
{%~ endif %}
46+
) = {{ definition | modelType(spec, 'Map<String, Any>') | raw }}(
47+
{%~ for property in definition.properties %}
48+
{{ property.name | escapeKeyword | removeDollarSign }},
49+
{%~ endfor %}
50+
{%~ if definition.additionalProperties %}
51+
data
52+
{%~ endif %}
53+
)
54+
{%~ endif %}
5555

56-
fun <T> convertTo(fromJson: (Map<String, Any>) -> T) =
57-
{{property.name | removeDollarSign}}.map { it.convertTo(fromJson = fromJson) }
58-
{% endif %}
59-
{% endfor %}
60-
{% endif %}
61-
{% endfor %}
56+
@Suppress("UNCHECKED_CAST")
57+
fun {% if definition.name | hasGenericType(spec) %}<T> {% endif %}from(
58+
map: Map<String, Any>,
59+
{%~ if definition.name | hasGenericType(spec) %}
60+
nestedType: Class<T>
61+
{%~ endif %}
62+
) = {{ definition | modelType(spec) | raw }}(
63+
{%~ for property in definition.properties %}
64+
{{ property.name | escapeKeyword | removeDollarSign }} = {% if property.sub_schema %}{% if property.type == 'array' %}(map["{{ property.name | escapeDollarSign }}"] as List<Map<String, Any>>).map { {{ property.sub_schema | caseUcfirst }}.from(map = it{% if definition.name | hasGenericType(spec) %}, nestedType{% endif %}) }{% else %}{{ property.sub_schema | caseUcfirst }}.from(map = map["{{property.name | escapeDollarSign }}"] as Map<String, Any>{% if definition.name | hasGenericType(spec) %}, nestedType{% endif %}){% endif %}{% else %}{% if property.type == "integer" or property.type == "number" %}({% endif %}map["{{ property.name | escapeDollarSign }}"]{% if property.type == "integer" or property.type == "number" %} as{% if not property.required %}?{% endif %} Number){% endif %}{% if property.type == "integer" %}{% if not property.required %}?{% endif %}.toLong(){% elseif property.type == "number" %}{% if not property.required %}?{% endif %}.toDouble(){% else %} as{% if not property.required %}?{% endif %} {{ property | propertyType(spec) | raw }}{% endif %}{% endif %},
65+
{%~ endfor %}
66+
{%~ if definition.additionalProperties %}
67+
data = map.jsonCast(to = nestedType)
68+
{%~ endif %}
69+
)
70+
}
6271
}

0 commit comments

Comments
 (0)