Skip to content

Commit

Permalink
Fix provider coordinates vs token confusion (#306)
Browse files Browse the repository at this point in the history
Fixes #304
  • Loading branch information
pawelprazak authored Nov 21, 2023
1 parent fe694a2 commit 2b86c0c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
30 changes: 25 additions & 5 deletions codegen/src/PulumiDefinitionCoordinates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 0 additions & 16 deletions codegen/src/PulumiDefinitionCoordinates.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
17 changes: 12 additions & 5 deletions codegen/src/PulumiToken.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}

Expand Down
23 changes: 23 additions & 0 deletions codegen/src/PulumiToken.test.scala
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 2b86c0c

Please sign in to comment.