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

Improve acronym function names #250

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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.virtuslab.pulumikotlin.codegen.step2intermediate.Subject.Function
import org.virtuslab.pulumikotlin.codegen.step2intermediate.Subject.Resource
import org.virtuslab.pulumikotlin.codegen.utils.capitalize
import org.virtuslab.pulumikotlin.codegen.utils.decapitalize
import org.virtuslab.pulumikotlin.codegen.utils.lowercaseBefore

data class PulumiName(
val providerName: String,
Expand Down Expand Up @@ -202,6 +203,19 @@ data class PulumiName(
}
}

fun toResourceFunctionName(namingFlags: NamingFlags): String {
require(namingFlags.language == Kotlin)
val resourceName = toResourceName(namingFlags)
jplewa marked this conversation as resolved.
Show resolved Hide resolved
val numberOfCaps = resourceName.takeWhile { it.isUpperCase() || it.isDigit() }.count()
return if (numberOfCaps == resourceName.length) {
resourceName.lowercase()
} else if (numberOfCaps > 1) {
resourceName.lowercaseBefore(numberOfCaps - 1)
} else {
resourceName.decapitalize()
}
jplewa marked this conversation as resolved.
Show resolved Hide resolved
}

fun toClassName(namingFlags: NamingFlags): String {
val suffix = getNameSuffix(namingFlags)
return name.capitalize() + suffix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import org.virtuslab.pulumikotlin.codegen.step3codegen.TypeNameClashResolver
import org.virtuslab.pulumikotlin.codegen.step3codegen.addDeprecationWarningIfAvailable
import org.virtuslab.pulumikotlin.codegen.step3codegen.addDocs
import org.virtuslab.pulumikotlin.codegen.step3codegen.addDocsIfAvailable
import org.virtuslab.pulumikotlin.codegen.utils.decapitalize
import org.virtuslab.pulumikotlin.codegen.utils.letIf
import kotlin.reflect.KClass
import kotlin.reflect.full.declaredMemberProperties
Expand Down Expand Up @@ -212,7 +211,7 @@ object ResourceGenerator {
.build()

val resourceFunction = FunSpec
.builder(names.toResourceName(kotlinFlags).decapitalize())
.builder(names.toResourceFunctionName(kotlinFlags))
.addModifiers(KModifier.SUSPEND)
.returns(resourceClassName)
.addParameter("name", STRING)
Expand All @@ -237,7 +236,7 @@ object ResourceGenerator {
.build()

val resourceFunctionWithoutLambda = FunSpec
.builder(names.toResourceName(kotlinFlags).decapitalize())
.builder(names.toResourceFunctionName(kotlinFlags))
.returns(resourceClassName)
.addParameter("name", STRING)
.addStatement("val builder = %T()", resourceBuilderClassName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ fun String.capitalize() =
fun String.decapitalize() =
replaceFirstChar { it.lowercase(Locale.getDefault()) }

fun String.lowercaseBefore(index: Int) = take(index).lowercase() + drop(index)

/**
* Example:
* ```kt
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/org/virtuslab/pulumikotlin/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pulumi.core.Output
import io.github.cdklabs.projen.java.JavaProject
import io.github.cdklabs.projen.java.JavaProjectOptions
import org.junit.jupiter.api.Assertions.assertFalse
import org.virtuslab.pulumikotlin.codegen.step2intermediate.PulumiName
import org.virtuslab.pulumikotlin.codegen.step2intermediate.PulumiNamingConfiguration
import java.io.File
import java.time.LocalDateTime
Expand All @@ -25,6 +26,9 @@ internal fun namingConfigurationWithSlashInModuleFormat(
packageOverrides = packageOverrides,
)

internal fun pulumiName(providerName: String, baseNamespace: List<String>, moduleName: String, name: String) =
PulumiName(providerName, null, baseNamespace, moduleName, name, false)

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

internal fun <T> assertDoesNotContain(iterable: Iterable<T>, element: T, message: String? = null) {
Expand Down
60 changes: 39 additions & 21 deletions src/test/kotlin/org/virtuslab/pulumikotlin/codegen/CodegenTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.aws.acm.kotlin.certificate

suspend fun main() {
certificate("name") {
args {
Expand Down Expand Up @@ -81,7 +81,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.aws.acm.kotlin.certificate

suspend fun main() {
val resource1 = certificate("name") {
args {
Expand Down Expand Up @@ -137,7 +137,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.aws.acmpca.kotlin.AcmpcaFunctions.getCertificateAuthority

suspend fun main() {
val cert = getCertificateAuthority(arn = "www.wp.pl", tags = mapOf("a" to "b"))

Expand All @@ -153,7 +153,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.aws.acmpca.kotlin.AcmpcaFunctions.getCertificateAuthority

suspend fun main() {
val cert = getCertificateAuthority {
arn("www.wp.pl")
Expand Down Expand Up @@ -188,7 +188,7 @@ class CodegenTest {
import com.pulumi.core.Either

private const val RECORD_NAME = "record"

suspend fun main() {
val record = record(RECORD_NAME) {
name(RECORD_NAME)
Expand All @@ -199,7 +199,6 @@ class CodegenTest {

record.type
}

"""

assertGeneratedCodeAndSourceFileCompile(SCHEMA_AWS_CLASSIC_SUBSET_WITH_ONE_OF, code)
Expand All @@ -213,7 +212,7 @@ class CodegenTest {
import com.pulumi.aws.route53.kotlin.record

private const val RECORD_NAME = "record"

suspend fun main() {
val record = record(RECORD_NAME) {
name(RECORD_NAME)
Expand Down Expand Up @@ -241,7 +240,7 @@ class CodegenTest {
val code = """
import com.pulumi.aws.kotlin.awsProvider
import com.pulumi.aws.acm.kotlin.certificate

suspend fun main() {
certificate("name") {
args {
Expand Down Expand Up @@ -275,7 +274,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.appengine.kotlin.applicationUrlDispatchRules

suspend fun main() {
applicationUrlDispatchRules("resource-name") {
args {
Expand Down Expand Up @@ -303,7 +302,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.appengine.kotlin.ApplicationUrlDispatchRulesBuilder

suspend fun main() {
ApplicationUrlDispatchRulesBuilder()
}
Expand All @@ -317,7 +316,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.appengine.kotlin.ApplicationUrlDispatchRulesBuilder

suspend fun main() {
val builder: ApplicationUrlDispatchRulesBuilder = null!!
builder.build()
Expand All @@ -332,7 +331,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.appengine.kotlin.inputs.ApplicationUrlDispatchRulesDispatchRuleArgsBuilder

suspend fun main() {
ApplicationUrlDispatchRulesDispatchRuleArgsBuilder()
}
Expand All @@ -346,7 +345,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.appengine.kotlin.inputs.ApplicationUrlDispatchRulesDispatchRuleArgsBuilder

suspend fun main() {
val builder: ApplicationUrlDispatchRulesDispatchRuleArgsBuilder = null!!
builder.build()
Expand All @@ -361,7 +360,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.appengine.kotlin.applicationUrlDispatchRules

suspend fun main() {
applicationUrlDispatchRules("resource-name") {
args {
Expand Down Expand Up @@ -540,7 +539,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.googlenative.cloudbuild.v1.inputs.StorageSourceArgs

suspend fun main() {
StorageSourceArgs.builder()
.`object`("object")
Expand All @@ -556,7 +555,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.googlenative.accesscontextmanager.v1.kotlin.Accesscontextmanager_v1Functions

suspend fun main() {
Accesscontextmanager_v1Functions.getAccessLevel {
accessLevelFormat("format")
Expand Down Expand Up @@ -621,7 +620,6 @@ class CodegenTest {
val code = """
import com.pulumi.kubernetes.apiextensions.v1.kotlin.customResourceDefinitionPatch


suspend fun main() {
customResourceDefinitionPatch("name") {
args {
Expand Down Expand Up @@ -651,7 +649,6 @@ class CodegenTest {
val code = """
import com.pulumi.kubernetes.core.v1.kotlin.limitRange


suspend fun main() {
limitRange("name") {
args {
Expand All @@ -676,7 +673,6 @@ class CodegenTest {
val code = """
import com.pulumi.kubernetes.autoscaling.v2.kotlin.horizontalPodAutoscaler


suspend fun main() {
horizontalPodAutoscaler("name") {
args {
Expand Down Expand Up @@ -752,7 +748,7 @@ class CodegenTest {
val code = """
import com.pulumi.core.Output
import com.pulumi.googlenative.compute.v1.kotlin.instance

suspend fun main() {
instance("instance") {
args {
Expand All @@ -776,7 +772,7 @@ class CodegenTest {
// language=kotlin
val code = """
import com.pulumi.gcp.compute.kotlin.instance

suspend fun main() {
instance("instance") {
args {
Expand All @@ -791,6 +787,24 @@ class CodegenTest {
assertGeneratedCodeAndSourceFileCompile(SCHEMA_GOOGLE_CLASSIC_SUBSET_WITH_INSTANCE, code)
}

@Test
fun `a proparly decapitalized function name is generated for resources whose names start with an acronym`() {
// language=kotlin
val code = """
import com.pulumi.gcp.projects.kotlin.iamMember

suspend fun main() {
iamMember("member") {
args {
member("member")
}
}
}
"""

assertGeneratedCodeAndSourceFileCompile(SCHEMA_GOOGLE_CLASSIC_WITH_ACRONYM, code)
}

private fun assertGeneratedCodeCompiles(testSchema: TestSchema) {
assertGeneratedCodeAndSourceFilesCompile(testSchema, emptyMap())
}
Expand Down Expand Up @@ -1043,3 +1057,7 @@ private val SCHEMA_EQUINIX_METAL_WITH_INDEX = TestSchema(
"schema-equinix-metal-3.3.0-alpha-with-index.json",
"com.pulumi:equinix-metal:3.3.0-alpha.1687671105+a2a938cd",
)
private val SCHEMA_GOOGLE_CLASSIC_WITH_ACRONYM = TestSchema(
"schema-gcp-classic-6.59.0-with-acronym-in-resource-name.json",
"com.pulumi:gcp:6.59.0",
)
Loading