-
Notifications
You must be signed in to change notification settings - Fork 107
erase_at_pointer #1138
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?
erase_at_pointer #1138
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,110 @@ 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) | ||
| { | ||
| ec.clear(); | ||
| if(sv.empty()){ | ||
| BOOST_JSON_FAIL(ec, error::syntax); | ||
|
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.
|
||
| return false; | ||
| } | ||
|
|
||
| string_view previous_segment; | ||
|
|
||
| string_view segment = detail::next_segment(sv, ec); | ||
|
|
||
| auto result = this; | ||
| auto previous_result = this; | ||
|
|
||
| while (true) | ||
|
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. Looking at this loop, this is pretty much So, why not do that instead of duplicating
Author
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. I will check that part of the code, thank you |
||
| { | ||
| if (ec.failed()) | ||
| return false; | ||
|
|
||
| if (!result) | ||
| { | ||
| BOOST_JSON_FAIL(ec, error::not_found); | ||
| return false; | ||
| } | ||
|
|
||
| if( segment.empty() ) | ||
| break; | ||
|
|
||
| previous_segment = segment; | ||
| previous_result = result; | ||
|
|
||
| switch (result->kind()) | ||
| { | ||
| case boost::json::kind::object: { | ||
| auto& obj = result->get_object(); | ||
|
|
||
| detail::pointer_token const token(segment); | ||
| segment = detail::next_segment(sv, ec); | ||
| if (ec.failed()) | ||
| return false; | ||
|
|
||
| result = detail::if_contains_token(obj, token); | ||
| if( !result ) | ||
| { | ||
| BOOST_JSON_FAIL(ec, error::not_found); | ||
| return false; | ||
| } | ||
| break; | ||
| } | ||
| case boost::json::kind::array: { | ||
| auto const index = detail::parse_number_token(segment, ec); | ||
| segment = detail::next_segment(sv, ec); | ||
| if (ec.failed()) | ||
| return false; | ||
|
|
||
| auto& arr = result->get_array(); | ||
| result = arr.if_contains(index); | ||
| if( !result ) | ||
| { | ||
| BOOST_JSON_FAIL(ec, error::past_the_end); | ||
|
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.
|
||
| return false; | ||
| } | ||
| break; | ||
| } | ||
| default: { | ||
| BOOST_JSON_FAIL(ec, error::value_is_scalar); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| switch (previous_result->kind()) | ||
| { | ||
| case boost::json::kind::object: { | ||
| auto& obj = previous_result->get_object(); | ||
| detail::pointer_token const token(previous_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(previous_segment, ec); | ||
| auto& arr = previous_result->get_array(); | ||
| if (arr.if_contains(index)){ | ||
| arr.erase(arr.begin() + index); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| default: { | ||
| BOOST_JSON_FAIL(ec, error::value_is_scalar); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } // namespace json | ||
| } // namespace boost | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3035,6 +3035,19 @@ class value | |
|
|
||
| //------------------------------------------------------ | ||
|
|
||
| /** Remove an element via JSON Pointer. | ||
|
|
||
| @{ | ||
| */ | ||
| BOOST_JSON_DECL | ||
| bool | ||
| erase_at_pointer( | ||
|
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. There should also be a non-throwing overload. |
||
| string_view sv, | ||
| system::error_code& ec); | ||
|
|
||
| /// @} | ||
| //------------------------------------------------------ | ||
|
|
||
| /** Check if two values are equal. | ||
|
|
||
| Two values are equal when they are the same kind and their referenced | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ local SOURCES = | |
| doc_uses_allocator.cpp | ||
| doc_using_numbers.cpp | ||
| double.cpp | ||
| erase_at_pointer.cpp | ||
|
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. I don't think this feature warrants a separate test file. Just use |
||
| error.cpp | ||
| fwd.cpp | ||
| json.cpp | ||
|
|
||
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.
Please, reformat here and elsewhere. The project doesn't do such token alignment.
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.
😓