Skip to content

Commit

Permalink
Fix generation of wrapper to recognize pagingsource as an async retur…
Browse files Browse the repository at this point in the history
…n type. Add DoorJsonRequest and DoorJsonResponse.
  • Loading branch information
mikedawson committed Feb 28, 2023
1 parent 42ff6ac commit 2228e35
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ plugins {


group 'com.github.UstadMobile.door'
version '0.0.63o1'
version '0.0.63o4'

ext.localProperties = new Properties()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fun TypeSpec.Builder.addDaoFunctionDelegate(

//If running on JS, non-suspended (sync) version is NOT allowed
val isSyncFunctionOnJs = doorTarget == DoorTarget.JS && !overridingFunction.isSuspended
&& (overridingFunction.returnType?.isDataSourceFactoryOrLiveData() == false)
&& (overridingFunction.returnType?.isAsynchronousReturnType() == false)

val entityParam = overridingFunction.parameters.firstOrNull()
val entityComponentClassDecl: KSClassDeclaration? =
Expand All @@ -112,7 +112,7 @@ fun TypeSpec.Builder.addDaoFunctionDelegate(
.applyIf(isSyncFunctionOnJs) {
if(doorTarget == DoorTarget.JS && !overridingFunction.isSuspended) {
add("throw %T(%S)\n", ClassName("kotlin", "IllegalStateException"),
"Synchronous db access is NOT possible on Javascript!")
"${daoFunDeclaration.simpleName.asString()}: synchronous db access is NOT possible on Javascript!")
}
}
.applyIf(!isSyncFunctionOnJs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.ustadmobile.door.lifecycle.LiveData
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.ustadmobile.door.jdbc.TypesKmp

import kotlinx.coroutines.flow.Flow
/**
* Map a Kotlin Type to JDBC Types constant
*/
Expand All @@ -21,15 +21,23 @@ fun TypeName.toSqlTypesInt() = when {
else -> throw IllegalArgumentException("Could not get sqlTypeInt for: $this")
}

internal fun TypeName.isDataSourceFactory(paramTypeFilter: (List<TypeName>) -> Boolean = {true}): Boolean {
internal fun TypeName.isDataSourceFactory(): Boolean {
return this is ParameterizedTypeName
&& this.rawType == com.ustadmobile.door.paging.DataSourceFactory::class.asClassName()
&& paramTypeFilter(this.typeArguments)
}

internal fun TypeName.isPagingSource(): Boolean {
return this is ParameterizedTypeName
&& this.rawType == com.ustadmobile.door.paging.PagingSource::class.asClassName()
}

internal fun TypeName.isFlow(): Boolean {
return this is ParameterizedTypeName && this.rawType == Flow::class.asClassName()
}


internal fun TypeName.isDataSourceFactoryOrLiveData() = this is ParameterizedTypeName
&& (this.isDataSourceFactory() || this.rawType == LiveData::class.asClassName())
internal fun TypeName.isAsynchronousReturnType() = this is ParameterizedTypeName
&& (isDataSourceFactory() || isPagingSource() || isFlow () || this.rawType == LiveData::class.asClassName())

fun TypeName.isListOrArray() = (this is ClassName && this.canonicalName =="kotlin.Array")
|| (this is ParameterizedTypeName && this.rawType == List::class.asClassName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ val KSFunctionDeclaration.useSuspendedQuery: Boolean
}

return isSuspended ||
(returnTypeDecl?.isLiveData() == true) ||
(returnTypeDecl?.isDataSourceFactory() == true) ||
(returnTypeDecl?.isPagingSource() == true)
returnTypeDecl?.isLiveData() == true ||
returnTypeDecl?.isDataSourceFactory() == true ||
returnTypeDecl?.isFlow() == true ||
returnTypeDecl?.isPagingSource() == true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ustadmobile.door.requests

/**
* Simple data class that is used as a common representation of a json http request. This can then be sent using KTOR
* over HTTP, Android Interprocess (IPC) Messenger Service, or Bluetooth.
*
* An extension function will be generated for each database function that will create a request object roughly as follows:
*
* fun DaoClass.functionName_JsonRequest(repoConfig, param1: String, param2) {
* return DoorJsonRequest(
* method = "GET",
* protocol = "http",
* path = "repoConfig.path/DaoName/functionName",
* queryParams = mapOf("param1" to param1,
* "param2" to param2),
* )
* }
*
* @param queryParams map of query parameters to send. Simplified by design not to allow duplicate keys
* @param headers map of headers to send. Simplified by design not to allow duplicate keys
*/
data class DoorJsonRequest(
val method: Method,
val protocol: String,
val host: String,
val path: String,
val queryParams: Map<String, String>,
val headers: Map<String, String>,
val requestBody: String? = null,
){

enum class Method {
GET, POST, PUT
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ustadmobile.door.requests

/**
* Simple data class that is used as a common representation of a json http response. This can then be sent using KTOR
* over HTTP, Android Interprocess (IPC) Messenger Service, or Bluetooth.
*/
data class DoorJsonResponse(
val statusCode: Int,
val headers: Map<String, String>,
val responseBody: String?
) {
}

0 comments on commit 2228e35

Please sign in to comment.