Skip to content

Commit 4d99588

Browse files
committed
Complete kotlin refactor
1 parent 54cf128 commit 4d99588

File tree

7 files changed

+58
-15
lines changed

7 files changed

+58
-15
lines changed

src/SDK/Language/Android.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function getFiles(): array
253253
[
254254
'scope' => 'service',
255255
'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/services/{{ service.name | caseUcfirst }}ServiceTest.kt',
256-
'template' => '/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig',
256+
'template' => '/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig',
257257
],
258258
[
259259
'scope' => 'default',

src/SDK/Language/Kotlin.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -353,25 +353,50 @@ public function getFiles(): array
353353
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Permission.kt',
354354
'template' => '/kotlin/src/main/kotlin/io/appwrite/Permission.kt.twig',
355355
],
356+
[
357+
'scope' => 'default',
358+
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/PermissionTest.kt',
359+
'template' => '/kotlin/src/test/kotlin/io/appwrite/PermissionTest.kt.twig',
360+
],
356361
[
357362
'scope' => 'default',
358363
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Role.kt',
359364
'template' => '/kotlin/src/main/kotlin/io/appwrite/Role.kt.twig',
360365
],
366+
[
367+
'scope' => 'default',
368+
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/RoleTest.kt',
369+
'template' => '/kotlin/src/test/kotlin/io/appwrite/RoleTest.kt.twig',
370+
],
361371
[
362372
'scope' => 'default',
363373
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/ID.kt',
364374
'template' => '/kotlin/src/main/kotlin/io/appwrite/ID.kt.twig',
365375
],
376+
[
377+
'scope' => 'default',
378+
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/IDTest.kt',
379+
'template' => '/kotlin/src/test/kotlin/io/appwrite/IDTest.kt.twig',
380+
],
366381
[
367382
'scope' => 'default',
368383
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Query.kt',
369384
'template' => '/kotlin/src/main/kotlin/io/appwrite/Query.kt.twig',
370385
],
386+
[
387+
'scope' => 'default',
388+
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/QueryTest.kt',
389+
'template' => '/kotlin/src/test/kotlin/io/appwrite/QueryTest.kt.twig',
390+
],
391+
[
392+
'scope' => 'default',
393+
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Response.kt',
394+
'template' => '/kotlin/src/main/kotlin/io/appwrite/Response.kt.twig',
395+
],
371396
[
372397
'scope' => 'default',
373398
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/coroutines/Callback.kt',
374-
'template' => '/android/library/src/main/java/io/appwrite/coroutines/Callback.kt.twig',
399+
'template' => '/kotlin/src/main/kotlin/io/appwrite/coroutines/Callback.kt.twig',
375400
],
376401
[
377402
'scope' => 'default',
@@ -404,6 +429,11 @@ public function getFiles(): array
404429
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}.kt',
405430
'template' => '/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig',
406431
],
432+
[
433+
'scope' => 'service',
434+
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}ServiceTest.kt',
435+
'template' => '/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig',
436+
],
407437
[
408438
'scope' => 'default',
409439
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/InputFile.kt',
@@ -419,6 +449,11 @@ public function getFiles(): array
419449
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.kt',
420450
'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Model.kt.twig',
421451
],
452+
[
453+
'scope' => 'definition',
454+
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}Test.kt',
455+
'template' => '/kotlin/src/test/kotlin/io/appwrite/models/ModelTest.kt.twig',
456+
],
422457
];
423458
}
424459

@@ -443,7 +478,7 @@ public function getFilters(): array
443478
protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'T'): string
444479
{
445480
if ($method['type'] === 'webAuth') {
446-
return 'Bool';
481+
return 'Boolean';
447482
}
448483
if ($method['type'] === 'location') {
449484
return 'ByteArray';

templates/android/library/build.gradle.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ dependencies {
7979
testImplementation "androidx.test.ext:junit-ktx:1.1.5"
8080
testImplementation "androidx.test:core-ktx:1.5.0"
8181
testImplementation "org.robolectric:robolectric:4.10.3"
82-
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
82+
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"
8383

8484
androidTestImplementation 'androidx.test:runner:1.5.2'
8585
}

templates/kotlin/build.gradle.twig

+2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ dependencies {
3636
implementation("com.squareup.okhttp3:logging-interceptor")
3737
implementation("com.google.code.gson:gson:2.9.0")
3838

39+
testImplementation 'io.mockk:mockk:1.13.7'
3940
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
41+
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"
4042
}
4143

4244
test {

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package {{ sdk.namespace | caseDot }}
22

33
import com.google.gson.GsonBuilder
44
import com.google.gson.reflect.TypeToken
5+
import {{ sdk.namespace | caseDot }}.Response
56
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
67
import {{ sdk.namespace | caseDot }}.extensions.fromJson
78
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
@@ -109,7 +110,6 @@ class Client @JvmOverloads constructor(
109110

110111
val builder = OkHttpClient()
111112
.newBuilder()
112-
.cookieJar(JavaNetCookieJar(cookieJar))
113113

114114
if (!selfSigned) {
115115
http = builder.build()

templates/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig

-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ class {{ service.name | caseUcfirst }} : Service {
3636
{%~ endif %}
3737
@Throws({{ spec.title | caseUcfirst }}Exception::class)
3838
suspend fun {% if method.responseModel | hasGenericType(spec) %}{{ '<T>' | raw }} {% endif %}{{ method.name | caseCamel }}(
39-
{%~ if method.type == "webAuth" %}
40-
activity: ComponentActivity,
41-
{%~ endif %}
4239
{%~ for parameter in method.parameters.all %}
4340
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
4441
{%~ endfor %}

templates/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig templates/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
package {{ sdk.namespace | caseDot }}.services
22

3-
import androidx.activity.ComponentActivity
4-
import androidx.test.core.app.ApplicationProvider
5-
import androidx.test.ext.junit.runners.AndroidJUnit4
63
import {{ sdk.namespace | caseDot }}.Client
74
import {{ sdk.namespace | caseDot }}.Response
8-
import {{ sdk.namespace | caseDot }}.WebAuthComponent
95
import {{ sdk.namespace | caseDot }}.models.InputFile
106
import io.mockk.coEvery
117
import io.mockk.mockkObject
128
import io.mockk.spyk
139
import kotlinx.coroutines.test.runTest
1410
import org.junit.Before
1511
import org.junit.Test
16-
import org.junit.runner.RunWith
1712
import kotlin.test.assertIs
1813

14+
{% if language.name == 'Android' %}
15+
import androidx.activity.ComponentActivity
16+
import androidx.test.core.app.ApplicationProvider
17+
import androidx.test.ext.junit.runners.AndroidJUnit4
18+
import {{ sdk.namespace | caseDot }}.WebAuthComponent
19+
import org.junit.runner.RunWith
20+
1921
@RunWith(AndroidJUnit4::class)
22+
{% endif %}
2023
class {{service.name | caseUcfirst}}ServiceTest {
2124
private lateinit var client: Client
2225
private lateinit var {{service.name | caseCamel}}: {{service.name | caseUcfirst}}
2326

2427
@Before
2528
fun setup() {
26-
client = spyk(Client(ApplicationProvider.getApplicationContext()))
29+
client = spyk(Client({% if language.name == 'Android' %}ApplicationProvider.getApplicationContext(){% endif %}))
2730
{{service.name | caseCamel}} = {{service.name | caseUcfirst}}(client)
2831
}
2932

@@ -46,10 +49,16 @@ class {{service.name | caseUcfirst}}ServiceTest {
4649
{% endif %}
4750

4851
{%~ if method.type == 'webAuth' ~%}
52+
{%~ if language.name == 'Android' ~%}
4953
mockkObject(WebAuthComponent.Companion)
5054
coEvery { WebAuthComponent.Companion.authenticate(any(), any(), any(), any()) } answers {
5155
arg<((Result<String>) -> Unit)?>(3)?.invoke(Result.success("http://localhost/oauth?key=example&secret=example"))
5256
}
57+
{%~ else ~%}
58+
coEvery {
59+
client.awaitResponse<Any>(any(), any())
60+
} returns Response(true)
61+
{%~ endif ~%}
5362
{%~ else ~%}
5463
coEvery {
5564
client.awaitResponse<Any>(any(), any())
@@ -59,7 +68,7 @@ class {{service.name | caseUcfirst}}ServiceTest {
5968
runTest {
6069
val response = {{service.name | caseCamel}}.{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
6170
{{parameter.name | escapeKeyword | caseCamel}} = {% if parameter.type == 'object' %}mapOf<String, Any>(){% elseif parameter.type == 'array' %}listOf(){% elseif parameter.type == 'file' %}InputFile.fromBytes(byteArrayOf(), mimeType = "image/png"){% elseif parameter.type == 'boolean' %}true{% elseif parameter.type == 'string' %}"{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}"{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%},{%~ endfor ~%}
62-
{%~ if method.type == "webAuth" %}
71+
{%~ if method.type == "webAuth" and language.name == 'Android' %}
6372
activity = ComponentActivity(),
6473
{%~ endif %}
6574
)

0 commit comments

Comments
 (0)