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 Nullable Tables #66

Closed
JonathanxD opened this issue Aug 27, 2021 · 1 comment · Fixed by #69
Closed

Support for Nullable Tables #66

JonathanxD opened this issue Aug 27, 2021 · 1 comment · Fixed by #69
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@JonathanxD
Copy link

Given the following code:

class TomlReadTest : FunSpec({
    test("toml read nullable") {
        @Serializable
        data class Key(val value: Long)
        @Serializable
        data class Config(val key: Key?)

        val mapper = Toml(
            config = KtomlConf(
                ignoreUnknownNames = true,
                emptyValuesAllowed = true
            )
        )
        val toml = mapper.decodeFromString<Config>("""            
            [key]
            value = 1            
        """.trimIndent())

        assertNotNull(toml)
        assertEquals(1L, toml.key?.value)
    }
})

It is expected to deserialize Config with key = Key(1L), however ktoml fails with:

This kind of node should not be processed in TomlDecoder.decodeValue(): com.akuleshov7.ktoml.parsers.node.TomlTable@6d9c9c74
com.akuleshov7.ktoml.exceptions.InternalDecodingException: This kind of node should not be processed in TomlDecoder.decodeValue(): com.akuleshov7.ktoml.parsers.node.TomlTable@6d9c9c74
	at com.akuleshov7.ktoml.decoders.TomlDecoder.decodeKeyValue(TomlDecoder.kt:96)
	at com.akuleshov7.ktoml.decoders.TomlDecoder.decodeValue(TomlDecoder.kt:25)
	at com.akuleshov7.ktoml.decoders.TomlDecoder.decodeNotNullMark(TomlDecoder.kt:36)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeNullableSerializableElement(AbstractDecoder.kt:79)

Changing to a property with default value does work:

class TomlReadTest : FunSpec({
    test("toml read nullable") {
        @Serializable
        data class Key(val value: Long)
        @Serializable
        data class Config(val key: Key = Key(0L))

        val mapper = Toml(
            config = KtomlConf(
                ignoreUnknownNames = true,
                emptyValuesAllowed = true
            )
        )
        val toml = mapper.decodeFromString<Config>("""            
            [key]
            value = 1            
        """.trimIndent())

        assertNotNull(toml)
        assertEquals(1L, toml.key.value)
    }
})

All my code base uses Kotlinx Serialization, only the configuration parsing is using Jackson and I would like to remove this.

@orchestr7
Copy link
Owner

orchestr7 commented Aug 28, 2021

Hello @JonathanxD! Thank you for this issue! I even did not think that someone will use a nullable table… I will try to support it in the nearest release.

But it looks that I will need to change the logic significantly.

PS I am actually confused that the default value for tables worked. I have supported it by chance🤦‍♂️

UPD: fixed in #69

@orchestr7 orchestr7 self-assigned this Aug 28, 2021
@orchestr7 orchestr7 added bug Something isn't working good first issue Good for newcomers labels Aug 28, 2021
@orchestr7 orchestr7 linked a pull request Aug 30, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants