ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
sourceResolver.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_dpm_libmerge
4  * @copyright (c) Copyright ESO 2022
5  * All Rights Reserved
6  * ESO (eso.org) is an Intergovernmental Organisation, and therefore special legal conditions apply.
7  *
8  * @brief Defines daq::dpm::SourceResolver.
9  */
11 
12 #include <fmt/format.h>
13 #include <fmt/ostream.h>
14 
15 namespace daq::dpm {
16 
17 std::ostream& operator<<(std::ostream& os, SourceResolver::SourceFile const& source) {
18  os << "source name: \"" << source.source_name << "\", origin: \"" << source.origin << '"';
19  return os;
20 }
21 
22 SourceResolver::SourceResolver(Mapping mapping) : m_mapping(std::move(mapping)) {
23 }
24 
26  std::filesystem::path const& path) {
27  m_mapping[source] = path.native();
28 }
29 
31  -> std::filesystem::path {
32  auto it = m_mapping.find(source);
33  if (it == m_mapping.end()) {
34  throw SourceNotFound(
35  fmt::format("Could not resolve local filename for source: '{}' and origin: '{}'",
36  source.source_name,
37  source.origin));
38  }
39  return std::filesystem::path(it->second);
40 }
41 
42 auto SourceResolver::GetMapping() const noexcept -> Mapping const& {
43  return m_mapping;
44 }
45 
46 void SourceResolver::SetMapping(Mapping mapping) noexcept {
47  m_mapping.swap(mapping);
48 }
49 
50 // NOLINTNEXTLINE
51 
52 // NOLINTNEXTLINE
54  j = nlohmann::json::array();
55  for (auto const& s : p) {
56  j.emplace_back(nlohmann::json{
57  {"sourceName", s.first.source_name},
58  {"origin", s.first.origin},
59  {"localPath", s.second}});
60  }
61 }
62 // NOLINTNEXTLINE
64  p.clear();
65  for (auto const& s : j) {
66  p.emplace(SourceResolver::SourceFile{s["sourceName"], s["origin"]}, s["localPath"]);
67  }
68 }
69 
70 } // namespace daq::dpm
sourceResolver.hpp
Declares daq::dpm::SourceResolver.
daq::dpm::from_json
void from_json(nlohmann::json const &j, SourceResolver::Mapping &p)
Definition: sourceResolver.cpp:63
daq::dpm::SourceResolver::GetMapping
auto GetMapping() const noexcept -> Mapping const &
Get native representation of source mapping for serialization.
Definition: sourceResolver.cpp:42
daq::dpm::SourceResolver::Mapping
std::map< SourceFile, std::string > Mapping
Definition: sourceResolver.hpp:48
daq::dpm::SourceResolver::SourceFile::source_name
std::string source_name
Named by user.
Definition: sourceResolver.hpp:33
daq::dpm::to_json
void to_json(nlohmann::json &j, SourceResolver::Mapping const &p)
Definition: sourceResolver.cpp:53
daq::dpm::SourceResolver::Resolve
auto Resolve(SourceFile const &source) const -> std::filesystem::path
Resolves local file that was previously added with Add().
Definition: sourceResolver.cpp:30
daq::dpm
Definition: testDpSpec.cpp:16
daq::dpm::SourceResolver::Add
void Add(SourceFile const &source, std::filesystem::path const &path)
Adds path so it is resolved using source_name and @origin .
Definition: sourceResolver.cpp:25
daq::dpm::SourceResolver::SourceFile::origin
std::string origin
[user@]host:/path/
Definition: sourceResolver.hpp:37
daq::dpm::SourceResolver::SourceFile
Definition: sourceResolver.hpp:29
daq::dpm::SourceResolver::SourceResolver
SourceResolver()=default
Initialize resolver with no content.
error::SourceNotFound
@ SourceNotFound
Definition: main.cpp:43
daq::dpm::operator<<
std::ostream & operator<<(std::ostream &os, DaqController const &daq)
Definition: scheduler.cpp:58
daq::dpm::SourceResolver::SetMapping
void SetMapping(Mapping mapping) noexcept
Definition: sourceResolver.cpp:46
daq::json
nlohmann::json json
Definition: json.cpp:20