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 iterate through all tables? #2

Closed
emlai opened this issue Aug 18, 2016 · 4 comments
Closed

How to iterate through all tables? #2

emlai opened this issue Aug 18, 2016 · 4 comments

Comments

@emlai
Copy link
Contributor

emlai commented Aug 18, 2016

I have a use case where I would need to loop over all root-level tables to check whether each table contains a certain key. Currently this doesn't seem to be possible.

I was previously using YamlSwift, where this was possible using the dictionary getter:

for (key, value) in yaml.dictionary! {
    if let property = value["property"] {
        // use `key` and `property`
    }
}

It would be nice to have something similar in swift-toml, for example:

for (tableName, table) in toml.tables {
    if let property = try? table.double("property") {
        // use `tableName` and `property`
    }
}
@jdfergason
Copy link
Owner

I can see the use for this but it's not so straightforward to implement since swift-toml is basically storing key/values in a flat structure after it's parsed. I'm working on a function that will allow you to grab all tables beneath a given key path.

To grab all tables at the root level:

for (tableName, table) in toml.tables() { ... }

or, to get all the tables nested within table1:

for (tableName, table) in toml.tables("table1") { ... } 

It will take me a day or two to get this functionality in.

@emlai
Copy link
Contributor Author

emlai commented Aug 19, 2016

Awesome!

@jdfergason
Copy link
Owner

Added support for iterating over all tables at a given level with tables(_: [String]) method in 85fc130 and created v0.3.0 release.

@emlai
Copy link
Contributor Author

emlai commented Aug 20, 2016

Nice, thanks!

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