forked from daugeldauge/kinzhal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
daugeldauge#2 Lazy/provider injection
- Loading branch information
Ivan Samoylenko
committed
Jan 7, 2024
1 parent
64f84c2
commit e1c7788
Showing
9 changed files
with
146 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...sor/src/jvmMain/kotlin/com/daugeldauge/kinzhal/processor/generation/ComponentGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ssor/src/jvmMain/kotlin/com/daugeldauge/kinzhal/processor/generation/FactoryDependency.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.daugeldauge.kinzhal.processor.generation | ||
|
||
import com.daugeldauge.kinzhal.annotations.Assisted | ||
import com.daugeldauge.kinzhal.processor.model.Key | ||
import com.daugeldauge.kinzhal.processor.requireQualifiedName | ||
import com.daugeldauge.kinzhal.processor.resolveToUnderlying | ||
import com.daugeldauge.kinzhal.processor.toKey | ||
import com.google.devtools.ksp.symbol.KSValueParameter | ||
import com.squareup.kotlinpoet.CodeBlock | ||
import com.squareup.kotlinpoet.LambdaTypeName | ||
|
||
internal data class FactoryDependency( | ||
val parameterName: String, | ||
val key: Key, | ||
val isAssisted: Boolean, | ||
val laziness: Laziness, | ||
) { | ||
enum class Laziness { | ||
None, | ||
Lazy, | ||
Provider, | ||
} | ||
|
||
companion object { | ||
fun fromParameter(parameter: KSValueParameter): FactoryDependency { | ||
val resolvedParameter = parameter.type.resolveToUnderlying() | ||
val isAssisted = parameter.isAssisted | ||
|
||
val (laziness, adjustedType) = when { | ||
isAssisted -> Laziness.None to parameter.type | ||
|
||
resolvedParameter.declaration.qualifiedName?.asString() == Lazy::class.java.name -> { | ||
Laziness.Lazy to resolvedParameter.arguments.first().type!! | ||
} | ||
|
||
resolvedParameter.declaration.qualifiedName?.asString() == "kotlin.Function0" -> { | ||
Laziness.Provider to resolvedParameter.arguments.first().type!! | ||
} | ||
|
||
else -> Laziness.None to parameter.type | ||
} | ||
return FactoryDependency( | ||
parameterName = parameter.name!!.asString(), | ||
key = adjustedType.toKey(parameter.annotations), | ||
laziness = laziness, | ||
isAssisted = isAssisted, | ||
) | ||
} | ||
} | ||
} | ||
|
||
internal val FactoryDependency.providerName: String | ||
get() = if (isAssisted) { | ||
parameterName | ||
} else { | ||
"${parameterName}Provider" | ||
} | ||
|
||
internal val FactoryDependency.providerType: LambdaTypeName | ||
get() = LambdaTypeName.get(returnType = key.asTypeName()) | ||
|
||
internal fun CodeBlock.Builder.addProviderCodeBlock(dependency: FactoryDependency, transitiveProvider: Boolean) { | ||
if (transitiveProvider) { | ||
add("%N", dependency.providerName) | ||
} else { | ||
when (dependency.laziness) { | ||
FactoryDependency.Laziness.None -> add("%N()", dependency.providerName) | ||
FactoryDependency.Laziness.Lazy -> add("lazy { %N() }", dependency.providerName) | ||
FactoryDependency.Laziness.Provider -> add("%N", dependency.providerName) | ||
} | ||
} | ||
} | ||
|
||
private val KSValueParameter.isAssisted: Boolean | ||
get() = annotations.any { | ||
it.annotationType.resolve().declaration.qualifiedName?.asString() == Assisted::class.requireQualifiedName() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.