mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-18 06:49:20 +00:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
ef9828d817 | |||
d1a5bdd04d |
22
CHANGELOG.md
22
CHANGELOG.md
@@ -1,27 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## 0.24.4
|
|
||||||
|
|
||||||
* `Repos`:
|
|
||||||
* `Exposed`:
|
|
||||||
* Improve `CommonExposedRepo.selectByIds`
|
|
||||||
* `FSM`:
|
|
||||||
* Fixes and improvements
|
|
||||||
|
|
||||||
## 0.24.3
|
|
||||||
|
|
||||||
* `Ksp`:
|
|
||||||
* `Sealed`:
|
|
||||||
* Fixes in processing of `GenerateSealedTypesWorkaround` annotations
|
|
||||||
|
|
||||||
## 0.24.2
|
|
||||||
|
|
||||||
* `Versions`:
|
|
||||||
* `Exposed`: `0.57.0` -> `0.58.0`
|
|
||||||
* `Ksp`:
|
|
||||||
* `Sealed`:
|
|
||||||
* Add annotation `GenerateSealedTypesWorkaround` which allow to generate `subtypes` lists
|
|
||||||
|
|
||||||
## 0.24.1
|
## 0.24.1
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@@ -12,7 +12,6 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
api project(":micro_utils.common")
|
api project(":micro_utils.common")
|
||||||
api project(":micro_utils.coroutines")
|
api project(":micro_utils.coroutines")
|
||||||
api libs.kslog
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,5 @@
|
|||||||
package dev.inmo.micro_utils.fsm.common
|
package dev.inmo.micro_utils.fsm.common
|
||||||
|
|
||||||
import dev.inmo.kslog.common.TagLogger
|
|
||||||
import dev.inmo.kslog.common.e
|
|
||||||
import dev.inmo.micro_utils.common.Optional
|
import dev.inmo.micro_utils.common.Optional
|
||||||
import dev.inmo.micro_utils.coroutines.*
|
import dev.inmo.micro_utils.coroutines.*
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
||||||
@@ -70,7 +68,6 @@ open class DefaultStatesMachine <T: State>(
|
|||||||
protected val handlers: List<CheckableHandlerHolder<in T, T>>,
|
protected val handlers: List<CheckableHandlerHolder<in T, T>>,
|
||||||
protected val onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler()
|
protected val onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler()
|
||||||
) : StatesMachine<T> {
|
) : StatesMachine<T> {
|
||||||
protected val logger = TagLogger(this::class.simpleName!!)
|
|
||||||
/**
|
/**
|
||||||
* Will call [launchStateHandling] for state handling
|
* Will call [launchStateHandling] for state handling
|
||||||
*/
|
*/
|
||||||
@@ -99,13 +96,7 @@ open class DefaultStatesMachine <T: State>(
|
|||||||
statesJobsMutex.withLock {
|
statesJobsMutex.withLock {
|
||||||
statesJobs[actualState] ?.cancel()
|
statesJobs[actualState] ?.cancel()
|
||||||
statesJobs[actualState] = scope.launch {
|
statesJobs[actualState] = scope.launch {
|
||||||
runCatching {
|
|
||||||
performUpdate(actualState)
|
performUpdate(actualState)
|
||||||
}.onFailure {
|
|
||||||
logger.e(it) {
|
|
||||||
"Unable to perform update of state from $actualState"
|
|
||||||
}
|
|
||||||
}.getOrThrow()
|
|
||||||
}.also { job ->
|
}.also { job ->
|
||||||
job.invokeOnCompletion { _ ->
|
job.invokeOnCompletion { _ ->
|
||||||
scope.launch {
|
scope.launch {
|
||||||
|
@@ -9,12 +9,7 @@ interface StatesManager<T : State> {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It is expected, that [new] state will be saved in manager.
|
* Must set current set using [State.context]
|
||||||
*
|
|
||||||
* If [new] context will not be equal to [old] one, it must do some check of availability for replacement
|
|
||||||
* of potentially exists state on [new] context. If this state can't be replaced, it will throw [IllegalStateException]
|
|
||||||
*
|
|
||||||
* @throws IllegalStateException - in case when [new] [State] can't be set
|
|
||||||
*/
|
*/
|
||||||
suspend fun update(old: T, new: T)
|
suspend fun update(old: T, new: T)
|
||||||
|
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
package dev.inmo.micro_utils.fsm.common
|
package dev.inmo.micro_utils.fsm.common
|
||||||
|
|
||||||
import dev.inmo.kslog.common.e
|
|
||||||
import dev.inmo.micro_utils.common.*
|
import dev.inmo.micro_utils.common.*
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
|
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
|
||||||
@@ -45,13 +44,7 @@ open class DefaultUpdatableStatesMachine<T : State>(
|
|||||||
val job = previousState.mapOnPresented {
|
val job = previousState.mapOnPresented {
|
||||||
statesJobs.remove(it)
|
statesJobs.remove(it)
|
||||||
} ?.takeIf { it.isActive } ?: scope.launch {
|
} ?.takeIf { it.isActive } ?: scope.launch {
|
||||||
runCatching {
|
|
||||||
performUpdate(actualState)
|
performUpdate(actualState)
|
||||||
}.onFailure {
|
|
||||||
logger.e(it) {
|
|
||||||
"Unable to perform update of state up to $actualState"
|
|
||||||
}
|
|
||||||
}.getOrThrow()
|
|
||||||
}.also { job ->
|
}.also { job ->
|
||||||
job.invokeOnCompletion { _ ->
|
job.invokeOnCompletion { _ ->
|
||||||
scope.launch {
|
scope.launch {
|
||||||
|
@@ -1,11 +1,10 @@
|
|||||||
package dev.inmo.micro_utils.fsm.common.managers
|
package dev.inmo.micro_utils.fsm.common.managers
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.SmartRWLocker
|
|
||||||
import dev.inmo.micro_utils.coroutines.withReadAcquire
|
|
||||||
import dev.inmo.micro_utils.coroutines.withWriteLock
|
|
||||||
import dev.inmo.micro_utils.fsm.common.State
|
import dev.inmo.micro_utils.fsm.common.State
|
||||||
import dev.inmo.micro_utils.fsm.common.StatesManager
|
import dev.inmo.micro_utils.fsm.common.StatesManager
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implement this repo if you want to use some custom repo for [DefaultStatesManager]
|
* Implement this repo if you want to use some custom repo for [DefaultStatesManager]
|
||||||
@@ -20,14 +19,6 @@ interface DefaultStatesManagerRepo<T : State> {
|
|||||||
* NOT be removed
|
* NOT be removed
|
||||||
*/
|
*/
|
||||||
suspend fun removeState(state: T)
|
suspend fun removeState(state: T)
|
||||||
|
|
||||||
/**
|
|
||||||
* Semantically, calls [removeState] and then [set]
|
|
||||||
*/
|
|
||||||
suspend fun removeAndSet(toRemove: T, toSet: T) {
|
|
||||||
removeState(toRemove)
|
|
||||||
set(toSet)
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @return Current list of available and saved states
|
* @return Current list of available and saved states
|
||||||
*/
|
*/
|
||||||
@@ -67,7 +58,7 @@ open class DefaultStatesManager<T : State>(
|
|||||||
protected val _onEndChain = MutableSharedFlow<T>(0)
|
protected val _onEndChain = MutableSharedFlow<T>(0)
|
||||||
override val onEndChain: Flow<T> = _onEndChain.asSharedFlow()
|
override val onEndChain: Flow<T> = _onEndChain.asSharedFlow()
|
||||||
|
|
||||||
protected val internalLocker = SmartRWLocker()
|
protected val mapMutex = Mutex()
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
repo: DefaultStatesManagerRepo<T>,
|
repo: DefaultStatesManagerRepo<T>,
|
||||||
@@ -77,30 +68,28 @@ open class DefaultStatesManager<T : State>(
|
|||||||
onUpdateContextsConflictResolver = onContextsConflictResolver
|
onUpdateContextsConflictResolver = onContextsConflictResolver
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun update(old: T, new: T) = internalLocker.withWriteLock {
|
override suspend fun update(old: T, new: T) = mapMutex.withLock {
|
||||||
val stateByOldContext: T? = repo.getContextState(old.context)
|
val stateByOldContext: T? = repo.getContextState(old.context)
|
||||||
when {
|
when {
|
||||||
stateByOldContext != old -> return@withWriteLock
|
stateByOldContext != old -> return@withLock
|
||||||
old.context == new.context -> {
|
stateByOldContext == null || old.context == new.context -> {
|
||||||
repo.removeAndSet(old, new)
|
repo.removeState(old)
|
||||||
|
repo.set(new)
|
||||||
_onChainStateUpdated.emit(old to new)
|
_onChainStateUpdated.emit(old to new)
|
||||||
}
|
}
|
||||||
old.context != new.context -> {
|
else -> {
|
||||||
val stateOnNewOneContext = repo.getContextState(new.context)
|
val stateOnNewOneContext = repo.getContextState(new.context)
|
||||||
if (stateOnNewOneContext == null || onUpdateContextsConflictResolver(old, new, stateOnNewOneContext)) {
|
if (stateOnNewOneContext == null || onUpdateContextsConflictResolver(old, new, stateOnNewOneContext)) {
|
||||||
stateOnNewOneContext ?.let { endChainWithoutLock(it) }
|
stateOnNewOneContext ?.let { endChainWithoutLock(it) }
|
||||||
repo.removeAndSet(old, new)
|
repo.removeState(old)
|
||||||
|
repo.set(new)
|
||||||
_onChainStateUpdated.emit(old to new)
|
_onChainStateUpdated.emit(old to new)
|
||||||
} else {
|
|
||||||
error(
|
|
||||||
"Unable to update state from $old to $new due to false answer from $onUpdateContextsConflictResolver and state on old context $stateOnNewOneContext"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun startChain(state: T) = internalLocker.withWriteLock {
|
override suspend fun startChain(state: T) = mapMutex.withLock {
|
||||||
val stateOnContext = repo.getContextState(state.context)
|
val stateOnContext = repo.getContextState(state.context)
|
||||||
if (stateOnContext == null || onStartContextsConflictResolver(stateOnContext, state)) {
|
if (stateOnContext == null || onStartContextsConflictResolver(stateOnContext, state)) {
|
||||||
stateOnContext ?.let {
|
stateOnContext ?.let {
|
||||||
@@ -119,13 +108,11 @@ open class DefaultStatesManager<T : State>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun endChain(state: T) {
|
override suspend fun endChain(state: T) {
|
||||||
internalLocker.withWriteLock {
|
mapMutex.withLock {
|
||||||
endChainWithoutLock(state)
|
endChainWithoutLock(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getActiveStates(): List<T> = internalLocker.withReadAcquire {
|
override suspend fun getActiveStates(): List<T> = repo.getStates()
|
||||||
repo.getStates()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,59 +1,25 @@
|
|||||||
package dev.inmo.micro_utils.fsm.repos.common
|
package dev.inmo.micro_utils.fsm.repos.common
|
||||||
|
|
||||||
import dev.inmo.kslog.common.TagLogger
|
|
||||||
import dev.inmo.kslog.common.i
|
|
||||||
import dev.inmo.micro_utils.coroutines.SmartRWLocker
|
|
||||||
import dev.inmo.micro_utils.coroutines.withReadAcquire
|
|
||||||
import dev.inmo.micro_utils.coroutines.withWriteLock
|
|
||||||
import dev.inmo.micro_utils.fsm.common.State
|
import dev.inmo.micro_utils.fsm.common.State
|
||||||
import dev.inmo.micro_utils.fsm.common.managers.DefaultStatesManagerRepo
|
import dev.inmo.micro_utils.fsm.common.managers.DefaultStatesManagerRepo
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
import dev.inmo.micro_utils.repos.pagination.getAll
|
import dev.inmo.micro_utils.repos.pagination.getAll
|
||||||
import dev.inmo.micro_utils.repos.unset
|
|
||||||
|
|
||||||
class KeyValueBasedDefaultStatesManagerRepo<T : State>(
|
class KeyValueBasedDefaultStatesManagerRepo<T : State>(
|
||||||
private val keyValueRepo: KeyValueRepo<Any, T>
|
private val keyValueRepo: KeyValueRepo<Any, T>
|
||||||
) : DefaultStatesManagerRepo<T> {
|
) : DefaultStatesManagerRepo<T> {
|
||||||
private val locker = SmartRWLocker()
|
|
||||||
private val logger = TagLogger("KeyValueBasedDefaultStatesManagerRepo")
|
|
||||||
override suspend fun set(state: T) {
|
override suspend fun set(state: T) {
|
||||||
locker.withWriteLock {
|
|
||||||
keyValueRepo.set(state.context, state)
|
keyValueRepo.set(state.context, state)
|
||||||
logger.i { "Set ${state.context} value to $state" }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun removeState(state: T) {
|
override suspend fun removeState(state: T) {
|
||||||
locker.withWriteLock {
|
|
||||||
if (keyValueRepo.get(state.context) == state) {
|
if (keyValueRepo.get(state.context) == state) {
|
||||||
keyValueRepo.unset(state.context)
|
keyValueRepo.unset(state.context)
|
||||||
logger.i { "Unset $state" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun removeAndSet(toRemove: T, toSet: T) {
|
|
||||||
locker.withWriteLock {
|
|
||||||
when {
|
|
||||||
toRemove.context == toSet.context -> {
|
|
||||||
keyValueRepo.set(toSet.context, toSet)
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
keyValueRepo.set(toSet.context, toSet)
|
|
||||||
keyValueRepo.unset(toRemove)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getStates(): List<T> = locker.withReadAcquire {
|
override suspend fun getStates(): List<T> = keyValueRepo.getAll { keys(it) }.map { it.second }
|
||||||
keyValueRepo.getAll { keys(it) }.map { it.second }
|
override suspend fun getContextState(context: Any): T? = keyValueRepo.get(context)
|
||||||
}
|
|
||||||
override suspend fun getContextState(context: Any): T? = locker.withReadAcquire {
|
|
||||||
keyValueRepo.get(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun contains(context: Any): Boolean = locker.withReadAcquire {
|
override suspend fun contains(context: Any): Boolean = keyValueRepo.contains(context)
|
||||||
keyValueRepo.contains(context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.24.4
|
version=0.24.1
|
||||||
android_code_version=283
|
android_code_version=280
|
||||||
|
@@ -7,7 +7,7 @@ kt-coroutines = "1.10.1"
|
|||||||
kslog = "1.4.0"
|
kslog = "1.4.0"
|
||||||
|
|
||||||
jb-compose = "1.7.3"
|
jb-compose = "1.7.3"
|
||||||
jb-exposed = "0.58.0"
|
jb-exposed = "0.57.0"
|
||||||
jb-dokka = "2.0.0"
|
jb-dokka = "2.0.0"
|
||||||
|
|
||||||
sqlite = "3.47.2.0"
|
sqlite = "3.47.2.0"
|
||||||
|
@@ -3,15 +3,9 @@ package dev.inmo.micro_utils.ksp.sealed.generator
|
|||||||
import com.google.devtools.ksp.KspExperimental
|
import com.google.devtools.ksp.KspExperimental
|
||||||
import com.google.devtools.ksp.getAnnotationsByType
|
import com.google.devtools.ksp.getAnnotationsByType
|
||||||
import com.google.devtools.ksp.symbol.KSClassDeclaration
|
import com.google.devtools.ksp.symbol.KSClassDeclaration
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedTypesWorkaround
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround as OldGenerateSealedWorkaround
|
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround as OldGenerateSealedWorkaround
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
@OptIn(KspExperimental::class)
|
||||||
val KSClassDeclaration.getGenerateSealedWorkaroundAnnotation
|
val KSClassDeclaration.getGenerateSealedWorkaroundAnnotation
|
||||||
get() = (getAnnotationsByType(GenerateSealedWorkaround::class).firstOrNull() ?: getAnnotationsByType(OldGenerateSealedWorkaround::class).firstOrNull())
|
get() = (getAnnotationsByType(GenerateSealedWorkaround::class).firstOrNull() ?: getAnnotationsByType(OldGenerateSealedWorkaround::class).firstOrNull())
|
||||||
|
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
|
||||||
val KSClassDeclaration.getGenerateSealedTypesWorkaroundAnnotation
|
|
||||||
get() = getAnnotationsByType(GenerateSealedTypesWorkaround::class).firstOrNull()
|
|
||||||
|
@@ -6,17 +6,21 @@ import com.google.devtools.ksp.processing.CodeGenerator
|
|||||||
import com.google.devtools.ksp.processing.Resolver
|
import com.google.devtools.ksp.processing.Resolver
|
||||||
import com.google.devtools.ksp.processing.SymbolProcessor
|
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||||
import com.google.devtools.ksp.symbol.*
|
import com.google.devtools.ksp.symbol.*
|
||||||
import com.squareup.kotlinpoet.*
|
import com.squareup.kotlinpoet.ClassName
|
||||||
|
import com.squareup.kotlinpoet.CodeBlock
|
||||||
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
|
import com.squareup.kotlinpoet.FunSpec
|
||||||
|
import com.squareup.kotlinpoet.KModifier
|
||||||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
||||||
|
import com.squareup.kotlinpoet.PropertySpec
|
||||||
|
import com.squareup.kotlinpoet.asTypeName
|
||||||
import com.squareup.kotlinpoet.ksp.toClassName
|
import com.squareup.kotlinpoet.ksp.toClassName
|
||||||
import dev.inmo.micro_ksp.generator.buildSubFileName
|
import dev.inmo.micro_ksp.generator.buildSubFileName
|
||||||
import dev.inmo.micro_ksp.generator.companion
|
import dev.inmo.micro_ksp.generator.companion
|
||||||
import dev.inmo.micro_ksp.generator.findSubClasses
|
import dev.inmo.micro_ksp.generator.findSubClasses
|
||||||
import dev.inmo.micro_ksp.generator.writeFile
|
import dev.inmo.micro_ksp.generator.writeFile
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedTypesWorkaround
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
class Processor(
|
class Processor(
|
||||||
private val codeGenerator: CodeGenerator
|
private val codeGenerator: CodeGenerator
|
||||||
@@ -105,62 +109,6 @@ class Processor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
|
||||||
private fun FileSpec.Builder.generateSealedTypesWorkaround(
|
|
||||||
ksClassDeclaration: KSClassDeclaration,
|
|
||||||
resolver: Resolver
|
|
||||||
) {
|
|
||||||
val annotation = ksClassDeclaration.getGenerateSealedTypesWorkaroundAnnotation
|
|
||||||
val subClasses = ksClassDeclaration.resolveSubclasses(
|
|
||||||
searchIn = resolver.getAllFiles(),
|
|
||||||
allowNonSealed = annotation ?.includeNonSealedSubTypes ?: false
|
|
||||||
).distinct()
|
|
||||||
val subClassesNames = subClasses.filter {
|
|
||||||
it.getAnnotationsByType(GenerateSealedTypesWorkaround.Exclude::class).count() == 0
|
|
||||||
}.sortedBy {
|
|
||||||
(it.getAnnotationsByType(GenerateSealedTypesWorkaround.Order::class).firstOrNull()) ?.order ?: 0
|
|
||||||
}.map {
|
|
||||||
it.toClassName()
|
|
||||||
}.toList()
|
|
||||||
val className = ksClassDeclaration.toClassName()
|
|
||||||
val setType = Set::class.asTypeName().parameterizedBy(
|
|
||||||
KClass::class.asTypeName().parameterizedBy(
|
|
||||||
TypeVariableName(
|
|
||||||
"out ${ksClassDeclaration.asStarProjectedType().toClassName().simpleNames.joinToString(".")}",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
addProperty(
|
|
||||||
PropertySpec.builder(
|
|
||||||
"subtypes",
|
|
||||||
setType
|
|
||||||
).apply {
|
|
||||||
modifiers.add(
|
|
||||||
KModifier.PRIVATE
|
|
||||||
)
|
|
||||||
initializer(
|
|
||||||
CodeBlock.of(
|
|
||||||
"""setOf(${subClassesNames.joinToString(",\n") { it.simpleNames.joinToString(".") + "::class" }})"""
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}.build()
|
|
||||||
)
|
|
||||||
addFunction(
|
|
||||||
FunSpec.builder("subtypes").apply {
|
|
||||||
val companion = ksClassDeclaration.takeIf { it.isCompanionObject } ?.toClassName()
|
|
||||||
?: ksClassDeclaration.companion ?.toClassName()
|
|
||||||
?: ClassName(className.packageName, *className.simpleNames.toTypedArray(), "Companion")
|
|
||||||
receiver(companion)
|
|
||||||
returns(setType)
|
|
||||||
addCode(
|
|
||||||
CodeBlock.of(
|
|
||||||
"""return subtypes"""
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}.build()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
@OptIn(KspExperimental::class)
|
||||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||||
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
||||||
@@ -183,26 +131,6 @@ class Processor(
|
|||||||
}.build()
|
}.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(resolver.getSymbolsWithAnnotation(GenerateSealedTypesWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
|
||||||
val prefix = (it.getGenerateSealedTypesWorkaroundAnnotation) ?.prefix ?.takeIf {
|
|
||||||
it.isNotEmpty()
|
|
||||||
} ?: it.buildSubFileName.replaceFirst(it.simpleName.asString(), "")
|
|
||||||
it.writeFile(prefix = prefix, suffix = "SealedTypesWorkaround") {
|
|
||||||
FileSpec.builder(
|
|
||||||
it.packageName.asString(),
|
|
||||||
"${it.simpleName.getShortName()}SealedTypesWorkaround"
|
|
||||||
).apply {
|
|
||||||
addFileComment(
|
|
||||||
"""
|
|
||||||
THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
|
|
||||||
TO REGENERATE IT JUST DELETE FILE
|
|
||||||
ORIGINAL FILE: ${it.containingFile ?.fileName}
|
|
||||||
""".trimIndent()
|
|
||||||
)
|
|
||||||
generateSealedTypesWorkaround(it, resolver)
|
|
||||||
}.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
@@ -1,19 +1,14 @@
|
|||||||
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedTypesWorkaround
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
|
|
||||||
@GenerateSealedWorkaround
|
@GenerateSealedWorkaround
|
||||||
@GenerateSealedTypesWorkaround
|
|
||||||
sealed interface Test {
|
sealed interface Test {
|
||||||
@GenerateSealedWorkaround.Order(2)
|
@GenerateSealedWorkaround.Order(2)
|
||||||
@GenerateSealedTypesWorkaround.Exclude
|
|
||||||
object A : Test
|
object A : Test
|
||||||
@GenerateSealedWorkaround.Exclude
|
@GenerateSealedWorkaround.Exclude
|
||||||
@GenerateSealedTypesWorkaround.Order(2)
|
|
||||||
object B : Test
|
object B : Test
|
||||||
@GenerateSealedWorkaround.Order(0)
|
@GenerateSealedWorkaround.Order(0)
|
||||||
@GenerateSealedTypesWorkaround.Order(0)
|
|
||||||
object C : Test
|
object C : Test
|
||||||
|
|
||||||
// Required for successful sealed workaround generation
|
// Required for successful sealed workaround generation
|
||||||
|
@@ -1,12 +0,0 @@
|
|||||||
// THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
|
|
||||||
// TO REGENERATE IT JUST DELETE FILE
|
|
||||||
// ORIGINAL FILE: Test.kt
|
|
||||||
package dev.inmo.micro_utils.ksp.`sealed`.generator.test
|
|
||||||
|
|
||||||
import kotlin.collections.Set
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
private val subtypes: Set<KClass<out Test>> = setOf(Test.C::class,
|
|
||||||
Test.B::class)
|
|
||||||
|
|
||||||
public fun Test.Companion.subtypes(): Set<KClass<out Test>> = subtypes
|
|
@@ -1,40 +0,0 @@
|
|||||||
import dev.inmo.micro_utils.ksp.sealed.generator.test.subtypes
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.generator.test.values
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
class TestTests {
|
|
||||||
@Test
|
|
||||||
fun testThatAfterCompilationTestWorkaroundsHaveCorrectValues() {
|
|
||||||
val correctValues = arrayOf(
|
|
||||||
dev.inmo.micro_utils.ksp.sealed.generator.test.Test.C,
|
|
||||||
dev.inmo.micro_utils.ksp.sealed.generator.test.Test.A,
|
|
||||||
)
|
|
||||||
val correctSubtypes = arrayOf(
|
|
||||||
dev.inmo.micro_utils.ksp.sealed.generator.test.Test.C::class,
|
|
||||||
dev.inmo.micro_utils.ksp.sealed.generator.test.Test.B::class,
|
|
||||||
)
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
correctValues.size, dev.inmo.micro_utils.ksp.sealed.generator.test.Test.values().size
|
|
||||||
)
|
|
||||||
correctValues.forEachIndexed { index, value ->
|
|
||||||
assertTrue(
|
|
||||||
value === dev.inmo.micro_utils.ksp.sealed.generator.test.Test.values().elementAt(index)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
correctSubtypes.size, dev.inmo.micro_utils.ksp.sealed.generator.test.Test.subtypes().size
|
|
||||||
)
|
|
||||||
correctSubtypes.forEachIndexed { index, value ->
|
|
||||||
assertTrue(
|
|
||||||
value.qualifiedName != null
|
|
||||||
)
|
|
||||||
assertTrue(
|
|
||||||
value.qualifiedName === dev.inmo.micro_utils.ksp.sealed.generator.test.Test.subtypes().elementAt(index).qualifiedName
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,15 +0,0 @@
|
|||||||
package dev.inmo.micro_utils.ksp.sealed
|
|
||||||
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
annotation class GenerateSealedTypesWorkaround(
|
|
||||||
val prefix: String = "",
|
|
||||||
val includeNonSealedSubTypes: Boolean = false,
|
|
||||||
) {
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
annotation class Order(val order: Int)
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
annotation class Exclude
|
|
||||||
}
|
|
@@ -4,7 +4,7 @@ package dev.inmo.micro_utils.ksp.sealed
|
|||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
annotation class GenerateSealedWorkaround(
|
annotation class GenerateSealedWorkaround(
|
||||||
val prefix: String = "",
|
val prefix: String = "",
|
||||||
val includeNonSealedSubTypes: Boolean = false,
|
val includeNonSealedSubTypes: Boolean = false
|
||||||
) {
|
) {
|
||||||
@Retention(AnnotationRetention.BINARY)
|
@Retention(AnnotationRetention.BINARY)
|
||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
"extends": [
|
"extends": [
|
||||||
"config:recommended"
|
"config:base"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@@ -8,16 +8,8 @@ interface CommonExposedRepo<IdType, ObjectType> : ExposedRepo {
|
|||||||
val selectById: ISqlExpressionBuilder.(IdType) -> Op<Boolean>
|
val selectById: ISqlExpressionBuilder.(IdType) -> Op<Boolean>
|
||||||
val selectByIds: ISqlExpressionBuilder.(List<IdType>) -> Op<Boolean>
|
val selectByIds: ISqlExpressionBuilder.(List<IdType>) -> Op<Boolean>
|
||||||
get() = {
|
get() = {
|
||||||
if (it.isEmpty()) {
|
it.foldRight<IdType, Op<Boolean>?>(null) { id, acc ->
|
||||||
Op.FALSE
|
acc ?.or(selectById(id)) ?: selectById(id)
|
||||||
} else {
|
} ?: Op.FALSE
|
||||||
var op = it.firstOrNull() ?.let { selectById(it) } ?: Op.FALSE
|
|
||||||
var i = 1
|
|
||||||
while (i < it.size) {
|
|
||||||
op = op.or(selectById(it[i]))
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
op
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user