Skip to content

Commit

Permalink
introduce KotlinModifiableBuilder, fixes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Aug 29, 2024
1 parent 363d2e2 commit 113c781
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class KotlinAnnotationClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinAnnotationClassSpecBuilder, KotlinAnnotationClassSpec>,
ConstructorPropertySupport<KotlinAnnotationClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinModifiableBuilder<KotlinAnnotationClassSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinAnnotationClassSpecBuilder> {

companion object {
Expand Down Expand Up @@ -53,20 +54,18 @@ class KotlinAnnotationClassSpecBuilder internal constructor(
override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinAnnotationClassSpecBuilder = apply { delegate.addAnnotation(spec.get()) }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinAnnotationClassSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinAnnotationClassSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
override fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }
override fun addProperty(propertySpec: KotlinPropertySpecSupplier): KotlinAnnotationClassSpecBuilder = apply { delegate.addProperty(propertySpec.get()) }
override fun addType(typeSpec: TypeSpecSupplier) = builder { this.addType(typeSpec.get()) }

fun contextReceivers(vararg receiverTypes: TypeName): KotlinAnnotationClassSpecBuilder = builder { this.contextReceivers(*receiverTypes) }

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

override fun addType(typeSpec: TypeSpecSupplier) = builder { this.addType(typeSpec.get()) }

fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
}

override fun builder(block: TypeSpecBuilderReceiver) = apply { delegate.builder.block() }
override fun build(): KotlinAnnotationClassSpec {
if (constructorProperties.isNotEmpty()) {
delegate.primaryConstructorWithProperties(toList(constructorProperties.values))
Expand All @@ -91,8 +90,6 @@ class KotlinAnnotationClassSpecBuilder internal constructor(

return KotlinAnnotationClassSpec(className = className, spec = delegate.build())
}


}

@ExperimentalKotlinPoetApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import kotlin.reflect.KClass
class KotlinAnonymousClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinAnonymousClassSpecBuilder, KotlinAnonymousClassSpec>,
DelegatingBuilder<KotlinAnonymousClassSpecBuilder, TypeSpecBuilderReceiver>,
KotlinAnnotatableBuilder<KotlinAnonymousClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinAnonymousClassSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinAnonymousClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinAnonymousClassSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinAnonymousClassSpecBuilder> {
KotlinModifiableBuilder<KotlinAnonymousClassSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinAnonymousClassSpecBuilder>,
DelegatingBuilder<KotlinAnonymousClassSpecBuilder, TypeSpecBuilderReceiver> {

companion object {
fun builder(): KotlinAnonymousClassSpecBuilder = KotlinAnonymousClassSpecBuilder()
Expand All @@ -34,15 +35,13 @@ class KotlinAnonymousClassSpecBuilder internal constructor(
override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinAnonymousClassSpecBuilder = apply { delegate.addAnnotation(spec.get()) }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinAnonymousClassSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinAnonymousClassSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
override fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }
override fun addProperty(propertySpec: KotlinPropertySpecSupplier): KotlinAnonymousClassSpecBuilder = apply { delegate.addProperty(propertySpec.get()) }
override fun addType(typeSpec: TypeSpecSupplier) = builder { this.addType(typeSpec.get()) }


fun contextReceivers(vararg receiverTypes: TypeName): KotlinAnonymousClassSpecBuilder = builder { this.contextReceivers(*receiverTypes) }
fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }

fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }
fun superclass(superclass: TypeName) = builder { this.superclass(superclass) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class KotlinClassSpecBuilder internal constructor(
private val className: ClassName,
private val delegate: TypeSpecBuilder
) : KotlinGeneratorTypeSpecBuilder<KotlinClassSpecBuilder, KotlinClassSpec>,
KotlinAnnotatableBuilder<KotlinClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinClassSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinClassSpecBuilder>,
KotlinAnnotatableBuilder<KotlinClassSpecBuilder>,
KotlinModifiableBuilder<KotlinClassSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinClassSpecBuilder> {

companion object {
Expand All @@ -33,15 +34,12 @@ class KotlinClassSpecBuilder internal constructor(
override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinClassSpecBuilder = apply { delegate.addAnnotation(spec.get()) }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinClassSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinClassSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
override fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }
override fun addProperty(propertySpec: KotlinPropertySpecSupplier): KotlinClassSpecBuilder = apply { delegate.addProperty(propertySpec.get()) }
override fun addType(typeSpec: TypeSpecSupplier) = builder { this.addType(typeSpec.get()) }

fun contextReceivers(vararg receiverTypes: TypeName): KotlinClassSpecBuilder = builder { this.contextReceivers(*receiverTypes) }

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }

fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }
fun superclass(superclass: TypeName) = builder { this.superclass(superclass) }
Expand All @@ -60,92 +58,10 @@ class KotlinClassSpecBuilder internal constructor(

fun addInitializerBlock(block: CodeBlock) = builder { this.addInitializerBlock(block) }

override fun builder(block: TypeSpecBuilderReceiver) = apply { delegate.builder.block() }

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
}

override fun build(): KotlinClassSpec = KotlinClassSpec(
className = className,
spec = delegate.build()
)

// operator fun invoke(spec: KotlinDataClassSpec): _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder =
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder(
// className = spec.className,
// delegate = spec.get().toBuilder()
// )
//}
//
//private val constructorProperties = LinkedHashMap<String, ConstructorPropertySupplier>()
//
//operator fun invoke(block: TypeSpecBuilder.() -> Unit): _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder = apply {
// delegate.block()
// override fun addKdoc(kdoc: CodeBlock) = apply {
// delegate.addKdoc(kdoc)
// }
//
// fun addType(typeSpecSupplier: TypeSpecSupplier) = apply {
// delegate.addType(typeSpecSupplier.get())
// }
//
// fun addConstructorProperty(name: String, type: TypeName, block: _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinConstructorPropertyBuilder.() -> Unit = {}) = apply {
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder.Companion.constructorProperties[name] = buildConstructorProperty(name, type, block)
// }
//
// fun addConstructorProperty(constructorProperty: ConstructorPropertySupplier) = apply {
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder.Companion.constructorProperties[constructorProperty.name] = constructorProperty
// }
//
// /**
// * Finalize a data class based on its primary constructor parameters.
// *
// * * adds primary constructor.
// * * backs parameters with properties.
// */
// override fun build(): KotlinDataClassSpec {
// check(_root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder.Companion.constructorProperties.isNotEmpty()) { "Data class must have at least one property." }
//
// val constructor = FunSpec.constructorBuilder()
//
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder.Companion.constructorProperties.values.map(ConstructorPropertySupplier::get).forEach {
// constructor.addParameter(it.parameter.get())
// delegate.addProperty(it.property.get())
// }
//
// delegate.primaryConstructor(constructor.build())
//
// return KotlinDataClassSpec(className = className, spec = delegate.build())
// }
//
// override fun addAnnotation(annotation: ClassName): _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder = apply {
// fun addAnnotation(annotation: ClassName): _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder = apply {
// delegate.addAnnotation(annotation)
// }
//
// override fun get(): TypeSpec = build().get()
//
// }
override fun build(): KotlinClassSpec = KotlinClassSpec(className = className, spec = delegate.build())
}


// @Suppress("ClassName")
// object builder :
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.ToKotlinPoetSpecBuilder<KotlinDataClassSpec, _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder> {
//
// fun builder(className: ClassName) = _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder(className = className)
// operator fun invoke(packageName: String, name: String): _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder =
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder.Companion.builder.invoke(ClassName(packageName, name))
//
// fun builder(packageName: String, name: String) =
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder.Companion.builder.builder(ClassName(packageName, name))
// operator fun invoke(className: ClassName): _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder =
// _root_ide_package_.io.toolisticon.kotlin.generation._BAK.KotlinDataClassBuilder.KotlinDataClassSpecBuilder(
// className = className,
// delegate = TypeSpec.classBuilder(className)
// )
//
// }

@ExperimentalKotlinPoetApi
typealias KotlinClassSpecBuilderReceiver = KotlinClassSpecBuilder.() -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class KotlinCompanionObjectSpecBuilder internal constructor(
KotlinAnnotatableBuilder<KotlinCompanionObjectSpecBuilder>,
KotlinDocumentableBuilder<KotlinCompanionObjectSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinCompanionObjectSpecBuilder>,
KotlinModifiableBuilder<KotlinCompanionObjectSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinCompanionObjectSpecBuilder> {

companion object {
Expand All @@ -35,6 +36,7 @@ class KotlinCompanionObjectSpecBuilder internal constructor(
override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinCompanionObjectSpecBuilder = apply { delegate.addAnnotation(spec.get()) }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinCompanionObjectSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinCompanionObjectSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
override fun addModifiers(vararg modifiers: KModifier) = builder { delegate.addModifiers(*modifiers) }
override fun addProperty(propertySpec: KotlinPropertySpecSupplier): KotlinCompanionObjectSpecBuilder = apply { delegate.addProperty(propertySpec.get()) }
override fun addType(typeSpec: TypeSpecSupplier) = builder { delegate.addType(typeSpec.get()) }

Expand All @@ -50,7 +52,6 @@ class KotlinCompanionObjectSpecBuilder internal constructor(
)
}

fun addModifiers(vararg modifiers: KModifier) = builder { delegate.addModifiers(*modifiers) }

fun addTypeVariable(typeVariable: TypeVariableName) = builder { delegate.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder {
Expand Down Expand Up @@ -113,11 +114,9 @@ class KotlinCompanionObjectSpecBuilder internal constructor(

fun addInitializerBlock(block: CodeBlock) = builder { delegate.addInitializerBlock(block) }


override fun builder(block: TypeSpecBuilderReceiver) = apply { delegate.builder.block() }
override fun build(): KotlinCompanionObjectSpec = KotlinCompanionObjectSpec(spec = delegate.build())
}


@ExperimentalKotlinPoetApi
typealias KotlinCompanionObjectSpecBuilderReceiver = KotlinCompanionObjectSpecBuilder.() -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class KotlinConstructorPropertySpecBuilder internal constructor(
) : Builder<KotlinConstructorPropertySpec>,
KotlinAnnotatableBuilder<KotlinConstructorPropertySpecBuilder>,
KotlinConstructorPropertySpecSupplier,
KotlinDocumentableBuilder<KotlinConstructorPropertySpecBuilder> {
KotlinDocumentableBuilder<KotlinConstructorPropertySpecBuilder>,
KotlinModifiableBuilder<KotlinConstructorPropertySpecBuilder> {

companion object {
fun builder(name: String, type: TypeName): KotlinConstructorPropertySpecBuilder = KotlinConstructorPropertySpecBuilder(
Expand All @@ -44,14 +45,9 @@ class KotlinConstructorPropertySpecBuilder internal constructor(
}
}

fun makePrivate() = apply {
propertyBuilder.builder {
addModifiers(KModifier.PRIVATE)
}
}

override fun addAnnotation(spec: KotlinAnnotationSpecSupplier): KotlinConstructorPropertySpecBuilder = apply { parameterBuilder.addAnnotation(spec) }
override fun addKdoc(kdoc: KDoc): KotlinConstructorPropertySpecBuilder = apply { parameterBuilder.addKdoc(kdoc) }
override fun addModifiers(vararg modifiers: KModifier): KotlinConstructorPropertySpecBuilder = apply{ propertyBuilder.addModifiers(*modifiers) }

override fun build(): KotlinConstructorPropertySpec {
val parameter = parameterBuilder.build()
Expand All @@ -63,9 +59,7 @@ class KotlinConstructorPropertySpecBuilder internal constructor(

return KotlinConstructorPropertySpec(parameter = parameter, property = property)
}

override fun spec(): KotlinConstructorPropertySpec = build()

}

@ExperimentalKotlinPoetApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class KotlinDataClassSpecBuilder internal constructor(
KotlinAnnotatableBuilder<KotlinDataClassSpecBuilder>,
KotlinDocumentableBuilder<KotlinDataClassSpecBuilder>,
KotlinMemberSpecHolderBuilder<KotlinDataClassSpecBuilder>,
KotlinModifiableBuilder<KotlinDataClassSpecBuilder>,
KotlinTypeSpecHolderBuilder<KotlinDataClassSpecBuilder> {
companion object : KLogging() {
fun builder(name: String): KotlinDataClassSpecBuilder = builder(simpleClassName(name))
Expand All @@ -45,26 +46,14 @@ class KotlinDataClassSpecBuilder internal constructor(
override fun addConstructorProperty(spec: KotlinConstructorPropertySpecSupplier): KotlinDataClassSpecBuilder = apply { this.constructorProperties[spec.name] = spec }
override fun addFunction(funSpec: KotlinFunSpecSupplier): KotlinDataClassSpecBuilder = apply { delegate.addFunction(funSpec.get()) }
override fun addKdoc(kdoc: KDoc): KotlinDataClassSpecBuilder = apply { delegate.addKdoc(kdoc.get()) }
override fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }
override fun addProperty(propertySpec: KotlinPropertySpecSupplier): KotlinDataClassSpecBuilder = apply { delegate.addProperty(propertySpec.get()) }
override fun addType(typeSpec: TypeSpecSupplier) = builder { this.addType(typeSpec.get()) }

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
}

override fun build(): KotlinDataClassSpec {
check(constructorProperties.isNotEmpty()) { "Data class must have at least one property." }

delegate.primaryConstructorWithProperties(toList(constructorProperties.values))

return KotlinDataClassSpec(className = className, spec = delegate.build())
}

fun contextReceivers(vararg receiverTypes: TypeName) = builder { this.contextReceivers(*receiverTypes) }

fun addOriginatingElement(originatingElement: Element) = builder { this.addOriginatingElement(originatingElement) }

fun addModifiers(vararg modifiers: KModifier) = builder { this.addModifiers(*modifiers) }

fun addTypeVariable(typeVariable: TypeVariableName) = builder { this.addTypeVariable(typeVariable) }
fun primaryConstructor(primaryConstructor: FunSpecSupplier?) = builder { this.primaryConstructor(primaryConstructor?.get()) }
Expand All @@ -83,6 +72,16 @@ class KotlinDataClassSpecBuilder internal constructor(
fun addSuperinterface(superinterface: TypeName, constructorParameter: String) = builder { this.addSuperinterface(superinterface, constructorParameter) }

fun addInitializerBlock(block: CodeBlock) = builder { this.addInitializerBlock(block) }

override fun builder(block: TypeSpecBuilderReceiver) = apply { delegate.builder.block() }
override fun build(): KotlinDataClassSpec {
check(constructorProperties.isNotEmpty()) { "Data class must have at least one property." }

delegate.primaryConstructorWithProperties(toList(constructorProperties.values))

return KotlinDataClassSpec(className = className, spec = delegate.build())
}

}

@ExperimentalKotlinPoetApi
Expand Down
Loading

0 comments on commit 113c781

Please sign in to comment.