From 27930755af04937d41cd455aa46e6a2d13b24e76 Mon Sep 17 00:00:00 2001 From: Julia Plewa Date: Tue, 23 Apr 2024 23:18:24 +0200 Subject: [PATCH] add a workaround for nulls in non-nullable fields Signed-off-by: Julia Plewa --- .../step3codegen/types/TypeGenerator.kt | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt b/src/main/kotlin/org/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt index 9b1e7396..ca0576ba 100644 --- a/src/main/kotlin/org/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt +++ b/src/main/kotlin/org/virtuslab/pulumikotlin/codegen/step3codegen/types/TypeGenerator.kt @@ -46,6 +46,11 @@ import kotlin.streams.asSequence object TypeGenerator { + private val nullabilityExceptions = listOf( + NullabilityException("com.pulumi.gcp.organizations.kotlin.outputs", "GetProjectResult", "autoCreateNetwork"), + NullabilityException("com.pulumi.gcp.organizations.kotlin.outputs", "GetProjectResult", "skipDelete"), + ) + fun generateTypes( types: List, generationOptions: GenerationOptions = GenerationOptions(), @@ -56,7 +61,7 @@ object TypeGenerator { val isFunction = usageKind.subject == Function val isOutput = usageKind.direction == Output val fields = if (isFunction || isOutput) { - prepareFieldsForTypesUsedForFunctionsOrAsOutput(type) + prepareFieldsForTypesUsedForFunctionsOrAsOutput(typeNameClashResolver, type) } else { prepareFields(type) } @@ -82,16 +87,24 @@ object TypeGenerator { ) } - private fun prepareFieldsForTypesUsedForFunctionsOrAsOutput(type: ComplexType) = - type.fields.map { (name, typeAndOptionality) -> + private fun prepareFieldsForTypesUsedForFunctionsOrAsOutput( + typeNameClashResolver: TypeNameClashResolver, + type: ComplexType, + ): List> { + val className = typeNameClashResolver.kotlinNames(type.metadata) + return type.fields.map { (fieldName, typeAndOptionality) -> + val isNullabilityException = nullabilityExceptions.contains( + NullabilityException(className.packageName, className.className, fieldName), + ) Field( - name, + fieldName, NormalField(typeAndOptionality.type) { it }, - typeAndOptionality.type !is OptionalType, + !(typeAndOptionality.type is OptionalType || isNullabilityException), overloads = emptyList(), typeAndOptionality.kDoc, ) } + } private fun generateFile(context: Context, typeNameClashResolver: TypeNameClashResolver): FileSpec { val typeMetadata = context.typeMetadata @@ -307,4 +320,10 @@ object TypeGenerator { val fields: List>, val options: GenerationOptions, ) + + private data class NullabilityException( + val packageName: String, + val typeName: String, + val fieldName: String, + ) }