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 0aae79d
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 77 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,60 @@
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 toJavaMethod() =
MemberName("com.pulumi.kotlin", "toJava")

fun pulumiDslMarkerAnnotation(): ClassName {
return ClassName("com.pulumi.kotlin", "PulumiTagMarker")
}
fun pulumiDslMarkerAnnotation() =
PulumiTagMarker::class.asClassName()

fun toKotlinMethod(): MemberName {
return MemberName("com.pulumi.kotlin", "toKotlin")
}
fun toKotlinMethod() =
MemberName("com.pulumi.kotlin", "toKotlin")

fun applySuspendExtensionMethod(): MemberName {
return MemberName("com.pulumi.kotlin", "applySuspend")
}
fun applySuspendExtensionMethod() =
MemberName("com.pulumi.kotlin", "applySuspend")

fun applyValueExtensionMethod(): MemberName {
return MemberName("com.pulumi.kotlin", "applyValue")
}
fun applyValueExtensionMethod() =
MemberName("com.pulumi.kotlin", "applyValue")

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(leftType: TypeName, rightType: TypeName) =
Pair::class.asTypeName().parameterizedBy(leftType, rightType)
}

object Java {
object Pulumi {
fun outputOfMethod(): MemberName =
fun outputOfMethod() =
outputClass().member("of")

fun outputClass(): ClassName {
return ClassName("com.pulumi.core", "Output")
}

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,17 +1,18 @@
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.ReferencedType
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())

Expand All @@ -21,19 +22,29 @@ object KotlinPoetExtensions {
fun FileSpec.Builder.addImport(memberName: MemberName) =
addImport(memberName.packageName, memberName.simpleName)

fun FileSpec.Builder.addImports(vararg memberNames: MemberName): FileSpec.Builder {
memberNames.forEach {
addImport(it)
fun FileSpec.Builder.addImports(vararg memberNames: MemberName) =
apply {
memberNames.forEach {
addImport(it)
}
}
return this
}

fun FileSpec.Builder.addTypes(types: Iterable<TypeSpec>): FileSpec.Builder {
types.forEach { addType(it) }
return this
}
fun FileSpec.Builder.addTypes(vararg typeSpecs: TypeSpec) =
addTypes(typeSpecs.toList())

fun FileSpec.Builder.addTypes(types: Iterable<TypeSpec>) =
apply {
types.forEach {
addType(it)
}
}

fun ClassName.parameterizedBy(type: Type) =
parameterizedBy(type.toTypeName())

fun <T : ReferencedType> ClassName.parameterizedBy(field: Field<T>) =
parameterizedBy(field.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 @@ -6,6 +6,7 @@ import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.KModifier.INTERNAL
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.TypeSpec
Expand All @@ -22,6 +23,7 @@ import com.virtuslab.pulumikotlin.codegen.step2intermediate.MoreTypes.Kotlin.Pul
import com.virtuslab.pulumikotlin.codegen.step2intermediate.NamingFlags
import com.virtuslab.pulumikotlin.codegen.step2intermediate.ResourceType
import com.virtuslab.pulumikotlin.codegen.step2intermediate.Subject.Resource
import com.virtuslab.pulumikotlin.codegen.step3codegen.KotlinPoetExtensions.parameterizedBy
import com.virtuslab.pulumikotlin.codegen.step3codegen.addDeprecationWarningIfAvailable
import com.virtuslab.pulumikotlin.codegen.step3codegen.addDocs
import com.virtuslab.pulumikotlin.codegen.step3codegen.addDocsIfAvailable
Expand Down Expand Up @@ -53,12 +55,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, outputClass().parameterizedBy(field))
.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 Down Expand Up @@ -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 @@ -12,7 +12,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<FunSpec> {
val normalField = setter.fieldType as? NormalField<*> ?: return emptyList()
val type = normalField.type as? MapType ?: return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunSpec> {
val outputWrappedField = setter.fieldType as? OutputWrappedField<ReferencedType> ?: return emptyList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,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 0aae79d

Please sign in to comment.