Skip to content
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
17 changes: 17 additions & 0 deletions kotlin-code-generation/src/main/kotlin/_types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.toolisticon.kotlin.generation

import com.squareup.kotlinpoet.ClassName
import java.util.function.Supplier
import kotlin.reflect.KClass

/**
* Marks a type as capable of building a new product.
Expand All @@ -22,6 +23,22 @@ interface WithClassName {
val className: ClassName
}

/**
* An implementing type can provide a generic value for a tag key.
* Behaves as [com.squareup.kotlinpoet.Taggable] but hides the implementation of the tg provider.
*/
interface WithTags {
/**
* @see [com.squareup.kotlinpoet.Taggable.tag]
*/
fun <T : Any> tag(type: KClass<T>): T?
}

/**
* Reified access to [WithTags.tag].
*/
inline fun <reified T : Any> WithTags.tag(): T? = tag(T::class)

/**
* ClassName when used as a file name for fileSpec.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.toolisticon.kotlin.generation.spec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import io.toolisticon.kotlin.generation.WithClassName
import io.toolisticon.kotlin.generation.WithTags
import io.toolisticon.kotlin.generation.poet.FileSpecSupplier
import kotlin.reflect.KClass

Expand All @@ -11,7 +12,7 @@ import kotlin.reflect.KClass
*/
data class KotlinFileSpec(
private val spec: FileSpec
) : KotlinGeneratorSpec<KotlinFileSpec, FileSpec, FileSpecSupplier>, KotlinFileSpecSupplier, TaggableSpec, KotlinFileSpecIterable {
) : KotlinGeneratorSpec<KotlinFileSpec, FileSpec, FileSpecSupplier>, KotlinFileSpecSupplier, WithTags, KotlinFileSpecIterable {

val packageName: String = spec.packageName
val rootName: String = spec.name
Expand All @@ -37,6 +38,7 @@ interface KotlinFileSpecSupplier : KotlinGeneratorSpecSupplier<KotlinFileSpec>,
* List that contains multiple [KotlinFileSpec]s.
*/
@JvmInline
@Suppress("JavaDefaultMethodsNotOverriddenByDelegation")
value class KotlinFileSpecList(private val fileSpecs: List<KotlinFileSpec>) : List<KotlinFileSpec> by fileSpecs, KotlinFileSpecIterable {
companion object {

Expand Down
24 changes: 6 additions & 18 deletions kotlin-code-generation/src/main/kotlin/spec/_types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.WithClassName
import io.toolisticon.kotlin.generation.poet.*
import io.toolisticon.kotlin.generation.WithTags
import io.toolisticon.kotlin.generation.poet.KDoc
import io.toolisticon.kotlin.generation.poet.PoetSpec
import io.toolisticon.kotlin.generation.poet.PoetSpecSupplier
import io.toolisticon.kotlin.generation.poet.TypeSpecSupplier
import io.toolisticon.kotlin.generation.support.SUPPRESS_UNUSED
import kotlin.reflect.KClass

interface KotlinGeneratorSpecSupplier<GENERATOR_SPEC> {
fun spec(): GENERATOR_SPEC
Expand All @@ -24,7 +27,7 @@
}

@ExperimentalKotlinPoetApi
sealed interface KotlinDocumentableSpec : TaggableSpec {
sealed interface KotlinDocumentableSpec : WithTags {

Check warning on line 30 in kotlin-code-generation/src/main/kotlin/spec/_types.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

kotlin-code-generation/src/main/kotlin/spec/_types.kt#L30

KotlinDocumentableSpec is missing required documentation.
val kdoc: KDoc
}

Expand All @@ -41,21 +44,6 @@
addType(this@toFileSpec)
}

/**
* Marks Spec as [com.squareup.kotlinpoet.Taggable].
*/
sealed interface TaggableSpec {
/**
* @see [com.squareup.kotlinpoet.Taggable.tag]
*/
fun <T : Any> tag(type: KClass<T>): T?
}

/**
* Reified access to [TaggableSpec.tag].
*/
inline fun <reified T : Any> TaggableSpec.tag(): T? = tag(T::class)

/**
* Tags a spec with extra type.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.toolisticon.kotlin.generation.spi

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.WithTags
import kotlin.reflect.KClass

/**
Expand All @@ -13,7 +14,7 @@
* Hint: for implementing a concrete context, use the [io.toolisticon.kotlin.generation.spi.context.KotlinCodeGenerationContextBase].
*/
@ExperimentalKotlinPoetApi
interface KotlinCodeGenerationContext<SELF : KotlinCodeGenerationContext<SELF>> {
interface KotlinCodeGenerationContext<SELF : KotlinCodeGenerationContext<SELF>> : WithTags {

/**
* SELF type of the context implementation, used to verify against [io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationSpi.contextType].
Expand All @@ -38,6 +39,8 @@
fun <PROCESSOR : KotlinCodeGenerationProcessor<CONTEXT, INPUT, BUILDER>, CONTEXT : KotlinCodeGenerationContext<CONTEXT>, INPUT : Any, BUILDER : Any> processors(
processorType: KClass<PROCESSOR>
): List<PROCESSOR> = registry.processors.filterIsInstance(processorType.java)

override fun <T : Any> tag(type: KClass<T>): T? = null

Check warning on line 43 in kotlin-code-generation/src/main/kotlin/spi/KotlinCodeGenerationContext.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/spi/KotlinCodeGenerationContext.kt#L43

Added line #L43 was not covered by tests
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.toolisticon.kotlin.generation.spi

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.WithTags
import java.util.function.BiPredicate
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf
Expand All @@ -14,7 +15,7 @@
* * [KotlinCodeGenerationProcessor] - visitor pattern to modify spec builders before the spec is build.
*/
@ExperimentalKotlinPoetApi
sealed interface KotlinCodeGenerationSpi<CONTEXT : KotlinCodeGenerationContext<CONTEXT>, INPUT : Any> : Comparable<KotlinCodeGenerationSpi<*, *>>, BiPredicate<CONTEXT, Any> {
sealed interface KotlinCodeGenerationSpi<CONTEXT : KotlinCodeGenerationContext<CONTEXT>, INPUT : Any> : Comparable<KotlinCodeGenerationSpi<*, *>>, BiPredicate<CONTEXT, Any>, WithTags {
companion object {
val metaInfServices = "META-INF/services/${KotlinCodeGenerationSpi::class.qualifiedName}"
const val DEFAULT_ORDER = 0
Expand Down Expand Up @@ -57,4 +58,6 @@
* @return `true` when the spi shoud be applied.
*/
override fun test(context: CONTEXT, input: Any): Boolean = context::class.isSubclassOf(contextType) && input::class.isSubclassOf(inputType)

override fun <T : Any> tag(type: KClass<T>): T? = null

Check warning on line 62 in kotlin-code-generation/src/main/kotlin/spi/KotlinCodeGenerationSpi.kt

View check run for this annotation

Codecov / codecov/patch

kotlin-code-generation/src/main/kotlin/spi/KotlinCodeGenerationSpi.kt#L62

Added line #L62 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package io.toolisticon.kotlin.generation._test

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ internal class KotlinCodeGenerationServiceRepositoryTest {
contextType = TestContext::class,
inputType = TestInput::class,
) {
override fun invoke(context: TestContext, input: TestInput): KotlinDataClassSpec {
TODO("Not yet implemented")
}
override fun invoke(context: TestContext, input: TestInput): KotlinDataClassSpec = TODO("Not yet implemented")

override val specType: KClass<KotlinDataClassSpec> = KotlinDataClassSpec::class

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package io.toolisticon.kotlin.generation.spi

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
Expand All @@ -16,6 +15,7 @@ import io.toolisticon.kotlin.generation.spi.strategy.executeAll
import io.toolisticon.kotlin.generation.spi.strategy.executeSingle
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass

@OptIn(ExperimentalKotlinPoetApi::class)
class KotlinCodeGenerationStrategyListTest {
Expand All @@ -24,6 +24,8 @@ class KotlinCodeGenerationStrategyListTest {
override fun invoke(context: EmptyContext, input: InputA): KotlinDataClassSpec = buildDataClass(input.className) {
input.fields.map { (k, v) -> buildConstructorProperty(k, v) }.forEach(this::addConstructorProperty)
}

override fun <T : Any> tag(type: KClass<T>): T? = TODO("Not yet implemented")
}

class BStrategy : DataClassBStrategy() {
Expand Down