ORE Studio 0.0.4
Loading...
Searching...
No Matches
/home/runner/work/OreStudio/OreStudio/projects/ores.database/include/ores.database/repository/mapper_helpers.hpp

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.

Template Parameters
SourceThe source vector element type
DestThe destination vector element type
MapFuncThe mapping function type (typically a lambda or function pointer)
Parameters
sourceThe source vector to map from
map_funcThe function to map individual elements (Source -> Dest)
lgThe logger to use for logging
log_prefixThe prefix for log messages (e.g., "db entities" or "domain entities")
Returns
A vector of destination objects

auto domain_vec = map_vector<entity_type, domain_type>( entities, [](const auto& e) { return mapper::map(e); }, lg, "db entities" );

/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#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) {
using namespace ores::logging;
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
timestamp_to_timepoint(const db_timestamp& ts) {
}
timepoint_to_timestamp(const std::chrono::system_clock::time_point& tp,
logging::logger_t& lg) {
using namespace ores::logging;
// Strip the Z suffix — db_timestamp::from_string expects no timezone designator
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