Skip to content

Commit

Permalink
feat: fix wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudshiftchris committed Mar 16, 2024
1 parent 7c2593f commit a98043a
Show file tree
Hide file tree
Showing 4,197 changed files with 28,420 additions and 44,266 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cloudshift.awscdkdsl.build.dsl.model.type

import cloudshift.awscdkdsl.build.dsl.isBuilderClass
import cloudshift.awscdkdsl.build.dsl.isCdkClass
import cloudshift.awscdkdsl.build.dsl.mappedClassName
import cloudshift.awscdkdsl.build.dsl.model.CdkModel
import com.squareup.kotlinpoet.ANY
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.MAP
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.TypeSpec
import kotlin.reflect.KClass

internal object CdkWrappersGenerator {
internal fun generateCdkWrappersObject(model: CdkModel): FileSpec {
val builder = TypeSpec.objectBuilder(ClassName)
.addModifiers(KModifier.INTERNAL)

val resolveWrapperFunction = FunSpec.builder("resolveWrapperFunction")
.addParameter("klass", KClass::class)
.returns(KClass::class)
.addStatement(
"val qualifiedName = klass::class.qualifiedName ?: error(%P)",
"No qualified name for \${klass::class}",
)
.addStatement("return typeMap[qualifiedName] ?: klass.superclasses.filterNot { it.java.isInterface || it.isAbstract || it == Any::class }.firstOrNull()?.let { resolveWrapperFunction(it) }")
.build()

val wrapFunction = FunSpec.builder("wrap")
.addModifiers(KModifier.INTERNAL)
.addParameter("cdkObject", ANY)
.returns(ANY)
.addCode("%L", resolveWrapperFunction)
.addStatement(
"return resolveWrapperFunction(cdkObject::class)?.invoke(cdkObject) ?: error(%P)",
"No wrapper found for type \${cdkObject::class}",
)

builder.addFunction(wrapFunction.build())

val mapBuilder = CodeBlock.builder()
.addStatement("mapOf(")
model.classes.filter {
it.className.isCdkClass && !(it.isEnum || it.isInterface || it.isAbstract || it.className.isBuilderClass)
}.map {
mapBuilder.addStatement(
"\"%L\" to { %T.wrap(it as %T) },",
it.className,
it.className.mappedClassName(),
it.className,
)
}
mapBuilder.addStatement(")")

val lambdaTypeName = LambdaTypeName.get(
receiver = ANY,
returnType = ANY,
)
val typeMap = PropertySpec.builder("typeMap", MAP.parameterizedBy(STRING, lambdaTypeName))
.addModifiers(KModifier.PRIVATE)
.initializer(mapBuilder.build())
.build()

builder.addProperty(typeMap)

return FileSpec.builder(ClassName)
.addType(builder.build())
.build()
}

val ClassName = ClassName("io.cloudshiftdev.awscdk.common", "CdkObjectWrappers")
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ internal object WrapperTypeGenerator {
generateWrapperTypeFile(it, ctx)
}

return specs + generateCdkObject()
return specs + generateCdkObject() + CdkWrappersGenerator.generateCdkWrappersObject(model)
}


private fun generateWrapperTypeFile(
cdkClass: CdkClass,
ctx: TypeGeneratorContext
Expand Down Expand Up @@ -73,7 +72,6 @@ internal object WrapperTypeGenerator {
else -> {
val superClass = when {
cdkClass.superClass.isJssiClass || cdkClass.superClass == ANY -> {
// TODO - pass in ctr param
CdkObject
}

Expand Down Expand Up @@ -267,15 +265,16 @@ internal object WrapperTypeGenerator {
}

if (cdkClass.isInterface || cdkClass.isAbstract) {
val wrapperClass = cdkClass.className.mappedClassName().nestedClass("Wrapper")
val wrapper = generateInterfaceWrapper(wrapperClass, cdkClass, ctx)
typeBuilder.addType(wrapper)
if (ctx.model.innerClasses(cdkClass.className).any { it.isBuilderClass }) {
typeBuilder.addType(generateInterfaceWrapper(cdkClass, ctx))
}
companionBuilder.addFunction(
FunSpec.builder("wrap")
.addModifiers(KModifier.INTERNAL)
.returns(cdkClass.className.mappedClassName())
.addParameter(CdkObjectName, cdkClass.className)
.addStatement("return %T(%N)", wrapperClass, CdkObjectName)
.addStatement("return %T.wrap(%N) as %T", CdkWrappersGenerator.ClassName, CdkObjectName,
cdkClass.className.mappedClassName())
.build(),
)
companionBuilder.addFunction(
Expand Down Expand Up @@ -348,11 +347,10 @@ internal object WrapperTypeGenerator {
}

private fun generateInterfaceWrapper(
wrapperClass: ClassName,
cdkClass: CdkClass,
ctx: TypeGeneratorContext,
): TypeSpec {

val wrapperClass = cdkClass.className.mappedClassName().nestedClass("Wrapper")
val wrapperBuilder = TypeSpec.classBuilder(wrapperClass)
.addModifiers(KModifier.PRIVATE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ kotlin {
languageVersion = JavaLanguageVersion.of(17)
vendor = JvmVendorSpec.AMAZON
}
when {
System.getenv("CI") != null -> {
kotlinDaemonJvmArgs = listOf("-Xms20g", "-Xmx20g")
}
else -> {
kotlinDaemonJvmArgs = listOf("-Xms10g", "-Xmx10g")
}
kotlinDaemonJvmArgs = when {
System.getenv("CI") != null -> listOf("-Xms20g", "-Xmx20g")
else -> listOf("-Xms12g", "-Xmx12g")
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = io.cloudshiftdev.kotlin-cdk-wrapper
version = 0.7.4
version = 0.7.5

kotlin.code.style=official

Expand Down
1 change: 1 addition & 0 deletions kotlin-cdk-wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {

dependencies {
implementation(libs.awscdk)
implementation(kotlin("reflect"))
api(project(":kotlin-cdk-wrapper-common"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.Any
import kotlin.Boolean
import kotlin.String
Expand Down Expand Up @@ -446,7 +447,8 @@ public interface AppProps {
return Wrapper(builderImpl.apply(block).build())
}

internal fun wrap(cdkObject: software.amazon.awscdk.AppProps): AppProps = Wrapper(cdkObject)
internal fun wrap(cdkObject: software.amazon.awscdk.AppProps): AppProps =
CdkObjectWrappers.wrap(cdkObject) as AppProps

internal fun unwrap(wrapped: AppProps): software.amazon.awscdk.AppProps = (wrapped as
CdkObject).cdkObject as software.amazon.awscdk.AppProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit

Expand Down Expand Up @@ -279,7 +280,7 @@ public interface ArnComponents {
}

internal fun wrap(cdkObject: software.amazon.awscdk.ArnComponents): ArnComponents =
Wrapper(cdkObject)
CdkObjectWrappers.wrap(cdkObject) as ArnComponents

internal fun unwrap(wrapped: ArnComponents): software.amazon.awscdk.ArnComponents = (wrapped as
CdkObject).cdkObject as software.amazon.awscdk.ArnComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit
import kotlin.jvm.JvmName
Expand Down Expand Up @@ -145,7 +146,8 @@ public interface AssetManifestDockerImageDestination {
}

internal fun wrap(cdkObject: software.amazon.awscdk.AssetManifestDockerImageDestination):
AssetManifestDockerImageDestination = Wrapper(cdkObject)
AssetManifestDockerImageDestination = CdkObjectWrappers.wrap(cdkObject) as
AssetManifestDockerImageDestination

internal fun unwrap(wrapped: AssetManifestDockerImageDestination):
software.amazon.awscdk.AssetManifestDockerImageDestination = (wrapped as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit
import kotlin.jvm.JvmName
Expand Down Expand Up @@ -143,7 +144,8 @@ public interface AssetManifestFileDestination {
}

internal fun wrap(cdkObject: software.amazon.awscdk.AssetManifestFileDestination):
AssetManifestFileDestination = Wrapper(cdkObject)
AssetManifestFileDestination = CdkObjectWrappers.wrap(cdkObject) as
AssetManifestFileDestination

internal fun unwrap(wrapped: AssetManifestFileDestination):
software.amazon.awscdk.AssetManifestFileDestination = (wrapped as CdkObject).cdkObject as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit
import kotlin.jvm.JvmName
Expand Down Expand Up @@ -255,7 +256,7 @@ public interface AssetOptions {
}

internal fun wrap(cdkObject: software.amazon.awscdk.AssetOptions): AssetOptions =
Wrapper(cdkObject)
CdkObjectWrappers.wrap(cdkObject) as AssetOptions

internal fun unwrap(wrapped: AssetOptions): software.amazon.awscdk.AssetOptions = (wrapped as
CdkObject).cdkObject as software.amazon.awscdk.AssetOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit
import kotlin.collections.List
Expand Down Expand Up @@ -341,7 +342,7 @@ public interface AssetStagingProps : FingerprintOptions, AssetOptions {
}

internal fun wrap(cdkObject: software.amazon.awscdk.AssetStagingProps): AssetStagingProps =
Wrapper(cdkObject)
CdkObjectWrappers.wrap(cdkObject) as AssetStagingProps

internal fun unwrap(wrapped: AssetStagingProps): software.amazon.awscdk.AssetStagingProps =
(wrapped as CdkObject).cdkObject as software.amazon.awscdk.AssetStagingProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit

Expand Down Expand Up @@ -102,7 +103,8 @@ public interface BootstraplessSynthesizerProps {
}

internal fun wrap(cdkObject: software.amazon.awscdk.BootstraplessSynthesizerProps):
BootstraplessSynthesizerProps = Wrapper(cdkObject)
BootstraplessSynthesizerProps = CdkObjectWrappers.wrap(cdkObject) as
BootstraplessSynthesizerProps

internal fun unwrap(wrapped: BootstraplessSynthesizerProps):
software.amazon.awscdk.BootstraplessSynthesizerProps = (wrapped as CdkObject).cdkObject as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.String
import kotlin.Unit
import kotlin.collections.List
Expand Down Expand Up @@ -529,7 +530,7 @@ public interface BundlingOptions {
}

internal fun wrap(cdkObject: software.amazon.awscdk.BundlingOptions): BundlingOptions =
Wrapper(cdkObject)
CdkObjectWrappers.wrap(cdkObject) as BundlingOptions

internal fun unwrap(wrapped: BundlingOptions): software.amazon.awscdk.BundlingOptions = (wrapped
as CdkObject).cdkObject as software.amazon.awscdk.BundlingOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.Boolean
import kotlin.Unit

Expand Down Expand Up @@ -88,7 +89,8 @@ public interface CfnAutoScalingReplacingUpdate {
}

internal fun wrap(cdkObject: software.amazon.awscdk.CfnAutoScalingReplacingUpdate):
CfnAutoScalingReplacingUpdate = Wrapper(cdkObject)
CfnAutoScalingReplacingUpdate = CdkObjectWrappers.wrap(cdkObject) as
CfnAutoScalingReplacingUpdate

internal fun unwrap(wrapped: CfnAutoScalingReplacingUpdate):
software.amazon.awscdk.CfnAutoScalingReplacingUpdate = (wrapped as CdkObject).cdkObject as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.Boolean
import kotlin.Number
import kotlin.String
Expand Down Expand Up @@ -434,7 +435,8 @@ public interface CfnAutoScalingRollingUpdate {
}

internal fun wrap(cdkObject: software.amazon.awscdk.CfnAutoScalingRollingUpdate):
CfnAutoScalingRollingUpdate = Wrapper(cdkObject)
CfnAutoScalingRollingUpdate = CdkObjectWrappers.wrap(cdkObject) as
CfnAutoScalingRollingUpdate

internal fun unwrap(wrapped: CfnAutoScalingRollingUpdate):
software.amazon.awscdk.CfnAutoScalingRollingUpdate = (wrapped as CdkObject).cdkObject as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.Boolean
import kotlin.Unit

Expand Down Expand Up @@ -85,7 +86,8 @@ public interface CfnAutoScalingScheduledAction {
}

internal fun wrap(cdkObject: software.amazon.awscdk.CfnAutoScalingScheduledAction):
CfnAutoScalingScheduledAction = Wrapper(cdkObject)
CfnAutoScalingScheduledAction = CdkObjectWrappers.wrap(cdkObject) as
CfnAutoScalingScheduledAction

internal fun unwrap(wrapped: CfnAutoScalingScheduledAction):
software.amazon.awscdk.CfnAutoScalingScheduledAction = (wrapped as CdkObject).cdkObject as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.Number
import kotlin.Unit

Expand Down Expand Up @@ -81,7 +82,8 @@ public interface CfnCodeDeployBlueGreenAdditionalOptions {
}

internal fun wrap(cdkObject: software.amazon.awscdk.CfnCodeDeployBlueGreenAdditionalOptions):
CfnCodeDeployBlueGreenAdditionalOptions = Wrapper(cdkObject)
CfnCodeDeployBlueGreenAdditionalOptions = CdkObjectWrappers.wrap(cdkObject) as
CfnCodeDeployBlueGreenAdditionalOptions

internal fun unwrap(wrapped: CfnCodeDeployBlueGreenAdditionalOptions):
software.amazon.awscdk.CfnCodeDeployBlueGreenAdditionalOptions = (wrapped as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package io.cloudshiftdev.awscdk

import io.cloudshiftdev.awscdk.common.CdkDslMarker
import io.cloudshiftdev.awscdk.common.CdkObject
import io.cloudshiftdev.awscdk.common.CdkObjectWrappers
import kotlin.Unit
import kotlin.jvm.JvmName

Expand Down Expand Up @@ -145,7 +146,8 @@ public interface CfnCodeDeployBlueGreenApplication {
}

internal fun wrap(cdkObject: software.amazon.awscdk.CfnCodeDeployBlueGreenApplication):
CfnCodeDeployBlueGreenApplication = Wrapper(cdkObject)
CfnCodeDeployBlueGreenApplication = CdkObjectWrappers.wrap(cdkObject) as
CfnCodeDeployBlueGreenApplication

internal fun unwrap(wrapped: CfnCodeDeployBlueGreenApplication):
software.amazon.awscdk.CfnCodeDeployBlueGreenApplication = (wrapped as CdkObject).cdkObject
Expand Down
Loading

0 comments on commit a98043a

Please sign in to comment.