Maps a vector of source objects to a vector of destination objects.
Maps a vector of source objects to a vector of destination objects.This template function provides a generic way to map vectors using std::ranges::transform. It handles logging, memory pre-allocation, and the transformation in a consistent way.
auto domain_vec = map_vector<entity_type, domain_type>( entities, [](const auto& e) { return mapper::map(e); }, lg, "db entities" );
#ifndef ORES_DATABASE_REPOSITORY_MAPPER_HELPERS_HPP
#define ORES_DATABASE_REPOSITORY_MAPPER_HELPERS_HPP
#include <vector>
#include <chrono>
#include <sqlgen/postgres.hpp>
#include "ores.logging/make_logger.hpp"
#include "ores.platform/time/datetime.hpp"
#include "ores.platform/time/time_utils.hpp"
#include "ores.database/repository/db_types.hpp"
template<typename Source, typename Dest, typename MapFunc>
std::vector<Dest> map_vector(
const std::vector<Source>& source,
MapFunc&& map_func,
logging::logger_t& lg,
const std::string& log_prefix) {
BOOST_LOG_SEV(lg, debug) << "Mapping " << log_prefix
<< ". Total: " << source.size();
std::vector<Dest> result;
result.reserve(source.size());
std::ranges::transform(source, std::back_inserter(result),
std::forward<MapFunc>(map_func));
BOOST_LOG_SEV(lg, debug) << "Mapped " << log_prefix << ".";
return result;
}
inline std::chrono::system_clock::time_point
timestamp_to_timepoint(std::string_view timestamp_str) {
std::string{timestamp_str});
}
inline std::chrono::system_clock::time_point
}
timepoint_to_timestamp(const std::chrono::system_clock::time_point& tp,
logging::logger_t& lg) {
const auto bare = s.substr(0, s.size() - 1);
const auto r = db_timestamp::from_string(bare);
if (!r) {
BOOST_LOG_SEV(lg, error) << "Error converting timepoint to timestamp";
return {};
}
return r.value();
}
}
#endif
Repository infrastructure and bitemporal operations.
Definition bitemporal_operations.hpp:31
ores::database::repository::db_timestamp db_timestamp
Canonical database timestamp type.
Definition database_info_entity.hpp:31
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
static std::chrono::system_clock::time_point from_iso8601_utc(const std::string &str)
Parses an ISO 8601 UTC string to a time point.
Definition datetime.cpp:45
static std::string to_iso8601_utc(const std::chrono::system_clock::time_point &tp)
Serialises a time point to ISO 8601 UTC string with 'Z' suffix.
Definition datetime.cpp:30
static std::chrono::system_clock::time_point to_time_point_utc(std::tm tm)
Converts a UTC tm struct directly to a system_clock::time_point.
Definition time_utils.cpp:64