-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from driessamyn/coroutines-docs
Doc updates and small refactoring
- Loading branch information
Showing
8 changed files
with
321 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 17 additions & 74 deletions
91
core/src/main/kotlin/net/samyn/kapper/internal/AutoConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,31 @@ | ||
package net.samyn.kapper.internal | ||
|
||
import net.samyn.kapper.KapperParseException | ||
import net.samyn.kapper.KapperUnsupportedOperationException | ||
import java.nio.ByteBuffer | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime | ||
import java.util.Calendar | ||
import java.util.Date | ||
import java.util.UUID | ||
import kotlin.reflect.KClass | ||
|
||
internal object AutoConverter { | ||
internal class AutoConverter( | ||
// register converters | ||
// we can/should extend this to allow users to register custom converters. | ||
private val converters: Map<KClass<*>, (Any) -> Any> = | ||
mapOf( | ||
UUID::class to ::convertUUID, | ||
LocalDate::class to ::convertLocalDate, | ||
LocalDateTime::class to ::convertLocalDateTime, | ||
LocalTime::class to ::convertLocalTime, | ||
Instant::class to ::convertInstant, | ||
), | ||
) { | ||
fun convert( | ||
value: Any, | ||
target: KClass<*>, | ||
): Any { | ||
val converted = | ||
when (target) { | ||
UUID::class -> { | ||
if (value is String) { | ||
try { | ||
UUID.fromString(value) | ||
} catch (e: Exception) { | ||
throw KapperParseException( | ||
"Cannot parse $value to UUID", | ||
e, | ||
) | ||
} | ||
} else if (value is ByteArray) { | ||
value.asUUID() | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} | ||
} | ||
LocalDate::class -> | ||
if (value is Date) { | ||
val cal = Calendar.getInstance() | ||
cal.time = value | ||
LocalDate.of(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH)) | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} | ||
LocalDateTime::class -> | ||
if (value is Instant) { | ||
LocalDateTime.ofInstant(value, java.time.ZoneOffset.UTC) | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} | ||
LocalTime::class -> | ||
if (value is Instant) { | ||
LocalTime.ofInstant(value, java.time.ZoneOffset.UTC) | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} | ||
Instant::class -> | ||
if (value is LocalTime) { | ||
value.atDate(LocalDate.now()).toInstant(java.time.ZoneOffset.UTC) | ||
} else if (value is LocalDateTime) { | ||
value.atZone(java.time.ZoneOffset.UTC).toInstant() | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} | ||
else -> | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} | ||
return converted | ||
} | ||
|
||
private fun ByteArray.asUUID(): UUID { | ||
val b = ByteBuffer.wrap(this) | ||
return UUID(b.getLong(), b.getLong()) | ||
} | ||
): Any = | ||
// Kover considers this not covered, which I think is a bug. Will reproduce in separate project and raise issue. | ||
converters[target]?.invoke(value) ?: throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to ${target.qualifiedName}", | ||
) | ||
} |
87 changes: 87 additions & 0 deletions
87
core/src/main/kotlin/net/samyn/kapper/internal/Converters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package net.samyn.kapper.internal | ||
|
||
import net.samyn.kapper.KapperParseException | ||
import net.samyn.kapper.KapperUnsupportedOperationException | ||
import java.nio.ByteBuffer | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.LocalTime | ||
import java.util.Calendar | ||
import java.util.Date | ||
import java.util.UUID | ||
|
||
internal fun convertInstant(value: Any): Instant = | ||
when (value) { | ||
is LocalTime -> { | ||
value.atDate(LocalDate.now()).toInstant(java.time.ZoneOffset.UTC) | ||
} | ||
|
||
is LocalDateTime -> { | ||
value.atZone(java.time.ZoneOffset.UTC).toInstant() | ||
} | ||
|
||
else -> { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to Instant", | ||
) | ||
} | ||
} | ||
|
||
internal fun convertLocalTime(value: Any): LocalTime = | ||
if (value is Instant) { | ||
LocalTime.ofInstant(value, java.time.ZoneOffset.UTC) | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to LocalTime", | ||
) | ||
} | ||
|
||
internal fun convertLocalDateTime(value: Any): LocalDateTime = | ||
if (value is Instant) { | ||
LocalDateTime.ofInstant(value, java.time.ZoneOffset.UTC) | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to LocalDateTime", | ||
) | ||
} | ||
|
||
internal fun convertLocalDate(value: Any): LocalDate = | ||
if (value is Date) { | ||
val cal = Calendar.getInstance() | ||
cal.time = value | ||
LocalDate.of(cal[Calendar.YEAR], cal[Calendar.MONTH] + 1, cal[Calendar.DAY_OF_MONTH]) | ||
} else { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to LocalDate", | ||
) | ||
} | ||
|
||
internal fun convertUUID(value: Any): UUID = | ||
when (value) { | ||
is String -> { | ||
try { | ||
UUID.fromString(value) | ||
} catch (e: Exception) { | ||
throw KapperParseException( | ||
"Cannot parse $value to UUID", | ||
e, | ||
) | ||
} | ||
} | ||
|
||
is ByteArray -> { | ||
value.asUUID() | ||
} | ||
|
||
else -> { | ||
throw KapperUnsupportedOperationException( | ||
"Cannot auto-convert from ${value.javaClass} to UUID", | ||
) | ||
} | ||
} | ||
|
||
fun ByteArray.asUUID(): UUID { | ||
val b = ByteBuffer.wrap(this) | ||
return UUID(b.getLong(), b.getLong()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.