decrease number of expected functions

This commit is contained in:
InsanusMokrassar 2020-02-13 12:56:04 +06:00
parent 1b87562035
commit ea29a801fd
10 changed files with 42 additions and 53 deletions

View File

@ -36,7 +36,7 @@ kotlin {
commonMain {
dependencies {
implementation kotlin('stdlib')
api "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
api kotlin('reflect')
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlin_serialisation_runtime_version"
}
}

View File

@ -1,11 +1,10 @@
package com.insanusmokrassar.sdi
import com.insanusmokrassar.sdi.utils.*
import com.insanusmokrassar.sdi.utils.createModuleBasedOnConfigRoot
import kotlinx.serialization.*
import kotlinx.serialization.internal.*
import kotlinx.serialization.internal.HashMapSerializer
import kotlinx.serialization.internal.StringSerializer
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlin.reflect.KClass
@ImplicitReflectionSerializer
@Serializable(ModuleSerializer::class)

View File

@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi.utils
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.modules.SerializerAlreadyRegisteredException
import kotlinx.serialization.modules.SerializersModuleBuilder
import kotlin.reflect.KClass
@ImplicitReflectionSerializer
@ -13,7 +14,7 @@ internal class DependencyResolver<T : Any>(
private val dependencyGetter: (String) -> Any
) : KSerializer<T> {
private val originalSerializer: KSerializer<T> = try {
resolveSerializerByKClass(kClass)
kClass.serializer()
} catch (e: Exception) {
PolymorphicSerializer(kClass)
}

View File

@ -1,8 +1,9 @@
package com.insanusmokrassar.sdi.utils
import kotlinx.serialization.*
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.modules.SerializerAlreadyRegisteredException
import kotlinx.serialization.modules.SerializersModule
private typealias PackageOrOtherDependencyNamePair = Pair<String?, String?>
@ -63,25 +64,26 @@ internal fun createModuleBasedOnConfigRoot(jsonObject: JsonObject): Json {
key to packageName
}.toMap()
return Json(
configuration = JsonConfiguration(useArrayPolymorphism = true),
context = SerializersModule {
keysToPackages.values.forEach {
val kclass = resolveKClassByPackageName(it)
val context = SerializersModule {
keysToPackages.values.forEach {
val kclass = resolveKClassByPackageName(it)
try {
DependencyResolver(
this,
kclass,
{ jsonStringFormat }
) {
caches.getValue(it).invoke()
}
} catch (e: SerializerAlreadyRegisteredException) {
// here we are thinking that already registered
try {
DependencyResolver(
this,
kclass,
{ jsonStringFormat }
) {
caches.getValue(it).invoke()
}
} catch (e: SerializerAlreadyRegisteredException) {
// here we are thinking that already registered
}
}
}
return Json(
configuration = JsonConfiguration(useArrayPolymorphism = true),
context = context
).also {
jsonStringFormat = it
}

View File

@ -1,24 +1,22 @@
package com.insanusmokrassar.sdi.utils
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.*
import kotlin.reflect.KClass
import kotlin.reflect.KType
@ImplicitReflectionSerializer
expect fun resolveSerializerByPackageName(packageName: String): KSerializer<*>
@ImplicitReflectionSerializer
expect fun <T : Any> resolveSerializerByKClass(kClass: KClass<T>): KSerializer<T>
expect fun resolveKClassByPackageName(packageName: String): KClass<*>
expect val KClass<*>.currentSupertypes: List<KType>
@ImplicitReflectionSerializer
internal fun <T : Any> resolveSerializerByKClass(kClass: KClass<T>): KSerializer<T> = kClass.serializer()
val KClass<*>.allSubclasses: Set<KClass<*>>
@ImplicitReflectionSerializer
internal fun resolveSerializerByPackageName(packageName: String): KSerializer<*> = resolveSerializerByKClass(
resolveKClassByPackageName(packageName)
)
internal val KClass<*>.allSubclasses: Set<KClass<*>>
get() {
val subclasses = mutableSetOf<KClass<*>>()
val leftToVisit = currentSupertypes.mapNotNull { it.classifier as? KClass<*> }.toMutableList()
val leftToVisit = supertypes.mapNotNull { it.classifier as? KClass<*> }.toMutableList()
while (leftToVisit.isNotEmpty()) {
val top = leftToVisit.removeAt(0)
if (subclasses.add(top)) {

View File

@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
interface List_ParentalAPI {
val services: List<List_ChildAPI>

View File

@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
interface SimpleCustomObject_ControllerAPI {
fun showUp()

View File

@ -2,7 +2,8 @@ package com.insanusmokrassar.sdi
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
interface Simple_ControllerAPI {
fun showUp()

View File

@ -1,7 +0,0 @@
package com.insanusmokrassar.sdi.utils
import kotlin.reflect.KClass
import kotlin.reflect.KType
actual val KClass<*>.currentSupertypes: List<KType>
get() = this.supertypes

View File

@ -1,12 +1,5 @@
package com.insanusmokrassar.sdi.utils
import kotlinx.serialization.*
import kotlin.reflect.KClass
@ImplicitReflectionSerializer
actual fun resolveSerializerByPackageName(packageName: String): KSerializer<*> = Class.forName(packageName).kotlin.serializer()
@ImplicitReflectionSerializer
actual fun <T : Any> resolveSerializerByKClass(kClass: KClass<T>): KSerializer<T> = kClass.serializer()
actual fun resolveKClassByPackageName(packageName: String): KClass<*> = Class.forName(packageName).kotlin