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

Relax javaDoc and add it to custom mapper function too #3554

Merged
merged 2 commits into from Oct 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -311,7 +311,7 @@ abstract class QueryGenerator(
}

protected fun addJavadoc(builder: FunSpec.Builder) {
if (query.javadoc != null) javadocText(query.javadoc)?.let { builder.addKdoc(it) }
if (query.javadoc != null) { builder.addKdoc(javadocText(query.javadoc)) }
}

protected open fun awaiting(): Pair<String, String>? = "%L" to ".await()"
Expand Down
Expand Up @@ -116,7 +116,7 @@ class SelectQueryGenerator(
}

private fun customResultTypeFunctionInterface(): FunSpec.Builder {
val function = FunSpec.builder(query.name)
val function = FunSpec.builder(query.name).also(::addJavadoc)
val params = mutableListOf<CodeBlock>()

query.arguments.sortedBy { it.index }.forEach { (_, argument) ->
Expand Down
Expand Up @@ -45,7 +45,7 @@ internal class TableInterfaceGenerator(private val table: LazyQuery) {
SqlDelightStmtIdentifier::class.java,
)
identifier?.childOfType(SqlTypes.JAVADOC)?.let { javadoc ->
javadocText(javadoc)?.let { typeSpec.addKdoc(it) }
typeSpec.addKdoc(javadocText(javadoc))
}

val constructor = FunSpec.constructorBuilder()
Expand Down
Expand Up @@ -29,9 +29,10 @@ import com.intellij.psi.PsiElement
*/
private val JAVADOC_TEXT_REGEX = Regex("/\\*\\*|\n \\*[ /]?| \\*/")

internal fun javadocText(javadoc: PsiElement): String? {
internal fun javadocText(javadoc: PsiElement): String {
return javadoc.text
.split(JAVADOC_TEXT_REGEX)
.dropWhile { it.isEmpty() }
.joinToString(separator = "\n") { it.trim() }
.removeSuffix("\n*/")
}
Expand Up @@ -13,6 +13,8 @@ import app.cash.sqldelight.core.TestDialect.SQLITE_3_38
import app.cash.sqldelight.dialects.hsql.HsqlDialect
import app.cash.sqldelight.dialects.mysql.MySqlDialect
import app.cash.sqldelight.dialects.postgresql.PostgreSqlDialect
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.LONG

internal val TestDialect.textType
get() = when (this) {
Expand All @@ -31,6 +33,12 @@ internal val TestDialect.intType
MYSQL, SQLITE_3_24, SQLITE_3_18, SQLITE_3_25, SQLITE_3_30, SQLITE_3_35, SQLITE_3_38, POSTGRESQL, HSQL -> "INTEGER"
}

internal val TestDialect.intKotlinType
get() = when (this) {
SQLITE_3_24, SQLITE_3_18, SQLITE_3_25, SQLITE_3_30, SQLITE_3_35, SQLITE_3_38 -> LONG
MYSQL, POSTGRESQL, HSQL -> INT
}

/**
* The [check] statement generated for the prepared statement type in the binder lambda for this dialect.
*
Expand All @@ -50,11 +58,10 @@ internal val TestDialect.binderCheck
*
* See [SelectQueryGenerator].
*/
internal val TestDialect.cursorCheck
get() = when {
dialect.isSqlite -> ""
else -> when (dialect) {
is PostgreSqlDialect, is HsqlDialect, is MySqlDialect -> "check(cursor is app.cash.sqldelight.driver.jdbc.JdbcCursor)\n "
else -> throw IllegalStateException("Unknown dialect: $this")
}
internal fun TestDialect.cursorCheck(whitespaces: Int) = when {
dialect.isSqlite -> ""
else -> when (dialect) {
is PostgreSqlDialect, is HsqlDialect, is MySqlDialect -> "check(cursor is app.cash.sqldelight.driver.jdbc.JdbcCursor)\n${" ".repeat(whitespaces)}"
else -> throw IllegalStateException("Unknown dialect: $this")
}
}
Expand Up @@ -5,10 +5,14 @@ import app.cash.sqldelight.core.TestDialect.SQLITE_3_18
import app.cash.sqldelight.core.compiler.MutatorQueryGenerator
import app.cash.sqldelight.core.compiler.SelectQueryGenerator
import app.cash.sqldelight.core.dialects.binderCheck
import app.cash.sqldelight.core.dialects.cursorCheck
import app.cash.sqldelight.core.dialects.intKotlinType
import app.cash.sqldelight.core.dialects.textType
import app.cash.sqldelight.test.util.FixtureCompiler
import com.google.common.truth.Truth.assertThat
import com.squareup.burst.BurstJUnit4
import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.LONG
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -193,14 +197,39 @@ class JavadocTest {
|
""".trimMargin(),
)

val int = testDialect.intKotlinType
val toInt = when (int) {
LONG -> ""
INT -> ".toInt()"
else -> error("Unknown kotlinType $int")
}

assertThat(selectGenerator.customResultTypeFunction().toString()).isEqualTo(
"""
|/**
| * Queries all values.
| */
|public fun <T : kotlin.Any> selectAll(mapper: (_id: $int, value_: kotlin.String) -> T): app.cash.sqldelight.Query<T> = app.cash.sqldelight.Query(-585795480, arrayOf("test"), driver, "Test.sq", "selectAll", ""${'"'}
||SELECT *
||FROM test
|""${'"'}.trimMargin()) { cursor ->
| ${testDialect.cursorCheck(2)}mapper(
| cursor.getLong(0)!!$toInt,
| cursor.getString(1)!!
| )
|}
|
""".trimMargin(),
)
}

@Test fun `select - misformatted javadoc`(testDialect: TestDialect) {
val file = FixtureCompiler.parseSql(
createTable(testDialect) + """
|/**
|Queries all values.
| */
|*/
|selectAll:
|SELECT *
|FROM test;
Expand Down
Expand Up @@ -1254,7 +1254,7 @@ class SelectQueryTypeTest {
| |SELECT birthday, age
| |FROM adults
| ""${'"'}.trimMargin()) { cursor ->
| ${dialect.cursorCheck}mapper(
| ${dialect.cursorCheck(4)}mapper(
| cursor.getString(0)?.let { childrenAdapter.birthdayAdapter.decode(it) },
| cursor.getString(1)
| )
Expand Down