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

Update at_path(toml::path) to handle missing component #168

Merged
merged 2 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
- **[@bobfang1992](https://github.com/bobfang1992)** - Reported a bug and created a [wrapper in python](https://github.com/bobfang1992/pytomlpp)
- **[@GiulioRomualdi](https://github.com/GiulioRomualdi)** - Added cmake+meson support
- **[@jonestristand](https://github.com/jonestristand)** - Designed and implemented the `toml::path`s feature
- **[@kcsaul](https://github.com/kcsaul)** - Fixed a bug
- **[@levicki](https://github.com/levicki)** - Helped design some new features
- **[@moorereason](https://github.com/moorereason)** - Reported a whole bunch of bugs
- **[@mosra](https://github.com/mosra)** - Created the awesome [m.css] used to generate the API docs
Expand Down
3 changes: 3 additions & 0 deletions include/toml++/impl/path.inl
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ TOML_NAMESPACE_START
// Error: invalid component
return {};
}

if (!current)
return {}; // not found
}

return node_view{ current };
Expand Down
6 changes: 6 additions & 0 deletions tests/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ TEST_CASE("path - accessing")

CHECK(tbl["d"][""]);
CHECK(tbl["d"][""] == at_path(tbl, toml::path("d.")));

CHECK(!at_path(tbl, toml::path("has.missing.component")));
}

SECTION("array")
Expand All @@ -535,6 +537,8 @@ TEST_CASE("path - accessing")
CHECK(tbl["b"][2]["c"]);
CHECK(tbl["b"][2]["c"] == arr.at_path(toml::path("[2].c")));
CHECK(tbl["b"][2]["c"] == arr.at_path(toml::path("[2] \t.c"))); // whitespace is allowed after array indexers

CHECK(!arr.at_path(toml::path("[3].missing.component")));
}

SECTION("indexing operator")
Expand Down Expand Up @@ -580,5 +584,7 @@ TEST_CASE("path - accessing")

CHECK(tbl["d"][""]);
CHECK(tbl["d"][""] == tbl[toml::path("d.")]);

CHECK(!tbl[toml::path("has.missing.component")]);
}
}
3 changes: 3 additions & 0 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11161,6 +11161,9 @@ TOML_NAMESPACE_START
// Error: invalid component
return {};
}

if (!current)
return {}; // not found
}

return node_view{ current };
Expand Down