11 #include <fmt/format.h>
15 using Json = nlohmann::json;
34 AssertIsObject<ErrorType>(json, breadcrumb);
37 if (json.contains(
"initialKeywords")) {
41 if (json.contains(
"keywordRules")) {
51 AssertIsObject<StartDaqV2SpecError>(json, breadcrumb);
53 if (json.contains(
"id")) {
57 if (json.contains(
"filePrefix")) {
61 if (json.contains(
"awaitCompletionInterval")) {
65 throw MakeParseException<ErrorType>(breadcrumb /
"awaitCompletionInterval",
66 "interval must be > 0.0s");
69 std::chrono::duration_cast<std::chrono::milliseconds>(
70 std::chrono::duration<double>(value));
73 if (json.contains(
"mergeTarget")) {
76 json[
"mergeTarget"],
"sourceName", breadcrumb /
"mergeTarget");
81 auto [json_sources, breadcrumb_sources] = GetMember<ErrorType>(
82 json,
"sources", breadcrumb, [](
Json const& member,
JsonPointer const& breadcrumb) {
83 AssertIsArray<ErrorType>(member, breadcrumb);
86 auto found_merge_target = 0u;
87 for (
auto const& json_source : json_sources) {
88 auto breadcrumb_source = breadcrumb_sources / index;
91 if (source_type ==
"primarySource") {
97 result.
sources.emplace_back(std::move(s));
98 }
else if (source_type ==
"metadataSource") {
102 found_merge_target++;
104 result.
sources.emplace_back(std::move(s));
105 }
else if (source_type ==
"fitsKeywords") {
110 throw MakeParseException<ErrorType>(
111 breadcrumb /
"mergeTarget" /
"sourceName",
112 "Merge target `{}` refers to fitsKeywords which cannot be a merge "
116 result.
sources.emplace_back(std::move(kws));
117 }
else if (source_type ==
"fitsFile") {
121 found_merge_target++;
123 result.
sources.emplace_back(std::move(file));
125 throw MakeUnknownVariantException<ErrorType>(
126 breadcrumb_source /
"type",
127 "'primarySource', 'metadataSource', 'fitsFile' or 'fitsKeywords'",
128 source_type.c_str());
133 if (found_merge_target == 0) {
134 throw MakeParseException<ErrorType>(
135 breadcrumb /
"mergeTarget" /
"sourceName",
136 "Merge target `{}` does not match any sources",
139 else if (found_merge_target > 1) {
140 throw MakeParseException<ErrorType>(
141 breadcrumb /
"mergeTarget" /
"sourceName",
142 "Merge target `{}` matches more than one source",
147 if (json.contains(
"receivers")) {
155 }
catch (std::exception
const& e) {
156 std::throw_with_nested(
163 return lhs.source_name == rhs.source_name && lhs.rr_uri == rhs.rr_uri &&
164 lhs.keyword_rules == rhs.keyword_rules;
169 return lhs.source_name == rhs.source_name && lhs.rr_uri == rhs.rr_uri &&
170 lhs.keyword_rules == rhs.keyword_rules;
175 return lhs.source_name == rhs.source_name;
179 return lhs.id == rhs.id && lhs.file_prefix == rhs.file_prefix &&
180 lhs.await_completion_interval == rhs.await_completion_interval &&
181 lhs.merge_target == rhs.merge_target && lhs.sources == rhs.sources &&
182 lhs.receivers == rhs.receivers;
199 out[
"type"] =
"primarySource";
205 out[
"type"] =
"metadataSource";
210 std::visit([&](
auto const& ds) {
to_json(out, ds); }, s);
220 json = nlohmann::json{
223 json[
"awaitCompletionInterval"] = std::chrono::duration_cast<std::chrono::duration<double>>(
nlohmann::json_pointer< Json > JsonPointer
StartDaqV2SpecError ErrorType
KeywordRules ParseKeywordRules(Json const &json, JsonPointer const &breadcrumb)
InitialKeywords ParseInitialKeywords(nlohmann::json const &json, nlohmann::json_pointer< nlohmann::json > const &breadcrumb)
std::optional< MergeTarget > merge_target
nlohmann::json_pointer< Json > JsonPointer
std::optional< std::chrono::milliseconds > await_completion_interval
void ParseDataSource(StartDaqV2Spec::DataSource &out, Json const &json, JsonPointer const &breadcrumb)
Parse the common parts of PrimaryDataSource and MetadataSource.
FitsFileSource ParseFitsFileSource(Json const &json, JsonPointer const &breadcrumb)
bool operator==(KeywordFilter const &lhs, KeywordFilter const &rhs) noexcept
FitsKeywordsSource ParseFitsKeywordsSource(Json const &json, JsonPointer const &breadcrumb)
std::variant< PrimaryDataSource, MetadataSource, FitsKeywordsSource, FitsFileSource > DataSourceTypes
ReceiverList ParseReceiverList(Json const &json, JsonPointer const &breadcrumb)
StartDaqV2Spec ParseStartDaqV2Spec(nlohmann::json const &json)
Parse StartDaqSpec.
void to_json(nlohmann::json &out, KeywordFilter const &s)
std::vector< DataSourceTypes > sources
Structure with a close mapping from JSON representation in the StartDaqV2 MAL request.
static T Get(Json const &json, char const *name, JsonPointer const &breadcrumb, bool allow_empty=true)
KeywordRules keyword_rules
std::optional< InitialKeywords > initial_keywords