Skip to content

Commit

Permalink
move rt utils from test to rt folder
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Dec 1, 2023
1 parent e8c8f8e commit cf9c94d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
10 changes: 10 additions & 0 deletions include/nigiri/rt/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>

namespace nigiri::rt {

std::string json_to_protobuf(std::string const& json);
std::string protobuf_to_json(std::string const& protobuf);

} // namespace nigiri::rt
32 changes: 32 additions & 0 deletions src/rt/util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "nigiri/rt/util.h"

#include "google/protobuf/util/json_util.h"

#include "utl/verify.h"

#include "gtfsrt/gtfs-realtime.pb.h"

namespace nigiri::rt {

std::string json_to_protobuf(std::string const& json) {
transit_realtime::FeedMessage msg;
auto const status = google::protobuf::util::JsonStringToMessage(json, &msg);
utl::verify(status.ok(), "json_to_protobuf: {}", status.message());
return msg.SerializeAsString();
}

std::string protobuf_to_json(std::string const& protobuf) {
transit_realtime::FeedMessage msg;
auto const success =
msg.ParseFromArray(reinterpret_cast<void const*>(protobuf.data()),
static_cast<int>(protobuf.size()));
utl::verify(success, "json_to_protobuf: read protobuf FeedMessage failed");

auto json = std::string{};
auto const status = google::protobuf::util::MessageToJsonString(
msg, &json, {.add_whitespace = true});
utl::verify(status.ok(), "protobuf_to_json: {}", status.message());
return json;
}

} // namespace nigiri::rt
3 changes: 2 additions & 1 deletion test/rt/gtfsrt_rt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "nigiri/rt/frun.h"
#include "nigiri/rt/gtfsrt_resolve_run.h"
#include "nigiri/rt/gtfsrt_update.h"
#include "nigiri/rt/util.h"
#include "nigiri/timetable.h"

#include "./util.h"
Expand Down Expand Up @@ -373,7 +374,7 @@ TEST(rt, gtfs_rt_update_1) {
auto rtt = rt::create_rt_timetable(tt, date::sys_days{2023_y / August / 10});

// Update.
auto const msg = test::json_to_protobuf(kTripUpdate);
auto const msg = rt::json_to_protobuf(kTripUpdate);
gtfsrt_update_buf(tt, rtt, source_idx_t{0}, "", msg);

// Print trip.
Expand Down
3 changes: 2 additions & 1 deletion test/rt/skip_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "nigiri/rt/frun.h"
#include "nigiri/rt/gtfsrt_resolve_run.h"
#include "nigiri/rt/gtfsrt_update.h"
#include "nigiri/rt/util.h"
#include "nigiri/timetable.h"

#include "./util.h"
Expand Down Expand Up @@ -124,7 +125,7 @@ TEST(rt, gtfs_rt_skip) {
rt::create_rt_timetable(tt, date::sys_days{2023_y / November / 26});

// Update.
auto const msg = test::json_to_protobuf(kTripUpdate);
auto const msg = rt::json_to_protobuf(kTripUpdate);
gtfsrt_update_buf(tt, rtt, source_idx_t{0}, "", msg);

// Print trip.
Expand Down
11 changes: 0 additions & 11 deletions test/rt/util.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "./util.h"

#include "google/protobuf/util/json_util.h"

#include "utl/verify.h"

namespace nigiri::test {

transit_realtime::FeedMessage to_feed_msg(std::vector<trip> const& trip_delays,
Expand Down Expand Up @@ -42,11 +38,4 @@ transit_realtime::FeedMessage to_feed_msg(std::vector<trip> const& trip_delays,
return msg;
}

std::string json_to_protobuf(std::string const& json) {
transit_realtime::FeedMessage msg;
auto const status = google::protobuf::util::JsonStringToMessage(json, &msg);
utl::verify(status.ok(), "json_to_protobuf: {}", status.message());
return msg.SerializeAsString();
}

} // namespace nigiri::test
2 changes: 0 additions & 2 deletions test/rt/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ std::uint64_t to_unix(T&& x) {
transit_realtime::FeedMessage to_feed_msg(std::vector<trip> const& trip_delays,
date::sys_seconds const msg_time);

std::string json_to_protobuf(std::string const& json);

} // namespace nigiri::test

0 comments on commit cf9c94d

Please sign in to comment.