Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support object modules #107

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ fun OutputStream.generateModuleFunctionDeclarationDefinition(def: KoinMetaData.D
generateDefinition(def) { "moduleInstance.${def.functionName}" }
}

fun OutputStream.generateObjectModuleFunctionDeclarationDefinition(
def: KoinMetaData.Definition.FunctionDefinition,
modulePath: String
) {
generateDefinition(def) { "$modulePath.${def.functionName}" }
}

fun OutputStream.generateFunctionDeclarationDefinition(def: KoinMetaData.Definition.FunctionDefinition) {
generateDefinition(def) { "${def.packageNamePrefix}${def.functionName}" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun generateClassModule(classFile: OutputStream, module: KoinMetaData.Module) {
// to able to call the function on this instance.
it is KoinMetaData.Definition.FunctionDefinition &&
it.isClassFunction
}) {
} && !module.type.isObject) {
classFile.appendText("${NEW_LINE}val moduleInstance = $modulePath()")
}

Expand All @@ -82,28 +82,34 @@ private fun generateDefinitions(
classFile: OutputStream
) {
val standardDefinitions = module.definitions.filter { it.isNotScoped() }
standardDefinitions.forEach { it.generateTargetDefinition(classFile) }
standardDefinitions.forEach { it.generateTargetDefinition(module, classFile) }

val scopeDefinitions = module.definitions.filter { it.isScoped() }
scopeDefinitions
.groupBy { it.scope }
.forEach { (scope, definitions) ->
classFile.appendText(generateScope(scope!!))
definitions.forEach {
it.generateTargetDefinition(classFile)
it.generateTargetDefinition(module, classFile)
}
// close scope
classFile.appendText("\n\t\t\t\t}")
}
}

private fun KoinMetaData.Definition.generateTargetDefinition(
module: KoinMetaData.Module,
classFile: OutputStream
) {
when (this) {
is KoinMetaData.Definition.FunctionDefinition -> {
if (isClassFunction) {
classFile.generateModuleFunctionDeclarationDefinition(this)
if (module.type.isObject) {
val modulePath = "${module.packageName}.${module.name}"
classFile.generateObjectModuleFunctionDeclarationDefinition(this, modulePath)
} else{
classFile.generateModuleFunctionDeclarationDefinition(this)
}
} else {
classFile.generateFunctionDeclarationDefinition(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ sealed class KoinMetaData {
}

enum class ModuleType {
FIELD, CLASS
FIELD, CLASS, OBJECT;

val isObject: Boolean
get() = this == OBJECT
}

sealed class Scope {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ class ModuleScanner(
val componentScan = getComponentScan(annotations)

val name = "$element"
val type = if (declaration.classKind == ClassKind.OBJECT) {
KoinMetaData.ModuleType.OBJECT
} else {
KoinMetaData.ModuleType.CLASS
}

val moduleMetadata = KoinMetaData.Module(
packageName = modulePackage,
name = name,
type = KoinMetaData.ModuleType.CLASS,
type = type,
componentScan = componentScan,
includes = includes,
visibility = declaration.getVisibility()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ import org.koin.compiler.generator.getFile
import org.koin.compiler.metadata.KoinMetaData
import java.io.OutputStream

private val ignored = listOf(
"kotlin.Any",
"androidx.lifecycle.SavedStateHandle",
"android.content.Context",
"android.app.Application"
)
private val ignored = listOf("kotlin.Any",)
private val classPrefix = "KoinDef"
private val generationPackage = "org.koin.ksp.generated"

Expand Down
Loading