From 2b86c0c0d9b4ca317d229d9cddbd8ed1b6502055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pra=C5=BCak?= Date: Tue, 21 Nov 2023 14:25:50 +0100 Subject: [PATCH] Fix provider coordinates vs token confusion (#306) Fixes #304 --- codegen/src/PulumiDefinitionCoordinates.scala | 30 +++++++++++++++---- .../PulumiDefinitionCoordinates.test.scala | 16 ---------- codegen/src/PulumiToken.scala | 17 +++++++---- codegen/src/PulumiToken.test.scala | 23 ++++++++++++++ 4 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 codegen/src/PulumiToken.test.scala diff --git a/codegen/src/PulumiDefinitionCoordinates.scala b/codegen/src/PulumiDefinitionCoordinates.scala index 9d081067..a9808d93 100644 --- a/codegen/src/PulumiDefinitionCoordinates.scala +++ b/codegen/src/PulumiDefinitionCoordinates.scala @@ -63,14 +63,34 @@ object PulumiDefinitionCoordinates { moduleToPackageParts: String => Seq[String], providerToPackageParts: String => Seq[String] ): PulumiDefinitionCoordinates = { - val PulumiToken(providerName, modulePortion, definitionName) = PulumiToken(typeToken) - PulumiDefinitionCoordinates( - providerPackageParts = providerToPackageParts(providerName), - modulePackageParts = moduleToPackageParts(modulePortion), - definitionName = definitionName + fromToken( + typeToken = PulumiToken(typeToken), + moduleToPackageParts = moduleToPackageParts, + providerToPackageParts = providerToPackageParts ) } + def fromToken( + typeToken: PulumiToken, + moduleToPackageParts: String => Seq[String], + providerToPackageParts: String => Seq[String] + ): PulumiDefinitionCoordinates = { + typeToken match { + case PulumiToken("pulumi", "providers", providerName) => + PulumiDefinitionCoordinates( + providerPackageParts = providerName :: Nil, + modulePackageParts = Utils.indexModuleName :: Nil, + definitionName = Utils.providerTypeName + ) + case PulumiToken(providerName, moduleName, definitionName) => + PulumiDefinitionCoordinates( + providerPackageParts = providerToPackageParts(providerName), + modulePackageParts = moduleToPackageParts(moduleName), + definitionName = definitionName + ) + } + } + private def inputsPackage = "inputs" private def outputsPackage = "outputs" private def enumsPackage = "enums" diff --git a/codegen/src/PulumiDefinitionCoordinates.test.scala b/codegen/src/PulumiDefinitionCoordinates.test.scala index b478abea..2cebc4ba 100644 --- a/codegen/src/PulumiDefinitionCoordinates.test.scala +++ b/codegen/src/PulumiDefinitionCoordinates.test.scala @@ -179,19 +179,3 @@ class PulumiDefinitionCoordinatesTest extends munit.FunSuite { } } -//noinspection ScalaFileName,TypeAnnotation -class TypeTokenTest extends munit.FunSuite { - test("must provide string representation") { - val t1 = PulumiToken("provider", "index", "SomeType") - assertEquals(t1.asString, "provider:index:SomeType") - val t2 = PulumiToken("provider:index:SomeType") - assertEquals(t2.asString, "provider:index:SomeType") - } - - test("must provide the missing module") { - val t1 = PulumiToken("provider", "", "SomeType") - assertEquals(t1.asString, "provider:index:SomeType") - val t2 = PulumiToken("provider::SomeType") - assertEquals(t2.asString, "provider:index:SomeType") - } -} diff --git a/codegen/src/PulumiToken.scala b/codegen/src/PulumiToken.scala index 5361657d..bf485729 100644 --- a/codegen/src/PulumiToken.scala +++ b/codegen/src/PulumiToken.scala @@ -3,12 +3,15 @@ package besom.codegen import scala.util.matching.Regex case class PulumiToken private (provider: String, module: String, name: String) { - def asString: String = s"${provider}:${module}:${name}" + def asString: String = s"${provider}:${module}:${name}" + def uniformed: String = asString.toLowerCase } object PulumiToken { - private val tokenFmt: String = "(.*):(.*)?:(.*)" // provider:module:name - private val tokenPattern: Regex = ("^" + tokenFmt + "$").r + private val tokenFmtShort: String = "([^:]*):([^:]*)" // provider:name + private val tokenPatternShort: Regex = ("^" + tokenFmtShort + "$").r + private val tokenFmt: String = "([^:]*):([^:]*)?:([^:]*)" // provider:module:name + private val tokenPattern: Regex = ("^" + tokenFmt + "$").r private def enforceNonEmptyModule(module: String): String = module match { @@ -17,14 +20,18 @@ object PulumiToken { } def apply(token: String): PulumiToken = token match { - case tokenPattern("pulumi", "providers", providerName) => - new PulumiToken(providerName, Utils.indexModuleName, Utils.providerTypeName) case tokenPattern(provider, module, name) => new PulumiToken( provider = provider, module = enforceNonEmptyModule(module), name = name ) + case tokenPatternShort(provider, name) => + new PulumiToken( + provider = provider, + module = Utils.indexModuleName, + name = name + ) case _ => throw TypeMapperError(s"Cannot parse Pulumi token: $token, tokenPattern: $tokenPattern") } diff --git a/codegen/src/PulumiToken.test.scala b/codegen/src/PulumiToken.test.scala new file mode 100644 index 00000000..813de982 --- /dev/null +++ b/codegen/src/PulumiToken.test.scala @@ -0,0 +1,23 @@ +package besom.codegen + +//noinspection ScalaFileName +class PulumiTokenTest extends munit.FunSuite { + test("apply") { + assertEquals(PulumiToken("aws:ec2:Instance"), PulumiToken("aws", "ec2", "Instance")) + assertEquals(PulumiToken("pulumi:providers:aws"), PulumiToken("pulumi", "providers", "aws")) + assertEquals(PulumiToken("pulumi", "providers", "aws"), PulumiToken("pulumi", "providers", "aws")) + } + test("asString") { + assertEquals(PulumiToken("aws:ec2:Instance").asString, "aws:ec2:Instance") + assertEquals(PulumiToken("provider", "index", "SomeType").asString, "provider:index:SomeType") + } + test("uniformed") { + assertEquals(PulumiToken("aws:ec2:Instance").uniformed, "aws:ec2:instance") + } + + test("enforce non-empty module") { + assertEquals(PulumiToken("provider", "", "SomeType").asString, "provider:index:SomeType") + assertEquals(PulumiToken("provider::SomeType").asString, "provider:index:SomeType") + assertEquals(PulumiToken("provider:SomeType").asString, "provider:index:SomeType") + } +}