Skip to content

Commit

Permalink
improve code readability (after review comments)
Browse files Browse the repository at this point in the history
  • Loading branch information
myhau committed Oct 20, 2022
1 parent 7435731 commit 896cde2
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ data class Assignment(val to: String, val expression: Expression) : Code() {
}

data class GroupedCode(val list: List<Code>) : Code() {

constructor(vararg code: Code) : this(code.toList())

override fun toCodeBlock(): CustomCodeBlock {
val blocks = list.map { it.toCodeBlock() }

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T : ReferencedType> {

abstract val type: T

abstract fun toTypeName(): TypeName
Expand All @@ -20,7 +20,7 @@ data class NormalField<T : ReferencedType>(override val type: T, val mappingCode

data class OutputWrappedField<T : ReferencedType>(override val type: T) : FieldType<T>() {
override fun toTypeName(): ParameterizedTypeName {
return outputClass(type.toTypeName())
return outputClass().parameterizedBy(type)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<TypeSpec>) =
apply {
types.forEach {
addType(it)
}
}
return this
}

fun FileSpec.Builder.addTypes(types: Iterable<TypeSpec>): 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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -100,8 +101,8 @@ object TypeGenerator {
.addImports(
applySuspendExtensionMethod(),
applyValueExtensionMethod(),
toJavaMethod(),
toKotlinMethod(),
toJavaExtensionMethod(),
toKotlinExtensionMethod(),
)
.addTypes(
generateArgsClass(context),
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunSpec> {
val normalField = setter.fieldType as? NormalField<ReferencedType> ?: return emptyList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunSpec> {
val typedField = setter.fieldType as? NormalField<*> ?: return emptyList()
val type = typedField.type as? ReferencedComplexType ?: return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunSpec> {
val normalField = setter.fieldType as? NormalField<*> ?: return emptyList()
val type = normalField.type as? ListType ?: return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ 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
import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetPatterns.builderPattern
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<FunSpec> {
val normalField = setter.fieldType as? NormalField<*> ?: return emptyList()
val type = normalField.type as? MapType ?: return emptyList()
Expand All @@ -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),
Expand All @@ -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()"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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<FunSpec> {
val outputWrappedField = setter.fieldType as? OutputWrappedField<ReferencedType> ?: return emptyList()

return listOf(
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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ReferencedType>, overload: FieldType<ReferencedType>) =
Setter(originalField.name, overload, originalField.required, originalField.kDoc)
Expand All @@ -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<FunSpec> {
Expand Down

0 comments on commit 896cde2

Please sign in to comment.