Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
Signed-off-by: Julia Plewa <[email protected]>
  • Loading branch information
jplewa committed Jul 13, 2023
1 parent 7f37233 commit 062cfee
Show file tree
Hide file tree
Showing 13 changed files with 708 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ object IntermediateRepresentationGenerator {
fun <V> createTypes(
map: Map<String, V>,
usageKind: UsageKind? = null,
isProvider: Boolean = false,
propertyExtractor: (V) -> RootTypeProperty?,
) = map
.mapValues { propertyExtractor(it.value) }
.filterNotNullValues()
.flatMap { (name, value) ->
createRootTypes(context, name, value, listOfNotNull(usageKind))
createRootTypes(context, name, value, listOfNotNull(usageKind), isProvider)
}

val syntheticTypes = listOf(
Expand All @@ -99,6 +100,7 @@ object IntermediateRepresentationGenerator {
createTypes(
mapOf(DEFAULT_PROVIDER_TOKEN to schema.provider),
UsageKind(Root, Resource, Input),
isProvider = true,
) { resource ->
ObjectProperty(
properties = resource.inputProperties,
Expand All @@ -119,7 +121,7 @@ object IntermediateRepresentationGenerator {

private fun createResources(types: Map<TypeKey, RootType>, context: Context): List<ResourceType> {
return context.schema.resources.mapNotNull { (typeToken, resource) ->
createResource(typeToken, resource, context, types)
createResource(typeToken, resource, context, types, isProvider = false)
}
}

Expand All @@ -128,7 +130,7 @@ object IntermediateRepresentationGenerator {
resource: SchemaModel.Resource,
context: Context,
types: Map<TypeKey, RootType>,
isProvider: Boolean = false,
isProvider: Boolean,
): ResourceType? {
val resultFields = resource.properties
.letIf(isProvider, ::filterStringProperties)
Expand Down Expand Up @@ -162,7 +164,7 @@ object IntermediateRepresentationGenerator {
private fun createFunctions(types: Map<TypeKey, RootType>, context: Context): List<FunctionType> {
return context.schema.functions.mapNotNull { (typeName, function) ->
try {
val pulumiName = PulumiName.from(typeName, context.namingConfiguration)
val pulumiName = PulumiName.from(typeName, context.namingConfiguration, isProvider = false)

val inputUsageKind = UsageKind(Root, Function, Input)
val argumentType = findTypeOrEmptyComplexType(
Expand Down Expand Up @@ -203,6 +205,7 @@ object IntermediateRepresentationGenerator {
typeName: String,
rootType: RootTypeProperty,
forcedUsageKinds: List<UsageKind> = emptyList(),
isProvider: Boolean,
): List<RootType> {
val usages = forcedUsageKinds.ifEmpty {
val allUsagesForTypeName = context.referenceFinder.getUsages(typeName)
Expand All @@ -216,7 +219,7 @@ object IntermediateRepresentationGenerator {
}

try {
val pulumiName = PulumiName.from(typeName, context.namingConfiguration)
val pulumiName = PulumiName.from(typeName, context.namingConfiguration, isProvider)
return usages.map { usage ->
when (rootType) {
is ObjectProperty -> ComplexType(
Expand Down Expand Up @@ -304,7 +307,7 @@ object IntermediateRepresentationGenerator {
} else if (context.referencedStringTypesResolver.shouldGenerateStringType(referencedTypeName)) {
StringType
} else {
val pulumiName = PulumiName.from(referencedTypeName, context.namingConfiguration)
val pulumiName = PulumiName.from(referencedTypeName, context.namingConfiguration, isProvider = false)
when (context.referenceFinder.resolve(referencedTypeName)) {
is ObjectProperty -> ReferencedComplexType(
TypeMetadata(pulumiName, usageKind, getKDoc(property)),
Expand Down Expand Up @@ -398,8 +401,11 @@ object IntermediateRepresentationGenerator {
name = with(name) {
PulumiName(
providerName.lowercase(),
namespace.map { it.lowercase() },
providerNameOverride?.lowercase(),
baseNamespace.map { it.lowercase() },
moduleName?.lowercase(),
name.lowercase(),
isProvider = isProvider,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ import org.virtuslab.pulumikotlin.codegen.utils.decapitalize

data class PulumiName(
val providerName: String,
val namespace: List<String>,
val providerNameOverride: String?,
val baseNamespace: List<String>,
val moduleName: String?,
val name: String,
val isProvider: Boolean = false,
val isProvider: Boolean,
) {
val namespace: List<String>
get() = (
baseNamespace +
(providerNameOverride ?: providerName) +
moduleName
)
.filterNotNull()
.filter { it.isNotBlank() }
.map { it.replace("-", "") }

private data class Modifiers(
val nameSuffix: String,
Expand Down Expand Up @@ -176,20 +187,16 @@ data class PulumiName(
}

fun toFunctionGroupObjectName(namingFlags: NamingFlags): String {
return when (namingFlags.language) {
Kotlin, Java -> {
if (namespace.isEmpty()) {
"${getProviderPrefix()}Functions"
} else {
namespace.last().replace(".", "_").capitalize() + "Functions"
}
}
return if (moduleName != null) {
moduleName.replace(".", "_").capitalize() + "Functions"
} else {
getProviderPrefix(namingFlags.language).replace(".", "_").capitalize() + "Functions"
}
}

fun toResourceName(namingFlags: NamingFlags): String {
return if (namingFlags.language == Kotlin && isProvider) {
"${getProviderPrefix()}${name.capitalize()}"
"${getProviderPrefix(namingFlags.language)}${name.capitalize()}"
} else {
name.capitalize()
}
Expand Down Expand Up @@ -217,7 +224,13 @@ data class PulumiName(
}
}

private fun getProviderPrefix() = providerName.split("-").joinToString("") { it.capitalize() }
private fun getProviderPrefix(languageType: LanguageType): String {
val splitProviderName = providerName.split("-")
return when (languageType) {
Kotlin -> splitProviderName.joinToString("") { it.capitalize() }
Java -> splitProviderName.joinToString("").capitalize()
}
}

private fun packageToString(packageList: List<String>): String {
return packageList.joinToString(".")
Expand Down Expand Up @@ -266,37 +279,46 @@ data class PulumiName(

require(segments.size == EXPECTED_NUMBER_OF_SEGMENTS_IN_TOKEN) { "Malformed token $token" }

fun substituteWithOverride(name: String) = namingConfiguration.packageOverrides[name] ?: name
fun substituteWithOverride(name: String) = namingConfiguration.packageOverrides[name]

val module = when (segments[1]) {
"providers" -> ""
"providers" -> null
else -> {
val moduleMatches = namingConfiguration.moduleFormatRegex.matchEntire(segments[1])
?.groupValues
.orEmpty()

if (moduleMatches.size < 2 || moduleMatches[1].startsWith("index")) {
""
null
} else {
moduleMatches[1]
}
}
}

val providerName = substituteWithOverride(namingConfiguration.providerName)
val moduleName = substituteWithOverride(module)

val namespace = (namingConfiguration.baseNamespace + providerName + moduleName)
.filter { it.isNotBlank() }
.map { it.replace("-", "") }
val providerNameOverride = substituteWithOverride(namingConfiguration.providerName)
val moduleName = module?.let { substituteWithOverride(it) } ?: module

val name = segments[2]

if (name.contains("/")) {
val namespace = (
namingConfiguration.baseNamespace +
(providerNameOverride ?: namingConfiguration.providerName) +
moduleName
)
.filterNotNull()
throw InvalidPulumiName(name, namespace)
}

return PulumiName(namingConfiguration.providerName, namespace, name, isProvider = isProvider)
return PulumiName(
namingConfiguration.providerName,
providerNameOverride,
namingConfiguration.baseNamespace,
moduleName,
name,
isProvider,
)
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/test/kotlin/org/virtuslab/pulumikotlin/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ internal fun <T> extractOutputValue(output: Output<T>?): T? {
return value
}

internal fun <T> concat(iterableOfIterables: Iterable<Iterable<T>>?): List<T> =
iterableOfIterables?.flatten().orEmpty()

internal fun <T> concat(vararg iterables: Iterable<T>?): List<T> =
concat(iterables.filterNotNull().asIterable())

internal fun namingConfigurationWithSlashInModuleFormat(providerName: String) =
PulumiNamingConfiguration.create(providerName = providerName, moduleFormat = "(.*)(?:/[^/]*)")
internal fun namingConfigurationWithSlashInModuleFormat(
providerName: String,
packageOverrides: Map<String, String> = emptyMap(),
) =
PulumiNamingConfiguration.create(
providerName = providerName,
moduleFormat = "(.*)(?:/[^/]*)",
packageOverrides = packageOverrides,
)

private fun messagePrefix(message: String?) = if (message == null) "" else "$message. "

Expand Down
Loading

0 comments on commit 062cfee

Please sign in to comment.