From be0fbd5203571ac8523f96803173c25886dfa57b Mon Sep 17 00:00:00 2001 From: Kevin Saul Date: Thu, 25 Aug 2022 17:13:22 +1200 Subject: [PATCH] Update at_path(toml::path) to handle missing component (#168) * test(paths): check at_path handles missing path component * fix(paths): update at_path to handle missing component --- README.md | 1 + include/toml++/impl/path.inl | 3 +++ tests/path.cpp | 6 ++++++ toml.hpp | 3 +++ 4 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 7a7ad47..477a5db 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/include/toml++/impl/path.inl b/include/toml++/impl/path.inl index 9ae4d3b..1c82c67 100644 --- a/include/toml++/impl/path.inl +++ b/include/toml++/impl/path.inl @@ -504,6 +504,9 @@ TOML_NAMESPACE_START // Error: invalid component return {}; } + + if (!current) + return {}; // not found } return node_view{ current }; diff --git a/tests/path.cpp b/tests/path.cpp index f57d2ed..84b2591 100644 --- a/tests/path.cpp +++ b/tests/path.cpp @@ -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") @@ -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") @@ -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")]); } } diff --git a/toml.hpp b/toml.hpp index d0e1f37..83ea245 100644 --- a/toml.hpp +++ b/toml.hpp @@ -11161,6 +11161,9 @@ TOML_NAMESPACE_START // Error: invalid component return {}; } + + if (!current) + return {}; // not found } return node_view{ current };