Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithArgs (copy) method added to args companion object #522

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions codegen/src/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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] =
Expand Down Expand Up @@ -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
}
Expand Down
71 changes: 71 additions & 0 deletions codegen/src/CodeGen.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down Expand Up @@ -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] =
Expand All @@ -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] =
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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] =
Expand Down
4 changes: 3 additions & 1 deletion codegen/src/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading