Skip to content

Commit

Permalink
refactor(#9): refactor file reading as a given type or null
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Dec 27, 2021
1 parent 1e46cae commit 0d21bf6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/main/kotlin/io/github/kotools/csv/DataType.kt
Expand Up @@ -2,6 +2,7 @@ package io.github.kotools.csv

import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.KVisibility
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.full.starProjectedType
Expand Down Expand Up @@ -34,9 +35,11 @@ private constructor(private val value: KClass<T>) {
}

companion object {
infix fun <T : Any> createOrNull(type: KClass<T>): DataType<T>? {
if (!type.isData) return null
return DataType(type)
}
infix fun <T : Any> createOrNull(type: KClass<T>): DataType<T>? =
if (!type.isData || type.visibility?.isNotInternal() == true) null
else DataType(type)

private fun KVisibility.isNotInternal(): Boolean =
this != KVisibility.INTERNAL && this != KVisibility.PUBLIC
}
}
16 changes: 7 additions & 9 deletions src/test/kotlin/io/github/kotools/csv/ReaderTest.kt
Expand Up @@ -26,6 +26,9 @@ private fun <C : Collection<T>?, T> awaitAndAssertIsValid(
}
}

@Suppress("unused")
class InvalidExample(val first: String, val second: Int, val third: Boolean)

class ReaderTest {
private val validConfiguration: Reader.() -> Unit by lazy {
{
Expand Down Expand Up @@ -99,9 +102,8 @@ class ReaderTest {

@Test
fun `should fail with invalid type`(): Unit = runBlocking {
assertNull {
csvReaderOrNullAs<InvalidExample> { }
}
assertNull { csvReaderOrNullAs<InvalidExample>(validConfiguration) }
assertNull { csvReaderOrNullAs<PrivateExample>(validConfiguration) }
}

@Test
Expand All @@ -128,13 +130,9 @@ class ReaderTest {
}
}

data class Example(
val first: String,
val second: Int,
val third: Boolean
)
data class Example(val first: String, val second: Int, val third: Boolean)

data class InvalidExample(
private data class PrivateExample(
val first: String,
val second: Int,
val third: Boolean
Expand Down

0 comments on commit 0d21bf6

Please sign in to comment.