Skip to content

Commit 99676bc

Browse files
committed
Add Android response model template
1 parent 5a57afe commit 99676bc

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/SDK/Language/Android.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ public function getFiles()
359359
'template' => '/android/example-java/.gitignore',
360360
'minify' => false,
361361
],
362+
[
363+
'scope' => 'definition',
364+
'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt',
365+
'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig',
366+
'minify' => false,
367+
],
362368
];
363369
}
364370
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 %}{{property.type | typeName}}{% endif %}{% endmacro %}
2+
package {{ sdk.namespace | caseDot }}.models
3+
4+
/// {{ definition.description }}
5+
data class {{ definition.name | caseUcfirst }}(
6+
{% for property in definition.properties %}
7+
{% if property.required %}val{% else%}var{% endif %} {{ property.name | escapeKeyword | removeDollarSign }}: {{_self.sub_schema(property)}}{% if not property.required %}? = {{ property.default }}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
8+
9+
{% endfor %}
10+
{% if definition.additionalProperties %}
11+
val data: Map<String, Any>
12+
{% endif %}
13+
) {
14+
companion object {
15+
fun from(map: Map<String, Any>) = {{ definition.name | caseUcfirst }}(
16+
{% for property in definition.properties %}
17+
{{ 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 %}map["{{ property.name | escapeDollarSign }}"] as{% if not property.required %}?{% endif %} {{ _self.sub_schema(property) }}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
18+
19+
{% endfor %}
20+
{% if definition.additionalProperties %}
21+
data = map
22+
{% endif %}
23+
)
24+
}
25+
26+
fun toMap(): Map<String, Any> = mapOf(
27+
{% for property in definition.properties %}
28+
"{{ 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 %}
29+
30+
{% endfor %}
31+
{% if definition.additionalProperties %}
32+
"data" to data
33+
{% endif %}
34+
)
35+
{% if definition.additionalProperties %}
36+
37+
fun <T> convertTo(fromJson: (Map<String, Any>) -> T): T {
38+
return fromJson(data)
39+
}
40+
{% endif %}
41+
{% for property in definition.properties %}
42+
{% if property.sub_schema %}
43+
{% for def in spec.definitions %}
44+
{% if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %}
45+
46+
fun <T> convertTo(fromJson: (Map<String, Any>) -> T) =
47+
{{property.name | removeDollarSign}}.map { it.convertTo(fromJson = fromJson) }
48+
{% endif %}
49+
{% endfor %}
50+
{% endif %}
51+
{% endfor %}
52+
}

0 commit comments

Comments
 (0)