ktoml icon indicating copy to clipboard operation
ktoml copied to clipboard

Fetch a primitive value with a TOML path, so I don't need to create my own classes

Open aSemy opened this issue 1 year ago • 3 comments

In TomlJ I can extract values without needing to create my own classes

[content]
version = "1.2.3"
import org.tomlj.Toml

val libs = Toml.parse(File("data.toml").toPath())
val externalVersion = libs.getString("content.version")

I'd like to be able to do this with Ktoml.

I tried to use partiallyDecodeFromString():

Toml.partiallyDecodeFromString(String.serializer(), File("data.toml").readText(), "content.version")

But I got an error:

> Cannot find table with name <content.version> in the toml input.  Are you sure that this table exists in the input? Not able to decode this toml part.

aSemy avatar Jan 27 '24 17:01 aSemy

Hm, and what’s about anonymous maps we released last time? @aSemy

orchestr7 avatar Jan 29 '24 19:01 orchestr7

Something like was requested here: https://github.com/akuleshov7/ktoml/issues/234

orchestr7 avatar Jan 29 '24 19:01 orchestr7

Does this help? https://github.com/akuleshov7/ktoml#qa

[a]
    b = 42
    c = "String"
    [a.innerTable]
        d = 5
    [a.otherInnerTable]
        d = "String"

// MyClass(a={b=42, c=String, innerTable={d=5}, otherInnerTable={d=String}})
@Serializable
data class MyClass(
    val a: Map<String, Map<String, String>>
)

orchestr7 avatar Jan 29 '24 19:01 orchestr7