Skip to content

Commit

Permalink
Merge pull request #107 from ghasemdev/feat-object-module
Browse files Browse the repository at this point in the history
feat: support object modules
  • Loading branch information
arnaudgiuliani authored Jun 27, 2024
2 parents e14b5e9 + bfaa3d5 commit c398a66
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
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 @@ -56,7 +56,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 @@ -33,10 +33,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,
isCreatedAtStart = isCreatedAtStart,
Expand Down

0 comments on commit c398a66

Please sign in to comment.