Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou committed May 25, 2024
1 parent 4f46705 commit ad990aa
Show file tree
Hide file tree
Showing 12 changed files with 704 additions and 704 deletions.
2 changes: 1 addition & 1 deletion example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main(int argc, const char *argv[])
auto &json_result = std::get<json::Object>(result);
if (status == Status::Ok)
{
auto &routes = json_result.values["routes"].get<json::Array>();
auto &routes = std::get<json::Array>(json_result.values["routes"]);

// Let's just use the first route
auto &route = routes.values.at(0).get<json::Object>();
Expand Down
2 changes: 1 addition & 1 deletion include/nodejs/node_osrm_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <> Napi::Value inline render(const Napi::Env &env, const ObjectOrString
{
// Convert osrm::json object tree into matching v8 object tree
Napi::Value value;
renderToV8(env, value, result.get<osrm::json::Object>());
renderToV8(env, value, std::get<osrm::json::Object>(result));
return value;
}
else
Expand Down
3 changes: 1 addition & 2 deletions include/util/geojson_debug_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class GeojsonLogger
if (!first)
ofs << ",\n\t\t";

(void)object;
// util::json::render(ofs, std::get<util::json::Object>(object));
util::json::render(ofs, std::get<util::json::Object>(object));

first = false;
}
Expand Down
4 changes: 2 additions & 2 deletions include/util/json_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ struct Null
*/
using Value = std::variant<String,
Number,
boost::recursive_wrapper<Object>,
boost::recursive_wrapper<Array>,
Object,
Array,
True,
False,
Null>;
Expand Down
50 changes: 25 additions & 25 deletions include/util/json_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,35 @@ template <typename Out> struct Renderer
write(buffer.data(), buffer.size());
}

void operator()(const boost::recursive_wrapper<Object> &)
void operator()(const Object &object)
{
// write('{');
// for (auto it = object.values.begin(), end = object.values.end(); it != end;)
// {
// write('\"');
// write(it->first);
// write<>("\":");
// std::visit(Renderer(out), it->second);
// if (++it != end)
// {
// write(',');
// }
// }
// write('}');
write('{');
for (auto it = object.values.begin(), end = object.values.end(); it != end;)
{
write('\"');
write(it->first);
write<>("\":");
std::visit(Renderer(out), it->second);
if (++it != end)
{
write(',');
}
}
write('}');
}

void operator()(const boost::recursive_wrapper<Array> &)
void operator()(const Array &array)
{
// write('[');
// for (auto it = array.values.cbegin(), end = array.values.cend(); it != end;)
// {
// std::visit(Renderer(out), *it);
// if (++it != end)
// {
// write(',');
// }
// }
// write(']');
write('[');
for (auto it = array.values.cbegin(), end = array.values.cend(); it != end;)
{
std::visit(Renderer(out), *it);
if (++it != end)
{
write(',');
}
}
write(']');
}

void operator()(const True &) { write<>("true"); }
Expand Down
20 changes: 10 additions & 10 deletions unit_tests/library/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ BOOST_AUTO_TEST_CASE(test_json_linestring)

auto geom = engine::api::json::makeGeoJSONGeometry(begin(locations), end(locations));

const auto type = geom.values["type"].get<util::json::String>().value;
const auto type = std::get<util::json::String>(geom.values["type"]).value;
BOOST_CHECK_EQUAL(type, "LineString");

const auto coords = geom.values["coordinates"].get<util::json::Array>().values;
const auto coords = std::get<util::json::Array>(geom.values["coordinates"]).values;
BOOST_CHECK_EQUAL(coords.size(), 3); // array of three location arrays

for (const auto &each : coords)
{
const auto loc = each.get<util::json::Array>().values;
const auto loc = std::get<util::json::Array>(each).values;
BOOST_CHECK_EQUAL(loc.size(), 2);

const auto lon = loc[0].get<util::json::Number>().value;
const auto lat = loc[1].get<util::json::Number>().value;
const auto lon = std::get<util::json::Number>(loc[0]).value;
const auto lat = std::get<util::json::Number>(loc[1]).value;

(void)lon;
(void)lat;
Expand All @@ -46,19 +46,19 @@ BOOST_AUTO_TEST_CASE(test_json_single_point)

auto geom = engine::api::json::makeGeoJSONGeometry(begin(locations), end(locations));

const auto type = geom.values["type"].get<util::json::String>().value;
const auto type = std::get<util::json::String>(geom.values["type"]).value;
BOOST_CHECK_EQUAL(type, "LineString");

const auto coords = geom.values["coordinates"].get<util::json::Array>().values;
const auto coords = std::get<util::json::Array>(geom.values["coordinates"]).values;
BOOST_CHECK_EQUAL(coords.size(), 2); // array of two location arrays

for (const auto &each : coords)
{
const auto loc = each.get<util::json::Array>().values;
const auto loc = std::get<util::json::Array>(each).values;
BOOST_CHECK_EQUAL(loc.size(), 2);

const auto lon = loc[0].get<util::json::Number>().value;
const auto lat = loc[1].get<util::json::Number>().value;
const auto lon = std::get<util::json::Number>(loc[0]).value;
const auto lat = std::get<util::json::Number>(loc[1]).value;

(void)lon;
(void)lat;
Expand Down
31 changes: 16 additions & 15 deletions unit_tests/library/match.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <boost/test/unit_test.hpp>
#include <variant>

#include "coordinates.hpp"
#include "fixture.hpp"
Expand Down Expand Up @@ -48,32 +49,32 @@ void test_match(bool use_json_only_api)
const auto code = std::get<json::String>(json_result.values.at("code")).value;
BOOST_CHECK_EQUAL(code, "Ok");

const auto &tracepoints = json_result.values.at("tracepoints").get<json::Array>().values;
const auto &tracepoints = std::get<json::Array>(json_result.values.at("tracepoints")).values;
BOOST_CHECK_EQUAL(tracepoints.size(), params.coordinates.size());

const auto &matchings = json_result.values.at("matchings").get<json::Array>().values;
const auto &matchings = std::get<json::Array>(json_result.values.at("matchings")).values;
const auto &number_of_matchings = matchings.size();
for (const auto &waypoint : tracepoints)
{
if (waypoint.is<mapbox::util::recursive_wrapper<util::json::Object>>())
if (std::holds_alternative<util::json::Object>(waypoint))
{
BOOST_CHECK(waypoint_check(waypoint));
const auto &waypoint_object = std::get<json::Object>(waypoint);
const auto matchings_index =
waypoint_object.values.at("matchings_index").get<json::Number>().value;
std::get<json::Number>(waypoint_object.values.at("matchings_index")).value;
const auto waypoint_index =
waypoint_object.values.at("waypoint_index").get<json::Number>().value;
const auto &route_legs = matchings[matchings_index]
.get<json::Object>()
std::get<json::Number>(waypoint_object.values.at("waypoint_index")).value;
const auto &route_legs = std::get<json::Array>(std::get<json::Object>(matchings[matchings_index]
)
.values.at("legs")
.get<json::Array>()
)
.values;
BOOST_CHECK_LT(waypoint_index, route_legs.size() + 1);
BOOST_CHECK_LT(matchings_index, number_of_matchings);
}
else
{
BOOST_CHECK(waypoint.is<json::Null>());
BOOST_CHECK(std::holds_alternative<json::Null>(waypoint));
}
}
}
Expand Down Expand Up @@ -121,23 +122,23 @@ void test_match_split(bool use_json_only_api)
const auto code = std::get<json::String>(json_result.values.at("code")).value;
BOOST_CHECK_EQUAL(code, "Ok");

const auto &tracepoints = json_result.values.at("tracepoints").get<json::Array>().values;
const auto &tracepoints =std::get<json::Array>(json_result.values.at("tracepoints")).values;
BOOST_CHECK_EQUAL(tracepoints.size(), params.coordinates.size());

const auto &matchings = json_result.values.at("matchings").get<json::Array>().values;
const auto &matchings = std::get<json::Array>(json_result.values.at("matchings")).values;
const auto &number_of_matchings = matchings.size();
BOOST_CHECK_EQUAL(number_of_matchings, 2);
std::size_t current_matchings_index = 0, expected_waypoint_index = 0;
for (const auto &waypoint : tracepoints)
{
if (waypoint.is<mapbox::util::recursive_wrapper<util::json::Object>>())
if (std::holds_alternative<util::json::Object>(waypoint))
{
BOOST_CHECK(waypoint_check(waypoint));
const auto &waypoint_object = std::get<json::Object>(waypoint);
const auto matchings_index =
waypoint_object.values.at("matchings_index").get<json::Number>().value;
std::get<json::Number>(waypoint_object.values.at("matchings_index")).value;
const auto waypoint_index =
waypoint_object.values.at("waypoint_index").get<json::Number>().value;
std::get<json::Number>(waypoint_object.values.at("waypoint_index")).value;

BOOST_CHECK_LT(matchings_index, number_of_matchings);

Expand All @@ -150,7 +151,7 @@ void test_match_split(bool use_json_only_api)
}
else
{
BOOST_CHECK(waypoint.is<json::Null>());
BOOST_CHECK(std::holds_alternative<json::Null>(waypoint));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions unit_tests/library/nearest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ osrm::Status run_nearest_json(const osrm::OSRM &osrm,
}
osrm::engine::api::ResultT result = osrm::json::Object();
auto rc = osrm.Nearest(params, result);
json_result = result.get<osrm::json::Object>();
json_result = std::get<osrm::json::Object>(result);
return rc;
}

Expand Down Expand Up @@ -50,7 +50,7 @@ void test_nearest_response(bool use_json_only_api)
for (const auto &waypoint : waypoints)
{
const auto &waypoint_object = std::get<json::Object>(waypoint);
const auto distance = waypoint_object.values.at("distance").get<json::Number>().value;
const auto distance = std::get<json::Number>(waypoint_object.values.at("distance")).value;
BOOST_CHECK(distance >= 0);
}
}
Expand Down Expand Up @@ -163,13 +163,13 @@ void test_nearest_response_for_location_in_small_component(bool use_json_only_ap

// Everything within ~20m (actually more) is still in small component.
// Nearest service should snap to road network without considering components.
const auto distance = waypoint_object.values.at("distance").get<json::Number>().value;
const auto distance = std::get<json::Number>(waypoint_object.values.at("distance")).value;
BOOST_CHECK_LT(distance, 20);

const auto &nodes = waypoint_object.values.at("nodes").get<json::Array>().values;
const auto &nodes = std::get<json::Array>(waypoint_object.values.at("nodes")).values;
BOOST_CHECK(nodes.size() == 2);
BOOST_CHECK(nodes[0].get<util::json::Number>().value != 0);
BOOST_CHECK(nodes[1].get<util::json::Number>().value != 0);
BOOST_CHECK(std::get<util::json::Number>(nodes[0]).value != 0);
BOOST_CHECK(std::get<util::json::Number>(nodes[1]).value != 0);
}
}
BOOST_AUTO_TEST_CASE(test_nearest_response_for_location_in_small_component_old_api)
Expand Down
Loading

0 comments on commit ad990aa

Please sign in to comment.