update dependencies and code after it

This commit is contained in:
InsanusMokrassar 2020-11-11 23:59:59 +06:00
parent bd6abae994
commit d4bca9d09f
17 changed files with 83 additions and 80 deletions

View File

@ -2,6 +2,12 @@
## 0.4.0
**ALL PROJECT HAS MIGRATED FROM PACKAGE `com.insanusmokrassar` TO `dev.inmo`**
* `Kotlin`: `1.3.72` -> `1.4.10`
* `Serialization`: `0.20.0` -> `1.0.1`
## 0.3.1
* `Kotlin`: `1.3.71` -> `1.3.72`

View File

@ -37,7 +37,7 @@ kotlin {
dependencies {
implementation kotlin('stdlib')
api kotlin('reflect')
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlin_serialisation_runtime_version"
api "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_serialisation_runtime_version"
}
}
commonTest {
@ -46,11 +46,6 @@ kotlin {
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
}
}
jvmTest {
dependencies {
implementation kotlin('test-junit')

View File

@ -1,6 +1,6 @@
kotlin.code.style=official
kotlin_version=1.3.72
kotlin_serialisation_runtime_version=0.20.0
kotlin_version=1.4.10
kotlin_serialisation_runtime_version=1.0.1
gradle_bintray_plugin_version=1.8.5
github_release_plugin_version=2.2.12

View File

@ -1,5 +0,0 @@
package com.insanusmokrassar.sdi
import kotlinx.serialization.ContextualSerialization
class Module internal constructor(base: Map<String, @ContextualSerialization Any>) : Map<String, Any> by base

View File

@ -1,6 +1,5 @@
package com.insanusmokrassar.sdi
package dev.inmo.sdi
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModuleBuilder
import kotlin.reflect.KClass
@ -8,15 +7,13 @@ import kotlin.reflect.KClass
internal val nonStrictJson = Json {
isLenient = true
ignoreUnknownKeys = true
serializeSpecialFloatingPointValues = true
}
@ImplicitReflectionSerializer
fun Json.loadModule(
json: String,
vararg additionalClassesToInclude: KClass<*>,
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
): Module = parse(
): Module = decodeFromString(
if (moduleBuilder != null) {
ModuleDeserializerStrategy(moduleBuilder, *additionalClassesToInclude)
} else {
@ -25,7 +22,6 @@ fun Json.loadModule(
json
)
@ImplicitReflectionSerializer
fun loadModule(
json: String,
vararg additionalClassesToInclude: KClass<*>,

View File

@ -0,0 +1,5 @@
package dev.inmo.sdi
import kotlinx.serialization.Contextual
class Module internal constructor(base: Map<String, @Contextual Any>) : Map<String, Any> by base

View File

@ -1,36 +1,36 @@
package com.insanusmokrassar.sdi
package dev.inmo.sdi
import com.insanusmokrassar.sdi.utils.createModuleBasedOnConfigRoot
import dev.inmo.sdi.utils.createModuleBasedOnConfigRoot
import kotlinx.serialization.*
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.SerializersModuleBuilder
import kotlin.reflect.KClass
@ImplicitReflectionSerializer
internal class ModuleDeserializerStrategy(
private val moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null,
private vararg val additionalClassesToInclude: KClass<*>
) : DeserializationStrategy<Module> {
private val internalSerializer = MapSerializer(String.serializer(), ContextSerializer(Any::class))
private val internalSerializer = MapSerializer(String.serializer(), ContextualSerializer(Any::class))
override val descriptor: SerialDescriptor
get() = internalSerializer.descriptor
@InternalSerializationApi
override fun deserialize(decoder: Decoder): Module {
val json = JsonObjectSerializer.deserialize(decoder)
val json = JsonObject.serializer().deserialize(decoder)
val jsonSerialFormat = createModuleBasedOnConfigRoot(
json,
moduleBuilder,
decoder.context,
decoder.serializersModule,
*additionalClassesToInclude
)
val resultJson = JsonObject(
json.keys.associateWith { JsonPrimitive(it) }
)
val map = jsonSerialFormat.fromJson(internalSerializer, resultJson)
val map = jsonSerialFormat.decodeFromJsonElement(internalSerializer, resultJson)
return Module(map)
}
override fun patch(decoder: Decoder, old: Module): Module = throw UpdateNotSupportedException("Module")
}

View File

@ -1,25 +1,25 @@
package com.insanusmokrassar.sdi.utils
package dev.inmo.sdi.utils
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.SerializersModuleBuilder
import kotlin.reflect.KClass
internal object AlreadyRegisteredException : Exception()
@ImplicitReflectionSerializer
internal class DependencyResolver<T : Any>(
serialModuleBuilder: SerializersModuleBuilder,
kClass: KClass<T>,
private val formatterGetter: () -> Json,
private val dependencyGetter: (String) -> Any
) : KSerializer<T> {
private val originalSerializer: KSerializer<T> = try {
kClass.serializer()
} catch (e: Exception) {
ContextSerializer(kClass)
}
@InternalSerializationApi
private val originalSerializer: KSerializer<T> = kClass.serializerOrNull() ?: ContextualSerializer(kClass)
private val objectsCache = mutableMapOf<String, T>()
@InternalSerializationApi
override val descriptor: SerialDescriptor = originalSerializer.descriptor
init {
@ -39,8 +39,9 @@ internal class DependencyResolver<T : Any>(
}
}
@InternalSerializationApi
override fun deserialize(decoder: Decoder): T {
val decoded = decoder.decodeSerializableValue(JsonElementSerializer)
val decoded = decoder.decodeSerializableValue(JsonElement.serializer())
return when {
decoded is JsonPrimitive && decoded.contentOrNull != null -> decoded.content.let { dependencyName ->
@Suppress("UNCHECKED_CAST")
@ -49,14 +50,15 @@ internal class DependencyResolver<T : Any>(
}
}
decoded is JsonArray -> {
val serializer = resolveSerializerByPackageName(decoded.getPrimitive(0).content)
val serializer = resolveSerializerByPackageName(decoded[0].jsonPrimitive.content)
@Suppress("UNCHECKED_CAST")
formatterGetter().fromJson(serializer, decoded[1]) as T
formatterGetter().decodeFromJsonElement(serializer, decoded[1]) as T
}
else -> formatterGetter().fromJson(originalSerializer, decoded)
else -> formatterGetter().decodeFromJsonElement(originalSerializer, decoded)
}
}
@InternalSerializationApi
override fun serialize(encoder: Encoder, value: T) {
objectsCache.keys.firstOrNull {
objectsCache[it] === value

View File

@ -1,6 +1,5 @@
package com.insanusmokrassar.sdi.utils
package dev.inmo.sdi.utils
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
@ -18,15 +17,15 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies
}
} ?: throw IllegalArgumentException("Value on dependency name \"$currentKey\" is invalid: provided $this, but expected package name or other dependency name string")
is JsonObject -> return currentKey to null
is JsonArray -> return getPrimitive(0).contentOrNull ?.let { it to null } ?: throw IllegalArgumentException("Value on first argument of dependency value must be its package as a string, but was provided ${get(0)}")
is JsonArray -> return get(0).jsonPrimitive.contentOrNull ?.let { it to null } ?: throw IllegalArgumentException("Value on first argument of dependency value must be its package as a string, but was provided ${get(0)}")
}
}
@ImplicitReflectionSerializer
@InternalSerializationApi
internal fun createModuleBasedOnConfigRoot(
jsonObject: JsonObject,
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null,
baseContext: SerialModule,
baseContext: SerializersModule,
vararg additionalClassesToInclude: KClass<*>
): Json {
lateinit var caches: Map<String, () -> Any>
@ -53,7 +52,7 @@ internal fun createModuleBasedOnConfigRoot(
}
is JsonArray -> {
if (elemValue.size > 1) {
elemValue.getObject(1)
elemValue[1].jsonObject
} else {
JsonObject(emptyMap())
}
@ -61,7 +60,7 @@ internal fun createModuleBasedOnConfigRoot(
}
val serializer = resolveSerializerByPackageName(packageName)
return@callback jsonStringFormat.fromJson(serializer, argumentsObject) as Any
return@callback jsonStringFormat.decodeFromJsonElement(serializer, argumentsObject) as Any
}
}.toMap()
@ -105,10 +104,10 @@ internal fun createModuleBasedOnConfigRoot(
}
}
)
return Json(
configuration = JsonConfiguration(useArrayPolymorphism = true),
context = context
).also {
return Json {
useArrayPolymorphism = true
serializersModule = context
}.also {
jsonStringFormat = it
}
}

View File

@ -1,7 +1,6 @@
package com.insanusmokrassar.sdi.utils
package dev.inmo.sdi.utils
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.*
import kotlinx.serialization.modules.*
import kotlin.reflect.KClass
@ -11,7 +10,7 @@ fun <T : Any> SerializersModuleBuilder.optionalContextual(
) = try {
contextual(kClass, kSerializer)
true
} catch (e: SerializerAlreadyRegisteredException) {
} catch (e: SerializationException) {
false
}

View File

@ -1,18 +1,21 @@
package com.insanusmokrassar.sdi.utils
package dev.inmo.sdi.utils
import kotlinx.serialization.*
import kotlin.reflect.KClass
import kotlin.reflect.KType
internal expect fun resolveKClassByPackageName(packageName: String): KClass<*>
@ImplicitReflectionSerializer
@InternalSerializationApi
internal fun <T : Any> resolveSerializerByKClass(kClass: KClass<T>): KSerializer<T> = kClass.serializer()
@ImplicitReflectionSerializer
@InternalSerializationApi
internal fun resolveSerializerByPackageName(packageName: String): KSerializer<*> = resolveSerializerByKClass(
resolveKClassByPackageName(packageName)
)
internal expect val <T : Any> KClass<T>.supertypes: List<KType>
internal val KClass<*>.allSubclasses: Set<KClass<*>>
get() {
val subclasses = mutableSetOf<KClass<*>>()

View File

@ -1,4 +1,4 @@
package com.insanusmokrassar.sdi
package dev.inmo.sdi
import kotlinx.serialization.*
import kotlin.test.Test
@ -12,11 +12,10 @@ interface List_ChildAPI {
}
@Serializable
class List_Parent(override val services: List<@ContextualSerialization List_ChildAPI>) : List_ParentalAPI
class List_Parent(override val services: List<@Contextual List_ChildAPI>) : List_ParentalAPI
@Serializable
class List_Child(override val names: List<String>) : List_ChildAPI
@ImplicitReflectionSerializer
class ListTest {
val servicesNum = 10

View File

@ -1,4 +1,4 @@
package com.insanusmokrassar.sdi
package dev.inmo.sdi
import kotlinx.serialization.*
import kotlin.test.Test
@ -12,28 +12,32 @@ interface SimpleCustomObject_ServiceAPI {
}
@Serializable
class SimpleCustomObject_Controller(@ContextualSerialization val service: SimpleCustomObject_ServiceAPI) : SimpleCustomObject_ControllerAPI {
class SimpleCustomObject_Controller(@Contextual val service: SimpleCustomObject_ServiceAPI) :
SimpleCustomObject_ControllerAPI {
override fun showUp() {
println("Inited with name \"${service.names}\"")
}
}
@Serializable
class SimpleCustomObject_CustomController1(@ContextualSerialization val service: SimpleCustomObject_ServiceAPI) : SimpleCustomObject_ControllerAPI {
class SimpleCustomObject_CustomController1(@Contextual val service: SimpleCustomObject_ServiceAPI) :
SimpleCustomObject_ControllerAPI {
override fun showUp() {
println("Inited with name \"${service.names}\"")
}
}
@Serializable
class SimpleCustomObject_CustomController2(@ContextualSerialization val service: SimpleCustomObject_BusinessService) : SimpleCustomObject_ControllerAPI {
class SimpleCustomObject_CustomController2(@Contextual val service: SimpleCustomObject_BusinessService) :
SimpleCustomObject_ControllerAPI {
override fun showUp() {
println("Inited with name \"${service.names}\"")
}
}
@Serializable
class SimpleCustomObject_CustomController3(@ContextualSerialization val service: SimpleCustomObject_ServiceAPI) : SimpleCustomObject_ControllerAPI {
class SimpleCustomObject_CustomController3(@Contextual val service: SimpleCustomObject_ServiceAPI) :
SimpleCustomObject_ControllerAPI {
override fun showUp() {
println("Inited with name \"${service.names}\"")
}
@ -43,7 +47,6 @@ class SimpleCustomObject_BusinessService(override val names: List<String>) : Sim
@Serializable
class SimpleCustomObject_BusinessService1(override val names: List<String>) : SimpleCustomObject_ServiceAPI
@ImplicitReflectionSerializer
class SimpleCustomObjectTest {
@Test
fun test_that_simple_config_correctly_work() {

View File

@ -1,4 +1,4 @@
package com.insanusmokrassar.sdi
package dev.inmo.sdi
import kotlinx.serialization.*
import kotlin.test.Test
@ -12,7 +12,7 @@ interface Simple_ServiceAPI {
}
@Serializable
class Simple_Controller(@ContextualSerialization val service: Simple_ServiceAPI) : Simple_ControllerAPI {
class Simple_Controller(@Contextual val service: Simple_ServiceAPI) : Simple_ControllerAPI {
override fun showUp() {
println("Inited with name \"${service.names}\"")
}
@ -20,7 +20,6 @@ class Simple_Controller(@ContextualSerialization val service: Simple_ServiceAPI)
@Serializable
class Simple_BusinessService(override val names: List<String>) : Simple_ServiceAPI
@ImplicitReflectionSerializer
class SimpleTest {
@Test
fun test_that_simple_config_correctly_work() {

View File

@ -1,5 +0,0 @@
package com.insanusmokrassar.sdi.utils
import kotlin.reflect.KClass
actual fun resolveKClassByPackageName(packageName: String): KClass<*> = Class.forName(packageName).kotlin

View File

@ -1,34 +1,33 @@
package com.insanusmokrassar.sdi
package dev.inmo.sdi
import kotlin.reflect.KClass
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModuleBuilder
import java.io.File
import java.io.InputStream
@ImplicitReflectionSerializer
fun Json.loadModule(
stream: InputStream,
vararg additionalClassesToInclude: KClass<*>,
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
) = loadModule(stream.reader().readText(), *additionalClassesToInclude, moduleBuilder = moduleBuilder)
@ImplicitReflectionSerializer
fun loadModule(
stream: InputStream,
vararg additionalClassesToInclude: KClass<*>,
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
) = nonStrictJson.loadModule(stream, *additionalClassesToInclude, moduleBuilder = moduleBuilder)
@ImplicitReflectionSerializer
fun Json.loadModule(
file: File,
vararg additionalClassesToInclude: KClass<*>,
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
) = loadModule(file.inputStream(), *additionalClassesToInclude, moduleBuilder = moduleBuilder)
@ImplicitReflectionSerializer
fun loadModule(
file: File,
vararg additionalClassesToInclude: KClass<*>,

View File

@ -0,0 +1,8 @@
package dev.inmo.sdi.utils
import kotlin.reflect.KClass
import kotlin.reflect.KType
actual fun resolveKClassByPackageName(packageName: String): KClass<*> = Class.forName(packageName).kotlin
internal actual val <T : Any> KClass<T>.supertypes: List<KType>
get() = supertypes