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

delete_at_pointer #1071

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
103 changes: 103 additions & 0 deletions include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,109 @@
return const_cast<value*>(self.find_pointer(ptr, ec));
}

std::pair<bool, string_view>
value::delete_at_pointer(

Check warning on line 419 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L419

Added line #L419 was not covered by tests
string_view sv,
system::error_code& ec)
{
ec.clear();

Check warning on line 423 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L423

Added line #L423 was not covered by tests


string_view previous_segment;
string_view sv_copy = sv;
string_view err_position;
string_view segment = detail::next_segment(sv, ec);
size_t shift = 0;

Check warning on line 430 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L426-L430

Added lines #L426 - L430 were not covered by tests

auto result = this;
auto previous_result = this;

Check warning on line 433 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L432-L433

Added lines #L432 - L433 were not covered by tests

while (true)
{
if (ec.failed())
return {false, err_position};

Check warning on line 438 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L437-L438

Added lines #L437 - L438 were not covered by tests

if (!result)

Check warning on line 440 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L440

Added line #L440 was not covered by tests
{
BOOST_JSON_FAIL(ec, error::not_found);
return {false, err_position};

Check warning on line 443 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L442-L443

Added lines #L442 - L443 were not covered by tests
}

if( segment.empty() )
break;

Check warning on line 447 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L446-L447

Added lines #L446 - L447 were not covered by tests

shift += segment.size();
err_position = sv_copy.substr(0, shift);

Check warning on line 450 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L449-L450

Added lines #L449 - L450 were not covered by tests

previous_segment = segment;
previous_result = result;

Check warning on line 453 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L452-L453

Added lines #L452 - L453 were not covered by tests

switch (result->kind())

Check warning on line 455 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L455

Added line #L455 was not covered by tests
{
case boost::json::kind::object: {
auto& obj = result->get_object();

Check warning on line 458 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L457-L458

Added lines #L457 - L458 were not covered by tests

detail::pointer_token const token(segment);
segment = detail::next_segment(sv, ec);

Check warning on line 461 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L460-L461

Added lines #L460 - L461 were not covered by tests

result = detail::if_contains_token(obj, token);
if( !result )

Check warning on line 464 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L463-L464

Added lines #L463 - L464 were not covered by tests
{
BOOST_JSON_FAIL(ec, error::not_found);
return {false, err_position};

Check warning on line 467 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L466-L467

Added lines #L466 - L467 were not covered by tests
}
break;

Check warning on line 469 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L469

Added line #L469 was not covered by tests
}
case boost::json::kind::array: {
auto const index = detail::parse_number_token(segment, ec);
segment = detail::next_segment(sv, ec);

Check warning on line 473 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L471-L473

Added lines #L471 - L473 were not covered by tests

auto& arr = result->get_array();
result = arr.if_contains(index);
if( !result )

Check warning on line 477 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L475-L477

Added lines #L475 - L477 were not covered by tests
{
BOOST_JSON_FAIL(ec, error::past_the_end);
return {false, err_position};

Check warning on line 480 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L479-L480

Added lines #L479 - L480 were not covered by tests
}
break;

Check warning on line 482 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L482

Added line #L482 was not covered by tests
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return {false, err_position};

Check warning on line 486 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L484-L486

Added lines #L484 - L486 were not covered by tests
}
}
}

Check warning on line 489 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L489

Added line #L489 was not covered by tests

err_position = {};

Check warning on line 491 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L491

Added line #L491 was not covered by tests

switch (previous_result->kind())

Check warning on line 493 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L493

Added line #L493 was not covered by tests
{
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, err_position};

Check warning on line 501 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L495-L501

Added lines #L495 - L501 were not covered by tests
}
return {false,err_position};

Check warning on line 503 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L503

Added line #L503 was not covered by tests
}
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, err_position};

Check warning on line 510 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L505-L510

Added lines #L505 - L510 were not covered by tests
}
return {false,err_position};

Check warning on line 512 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L512

Added line #L512 was not covered by tests
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return {false, err_position};

Check warning on line 516 in include/boost/json/impl/pointer.ipp

View check run for this annotation

Codecov / codecov/patch

include/boost/json/impl/pointer.ipp#L514-L516

Added lines #L514 - L516 were not covered by tests
}
}
}

value*
value::set_at_pointer(
string_view sv,
Expand Down
7 changes: 7 additions & 0 deletions include/boost/json/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3996,6 +3996,13 @@ class value
set_pointer_options const& opts = {} );
/** @} */

/** Remove an element via JSON Pointer.
*/
BOOST_JSON_DECL
std::pair<bool, string_view>
delete_at_pointer(
string_view sv,
system::error_code& ec);
//------------------------------------------------------

/** Return `true` if two values are equal.
Expand Down