From 896cde2b794acf66f41dc8c49eb6d2de40b48d16 Mon Sep 17 00:00:00 2001 From: Michal Fudala Date: Thu, 20 Oct 2022 15:44:28 +0200 Subject: [PATCH] improve code readability (after review comments) --- .../codegen/expressions/Expressions.kt | 3 - .../codegen/step2intermediate/MoreTypes.kt | 59 ++++++------------- .../codegen/step3codegen/FieldTypes.kt | 4 +- .../step3codegen/KotlinPoetExtensions.kt | 41 ++++++------- .../resources/ResourceGenerator.kt | 9 +-- .../step3codegen/types/EnumTypeGenerator.kt | 4 +- .../step3codegen/types/TypeGenerator.kt | 13 ++-- ...icGenerator.kt => BasicSetterGenerator.kt} | 2 +- ...rator.kt => ComplexTypeSetterGenerator.kt} | 2 +- ...enerator.kt => ListTypeSetterGenerator.kt} | 2 +- ...Generator.kt => MapTypeSetterGenerator.kt} | 8 ++- ...tor.kt => OutputWrappedSetterGenerator.kt} | 4 +- .../types/setters/SetterGenerator.kt | 15 +++-- 13 files changed, 73 insertions(+), 93 deletions(-) rename src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/{BasicGenerator.kt => BasicSetterGenerator.kt} (95%) rename src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/{ComplexTypeGenerator.kt => ComplexTypeSetterGenerator.kt} (95%) rename src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/{ListTypeGenerator.kt => ListTypeSetterGenerator.kt} (98%) rename src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/{MapTypeGenerator.kt => MapTypeSetterGenerator.kt} (86%) rename src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/{OutputWrappedGenerator.kt => OutputWrappedSetterGenerator.kt} (90%) diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/expressions/Expressions.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/expressions/Expressions.kt index 17bd8907..65ca2280 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/expressions/Expressions.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/expressions/Expressions.kt @@ -204,9 +204,6 @@ data class Assignment(val to: String, val expression: Expression) : Code() { } data class GroupedCode(val list: List) : Code() { - - constructor(vararg code: Code) : this(code.toList()) - override fun toCodeBlock(): CustomCodeBlock { val blocks = list.map { it.toCodeBlock() } diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step2intermediate/MoreTypes.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step2intermediate/MoreTypes.kt index 23efdc34..13bcc1b7 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step2intermediate/MoreTypes.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step2intermediate/MoreTypes.kt @@ -1,71 +1,46 @@ package com.virtuslab.pulumikotlin.codegen.step2intermediate +import com.pulumi.core.Output import com.pulumi.kotlin.ConvertibleToJava -import com.squareup.kotlinpoet.ClassName +import com.pulumi.kotlin.CustomArgs +import com.pulumi.kotlin.CustomArgsBuilder +import com.pulumi.kotlin.PulumiTagMarker import com.squareup.kotlinpoet.MemberName import com.squareup.kotlinpoet.MemberName.Companion.member -import com.squareup.kotlinpoet.ParameterizedTypeName -import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy -import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.asClassName +import com.squareup.kotlinpoet.asTypeName object MoreTypes { object Kotlin { object Pulumi { - fun toJavaMethod(): MemberName { - return MemberName("com.pulumi.kotlin", "toJava") - } + fun toJavaExtensionMethod() = MemberName("com.pulumi.kotlin", "toJava") - fun pulumiDslMarkerAnnotation(): ClassName { - return ClassName("com.pulumi.kotlin", "PulumiTagMarker") - } + fun toKotlinExtensionMethod() = MemberName("com.pulumi.kotlin", "toKotlin") - fun toKotlinMethod(): MemberName { - return MemberName("com.pulumi.kotlin", "toKotlin") - } + fun applySuspendExtensionMethod() = MemberName("com.pulumi.kotlin", "applySuspend") - fun applySuspendExtensionMethod(): MemberName { - return MemberName("com.pulumi.kotlin", "applySuspend") - } + fun applyValueExtensionMethod() = MemberName("com.pulumi.kotlin", "applyValue") - fun applyValueExtensionMethod(): MemberName { - return MemberName("com.pulumi.kotlin", "applyValue") - } + fun pulumiDslMarkerAnnotation() = PulumiTagMarker::class.asClassName() - fun convertibleToJavaClass(type: TypeName): ParameterizedTypeName = - ConvertibleToJava::class.asClassName().parameterizedBy(type) + fun convertibleToJavaClass() = ConvertibleToJava::class.asClassName() - fun customArgsClass(): ClassName { - return ClassName("com.pulumi.kotlin", "CustomArgs") - } + fun customArgsClass() = CustomArgs::class.asClassName() - fun customArgsBuilderClass(): ClassName { - return ClassName("com.pulumi.kotlin", "CustomArgsBuilder") - } + fun customArgsBuilderClass() = CustomArgsBuilder::class.asClassName() } - fun coroutinesFutureAwaitExtensionMethod(): MemberName { - return MemberName("kotlinx.coroutines.future", "await") - } + fun coroutinesFutureAwaitExtensionMethod() = MemberName("kotlinx.coroutines.future", "await") - fun pairClass(leftType: TypeName, rightType: TypeName): ParameterizedTypeName { - return ClassName("kotlin", "Pair").parameterizedBy(leftType, rightType) - } + fun pairClass() = Pair::class.asTypeName() } object Java { object Pulumi { - fun outputOfMethod(): MemberName = - outputClass().member("of") - - fun outputClass(): ClassName { - return ClassName("com.pulumi.core", "Output") - } + fun outputOfMethod() = outputClass().member("of") - fun outputClass(type: TypeName): ParameterizedTypeName { - return ClassName("com.pulumi.core", "Output").parameterizedBy(type) - } + fun outputClass() = Output::class.asClassName() } } } diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/FieldTypes.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/FieldTypes.kt index 99298d78..4bafcb21 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/FieldTypes.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/FieldTypes.kt @@ -4,9 +4,9 @@ import com.squareup.kotlinpoet.ParameterizedTypeName import com.squareup.kotlinpoet.TypeName import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Java.Pulumi.outputClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.ReferencedType +import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetExtensions.parameterizedBy sealed class FieldType { - abstract val type: T abstract fun toTypeName(): TypeName @@ -20,7 +20,7 @@ data class NormalField(override val type: T, val mappingCode data class OutputWrappedField(override val type: T) : FieldType() { override fun toTypeName(): ParameterizedTypeName { - return outputClass(type.toTypeName()) + return outputClass().parameterizedBy(type) } } diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/KotlinPoetExtensions.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/KotlinPoetExtensions.kt index bf262225..fdbfed57 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/KotlinPoetExtensions.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/KotlinPoetExtensions.kt @@ -1,39 +1,40 @@ package com.virtuslab.pulumikotlin.codegen.step3codegen +import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.MemberName +import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.TypeSpec import com.virtuslab.pulumikotlin.codegen.expressions.Code +import com.virtuslab.pulumikotlin.codegen.step2intermediate.Type object KotlinPoetExtensions { - fun FileSpec.Builder.addTypes(vararg typeSpecs: TypeSpec) = - addTypes(typeSpecs.toList()) + fun TypeSpec.Builder.addFunctions(vararg funSpecs: FunSpec) = addFunctions(funSpecs.toList()) - fun TypeSpec.Builder.addFunctions(vararg funSpecs: FunSpec) = - addFunctions(funSpecs.toList()) + fun TypeSpec.Builder.addProperties(vararg propertySpecs: PropertySpec) = addProperties(propertySpecs.toList()) - fun TypeSpec.Builder.addProperties(vararg propertySpecs: PropertySpec) = - addProperties(propertySpecs.toList()) + fun FileSpec.Builder.addImport(memberName: MemberName) = addImport(memberName.packageName, memberName.simpleName) - fun FileSpec.Builder.addImport(memberName: MemberName) = - addImport(memberName.packageName, memberName.simpleName) + fun FileSpec.Builder.addImports(vararg memberNames: MemberName) = + apply { + memberNames.forEach { + addImport(it) + } + } + + fun FileSpec.Builder.addTypes(vararg typeSpecs: TypeSpec) = addTypes(typeSpecs.toList()) - fun FileSpec.Builder.addImports(vararg memberNames: MemberName): FileSpec.Builder { - memberNames.forEach { - addImport(it) + fun FileSpec.Builder.addTypes(types: Iterable) = + apply { + types.forEach { + addType(it) + } } - return this - } - fun FileSpec.Builder.addTypes(types: Iterable): FileSpec.Builder { - types.forEach { addType(it) } - return this - } + fun ClassName.parameterizedBy(vararg types: Type) = parameterizedBy(types.map { it.toTypeName() }) - fun CodeBlock.Builder.add(code: Code): CodeBlock.Builder { - return this.add(code.toCodeBlock().toKotlinPoetCodeBlock()) - } + fun CodeBlock.Builder.add(code: Code) = add(code.toCodeBlock().toKotlinPoetCodeBlock()) } diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/resources/ResourceGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/resources/ResourceGenerator.kt index 35f4f0a7..3b9fdffd 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/resources/ResourceGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/resources/ResourceGenerator.kt @@ -15,7 +15,6 @@ import com.virtuslab.pulumikotlin.codegen.step2intermediate.Depth.Root import com.virtuslab.pulumikotlin.codegen.step2intermediate.Direction.Output import com.virtuslab.pulumikotlin.codegen.step2intermediate.LanguageType.Java import com.virtuslab.pulumikotlin.codegen.step2intermediate.LanguageType.Kotlin -import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Java.Pulumi.outputClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.customArgsBuilderClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.customArgsClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.pulumiDslMarkerAnnotation @@ -53,12 +52,8 @@ object ResourceGenerator { val javaResourceClassName = ClassName(names.toResourcePackage(javaFlags), names.toResourceName(javaFlags)) val fields = resourceType.outputFields.map { field -> - PropertySpec.builder( - field.name, - outputClass( - field.fieldType.type.toTypeName().copy(nullable = !field.required), - ), - ) + PropertySpec + .builder(field.name, field.toTypeName()) .getter( FunSpec.getterBuilder() .addCode(toKotlinFunctionResource(field.name, field.fieldType.type, !field.required)) diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/EnumTypeGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/EnumTypeGenerator.kt index 17ce1f29..933491b5 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/EnumTypeGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/EnumTypeGenerator.kt @@ -3,12 +3,14 @@ package com.virtuslab.pulumikotlin.codegen.step3codegen.types import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.ParameterizedTypeName +import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeSpec import com.virtuslab.pulumikotlin.codegen.step2intermediate.EnumType import com.virtuslab.pulumikotlin.codegen.step2intermediate.LanguageType import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.convertibleToJavaClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.RootType import com.virtuslab.pulumikotlin.codegen.step2intermediate.TypeMetadata +import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetExtensions.parameterizedBy import com.virtuslab.pulumikotlin.codegen.step3codegen.types.ToJava.toJavaEnumFunction import com.virtuslab.pulumikotlin.codegen.step3codegen.types.ToKotlin.toKotlinEnumFunction @@ -41,7 +43,7 @@ object EnumTypeGenerator { private fun prepareConvertibleToJavaInterface(typeMetadata: TypeMetadata): ParameterizedTypeName { val javaNames = typeMetadata.names(LanguageType.Java) val javaClass = ClassName(javaNames.packageName, javaNames.className) - return convertibleToJavaClass(javaClass) + return convertibleToJavaClass().parameterizedBy(javaClass) } private fun buildEnumFileSpec(enumType: EnumType, enumTypeSpec: TypeSpec): FileSpec { diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt index 6903174d..a85e5248 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt @@ -7,6 +7,7 @@ import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.KModifier.INTERNAL import com.squareup.kotlinpoet.ParameterSpec +import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.TypeSpec import com.virtuslab.pulumikotlin.codegen.expressions.ConstructObjectExpression @@ -23,8 +24,8 @@ import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pul import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.applyValueExtensionMethod import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.convertibleToJavaClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.pulumiDslMarkerAnnotation -import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.toJavaMethod -import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.toKotlinMethod +import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.toJavaExtensionMethod +import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pulumi.toKotlinExtensionMethod import com.virtuslab.pulumikotlin.codegen.step2intermediate.ReferencedType import com.virtuslab.pulumikotlin.codegen.step2intermediate.RootType import com.virtuslab.pulumikotlin.codegen.step2intermediate.Subject.Function @@ -100,8 +101,8 @@ object TypeGenerator { .addImports( applySuspendExtensionMethod(), applyValueExtensionMethod(), - toJavaMethod(), - toKotlinMethod(), + toJavaExtensionMethod(), + toKotlinExtensionMethod(), ) .addTypes( generateArgsClass(context), @@ -127,7 +128,7 @@ object TypeGenerator { .addDocs("$classDocs\n$propertyDocs") .letIf(options.implementToJava) { val innerType = typeMetadata.names(LanguageType.Java).kotlinPoetClassName - val convertibleToJava = convertibleToJavaClass(innerType) + val convertibleToJava = convertibleToJavaClass().parameterizedBy(innerType) it .addSuperinterface(convertibleToJava) .addFunction(toJavaFunction(typeMetadata, fields)) @@ -265,6 +266,8 @@ object TypeGenerator { .build() } + // The number of characters does not matter here, 16 is an arbitrary number. + @Suppress("MagicNumber") private fun randomStringWith16Characters() = Random() .ints('a'.code, 'z'.code) diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/BasicGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/BasicSetterGenerator.kt similarity index 95% rename from src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/BasicGenerator.kt rename to src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/BasicSetterGenerator.kt index e05b38cf..10c29a99 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/BasicGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/BasicSetterGenerator.kt @@ -7,7 +7,7 @@ import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.addDoc import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.mappingCodeBlock import com.virtuslab.pulumikotlin.codegen.step3codegen.NormalField -object BasicGenerator : SetterGenerator { +object BasicSetterGenerator : SetterGenerator { override fun generate(setter: Setter): Iterable { val normalField = setter.fieldType as? NormalField ?: return emptyList() diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ComplexTypeGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ComplexTypeSetterGenerator.kt similarity index 95% rename from src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ComplexTypeGenerator.kt rename to src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ComplexTypeSetterGenerator.kt index 39ab4083..0692ea75 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ComplexTypeGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ComplexTypeSetterGenerator.kt @@ -7,7 +7,7 @@ import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.builde import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.builderPattern import com.virtuslab.pulumikotlin.codegen.step3codegen.NormalField -object ComplexTypeGenerator : SetterGenerator { +object ComplexTypeSetterGenerator : SetterGenerator { override fun generate(setter: Setter): Iterable { val typedField = setter.fieldType as? NormalField<*> ?: return emptyList() val type = typedField.type as? ReferencedComplexType ?: return emptyList() diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ListTypeGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ListTypeSetterGenerator.kt similarity index 98% rename from src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ListTypeGenerator.kt rename to src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ListTypeSetterGenerator.kt index 9c864e4d..5035f126 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ListTypeGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/ListTypeSetterGenerator.kt @@ -13,7 +13,7 @@ import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.listOf import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.mappingCodeBlock import com.virtuslab.pulumikotlin.codegen.step3codegen.NormalField -object ListTypeGenerator : SetterGenerator { +object ListTypeSetterGenerator : SetterGenerator { override fun generate(setter: Setter): Iterable { val normalField = setter.fieldType as? NormalField<*> ?: return emptyList() val type = normalField.type as? ListType ?: return emptyList() diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/MapTypeGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/MapTypeSetterGenerator.kt similarity index 86% rename from src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/MapTypeGenerator.kt rename to src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/MapTypeSetterGenerator.kt index 0a33e6e2..ece0a077 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/MapTypeGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/MapTypeSetterGenerator.kt @@ -2,9 +2,11 @@ package com.virtuslab.pulumikotlin.codegen.step3codegen.types.setters import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier.VARARG +import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.virtuslab.pulumikotlin.codegen.step2intermediate.MapType import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.pairClass import com.virtuslab.pulumikotlin.codegen.step2intermediate.ReferencedComplexType +import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetExtensions.parameterizedBy import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.BuilderSettingCodeBlock import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.addDocsToBuilderMethod import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.builderLambda @@ -12,7 +14,7 @@ import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.builde import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.mappingCodeBlock import com.virtuslab.pulumikotlin.codegen.step3codegen.NormalField -object MapTypeGenerator : SetterGenerator { +object MapTypeSetterGenerator : SetterGenerator { override fun generate(setter: Setter): Iterable { val normalField = setter.fieldType as? NormalField<*> ?: return emptyList() val type = normalField.type as? MapType ?: return emptyList() @@ -35,7 +37,7 @@ object MapTypeGenerator : SetterGenerator { listOf( builderPattern( name, - pairClass(leftInnerType.toTypeName(), builderLambda(rightInnerType)), + pairClass().parameterizedBy(leftInnerType.toTypeName(), builderLambda(rightInnerType)), kDoc, commonCodeBlock, parameterModifiers = listOf(VARARG), @@ -51,7 +53,7 @@ object MapTypeGenerator : SetterGenerator { .builder(name) .addParameter( "values", - pairClass(leftInnerType.toTypeName(), rightInnerType.toTypeName()), + pairClass().parameterizedBy(leftInnerType, rightInnerType), VARARG, ) .addCode(mappingCodeBlock(normalField, required = false, name, "values.toMap()")) diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/OutputWrappedGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/OutputWrappedSetterGenerator.kt similarity index 90% rename from src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/OutputWrappedGenerator.kt rename to src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/OutputWrappedSetterGenerator.kt index 68cbffb8..fa6f69ce 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/OutputWrappedGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/OutputWrappedSetterGenerator.kt @@ -6,7 +6,7 @@ import com.virtuslab.pulumikotlin.codegen.step2intermediate.ReferencedType import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.addDocsToBuilderMethod import com.virtuslab.pulumikotlin.codegen.step3codegen.OutputWrappedField -object OutputWrappedGenerator : SetterGenerator { +object OutputWrappedSetterGenerator : SetterGenerator { override fun generate(setter: Setter): Iterable { val outputWrappedField = setter.fieldType as? OutputWrappedField ?: return emptyList() @@ -14,7 +14,7 @@ object OutputWrappedGenerator : SetterGenerator { FunSpec .builder(setter.name) .addModifiers(SUSPEND) - .addParameter("value", outputWrappedField.toTypeName().copy(nullable = !setter.fieldRequired)) + .addParameter("value", outputWrappedField.toTypeName()) .addCode("this.%N = value", setter.name) .addDocsToBuilderMethod(setter.kDoc, "value") .build(), diff --git a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/SetterGenerator.kt b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/SetterGenerator.kt index 2ea7fda6..d2b06904 100644 --- a/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/SetterGenerator.kt +++ b/src/main/kotlin/com/virtuslab/pulumikotlin/codegen/step3codegen/types/setters/SetterGenerator.kt @@ -1,6 +1,7 @@ package com.virtuslab.pulumikotlin.codegen.step3codegen.types.setters import com.squareup.kotlinpoet.FunSpec +import com.squareup.kotlinpoet.TypeName import com.virtuslab.pulumikotlin.codegen.step2intermediate.ReferencedType import com.virtuslab.pulumikotlin.codegen.step3codegen.Field import com.virtuslab.pulumikotlin.codegen.step3codegen.FieldType @@ -16,6 +17,10 @@ data class Setter( val fieldRequired: Boolean, val kDoc: KDoc, ) { + + fun toTypeName(): TypeName = + fieldType.toTypeName().copy(nullable = !fieldRequired) + companion object { fun from(originalField: Field, overload: FieldType) = Setter(originalField.name, overload, originalField.required, originalField.kDoc) @@ -27,11 +32,11 @@ data class Setter( object AllSetterGenerators : SetterGenerator { private val generators = listOf( - BasicGenerator, - OutputWrappedGenerator, - ComplexTypeGenerator, - ListTypeGenerator, - MapTypeGenerator, + BasicSetterGenerator, + OutputWrappedSetterGenerator, + ComplexTypeSetterGenerator, + ListTypeSetterGenerator, + MapTypeSetterGenerator, ) override fun generate(setter: Setter): Iterable {