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

com.faendir.kotlin.autodsl.parameter.ParameterFactory.kt Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package com.faendir.kotlin.autodsl.parameter

import com.shadow.pluralize.singularize
import com.shadow.pluralize.utils.Plurality
import com.faendir.kotlin.autodsl.*
import com.faendir.kotlin.autodsl.parameter.CollectionType.ListType
import com.faendir.kotlin.autodsl.parameter.CollectionType.SetType
import com.squareup.kotlinpoet.asClassName
import kotlin.reflect.KClass

class ParameterFactory(private val resolver: SourceInfoResolver) {
    private val set = Set::class.asClassName()
    private val list = List::class.asClassName()
    private val collection = Collection::class.asClassName()
    private val iterable = Iterable::class.asClassName()

    fun getParameters(constructor: C): List = resolver.run {
        return constructor.getParameters().withIndex().map { (index, parameter) ->
            val type = parameter.getTypeName()
            val rawType = type.toRawType()
            val (hasNestedDsl, collectionType) = when (rawType) {
                set -> parameter.hasAnnotatedTypeArgument(AutoDsl::class) to SetType(findSingular(parameter, index))
                list, collection, iterable -> parameter.hasAnnotatedTypeArgument(AutoDsl::class) to ListType(findSingular(parameter, index))
                else -> (parameter.getTypeDeclaration()?.hasAnnotation(AutoDsl::class) == true) to null
            }
            Parameter(
                type,
                parameter.getName(),
                parameter.getDoc(),
                parameter.hasDefault(),
                parameter.getAnnotationProperty(AutoDslRequired::class, AutoDslRequired::group),
                index,
                hasNestedDsl,
                collectionType
            )
        }
    }

    private fun P.hasAnnotatedTypeArgument(annotation: KClass): Boolean = resolver.run {
        return getTypeArguments().firstOrNull()?.hasAnnotation(annotation) ?: false
    }

    private fun findSingular(parameter: P, index: Int): String = resolver.run {
        when {
            parameter.hasAnnotation(AutoDslSingular::class) -> parameter.getAnnotationProperty(AutoDslSingular::class, AutoDslSingular::value)
            parameter.getName().length < 3 -> parameter.getName()
            else -> parameter.getName().singularize(Plurality.CouldBeEither)
        } ?: "var$index"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy