Skip to content

Commit

Permalink
Update at_path(toml::path) to handle missing component (#168)
Browse files Browse the repository at this point in the history
* test(paths): check at_path handles missing path component

* fix(paths): update at_path to handle missing component
  • Loading branch information
kcsaul authored Aug 25, 2022
1 parent cc3c660 commit be0fbd5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
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

0 comments on commit be0fbd5

Please sign in to comment.