Skip to content

Commit

Permalink
codegen done, tests of packages now
Browse files Browse the repository at this point in the history
  • Loading branch information
lbialy committed Sep 18, 2024
1 parent 290eeed commit a4e7a42
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 91 deletions.
4 changes: 2 additions & 2 deletions besom-cats/src/main/scala/runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ import scala.reflect.Typeable
* @return
* The component resource.
*/
def component[A <: ComponentResource & Product: RegistersOutputs: Typeable](using ctx: Context)(
def component[A <: ComponentResource & Product: RegistersOutputs: Typeable](
name: NonEmptyString,
typ: ResourceType,
opts: ComponentResourceOptions = ComponentResourceOptions()
)(
f: Context ?=> ComponentBase ?=> A
f: ComponentBase ?=> A
): Output[A] = Pulumi.component(name, typ, opts)(f)
4 changes: 2 additions & 2 deletions besom-zio/src/main/scala/runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ import scala.reflect.Typeable
* @return
* The component resource.
*/
def component[A <: ComponentResource & Product: RegistersOutputs: Typeable](using ctx: Context)(
def component[A <: ComponentResource & Product: RegistersOutputs: Typeable](
name: NonEmptyString,
typ: ResourceType,
opts: ComponentResourceOptions = ComponentResourceOptions()
)(
f: Context ?=> ComponentBase ?=> A
f: ComponentBase ?=> A
): Output[A] = Pulumi.component(name, typ, opts)(f)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final case class CustomResource[A] private (
) extends besom.CustomResource

object CustomResource:
def apply[A: besom.types.Encoder: besom.types.Decoder](using ctx: besom.types.Context)(
def apply[A: besom.types.Encoder: besom.types.Decoder](
name: besom.util.NonEmptyString,
args: CustomResourceArgs[A],
opts: besom.ResourceOptsVariant.Component ?=> besom.ComponentResourceOptions = besom.ComponentResourceOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object CustomResourceArgs:
kind: String,
metadata: besom.types.Input.Optional[besom.api.kubernetes.meta.v1.inputs.ObjectMetaArgs] = scala.None,
spec: besom.types.Input[A]
)(using besom.types.Context): CustomResourceArgs[A] =
): CustomResourceArgs[A] =
new CustomResourceArgs(
apiVersion = apiVersion,
kind = kind,
Expand All @@ -27,7 +27,7 @@ object CustomResourceArgs:
kind: String = customResourceArgs.kind,
metadata: besom.types.Input.Optional[besom.api.kubernetes.meta.v1.inputs.ObjectMetaArgs] = customResourceArgs.metadata,
spec: besom.types.Input[A] = customResourceArgs.spec
)(using besom.types.Context): CustomResourceArgs[A] =
): CustomResourceArgs[A] =
new CustomResourceArgs(
apiVersion = apiVersion,
kind = kind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final case class CustomResourcePatch[A] private (
) extends besom.CustomResource

object CustomResourcePatch:
def apply[A: besom.types.Encoder: besom.types.Decoder](using ctx: besom.types.Context)(
def apply[A: besom.types.Encoder: besom.types.Decoder](
name: besom.util.NonEmptyString,
args: CustomResourcePatchArgs[A],
opts: besom.ResourceOptsVariant.Component ?=> besom.ComponentResourceOptions = besom.ComponentResourceOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object CustomResourcePatchArgs:
kind: String,
metadata: besom.types.Input.Optional[besom.api.kubernetes.meta.v1.inputs.ObjectMetaPatchArgs] = scala.None,
spec: besom.types.Input.Optional[A] = scala.None
)(using besom.types.Context): CustomResourcePatchArgs[A] =
): CustomResourcePatchArgs[A] =
new CustomResourcePatchArgs(
apiVersion = apiVersion,
kind = kind,
Expand All @@ -27,7 +27,7 @@ object CustomResourcePatchArgs:
kind: String = customResourcePatchArgs.kind,
metadata: besom.types.Input.Optional[besom.api.kubernetes.meta.v1.inputs.ObjectMetaPatchArgs] = customResourcePatchArgs.metadata,
spec: besom.types.Input.Optional[A] = customResourcePatchArgs.spec
)(using besom.types.Context): CustomResourcePatchArgs[A] =
): CustomResourcePatchArgs[A] =
new CustomResourcePatchArgs(
apiVersion = apiVersion,
kind = kind,
Expand Down
39 changes: 21 additions & 18 deletions codegen/src/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,12 @@ class CodeGen(using
val thisTypeRef = baseClassCoordinates.typeRef
val tokenLiteral = Lit.String(functionToken.asString)
val code =
m"""| def ${Term.Name(name)}(using ctx: besom.types.Context)(
m"""| def ${Term.Name(name)}(
| args: ${argsClassRef}${argsDefault},
| opts: besom.InvokeOptions = besom.InvokeOptions()
| ): besom.types.Output[${resultTypeRef}] =
| ): besom.types.Output[${resultTypeRef}] = besom.internal.Output.getContext.flatMap { implicit ctx =>
| ctx.call[$argsClassRef, $resultTypeRef, $thisTypeRef](${tokenLiteral}, args, this, opts)
| }
|""".stripMargin.parse[Stat].get

(supportClassSourceFiles, code)
Expand All @@ -490,14 +491,14 @@ class CodeGen(using
val tokenLit = Lit.String(token.asString)

val resourceDecoderInstance =
m""" given resourceDecoder(using besom.types.Context): besom.types.ResourceDecoder[$baseClassName] =
m""" given resourceDecoder: besom.types.ResourceDecoder[$baseClassName] =
| besom.internal.ResourceDecoder.derived[$baseClassName]
|""".stripMargin

val decoderInstanceName = if isRemoteComponent then "remoteComponentResourceDecoder" else "customResourceDecoder"

val decoderInstance =
m""" given decoder(using besom.types.Context): besom.types.Decoder[$baseClassName] =
m""" given decoder: besom.types.Decoder[$baseClassName] =
| besom.internal.Decoder.$decoderInstanceName[$baseClassName]
|""".stripMargin

Expand Down Expand Up @@ -528,12 +529,13 @@ class CodeGen(using
| * )
| * }}}
| */
| def apply(using ctx: besom.types.Context)(
| def apply(
| name: besom.util.NonEmptyString,
| args: ${argsClassName}${argsDefault},
| opts: besom.ResourceOptsVariant.$variant ?=> ${resourceOptsClass} = ${resourceOptsClass}()
| ): besom.types.Output[$baseClassName] =
| ): besom.types.Output[$baseClassName] = besom.internal.Output.getContext.flatMap { implicit ctx =>
| ctx.${resourceRegisterMethodName}[$baseClassName, $argsClassName](${tokenLit}, name, args, opts(using besom.ResourceOptsVariant.$variant))
| }
|
| private[besom] def typeToken: besom.types.ResourceType = ${tokenLit}
|
Expand Down Expand Up @@ -685,11 +687,12 @@ class CodeGen(using
val fileContent =
s"""|package ${methodCoordinates.packageRef}
|
|def ${Name(methodName)}(using ctx: besom.types.Context)(
|def ${Name(methodName)}(
| args: ${argsClassRef}${argsDefault},
| opts: besom.InvokeOptions = besom.InvokeOptions()
|): besom.types.Output[${resultTypeRef}] =
|): besom.types.Output[${resultTypeRef}] = besom.internal.Output.getContext.flatMap { implicit ctx =>
| ctx.invoke[$argsClassRef, $resultTypeRef](${token}, args, opts)
|}
|""".stripMargin

SourceFile(filePath = methodCoordinates.filePath, sourceCode = fileContent)
Expand Down Expand Up @@ -852,7 +855,7 @@ class CodeGen(using
val _ = scalameta.parseStatement(classDef)

val decoderInstance =
m"""| given decoder(using besom.types.Context): besom.types.Decoder[$className] =
m"""| given decoder: besom.types.Decoder[$className] =
| besom.internal.Decoder.derived[$className]
|""".stripMargin

Expand Down Expand Up @@ -1017,40 +1020,40 @@ class CodeGen(using

val derivedTypeclasses = {
lazy val providerArgsEncoderInstance =
m"""| given providerArgsEncoder(using besom.types.Context): besom.types.ProviderArgsEncoder[$argsClassName] =
m"""| given providerArgsEncoder: besom.types.ProviderArgsEncoder[$argsClassName] =
| besom.internal.ProviderArgsEncoder.derived[$argsClassName]
|""".stripMargin

lazy val argsEncoderInstance =
m"""| given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[$argsClassName] =
m"""| given argsEncoder: besom.types.ArgsEncoder[$argsClassName] =
| besom.internal.ArgsEncoder.derived[$argsClassName]
|""".stripMargin

if (isProvider)
m"""| given encoder(using besom.types.Context): besom.types.Encoder[$argsClassName] =
m"""| given encoder: besom.types.Encoder[$argsClassName] =
| besom.internal.Encoder.derived[$argsClassName]
|$providerArgsEncoderInstance""".stripMargin
else if (isResource)
m"""| given encoder(using besom.types.Context): besom.types.Encoder[$argsClassName] =
m"""| given encoder: besom.types.Encoder[$argsClassName] =
| besom.internal.Encoder.derived[$argsClassName]
|$argsEncoderInstance""".stripMargin
else
m"""| given encoder(using besom.types.Context): besom.types.Encoder[$argsClassName] =
m"""| given encoder: besom.types.Encoder[$argsClassName] =
| besom.internal.Encoder.derived[$argsClassName]
|$argsEncoderInstance""".stripMargin
}

m"""|object $argsClassName:
| def apply(
|${argsCompanionApplyParams.map(arg => s" ${arg.syntax}").mkString(",\n")}
| )(using besom.types.Context): $argsClassName =
| ): $argsClassName =
| new $argsClassName(
|${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 =
| ): $argsClassName =
| new $argsClassName(
|${argsCompanionApplyBodyArgs.map(arg => s" ${arg.syntax}").mkString(",\n")}
| )
Expand All @@ -1074,7 +1077,7 @@ class CodeGen(using
Term.Tuple(List(keyLit, decoder))
}.toList)
val name = decoderUniqueName(unionType)
name -> m"""| given ${name}(using besom.types.Context): besom.types.Decoder[$unionType] =
name -> m"""| given ${name}: besom.types.Decoder[$unionType] =
| besom.internal.Decoder.discriminated($keyPropertyNameLit, $indexesLit)
|""".stripMargin
case TypeMapper.UnionMapping.ByIndex(unionType, indexToType) =>
Expand All @@ -1084,7 +1087,7 @@ class CodeGen(using
Term.Tuple(List(keyLit, decoder))
}.toList)
val name = decoderUniqueName(unionType)
name -> m"""| given ${name}(using besom.types.Context): besom.types.Decoder[$unionType] =
name -> m"""| given ${name}: besom.types.Decoder[$unionType] =
| besom.internal.Decoder.nonDiscriminated($indexesLit)
|""".stripMargin
}
Expand Down
Loading

0 comments on commit a4e7a42

Please sign in to comment.