toml_datum_t distinguish between non-present keys and invalid conversions
dave96 opened this issue · comments
Hi!
I updated the the library in my project, which was using toml_raw_t
. I noticed that was now deprecated in favor of the toml_datum_t
interface.
However, in my case I assume missing keys in the configuration are fine (the configuration file does not need to be complete), but type conversion errors are reported to the user. However, when using toml_datum_t
, I can not distinguish between a missing key or a type conversion problem. Is there a canonical way to check if a key is present in a table with the new interface, or do i have to resort to using toml_raw_in(..) != NULL
?
Thanks in advance!
Hi Dave,
toml_key_in(), toml_array_in(), toml_table_in() should do what you want.
Thanks!
Hi @cktan
Thanks for the response, but how does toml_key_in()
help me if I have to provide its keyidx
? Do I have to iterate manually through every key in a table?
Note the following example for clarity:
# file.toml
example_str = "hello"
// program.c
//...
toml_table_t *table = toml_parse_file(...);
// Now I want to know if the key "example_str" has an invalid value or is missing in file.toml
// Before I could do:
toml_raw_t example = toml_raw_in(table, "example_str");
if (!example)
// Missing
if (!toml_rtos(example, &res))
// Wrong format
// Now:
toml_datum_t example = toml_string_in(table, "example_str");
if (!example.ok) {
// Might be in wrong format or missing
// Using toml_key_in as suggested
int found = 0;
for (int i = 0; i < toml_table_nkval(table); ++i) {
if (strcmp(toml_key_in(table, i), "example_str") == 0) {
found = 1;
break;
// Wrong format
}
}
if (!found)
// Missing
}
Is this the intended usage of the new interface? Wouldn't it be convenient to have a wrapper like toml_key_exists()
or similar?
Thanks in advance!
You're right. I thought toml_key_in(tab, x) takes a key in x, but it actually takes an int index.
I have added a function toml_key_exists(tab, key) for your use case.
Thanks!