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

Cannot iterate over sub tables of an inline table #215

Open
tsadowski opened this issue Mar 30, 2023 · 2 comments
Open

Cannot iterate over sub tables of an inline table #215

tsadowski opened this issue Mar 30, 2023 · 2 comments

Comments

@tsadowski
Copy link

Hello,

I use the following code to iterate over subtables of a table:

    fot = toml::find_or<toml::table>(tomlModel,"Body", toml::table{});
    for (const auto& node : fot)
    {
        try
        {
            // Get data
        }
        catch (const std::exception& e)
        {
            // Report Error
        }
    }

This works fine for the long table format...

[Body]
[Body.1]
nodes=[1, 2]

[Body.2]
nodes=[2, 3]

... but fails with the inline format.

Body = {1={nodes=[1,2]},2={nodes=[2,3]}}

The table "Body" is not found so there is nothing to iterate over.

Any hints?

Cheers,
Torsten

@ToruNiina
Copy link
Owner

Sorry, but I could not reproduce it with a simple toml file that includes Body only. It works with both files.

The internal representation of an inline table is the same as that of multiline table. So the same operation should be able to be applied.

If you have encountered this behavior in a toml file containing other tables, it may be explained by the difference of inline tables and (multiline) tables in the toml specification.

Multiline tables create a new table, so the table value is placed below root. But an inline table is defined as a key-value pair, so they are placed under the table to which the row belongs.

That means that the following toml file

[foo]
bar = "baz"
[Body.1]
nodes=[1, 2]

corresponds to the following JSON.

{
  "foo": {"bar": "baz"},
  "Body": {"1": {"nodes": [1, 2]}}
}

On the other hand, the following toml file

[foo]
bar = "baz"

Body = {1={nodes=[1,2]},2={nodes=[2,3]}} 

corresponds to the following JSON.

{
  "foo": { "bar": "baz", "Body": { "1": {"nodes": [1, 2]}, "2": {"nodes": [2, 3]} } }
}

It makes Body "not found".

@tsadowski
Copy link
Author

Dear ToruNiina,

thanks for the explanation.

That is probably the case. The table is swallowed by the previous table when it is inlined. So std::setw(xx) is a much more important parameter than I expected.

Cheers,
Torsten

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants