Skip to content

Commit 1d58a39

Browse files
committed
Apply to Kotlin
1 parent 9ef2204 commit 1d58a39

File tree

8 files changed

+170
-166
lines changed

8 files changed

+170
-166
lines changed

templates/android/library/src/main/java/io/appwrite/models/Model.kt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.appwrite.extensions.jsonCast
66
/**
77
* {{ definition.description }}
88
*/
9-
data class {{ definition | modelType(spec) | raw }}(
9+
{% if definition.properties | length != 0 or definition.additionalProperties %}data {% endif %}class {{ definition | modelType(spec) | raw }}(
1010
{%~ for property in definition.properties %}
1111
/**
1212
* {{ property.description }}

templates/android/library/src/main/java/io/appwrite/services/ServiceTemplate.kt.twig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package {{ sdk.namespace | caseDot }}.services
33
import android.net.Uri
44
import {{ sdk.namespace | caseDot }}.Client
55
import {{ sdk.namespace | caseDot }}.models.*
6+
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
7+
import {{ sdk.namespace | caseDot }}.extensions.classOf
68
{% if service.features.webAuth %}
79
import {{ sdk.namespace | caseDot }}.WebAuthComponent
810
import androidx.activity.ComponentActivity
911
{% endif %}
10-
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
1112
import okhttp3.Cookie
1213
{% if (service.features.webAuth or service.features.location) %}
1314
import okhttp3.HttpUrl
1415
import okhttp3.HttpUrl.Companion.toHttpUrl
1516
{% endif %}
1617
import java.io.File
17-
import kotlin.reflect.KClass
18-
import kotlin.reflect.typeOf
1918

19+
/**
20+
* {{ service.description | raw | replace({"\n": "", "\r": ""}) }}
21+
**/
2022
class {{ service.name | caseUcfirst }} : Service {
2123

2224
public constructor (client: Client) : super(client) { }
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
{% import 'kotlin/base/utils.twig' as utils%}
21
return client.call(
32
"{{ method.method | caseUpper }}",
43
path,
54
headers,
65
params,
7-
responseType = {{ utils.resultType(sdk.namespace, method) }}::class.java,
8-
{% if method.responseModel %}
6+
{%~ if method.responseModel | hasGenericType(spec) %}
7+
responseType = classOf(),
8+
{%~ else %}
9+
responseType = {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}::class.java,
10+
{%~ endif %}
11+
{%~ if method.responseModel %}
912
converter,
10-
{% endif %}
13+
{%~ endif %}
1114
)

templates/kotlin/base/requests/file.twig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
{% import 'kotlin/base/utils.twig' as utils %}
21
val idParamName: String? = {% if method.parameters.all | filter(p => p.isUploadID) | length > 0 %}{% for parameter in method.parameters.all | filter(parameter => parameter.isUploadID) %}"{{ parameter.name }}"{% endfor %}{% else %}null{% endif %}
32

4-
{% for parameter in method.parameters.all %}
5-
{% if parameter.type == 'file' %}
3+
{%~ for parameter in method.parameters.all %}
4+
{%~ if parameter.type == 'file' %}
65
val paramName = "{{ parameter.name }}"
7-
{% endif %}
8-
{% endfor %}
6+
{%~ endif %}
7+
{%~ endfor %}
98
return client.chunkedUpload(
109
path,
1110
headers,
1211
params,
13-
responseType = {{ utils.resultType(sdk.namespace, method) }}::class.java,
14-
{% if method.responseModel %}
12+
responseType = {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}::class.java,
13+
{%~ if method.responseModel %}
1514
converter,
16-
{% endif %}
15+
{%~ endif %}
1716
paramName,
1817
idParamName,
1918
onProgress,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
{% import 'kotlin/base/utils.twig' as utils%}
21
return client.call(
32
"{{ method.method | caseUpper }}",
43
path,
54
params = params,
6-
responseType = {{ utils.resultType(sdk.namespace, method) }}::class.java
5+
responseType = {{ method | returnType(spec, sdk.namespace | caseDot) | raw }}::class.java
76
)

templates/kotlin/base/utils.twig

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +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-
{% if definition.properties | length == 0 and not definition.additionalProperties %}
10-
class {{ definition.name | caseUcfirst }} {}
11-
{% else %}
12-
data class {{ definition.name | caseUcfirst }}(
13-
{% for property in definition.properties %}
9+
{% if definition.properties | length != 0 or definition.additionalProperties %}data {% endif %}class {{ definition | modelType(spec) | raw }}(
10+
{%~ for property in definition.properties %}
1411
/**
1512
* {{ property.description }}
16-
*
1713
*/
1814
@SerializedName("{{ property.name | escapeKeyword | escapeDollarSign}}")
19-
{% 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 }},
2018

21-
{% endfor %}
22-
{% if definition.additionalProperties %}
23-
val data: Map<String, Any>
24-
{% endif %}
19+
{%~ endfor %}
20+
{%~ if definition.additionalProperties %}
21+
/**
22+
* Additional properties
23+
*/
24+
@SerializedName("data")
25+
val data: T
26+
{%~ endif %}
2527
) {
26-
companion object {
27-
@Suppress("UNCHECKED_CAST")
28-
fun from(map: Map<String, Any>) = {{ definition.name | caseUcfirst }}(
29-
{% for property in definition.properties %}
30-
{{ 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 %}
31-
32-
{% endfor %}
33-
{% if definition.additionalProperties %}
34-
data = map
35-
{% endif %}
36-
)
37-
}
38-
3928
fun toMap(): Map<String, Any> = mapOf(
40-
{% for property in definition.properties %}
41-
"{{ 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 %}
42-
43-
{% endfor %}
44-
{% if definition.additionalProperties %}
45-
"data" to data
46-
{% 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 %}
4735
)
48-
{% if definition.additionalProperties %}
4936

50-
fun <T> convertTo(fromJson: (Map<String, Any>) -> T): T {
51-
return fromJson(data)
52-
}
53-
{% endif %}
54-
{% for property in definition.properties %}
55-
{% if property.sub_schema %}
56-
{% for def in spec.definitions %}
57-
{% 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 %}
5855

59-
fun <T> convertTo(fromJson: (Map<String, Any>) -> T) =
60-
{{property.name | removeDollarSign}}.map { it.convertTo(fromJson = fromJson) }
61-
{% endif %}
62-
{% endfor %}
63-
{% endif %}
64-
{% endfor %}
65-
}
66-
{% endif %}
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+
}
71+
}

0 commit comments

Comments
 (0)