Skip to content

Commit

Permalink
Complete kotlin refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarendorff committed Oct 22, 2023
1 parent 54cf128 commit 4d99588
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/SDK/Language/Android.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function getFiles(): array
[
'scope' => 'service',
'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/services/{{ service.name | caseUcfirst }}ServiceTest.kt',
'template' => '/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig',
'template' => '/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig',
],
[
'scope' => 'default',
Expand Down
39 changes: 37 additions & 2 deletions src/SDK/Language/Kotlin.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,50 @@ public function getFiles(): array
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Permission.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/Permission.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/PermissionTest.kt',
'template' => '/kotlin/src/test/kotlin/io/appwrite/PermissionTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Role.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/Role.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/RoleTest.kt',
'template' => '/kotlin/src/test/kotlin/io/appwrite/RoleTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/ID.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/ID.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/IDTest.kt',
'template' => '/kotlin/src/test/kotlin/io/appwrite/IDTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Query.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/Query.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/QueryTest.kt',
'template' => '/kotlin/src/test/kotlin/io/appwrite/QueryTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Response.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/Response.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/coroutines/Callback.kt',
'template' => '/android/library/src/main/java/io/appwrite/coroutines/Callback.kt.twig',
'template' => '/kotlin/src/main/kotlin/io/appwrite/coroutines/Callback.kt.twig',
],
[
'scope' => 'default',
Expand Down Expand Up @@ -404,6 +429,11 @@ public function getFiles(): array
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/services/ServiceTemplate.kt.twig',
],
[
'scope' => 'service',
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}ServiceTest.kt',
'template' => '/kotlin/src/test/kotlin/io/appwrite/services/ServiceTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/InputFile.kt',
Expand All @@ -419,6 +449,11 @@ public function getFiles(): array
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Model.kt.twig',
],
[
'scope' => 'definition',
'destination' => '/src/test/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}Test.kt',
'template' => '/kotlin/src/test/kotlin/io/appwrite/models/ModelTest.kt.twig',
],
];
}

Expand All @@ -443,7 +478,7 @@ public function getFilters(): array
protected function getReturnType(array $method, array $spec, string $namespace, string $generic = 'T'): string
{
if ($method['type'] === 'webAuth') {
return 'Bool';
return 'Boolean';
}
if ($method['type'] === 'location') {
return 'ByteArray';
Expand Down
2 changes: 1 addition & 1 deletion templates/android/library/build.gradle.twig
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ dependencies {
testImplementation "androidx.test.ext:junit-ktx:1.1.5"
testImplementation "androidx.test:core-ktx:1.5.0"
testImplementation "org.robolectric:robolectric:4.10.3"
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"

androidTestImplementation 'androidx.test:runner:1.5.2'
}
Expand Down
2 changes: 2 additions & 0 deletions templates/kotlin/build.gradle.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ dependencies {
implementation("com.squareup.okhttp3:logging-interceptor")
implementation("com.google.code.gson:gson:2.9.0")

testImplementation 'io.mockk:mockk:1.13.7'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package {{ sdk.namespace | caseDot }}

import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import {{ sdk.namespace | caseDot }}.Response
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
import {{ sdk.namespace | caseDot }}.extensions.fromJson
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
Expand Down Expand Up @@ -109,7 +110,6 @@ class Client @JvmOverloads constructor(

val builder = OkHttpClient()
.newBuilder()
.cookieJar(JavaNetCookieJar(cookieJar))

if (!selfSigned) {
http = builder.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class {{ service.name | caseUcfirst }} : Service {
{%~ endif %}
@Throws({{ spec.title | caseUcfirst }}Exception::class)
suspend fun {% if method.responseModel | hasGenericType(spec) %}{{ '<T>' | raw }} {% endif %}{{ method.name | caseCamel }}(
{%~ if method.type == "webAuth" %}
activity: ComponentActivity,
{%~ endif %}
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
{%~ endfor %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package {{ sdk.namespace | caseDot }}.services

import androidx.activity.ComponentActivity
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import {{ sdk.namespace | caseDot }}.Client
import {{ sdk.namespace | caseDot }}.Response
import {{ sdk.namespace | caseDot }}.WebAuthComponent
import {{ sdk.namespace | caseDot }}.models.InputFile
import io.mockk.coEvery
import io.mockk.mockkObject
import io.mockk.spyk
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertIs

{% if language.name == 'Android' %}
import androidx.activity.ComponentActivity
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import {{ sdk.namespace | caseDot }}.WebAuthComponent
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
{% endif %}
class {{service.name | caseUcfirst}}ServiceTest {
private lateinit var client: Client
private lateinit var {{service.name | caseCamel}}: {{service.name | caseUcfirst}}

@Before
fun setup() {
client = spyk(Client(ApplicationProvider.getApplicationContext()))
client = spyk(Client({% if language.name == 'Android' %}ApplicationProvider.getApplicationContext(){% endif %}))
{{service.name | caseCamel}} = {{service.name | caseUcfirst}}(client)
}

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

{%~ if method.type == 'webAuth' ~%}
{%~ if language.name == 'Android' ~%}
mockkObject(WebAuthComponent.Companion)
coEvery { WebAuthComponent.Companion.authenticate(any(), any(), any(), any()) } answers {
arg<((Result<String>) -> Unit)?>(3)?.invoke(Result.success("http://localhost/oauth?key=example&secret=example"))
}
{%~ else ~%}
coEvery {
client.awaitResponse<Any>(any(), any())
} returns Response(true)
{%~ endif ~%}
{%~ else ~%}
coEvery {
client.awaitResponse<Any>(any(), any())
Expand All @@ -59,7 +68,7 @@ class {{service.name | caseUcfirst}}ServiceTest {
runTest {
val response = {{service.name | caseCamel}}.{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
{{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 ~%}
{%~ if method.type == "webAuth" %}
{%~ if method.type == "webAuth" and language.name == 'Android' %}
activity = ComponentActivity(),
{%~ endif %}
)
Expand Down

0 comments on commit 4d99588

Please sign in to comment.