From 872d12855defda503d0e0e4fc4617001e5e5e71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gi=C5=BCka?= Date: Tue, 18 Jun 2024 14:43:59 +0200 Subject: [PATCH] WithArgs (copy) method add to args companion object --- codegen/src/CodeGen.scala | 14 +++++++ codegen/src/CodeGen.test.scala | 71 ++++++++++++++++++++++++++++++++++ codegen/src/Utils.scala | 4 +- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/codegen/src/CodeGen.scala b/codegen/src/CodeGen.scala index 770debc6..18f7e811 100644 --- a/codegen/src/CodeGen.scala +++ b/codegen/src/CodeGen.scala @@ -964,6 +964,9 @@ class CodeGen(using s"Class name for ${classCoordinates.typeRef} could not be found" ) ) + + val argsClassInstanceName = s"${argsClassName.value.head.toLower}${argsClassName.value.tail}" + val argsCompanionApplyParams = inputProperties.filter(_.constValue.isEmpty).map { propertyInfo => val paramType = if (propertyInfo.isOptional) scalameta.types.besom.types.InputOptional(propertyInfo.inputArgType) @@ -990,6 +993,10 @@ class CodeGen(using Term.Assign(Term.Name(propertyInfo.name.value), argValue) } + val argsCompanionWithArgsParams = argsCompanionApplyParams.map { param => + param.copy(default = Some(m"${argsClassInstanceName}.${param.name.syntax}".parse[Term].get)) + } + val derivedTypeclasses = { lazy val providerArgsEncoderInstance = m"""| given providerArgsEncoder(using besom.types.Context): besom.types.ProviderArgsEncoder[$argsClassName] = @@ -1023,6 +1030,13 @@ class CodeGen(using |${argsCompanionApplyBodyArgs.map(arg => s" ${arg.syntax}").mkString(",\n")} | ) | + | extension (${argsClassInstanceName}: ${argsClassName}) def withArgs( + |${argsCompanionWithArgsParams.map(arg => s" ${arg.syntax}").mkString(",\n")} + | )(using besom.types.Context): $argsClassName = + | new $argsClassName( + |${argsCompanionApplyBodyArgs.map(arg => s" ${arg.syntax}").mkString(",\n")} + | ) + | |$derivedTypeclasses |""".stripMargin.parse[Stat].get } diff --git a/codegen/src/CodeGen.test.scala b/codegen/src/CodeGen.test.scala index 4f98b97d..db46fed7 100644 --- a/codegen/src/CodeGen.test.scala +++ b/codegen/src/CodeGen.test.scala @@ -131,6 +131,13 @@ class CodeGenTest extends munit.FunSuite { | helmReleaseSettings = helmReleaseSettings.asOptionOutput(isSecret = false) | ) | + | extension (providerArgs: ProviderArgs) def withArgs( + | helmReleaseSettings: besom.types.Input.Optional[besom.api.example.inputs.HelmReleaseSettingsArgs] = providerArgs.helmReleaseSettings + | )(using besom.types.Context): ProviderArgs = + | new ProviderArgs( + | helmReleaseSettings = helmReleaseSettings.asOptionOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[ProviderArgs] = | besom.internal.Encoder.derived[ProviderArgs] | given providerArgsEncoder(using besom.types.Context): besom.types.ProviderArgsEncoder[ProviderArgs] = @@ -281,6 +288,13 @@ class CodeGenTest extends munit.FunSuite { | | ) | + | extension (clusterArgs: ClusterArgs) def withArgs( + | + | )(using besom.types.Context): ClusterArgs = + | new ClusterArgs( + | + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[ClusterArgs] = | besom.internal.Encoder.derived[ClusterArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[ClusterArgs] = @@ -301,6 +315,13 @@ class CodeGenTest extends munit.FunSuite { | | ) | + | extension (clusterGetKubeconfigArgs: ClusterGetKubeconfigArgs) def withArgs( + | + | )(using besom.types.Context): ClusterGetKubeconfigArgs = + | new ClusterGetKubeconfigArgs( + | + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[ClusterGetKubeconfigArgs] = | besom.internal.Encoder.derived[ClusterGetKubeconfigArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[ClusterGetKubeconfigArgs] = @@ -355,6 +376,15 @@ class CodeGenTest extends munit.FunSuite { | location = location.asOptionOutput(isSecret = false) | ) | + | extension (getClusterArgs: GetClusterArgs) def withArgs( + | clusterId: besom.types.Input[String] = getClusterArgs.clusterId, + | location: besom.types.Input.Optional[String] = getClusterArgs.location + | )(using besom.types.Context): GetClusterArgs = + | new GetClusterArgs( + | clusterId = clusterId.asOutput(isSecret = false), + | location = location.asOptionOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[GetClusterArgs] = | besom.internal.Encoder.derived[GetClusterArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[GetClusterArgs] = @@ -446,6 +476,13 @@ class CodeGenTest extends munit.FunSuite { | supportType = supportType.asOutput(isSecret = false) | ) | + | extension (multipleActivationKeyArgs: MultipleActivationKeyArgs) def withArgs( + | supportType: besom.types.Input[String | besom.api.azurenative.windowsesu.enums.SupportType] = multipleActivationKeyArgs.supportType + | )(using besom.types.Context): MultipleActivationKeyArgs = + | new MultipleActivationKeyArgs( + | supportType = supportType.asOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[MultipleActivationKeyArgs] = | besom.internal.Encoder.derived[MultipleActivationKeyArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[MultipleActivationKeyArgs] = @@ -527,6 +564,13 @@ class CodeGenTest extends munit.FunSuite { | userConfirmation = userConfirmation.asOutput(isSecret = false) | ) | + | extension (jobDefinitionArgs: JobDefinitionArgs) def withArgs( + | userConfirmation: besom.types.Input[besom.api.azurenative.hybriddata.enums.UserConfirmation] = jobDefinitionArgs.userConfirmation + | )(using besom.types.Context): JobDefinitionArgs = + | new JobDefinitionArgs( + | userConfirmation = userConfirmation.asOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[JobDefinitionArgs] = | besom.internal.Encoder.derived[JobDefinitionArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[JobDefinitionArgs] = @@ -628,6 +672,15 @@ class CodeGenTest extends munit.FunSuite { | size = size.asOutput(isSecret = false) | ) | + | extension (containerArgs: ContainerArgs) def withArgs( + | brightness: besom.types.Input[besom.api.plant.enums.ContainerBrightness] = containerArgs.brightness, + | size: besom.types.Input[besom.api.plant.enums.ContainerSize] = containerArgs.size + | )(using besom.types.Context): ContainerArgs = + | new ContainerArgs( + | brightness = brightness.asOutput(isSecret = false), + | size = size.asOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[ContainerArgs] = | besom.internal.Encoder.derived[ContainerArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[ContainerArgs] = @@ -728,6 +781,15 @@ class CodeGenTest extends munit.FunSuite { | spec = spec.asOptionOutput(isSecret = false) | ) | + | extension (eniConfigArgs: EniConfigArgs) def withArgs( + | spec: besom.types.Input.Optional[besom.api.kubernetes.crdk8samazonawscom.v1alpha1.inputs.EniConfigSpecArgs] = eniConfigArgs.spec + | )(using besom.types.Context): EniConfigArgs = + | new EniConfigArgs( + | apiVersion = besom.types.Output("crd.k8s.amazonaws.com/v1alpha1"), + | kind = besom.types.Output("ENIConfig"), + | spec = spec.asOptionOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[EniConfigArgs] = | besom.internal.Encoder.derived[EniConfigArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[EniConfigArgs] = @@ -781,6 +843,15 @@ class CodeGenTest extends munit.FunSuite { | subnet = subnet.asOptionOutput(isSecret = false) | ) | + | extension (eniConfigSpecArgs: EniConfigSpecArgs) def withArgs( + | securityGroups: besom.types.Input.Optional[scala.collection.immutable.List[besom.types.Input[String]]] = eniConfigSpecArgs.securityGroups, + | subnet: besom.types.Input.Optional[String] = eniConfigSpecArgs.subnet + | )(using besom.types.Context): EniConfigSpecArgs = + | new EniConfigSpecArgs( + | securityGroups = securityGroups.asOptionOutput(isSecret = false), + | subnet = subnet.asOptionOutput(isSecret = false) + | ) + | | given encoder(using besom.types.Context): besom.types.Encoder[EniConfigSpecArgs] = | besom.internal.Encoder.derived[EniConfigSpecArgs] | given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[EniConfigSpecArgs] = diff --git a/codegen/src/Utils.scala b/codegen/src/Utils.scala index 4ddf3921..dd79acaf 100644 --- a/codegen/src/Utils.scala +++ b/codegen/src/Utils.scala @@ -17,8 +17,10 @@ object Utils { // Name of the self parameter of resource methods val selfParameterName = "__self__" + // We have to limit number of arguments to 252 instead of 253 + // because class instance is counted as argument in withArgs extension method. // TODO: Find some workaround to enable passing the remaining arguments - val jvmMaxParamsCount = 253 // https://github.com/scala/bug/issues/7324 + val jvmMaxParamsCount = 252 // https://github.com/scala/bug/issues/7324 type FunctionName = String type FunctionToken = String