add kdocs to koin common module

This commit is contained in:
2025-04-14 10:03:26 +06:00
parent 5ec0a46089
commit 9c463d0338
8 changed files with 122 additions and 4 deletions

View File

@@ -4,8 +4,14 @@ import org.koin.core.definition.Definition
import org.koin.core.module.Module
/**
* Will be useful in case you need to declare some singles with one type several types, but need to separate them and do
* not care about how :)
* Declares a factory with a random qualifier in the Koin module.
* This is useful when you need to declare multiple factory definitions of the same type
* but want them to be uniquely identifiable without manually specifying qualifiers.
* Unlike singles, factories create a new instance each time they are requested.
*
* @param T The type of instance to be created by the factory
* @param definition The definition function that creates new instances
* @return A Koin definition for the factory with a random qualifier
*/
inline fun <reified T : Any> Module.factoryWithRandomQualifier(
noinline definition: Definition<T>

View File

@@ -4,6 +4,16 @@ import org.koin.core.definition.Definition
import org.koin.core.module.Module
import org.koin.core.qualifier.StringQualifier
/**
* Declares a factory with a string qualifier in the Koin module.
* This is a convenience function that wraps the string qualifier in a [StringQualifier].
* Unlike singles, factories create a new instance each time they are requested.
*
* @param T The type of instance to be created by the factory
* @param qualifier The string value to be used as a qualifier
* @param definition The definition function that creates new instances
* @return A Koin definition for the factory with the specified string qualifier
*/
inline fun <reified T : Any> Module.factory(
qualifier: String,
noinline definition: Definition<T>

View File

@@ -3,6 +3,21 @@ package dev.inmo.micro_utils.koin
import org.koin.core.Koin
import org.koin.core.scope.Scope
/**
* Retrieves all instances of type [T] from the current [Scope] and returns them as a distinct list.
* This function is useful when you want to avoid duplicate instances of the same type.
*
* @param T The type of instances to retrieve
* @return A list of distinct instances of type [T]
*/
inline fun <reified T : Any> Scope.getAllDistinct() = getAll<T>().distinct()
/**
* Retrieves all instances of type [T] from the [Koin] container and returns them as a distinct list.
* This function is useful when you want to avoid duplicate instances of the same type.
*
* @param T The type of instances to retrieve
* @return A list of distinct instances of type [T]
*/
inline fun <reified T : Any> Koin.getAllDistinct() = getAll<T>().distinct()

View File

@@ -3,6 +3,23 @@ package dev.inmo.micro_utils.koin
import org.koin.core.Koin
import org.koin.core.scope.Scope
/**
* Retrieves the first available instance of type [T] from the current scope.
* This is useful when you need any instance of a type and don't care which one.
*
* @param T The type of instance to retrieve
* @return The first available instance of type [T]
* @throws NoSuchElementException if no instances of type [T] are available
*/
inline fun <reified T : Any> Scope.getAny() = getAll<T>().first()
/**
* Retrieves the first available instance of type [T] from the Koin container.
* This is useful when you need any instance of a type and don't care which one.
*
* @param T The type of instance to retrieve
* @return The first available instance of type [T]
* @throws NoSuchElementException if no instances of type [T] are available
*/
inline fun <reified T : Any> Koin.getAny() = getAll<T>().first()

View File

@@ -7,33 +7,81 @@ import org.koin.core.instance.InstanceFactory
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.scope.Scope
/**
* Retrieves an instance of type [T] from the Koin container using a [BeanDefinition].
*
* @param T The type of instance to retrieve
* @param definition The bean definition to use for instance retrieval
* @param parameters Optional parameters to pass to the instance constructor
* @return An instance of type [T]
*/
fun <T> Koin.get(definition: BeanDefinition<T>, parameters: ParametersDefinition? = null): T = get(
definition.primaryType,
definition.qualifier,
parameters
)
/**
* Retrieves an instance of type [T] from the Koin container using an [InstanceFactory].
*
* @param T The type of instance to retrieve
* @param definition The instance factory to use for instance retrieval
* @param parameters Optional parameters to pass to the instance constructor
* @return An instance of type [T]
*/
fun <T> Koin.get(definition: InstanceFactory<T>, parameters: ParametersDefinition? = null): T = get(
definition.beanDefinition,
parameters
)
/**
* Retrieves an instance of type [T] from the Koin container using a [KoinDefinition].
*
* @param T The type of instance to retrieve
* @param definition The Koin definition to use for instance retrieval
* @param parameters Optional parameters to pass to the instance constructor
* @return An instance of type [T]
*/
fun <T> Koin.get(definition: KoinDefinition<T>, parameters: ParametersDefinition? = null): T = get(
definition.factory,
parameters
)
/**
* Retrieves an instance of type [T] from the current scope using a [BeanDefinition].
*
* @param T The type of instance to retrieve
* @param definition The bean definition to use for instance retrieval
* @param parameters Optional parameters to pass to the instance constructor
* @return An instance of type [T]
*/
fun <T> Scope.get(definition: BeanDefinition<T>, parameters: ParametersDefinition? = null): T = get(
definition.primaryType,
definition.qualifier,
parameters
)
/**
* Retrieves an instance of type [T] from the current scope using an [InstanceFactory].
*
* @param T The type of instance to retrieve
* @param definition The instance factory to use for instance retrieval
* @param parameters Optional parameters to pass to the instance constructor
* @return An instance of type [T]
*/
fun <T> Scope.get(definition: InstanceFactory<T>, parameters: ParametersDefinition? = null): T = get(
definition.beanDefinition,
parameters
)
/**
* Retrieves an instance of type [T] from the current scope using a [KoinDefinition].
*
* @param T The type of instance to retrieve
* @param definition The Koin definition to use for instance retrieval
* @param parameters Optional parameters to pass to the instance constructor
* @return An instance of type [T]
*/
fun <T> Scope.get(definition: KoinDefinition<T>, parameters: ParametersDefinition? = null): T = get(
definition.factory,
parameters

View File

@@ -3,4 +3,10 @@ package dev.inmo.micro_utils.koin
import com.benasher44.uuid.uuid4
import org.koin.core.qualifier.StringQualifier
/**
* Creates a [StringQualifier] with a random string value.
*
* @param randomFun A function that generates a random string. By default, it uses UUID4 string representation.
* @return A [StringQualifier] with a randomly generated string value
*/
fun RandomQualifier(randomFun: () -> String = { uuid4().toString() }) = StringQualifier(randomFun())

View File

@@ -4,8 +4,14 @@ import org.koin.core.definition.Definition
import org.koin.core.module.Module
/**
* Will be useful in case you need to declare some singles with one type several types, but need to separate them and do
* not care about how :)
* Declares a single instance with a random qualifier in the Koin module.
* This is useful when you need to declare multiple instances of the same type
* but want them to be uniquely identifiable without manually specifying qualifiers.
*
* @param T The type of instance to be created
* @param createdAtStart Whether the instance should be created when the Koin module starts (default: false)
* @param definition The definition function that creates the instance
* @return A Koin definition for the single instance with a random qualifier
*/
inline fun <reified T : Any> Module.singleWithRandomQualifier(
createdAtStart: Boolean = false,

View File

@@ -4,6 +4,16 @@ import org.koin.core.definition.Definition
import org.koin.core.module.Module
import org.koin.core.qualifier.StringQualifier
/**
* Declares a single instance with a string qualifier in the Koin module.
* This is a convenience function that wraps the string qualifier in a [StringQualifier].
*
* @param T The type of instance to be created
* @param qualifier The string value to be used as a qualifier
* @param createdAtStart Whether the instance should be created when the Koin module starts (default: false)
* @param definition The definition function that creates the instance
* @return A Koin definition for the single instance with the specified string qualifier
*/
inline fun <reified T : Any> Module.single(
qualifier: String,
createdAtStart: Boolean = false,