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

how to pass default value and check if a key exist in toml file #115

Open
billcxx opened this issue Dec 27, 2022 · 4 comments
Open

how to pass default value and check if a key exist in toml file #115

billcxx opened this issue Dec 27, 2022 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@billcxx
Copy link

billcxx commented Dec 27, 2022

I see in the Getting started Tutorials,

The get_value interface for processing the TOML data structure ensures that the data structure is complete throughout the whole process of reading it and will add the requested nodes if there are not present or will fill them in with default values. Convince yourself that the empty table indeed changed while reading by passing a serializer to it.

But how can I pass the default value? In call get_value(child, "reverse", reverse, .false.), is the fourth argument the default value?

Another question, is it possible to check if a key exists in a toml table?

@awvwgk awvwgk added the documentation Improvements or additions to documentation label Dec 28, 2022
@awvwgk
Copy link
Member

awvwgk commented Dec 28, 2022

The (optional) fourth argument in the getter interface for tables is the default value.

Usually you do not need to explicitly check for presence. The has_key type bound procedure can be used, but I recommend the error handling recipe instead on ways to check directly using the getter interface.

@billcxx
Copy link
Author

billcxx commented Dec 28, 2022

Thanks for the quick explanation. Sorry for my ignorance, but one more question, is the "error handling recipe" you mentioned referring to the stat optional argument in the get_value interface?

I see snippets like this in the How-Tos/Reporting errors

    call get_value(table, "timestep", config%timestep, 0.5, stat=stat, origin=origin)
    if (stat /= 0) then
      print '(a)', context%report("Cannot read timestep", &
        & origin, "expected real value")
      stop 1
    end if

@awvwgk
Copy link
Member

awvwgk commented Dec 28, 2022

I don't think there is an explicit recipe yet targeting this topic. The error reporting recipe does this touch a bit, also there are some mentions in the table handling recipe section.

If you have a specific use case it might be useful to add it to the recipes and describe how absent data is dealt with. However, the general design strategy of TOML Fortran's getter interface should allow to minimize the need to worry about the existence of keys or entries.

@billcxx
Copy link
Author

billcxx commented Dec 29, 2022

Thanks for the explanation. I'm just thinking what I may need for the future, so I don't have an actual use case yet. I will create issue/PR for documentation if I meet and solve problems dealing with absent data in my program.

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

No branches or pull requests

2 participants