Skip to content

Commit

Permalink
chore: refactor - move to poet package
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Jun 4, 2024
1 parent 444dbc0 commit 47ef360
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 90 deletions.
9 changes: 9 additions & 0 deletions kotlin-code-generation/src/main/kotlin/_builder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.toolisticon.kotlin.generation

import java.util.function.Supplier

fun interface Builder<P : Any> {
fun build(): P
}

interface BuilderSupplier<P : Any, B : Any> : Builder<P>, Supplier<B>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.toolisticon.kotlin.generation.builder
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.asClassName
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.Supressions.CLASS_NAME
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.parameterBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.propertyBuilder
Expand Down
4 changes: 1 addition & 3 deletions kotlin-code-generation/src/main/kotlin/builder/_builder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package io.toolisticon.kotlin.generation.builder
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget.FILE
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.spec.*
import kotlin.reflect.KClass


fun interface Builder<T> {
fun build(): T
}

sealed interface SpecBuilder<SELF : SpecBuilder<SELF, PRODUCT, SPEC, SPEC_BUILDER>, PRODUCT : SpecSupplier<SPEC>, SPEC, SPEC_BUILDER> : Builder<PRODUCT>

Expand Down
64 changes: 0 additions & 64 deletions kotlin-code-generation/src/main/kotlin/builder/poet/_adapter.kt

This file was deleted.

12 changes: 12 additions & 0 deletions kotlin-code-generation/src/main/kotlin/poet/AnnotatableBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.Annotatable
import com.squareup.kotlinpoet.AnnotationSpec
import io.toolisticon.kotlin.generation.BuilderSupplier

sealed interface AnnotatableBuilder<SELF : AnnotatableBuilder<SELF, S, B>, S : Annotatable, B : Annotatable.Builder<B>>
: BuilderSupplier<S, B>, Annotatable.Builder<SELF> {
override val annotations: MutableList<AnnotationSpec> get() = get().annotations

fun addAnnotation(builder: AnnotationSpecBuilder): SELF = addAnnotation(builder.build())
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget
import com.squareup.kotlinpoet.CodeBlock
import io.toolisticon.kotlin.generation.BuilderSupplier

@JvmInline
value class AnnotationSpecBuilder(val builder: AnnotationSpec.Builder) : KotlinPoetBuilderSupplier<AnnotationSpec, AnnotationSpec.Builder>,
value class AnnotationSpecBuilder(val builder: AnnotationSpec.Builder) : BuilderSupplier<AnnotationSpec, AnnotationSpec.Builder>,
TaggableBuilder<AnnotationSpecBuilder, AnnotationSpec, AnnotationSpec.Builder> {

val members: MutableList<CodeBlock> get() = get().members
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.CodeBlock.Builder
import io.toolisticon.kotlin.generation.BuilderSupplier

@JvmInline
value class CodeBlockBuilder(private val builder: CodeBlock.Builder) : KotlinPoetBuilderSupplier<CodeBlock, CodeBlock.Builder> {
value class CodeBlockBuilder(private val builder: CodeBlock.Builder) : BuilderSupplier<CodeBlock, Builder> {

fun isEmpty(): Boolean = builder.isEmpty()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.ContextReceivable
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.TypeName
import io.toolisticon.kotlin.generation.BuilderSupplier

@ExperimentalKotlinPoetApi
sealed interface ContextReceivableBuilder<SELF : ContextReceivableBuilder<SELF, S, B>, S : ContextReceivable, B : ContextReceivable.Builder<B>>
: BuilderSupplier<S, B>, ContextReceivable.Builder<SELF> {
override val contextReceiverTypes: MutableList<TypeName> get() = get().contextReceiverTypes
}
12 changes: 12 additions & 0 deletions kotlin-code-generation/src/main/kotlin/poet/DocumentableBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.Documentable
import io.toolisticon.kotlin.generation.BuilderSupplier

sealed interface DocumentableBuilder<SELF : DocumentableBuilder<SELF, S, B>, S : Documentable, B : Documentable.Builder<B>>
: BuilderSupplier<S, B>, Documentable.Builder<SELF> {
override val kdoc: CodeBlock.Builder get() = get().kdoc

fun addKdoc(builder: CodeBlockBuilder): SELF = addKdoc(builder.build())
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier
import kotlin.reflect.KClass

@JvmInline
value class FileSpecBuilder(private val builder: FileSpec.Builder) : KotlinPoetBuilderSupplier<FileSpec, FileSpec.Builder>,
value class FileSpecBuilder(private val builder: FileSpec.Builder) : BuilderSupplier<FileSpec, FileSpec.Builder>,
AnnotatableBuilder<FileSpecBuilder, FileSpec, FileSpec.Builder>,
MemberSpecHolderBuilder<FileSpecBuilder, FileSpec, FileSpec.Builder>,
TaggableBuilder<FileSpecBuilder, FileSpec, FileSpec.Builder>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier
import java.lang.reflect.Type
import javax.lang.model.element.Modifier
import kotlin.reflect.KClass

@ExperimentalKotlinPoetApi
@JvmInline
value class FunSpecBuilder(private val builder: FunSpec.Builder) : KotlinPoetBuilderSupplier<FunSpec, FunSpec.Builder>,
value class FunSpecBuilder(private val builder: FunSpec.Builder) : BuilderSupplier<FunSpec, FunSpec.Builder>,
AnnotatableBuilder<FunSpecBuilder, FunSpec, FunSpec.Builder>,
ContextReceivableBuilder<FunSpecBuilder, FunSpec, FunSpec.Builder>,
DocumentableBuilder<FunSpecBuilder, FunSpec, FunSpec.Builder>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.MemberSpecHolder
import com.squareup.kotlinpoet.PropertySpec
import io.toolisticon.kotlin.generation.BuilderSupplier

sealed interface MemberSpecHolderBuilder<SELF : MemberSpecHolderBuilder<SELF, S, B>, S : MemberSpecHolder, B : MemberSpecHolder.Builder<B>>
: BuilderSupplier<S, B>, MemberSpecHolder.Builder<SELF> {
override fun addProperty(propertySpec: PropertySpec): SELF

@OptIn(ExperimentalKotlinPoetApi::class)
fun addProperty(builder: PropertySpecBuilder): SELF = addProperty(builder.build())

override fun addFunction(funSpec: FunSpec): SELF

@OptIn(ExperimentalKotlinPoetApi::class)
fun addFunction(builder: FunSpecBuilder): SELF = addFunction(builder.build())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.OriginatingElementsHolder
import io.toolisticon.kotlin.generation.BuilderSupplier
import javax.lang.model.element.Element

sealed interface OriginatingElementsHolderBuilder<SELF : OriginatingElementsHolderBuilder<SELF, S, B>, S : OriginatingElementsHolder, B : OriginatingElementsHolder.Builder<B>>
: BuilderSupplier<S, B>, OriginatingElementsHolder.Builder<SELF> {
override val originatingElements: MutableList<Element> get() = get().originatingElements
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
import io.toolisticon.kotlin.generation.BuilderSupplier

@JvmInline
value class ParameterSpecBuilder(private val builder: ParameterSpec.Builder) : KotlinPoetBuilderSupplier<ParameterSpec, ParameterSpec.Builder>,
value class ParameterSpecBuilder(private val builder: ParameterSpec.Builder) : BuilderSupplier<ParameterSpec, ParameterSpec.Builder>,
AnnotatableBuilder<ParameterSpecBuilder, ParameterSpec, ParameterSpec.Builder>,
DocumentableBuilder<ParameterSpecBuilder, ParameterSpec, ParameterSpec.Builder>,
TaggableBuilder<ParameterSpecBuilder, ParameterSpec, ParameterSpec.Builder> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier
import kotlin.reflect.KClass

@ExperimentalKotlinPoetApi
@JvmInline
value class PropertySpecBuilder(private val builder: PropertySpec.Builder) : KotlinPoetBuilderSupplier<PropertySpec, PropertySpec.Builder>,
AnnotatableBuilder<PropertySpecBuilder,PropertySpec,PropertySpec.Builder>,
ContextReceivableBuilder<PropertySpecBuilder,PropertySpec,PropertySpec.Builder>,
DocumentableBuilder<PropertySpecBuilder,PropertySpec,PropertySpec.Builder>,
TaggableBuilder<PropertySpecBuilder,PropertySpec,PropertySpec.Builder>,
OriginatingElementsHolderBuilder<PropertySpecBuilder,PropertySpec,PropertySpec.Builder> {
value class PropertySpecBuilder(private val builder: PropertySpec.Builder) : BuilderSupplier<PropertySpec, PropertySpec.Builder>,
AnnotatableBuilder<PropertySpecBuilder, PropertySpec, PropertySpec.Builder>,
ContextReceivableBuilder<PropertySpecBuilder, PropertySpec, PropertySpec.Builder>,
DocumentableBuilder<PropertySpecBuilder, PropertySpec, PropertySpec.Builder>,
TaggableBuilder<PropertySpecBuilder, PropertySpec, PropertySpec.Builder>,
OriginatingElementsHolderBuilder<PropertySpecBuilder, PropertySpec, PropertySpec.Builder> {

val modifiers: MutableList<KModifier> get() = builder.modifiers
val typeVariables: MutableList<TypeVariableName> get() = builder.typeVariables
Expand Down
10 changes: 10 additions & 0 deletions kotlin-code-generation/src/main/kotlin/poet/TaggableBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.Taggable
import io.toolisticon.kotlin.generation.BuilderSupplier
import kotlin.reflect.KClass

sealed interface TaggableBuilder<SELF : TaggableBuilder<SELF, S, B>, S : Taggable, B : Taggable.Builder<B>>
: BuilderSupplier<S, B>, Taggable.Builder<SELF> {
override val tags: MutableMap<KClass<*>, Any> get() = get().tags
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeAliasSpec
import com.squareup.kotlinpoet.TypeVariableName
import io.toolisticon.kotlin.generation.BuilderSupplier

@JvmInline
value class TypeAliasSpecBuilder(private val builder: TypeAliasSpec.Builder) : KotlinPoetBuilderSupplier<TypeAliasSpec, TypeAliasSpec.Builder>,
value class TypeAliasSpecBuilder(private val builder: TypeAliasSpec.Builder) : BuilderSupplier<TypeAliasSpec, TypeAliasSpec.Builder>,
AnnotatableBuilder<TypeAliasSpecBuilder, TypeAliasSpec, TypeAliasSpec.Builder>,
DocumentableBuilder<TypeAliasSpecBuilder, TypeAliasSpec, TypeAliasSpec.Builder>,
TaggableBuilder<TypeAliasSpecBuilder, TypeAliasSpec, TypeAliasSpec.Builder> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier

@JvmInline
@ExperimentalKotlinPoetApi
value class TypeSpecBuilder(private val builder: TypeSpec.Builder) : KotlinPoetBuilderSupplier<TypeSpec, TypeSpec.Builder>,
value class TypeSpecBuilder(private val builder: TypeSpec.Builder) : BuilderSupplier<TypeSpec, TypeSpec.Builder>,
AnnotatableBuilder<TypeSpecBuilder, TypeSpec, TypeSpec.Builder>,
ContextReceivableBuilder<TypeSpecBuilder, TypeSpec, TypeSpec.Builder>,
DocumentableBuilder<TypeSpecBuilder, TypeSpec, TypeSpec.Builder>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier

sealed interface TypeSpecHolderBuilder<SELF : TypeSpecHolderBuilder<SELF, S, B>, S : TypeSpecHolder, B : TypeSpecHolder.Builder<B>>
: BuilderSupplier<S, B>, TypeSpecHolder.Builder<SELF> {
override fun addType(typeSpec: TypeSpec): SELF

@OptIn(ExperimentalKotlinPoetApi::class)
fun addType(builder: TypeSpecBuilder): SELF = addType(builder.build())
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.AnnotationSpec
import io.toolisticon.kotlin.generation.poet.AnnotationSpecBuilder
import org.junit.jupiter.api.Test

internal class AnnotationSpecBuilderTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.kotlin.generation.builder.poet
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
Expand Down

0 comments on commit 47ef360

Please sign in to comment.