Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add Boolean Conversion Support to JasyncRow #431

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

Merged
merged 3 commits into from
May 27, 2025
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
14 changes: 14 additions & 0 deletions 14 r2dbc-mysql/src/main/java/JasyncRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class JasyncRow(private val rowData: RowData, private val metadata: JasyncMetada
return when {
requestedType == Object::class.java -> value
requestedType == String::class.java -> value?.toString()
requestedType == java.lang.Boolean::class.java -> {
when (value) {
is Number -> value.toInt() != 0
is String -> value.toBoolean()
is Boolean -> value
else -> throw IllegalStateException("Cannot convert ${value?.javaClass?.simpleName} to Boolean")
}
}
value is Number -> {
when (requestedType) {
java.lang.Long::class.java -> value.toLong()
Expand All @@ -54,6 +62,12 @@ class JasyncRow(private val rowData: RowData, private val metadata: JasyncMetada
else -> throw IllegalStateException("unmatched requested type ${requestedType.simpleName}")
}
}
value is Boolean && requestedType.isPrimitive -> {
when (requestedType.name) {
"boolean" -> value
else -> throw IllegalStateException("Cannot convert Boolean to ${requestedType.name}")
}
}
value is LocalDateTime -> {
when (requestedType) {
LocalDate::class.java -> value.toLocalDate()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.jasync.r2dbc.mysql

import com.github.jasync.sql.db.RowData
import io.mockk.every
import io.mockk.mockk
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Unit tests for [JasyncRow].
*/
internal class JasyncRowTest {

@Test
fun testBooleanConversion() {
// Mock dependencies
val rowData = mockk<RowData>()
val metadata = mockk<JasyncMetadata>()

// Setup mock values for different types of data
every { rowData["boolTrue"] } returns true
every { rowData["boolFalse"] } returns false
every { rowData["numZero"] } returns 0
every { rowData["numOne"] } returns 1
every { rowData["stringTrue"] } returns "true"
every { rowData["stringFalse"] } returns "false"
every { rowData[0] } returns true
every { rowData[1] } returns 0
every { rowData[2] } returns "true"

val row = JasyncRow(rowData, metadata)

// Test conversion from Boolean to Boolean
assertTrue(row.get("boolTrue", java.lang.Boolean::class.java) as Boolean)
assertFalse(row.get("boolFalse", java.lang.Boolean::class.java) as Boolean)

// Test conversion from Number to Boolean
assertFalse(row.get("numZero", java.lang.Boolean::class.java) as Boolean)
assertTrue(row.get("numOne", java.lang.Boolean::class.java) as Boolean)

// Test conversion from String to Boolean
assertTrue(row.get("stringTrue", java.lang.Boolean::class.java) as Boolean)
assertFalse(row.get("stringFalse", java.lang.Boolean::class.java) as Boolean)

// Test conversion from various types using index
assertTrue(row.get(0, java.lang.Boolean::class.java) as Boolean)
assertFalse(row.get(1, java.lang.Boolean::class.java) as Boolean)
assertTrue(row.get(2, java.lang.Boolean::class.java) as Boolean)

// Test conversion to primitive boolean - using Boolean class in Kotlin
assertTrue(row.get("boolTrue", java.lang.Boolean::class.java) as Boolean)
assertFalse(row.get("boolFalse", java.lang.Boolean::class.java) as Boolean)
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.