All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.ansman.dagger.auto.compiler.common.rendering.HiltModuleBuilder.kt Maven / Gradle / Ivy

package se.ansman.dagger.auto.compiler.common.rendering

import dagger.Lazy
import dagger.multibindings.IntoMap
import dagger.multibindings.IntoSet
import se.ansman.dagger.auto.compiler.common.models.HiltModule
import javax.inject.Provider
import kotlin.reflect.KClass

interface HiltModuleBuilder {
    fun addProvider(
        name: String,
        returnType: DaggerType,
        isPublic: Boolean,
        parameters: List> = emptyList(),
        mode: ProviderMode = ProviderMode.Single,
        contents: (List) -> CodeBlock,
    ): HiltModuleBuilder

    fun addBinding(
        name: String,
        sourceType: DaggerType,
        returnType: DaggerType,
        isPublic: Boolean,
        mode: ProviderMode = ProviderMode.Single,
    ): HiltModuleBuilder

    fun addOptionalBinding(
        name: String,
        type: DaggerType,
        isPublic: Boolean,
    ): HiltModuleBuilder

    fun build(): SourceFile

    fun interface Factory {
        fun create(
            info: HiltModule,
        ): HiltModuleBuilder
    }

    sealed class Installation {
        abstract val components: Set

        data class InstallIn(override val components: Set) : Installation() {
            constructor(vararg components: ClassName) : this(components.toSet())
        }

        data class TestInstallIn(
            override val components: Set,
            val replaces: Set,
        ) : Installation()
    }

    sealed class Parameter {
        abstract val qualifiers: Set
    }

    data class Lazy(
        val type: Parameter,
    ) : Parameter() {
        override val qualifiers: Set get() = type.qualifiers

        constructor(
            type: TypeName,
            qualifiers: Set = emptySet(),
        ) : this(DaggerType(type, qualifiers))
    }

    data class Provider(
        val type: Parameter,
    ) : Parameter() {
        override val qualifiers: Set get() = type.qualifiers

        constructor(
            type: TypeName,
            qualifiers: Set = emptySet(),
        ) : this(DaggerType(type, qualifiers))
    }

    data class DaggerType(
        val type: TypeName,
        override val qualifiers: Set = emptySet(),
    ) : Parameter()

    sealed class ProviderMode {
        data object Single : ProviderMode()
        data object IntoSet : ProviderMode()
        data class IntoMap(val bindingKey: AnnotationSpec) : ProviderMode()
    }
}

fun  HiltModuleBuilder.ProviderMode.asAnnotations(
    annotationSpecFromAnnotation: (Annotation) -> AnnotationSpec,
) =
    when (this) {
        HiltModuleBuilder.ProviderMode.Single -> listOf()
        HiltModuleBuilder.ProviderMode.IntoSet -> listOf(annotationSpecFromAnnotation(IntoSet()))
        is HiltModuleBuilder.ProviderMode.IntoMap -> listOf(annotationSpecFromAnnotation(IntoMap()), bindingKey)
    }

fun  HiltModuleBuilder.Parameter.asParameterName(simpleName: TypeName.() -> String): String =
    when (this) {
        is HiltModuleBuilder.Lazy -> "lazy${type.asParameterName(simpleName).replaceFirstChar(Char::uppercaseChar)}"
        is HiltModuleBuilder.Provider -> "${
            type.asParameterName(simpleName).replaceFirstChar(Char::lowercaseChar)
        }Provider"

        is HiltModuleBuilder.DaggerType -> type.simpleName().replaceFirstChar(Char::lowercaseChar)
    }

fun  HiltModuleBuilder.Parameter.asTypeName(
    parameterizedTypeName: (rawType: KClass<*>, arguments: List) -> TypeName,
): TypeName = when (this) {
    is HiltModuleBuilder.Lazy -> parameterizedTypeName(lazy, listOf(type.asTypeName(parameterizedTypeName)))
    is HiltModuleBuilder.Provider -> parameterizedTypeName(provider, listOf(type.asTypeName(parameterizedTypeName)))
    is HiltModuleBuilder.DaggerType -> type
}

private val lazy = Lazy::class
private val provider = Provider::class




© 2015 - 2024 Weber Informatics LLC | Privacy Policy