diff --git a/CHANGELOG.md b/CHANGELOG.md index 77efb545d2f..c6a1a2bb924 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.22.0 +## 0.21.6 + +* `KSP`: + * `Sealed`: + * Fixes in generation + ## 0.21.5 * `KSP`: diff --git a/ksp/sealed/generator/src/main/kotlin/Processor.kt b/ksp/sealed/generator/src/main/kotlin/Processor.kt index de653834d08..6eca44452a7 100644 --- a/ksp/sealed/generator/src/main/kotlin/Processor.kt +++ b/ksp/sealed/generator/src/main/kotlin/Processor.kt @@ -26,7 +26,8 @@ class Processor( private fun KSClassDeclaration.findSealedConnection(potentialSealedParent: KSClassDeclaration): Boolean { val targetClassname = potentialSealedParent.qualifiedName ?.asString() return superTypes.any { - targetClassname == ((it.resolve().declaration as? KSClassDeclaration) ?.qualifiedName ?.asString()) || (it is KSClassDeclaration && it.getSealedSubclasses().any() && it.findSealedConnection(potentialSealedParent)) + val itAsDeclaration = it.resolve().declaration as? KSClassDeclaration ?: return@any false + targetClassname == (itAsDeclaration.qualifiedName ?.asString()) || (itAsDeclaration.getSealedSubclasses().any() && itAsDeclaration.findSealedConnection(potentialSealedParent)) } } diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt index 28a9b94983f..c029c83f954 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt @@ -39,6 +39,7 @@ interface WriteCRUDRepo : Repo { suspend fun update(id: IdType, value: InputValueType): ObjectType? suspend fun update(values: List>): List suspend fun deleteById(ids: List) + suspend fun deleteById(vararg ids: IdType) = deleteById(ids.toList()) } typealias WriteStandardCRUDRepo = WriteCRUDRepo diff --git a/repos/inmemory/src/commonTest/kotlin/MapCRUDRepoTest.kt b/repos/inmemory/src/commonTest/kotlin/MapCRUDRepoTest.kt new file mode 100644 index 00000000000..3dc84bfb78d --- /dev/null +++ b/repos/inmemory/src/commonTest/kotlin/MapCRUDRepoTest.kt @@ -0,0 +1,266 @@ +import dev.inmo.micro_utils.repos.MapCRUDRepo +import dev.inmo.micro_utils.repos.create +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals + +data class TestModel( + val testery: String, + val isTestery: Boolean, + val testeryAmount: Int +) + +class MapCRUDRepoTest { + /** + * A utility function used for generating non-intersecting ranges in cyclic environment, like [repeat] + * or for-loops + */ + private fun getIdRange(start: Int, base: Int) = (start * base)..(start * base + (base - 1)) + + @Test + fun mapCrudRepoTest() = runTest { + val base = 4 // define a bigger number to increase test intensity + val targetMap = mutableMapOf() + + // filling the map + val jobs = List(base) { jobId -> + launch(Dispatchers.Default) { // to make delays work + println("launching job $jobId") + delay((1..5).random().toLong()) + + val idsList = getIdRange(jobId, base).toMutableList() + println("created ids list $idsList") + val mapCRUDRepo = MapCRUDRepo( + targetMap, + createCallback = { new -> + val newObject = idsList.first() to new + idsList.remove(newObject.first) + println("created $newObject in map") + newObject + }, + updateCallback = { new, id, old -> + println("updated object at $id from $old to $new") + new + } + ) + for (id in getIdRange(jobId, base)) { + val newModel = TestModel( + testery = "testery$id", + isTestery = id % 2 == 0, + testeryAmount = id + ) + mapCRUDRepo.create(newModel) + } + } + } + + jobs.forEach { + println("joining job ${it.hashCode()}") + it.join() + } + + // validating the map + val validationJobs = List(base) { id -> + launch { + val idsList = getIdRange(id, base).toMutableList() + val mapCRUDRepo = MapCRUDRepo( + targetMap, + // these will not be used + createCallback = { new -> + val newObject = idsList.first() to new + idsList.remove(newObject.first) + println("created $newObject in map") + newObject + }, + updateCallback = { new, id, old -> + println("updated object at $id from $old to $new") + new + } + ) + for (i in getIdRange(id, base)) { + val newModel = TestModel( + testery = "testery$i", + isTestery = i % 2 == 0, + testeryAmount = i + ) + val oldModel = mapCRUDRepo.getById(i) + assertEquals(newModel, oldModel) + } + } + } + + validationJobs.forEach { + println("joining validation job ${it.hashCode()}") + it.join() + } + } + + @Test + fun updateMapCrudRepoTest() = runTest { + val base = 10 // define a bigger number to increase test intensity + val targetMap = mutableMapOf() + + // fill the map using stdlib method + repeat(base * base) { num -> + targetMap[num] = TestModel( + testery = "testery$num", + isTestery = num % 2 == 0, + testeryAmount = num + ) + } + + // update the map using MapCRUDRepo + val jobs = List(base) { jobId -> + launch { + println("launching job $jobId") + val idsList = getIdRange(jobId, base).toMutableList() + val mapCRUDRepo = MapCRUDRepo( + targetMap, + updateCallback = { new, id, old -> + println("updated object at $id from $old to $new") + new + }, + createCallback = { new -> + val newObject = idsList.first() to new + idsList.remove(newObject.first) + println("created $newObject in map") + newObject + } + ) + for (id in getIdRange(jobId, base)) { + val newModel = TestModel( + testery = "testery${id}new", + isTestery = id % 2 == 0, + testeryAmount = id + ) + mapCRUDRepo.update(id, newModel) + } + } + } + + jobs.forEach { + println("joining job ${it.hashCode()}") + it.join() + } + + // validating the map + val validationJobs = List(base) { jobId -> + launch { + val idsList = getIdRange(jobId, base).toMutableList() + val mapCRUDRepo = MapCRUDRepo( + targetMap, + updateCallback = { new, id, old -> + println("updated object at $id from $old to $new") + new + }, + createCallback = { new -> + val newObject = idsList.first() to new + idsList.remove(newObject.first) + println("created $newObject in map") + newObject + } + ) + for (i in getIdRange(jobId, base)) { + val newModel = TestModel( + testery = "testery${i}new", + isTestery = i % 2 == 0, + testeryAmount = i + ) + val oldModel = mapCRUDRepo.getById(i) + assertEquals(newModel, oldModel) + } + } + } + + validationJobs.forEach { + println("joining validation job ${it.hashCode()}") + it.join() + } + } + + @Test + fun deleteMapCrudRepoTest() = runTest { + val base = 10 // define a bigger number to increase test intensity + val targetMap = mutableMapOf() + + // fill the map using stdlib methods + repeat(base * base) { num -> + targetMap[num] = TestModel( + testery = "testery$num", + isTestery = num % 2 == 0, + testeryAmount = num + ) + } + + // delete every even element from the map + val jobs = List(base) { jobId -> + launch { + val idsList = getIdRange(jobId, base).toMutableList() + println("idsList: $idsList") + val mapCRUDRepo = MapCRUDRepo( + targetMap, + // these will not be used + createCallback = { new -> + val newObject = idsList.first() to new + idsList.remove(newObject.first) + println("created $newObject in map") + newObject + }, + updateCallback = { new, id, old -> + println("updated object at $id from $old to $new") + new + } + ) + for (id in getIdRange(jobId, base)) { + if (id % 2 == 0 /* testery.isTestery */) { + println("deleting $id") + mapCRUDRepo.deleteById(id) + } + } + } + } + + jobs.forEach { + println("joining job ${it.hashCode()}") + it.join() + } + + // validating the map + val validationJobs = List(base) { id -> + launch { + val idsList = getIdRange(id, base).toMutableList() + val mapCRUDRepo = MapCRUDRepo( + targetMap, + // these will not be used + createCallback = { new -> + val newObject = idsList.first() to new + idsList.remove(newObject.first) + println("created $newObject in map") + newObject + }, + updateCallback = { new, id, old -> + println("updated object at $id from $old to $new") + new + } + ) + for (id in getIdRange(id, base)) { + val newModel = TestModel( + testery = "testery${id}", + isTestery = id % 2 == 0, + testeryAmount = id + ) + val oldModel = mapCRUDRepo.getById(id) + assertEquals(if (newModel.isTestery) null else newModel, oldModel) + } + } + } + + validationJobs.forEach { + println("joining validation job ${it.hashCode()}") + it.join() + } + } +} \ No newline at end of file