-
Notifications
You must be signed in to change notification settings - Fork 106
Erase_at_pointer and related test #1161
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -520,6 +520,84 @@ value::set_at_pointer( | |
| return try_set_at_pointer(sv, ref, opts).value(); | ||
| } | ||
|
|
||
| bool | ||
| value::erase_at_pointer ( | ||
| string_view sv, | ||
| system::error_code& ec) noexcept | ||
| { | ||
| ec.clear(); | ||
| if(sv.empty()){ | ||
| BOOST_JSON_FAIL(ec, error::missing_slash); | ||
| return false; | ||
| } | ||
|
|
||
| string_view walk = sv; | ||
| string_view last_segment; | ||
| while (true) | ||
| { | ||
| last_segment = detail::next_segment(walk, ec); | ||
| if (ec.failed()) | ||
| return false; | ||
| if (walk.empty()) | ||
| break; | ||
| } | ||
|
|
||
| string_view const parent_sv( | ||
| sv.data(), | ||
| static_cast<std::size_t>(last_segment.data() - sv.data())); | ||
|
|
||
| value* parent = detail::walk_pointer( | ||
| *this, | ||
| parent_sv, | ||
| ec, | ||
| []( object& obj, detail::pointer_token token ) | ||
| { | ||
| return detail::if_contains_token(obj, token); | ||
| }, | ||
| []( array& arr, std::size_t index, system::error_code& ec ) -> value* | ||
| { | ||
| if( ec ) | ||
| return nullptr; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not covered by tests. See here: https://1161.json.prtest2.cppalliance.org/diff-report/include/boost/json/impl/pointer.ipp.html. There are also other lines that miss coverage. |
||
|
|
||
| return arr.if_contains(index); | ||
| }, | ||
| []( value&, string_view) | ||
| { | ||
| return std::false_type(); | ||
| }); | ||
|
|
||
| if (!parent) | ||
| return false; | ||
|
|
||
| switch (parent->kind()) | ||
| { | ||
| case boost::json::kind::object: { | ||
| auto& obj = parent->get_object(); | ||
| detail::pointer_token const token(last_segment); | ||
| key_value_pair* kv = detail::find_in_object(obj, token).first; | ||
| if (kv) { | ||
| obj.erase(kv); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| case boost::json::kind::array: { | ||
| auto const index = detail::parse_number_token(last_segment, ec); | ||
| auto& arr = parent->get_array(); | ||
| if (arr.if_contains(index)){ | ||
| arr.erase(arr.begin() + index); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| default: { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://1161.json.prtest2.cppalliance.org/diff-report/include/boost/json/impl/pointer.ipp.html#NL593 I am pretty sure this switch case is unreachable, so replace it with |
||
| BOOST_JSON_FAIL(ec, error::value_is_scalar); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } // namespace json | ||
| } // namespace boost | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it make more sense to just walk the entire pointer string and in object and array handlers update a
value* parentvariable (in the case of object you'd have to also store the key)? Then, after you finished walking:parentis a pointer toarray,objectornullptr(you can rely on that later in the switch case).