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

Support for optional data class parameters, tests for null encoding in data classes #6

Merged
merged 1 commit into from
Jun 22, 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
19 changes: 12 additions & 7 deletions src/main/kotlin/cc/ekblad/toml/transcoding/TomlDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fun <T : Any?> TomlDecoder.decode(value: TomlValue, target: KType): T {
return when (value) {
is TomlValue.List -> toList(value, target)
is TomlValue.Map -> toObject(value, target)
else -> throw TomlException.DecodingError(value, target)
else -> throw TomlException.DecodingError("no decoder registered for value/target pair", value, target)
}
}

Expand Down Expand Up @@ -132,10 +132,15 @@ private fun <T : Any> TomlDecoder.toDataClass(
): T {
val constructor = kClass.primaryConstructor!!
val tomlNamesByParameterName = mappingFor(kClass)
val parameters = constructor.parameters.map { constructorParameter ->
val parameters = mutableMapOf<KParameter, Any?>()
for (constructorParameter in constructor.parameters) {
val tomlName = tomlNamesByParameterName[constructorParameter.name] ?: constructorParameter.name
val decodedParameterValue = tomlMap.properties[tomlName]?.let { value ->
decode<Any>(value, constructorParameter.type)
val encodedParameterValue = tomlMap.properties[tomlName]
if (encodedParameterValue == null && constructorParameter.isOptional) {
continue
}
val decodedParameterValue = encodedParameterValue?.let { value ->
decode<Any?>(value, constructorParameter.type)
}
val parameterValue = decodedParameterValue ?: defaultValueFor(kClass, constructorParameter)
if (!constructorParameter.type.isMarkedNullable && parameterValue == null) {
Expand All @@ -145,13 +150,13 @@ private fun <T : Any> TomlDecoder.toDataClass(
kType
)
}
parameterValue
}.toTypedArray()
parameters[constructorParameter] = parameterValue
}

if (kClass.visibility == KVisibility.PRIVATE) {
constructor.isAccessible = true
}
return constructor.call(*parameters) as T
return constructor.callBy(parameters) as T
}

internal fun requireKClass(classifier: KClassifier?): KClass<*> =
Expand Down
101 changes: 94 additions & 7 deletions src/test/kotlin/cc/ekblad/toml/transcoder/BuiltinDecoderTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ import java.time.ZoneOffset
import java.util.SortedMap
import kotlin.reflect.full.createType
import kotlin.reflect.typeOf
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.test.*

@OptIn(ExperimentalStdlibApi::class, InternalAPI::class)
class BuiltinDecoderTests : StringTest {
Expand Down Expand Up @@ -503,4 +497,97 @@ class BuiltinDecoderTests : StringTest {
mapper.decodeWithDefaults(Bar(Foo(0, "defaulted"), 0), tomlWithEmptyFoo)
)
}

@Test
fun `can decode to null in data class`() {
data class Foo(val i: Int?)
val nullMapper = tomlMapper {
decoder<TomlValue.Integer, Int> { _, _ -> null }
}

assertEquals(nullMapper.decode(TomlValue.Map(
"i" to TomlValue.Integer(123)
)), Foo(null))
}

@Test
fun `cannot decode to null for non-nullable field in data class`() {
data class Foo(val i: Int)
val nullMapper = tomlMapper {
decoder<TomlValue.Integer, Int> { _, _ -> null }
}

assertFails {
nullMapper.decode<Foo>(TomlValue.Map(
"i" to TomlValue.Integer(123)
))
}
}

@Test
fun `optional arguments work properly in flat maps`() {
data class Foo(val a: Int = 1, val b: String = "default")
val toml = TomlValue.Map(
"a" to TomlValue.Integer(123),
)
assertEquals(
Foo(123, "default"),
mapper.decode(toml)
)
assertEquals(
Foo(1, "default"),
mapper.decode(TomlValue.Map())
)
}

@Test
fun `optional arguments work properly in nested maps`() {
data class Foo(val a: Int = 0, val b: String = "defaulted")
data class Bar(val x: Foo = Foo(), val y: Int = 0)
val toml = TomlValue.Map(
"x" to TomlValue.Map(
"a" to TomlValue.Integer(123)
),
"y" to TomlValue.Integer(321),
)
assertEquals(
Bar(Foo(123, "defaulted"), 321),
mapper.decode(toml)
)
}

@Test
fun `defaults take priority over optional arguments`() {
data class Foo(val a: Int = 1, val b: String = "default")
val toml = TomlValue.Map(
"a" to TomlValue.Integer(123),
)
assertEquals(
Foo(123, "defaulted"),
mapper.decodeWithDefaults(Foo(0, "defaulted"), toml)
)
assertEquals(
Foo(0, "defaulted"),
mapper.decodeWithDefaults(Foo(0, "defaulted"), TomlValue.Map())
)
}

@Test
fun `missing and null values are differentiated between`() {
data class Foo(val a: Int? = 1)
val toml = TomlValue.Map(
"a" to TomlValue.Integer(123),
)
val nullMapper = tomlMapper {
decoder<TomlValue.Integer, Int> { _, _ -> null }
}
assertEquals(
Foo(null),
nullMapper.decode(toml)
)
assertEquals(
Foo(1),
nullMapper.decode(TomlValue.Map())
)
}
}