11#include <fmt/format.h>
15using 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")) {
151 }
catch (std::exception
const& e) {
152 std::throw_with_nested(
ErrorType(
"Schema error while parsing StartDaqV2 specification"));
158 return lhs.source_name == rhs.source_name && lhs.rr_uri == rhs.rr_uri &&
159 lhs.keyword_rules == rhs.keyword_rules;
164 return lhs.source_name == rhs.source_name && lhs.rr_uri == rhs.rr_uri &&
165 lhs.keyword_rules == rhs.keyword_rules;
170 return lhs.source_name == rhs.source_name;
174 return lhs.id == rhs.id && lhs.file_prefix == rhs.file_prefix &&
175 lhs.await_completion_interval == rhs.await_completion_interval &&
176 lhs.merge_target == rhs.merge_target && lhs.sources == rhs.sources &&
177 lhs.receivers == rhs.receivers;
194 out[
"type"] =
"primarySource";
200 out[
"type"] =
"metadataSource";
205 std::visit([&](
auto const& ds) {
to_json(out, ds); }, s);
215 json = nlohmann::json{
218 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