28std::vector<nlohmann::json>
expand_instances(
const nlohmann::json &config, std::string_view list_key,
29 std::string_view venue, DefaultName &&default_name) {
30 std::vector<nlohmann::json> out;
31 const std::string key(list_key);
32 if (!config.contains(key)) {
33 auto &single = out.emplace_back(config);
34 if (!single.contains(
"name")) {
35 single[
"name"] = std::string(venue);
40 const auto &list = config[key];
41 if (!list.is_array() || list.empty()) {
42 throw std::runtime_error(std::format(
"{}: \"{}\" must be a non-empty array", venue, key));
44 nlohmann::json shared = config;
46 for (
size_t i = 0; i < list.size(); ++i) {
50 if (!list[i].is_object()) {
51 throw std::runtime_error(std::format(
"{}: \"{}\"[{}] must be an object", venue, key, i));
53 nlohmann::json instance = shared;
54 instance.merge_patch(list[i]);
55 if (!instance.contains(
"name")) {
56 instance[
"name"] = default_name(instance, i);
58 if (!instance.value(
"enabled",
true)) {
59 LOG_INFO(
"{} {} is disabled, skipping", venue, instance[
"name"].get<std::string>());
62 out.push_back(std::move(instance));
71void claim(std::unordered_map<T, std::string> &claimed,
const T &value,
const std::string &owner,
72 std::string_view what, std::string_view venue) {
73 auto [it, inserted] = claimed.emplace(value, owner);
77 if (it->second == owner) {
78 throw std::runtime_error(std::format(
"{}: '{}' uses {} {} twice", venue, owner, what, value));
80 throw std::runtime_error(std::format(
"{}: '{}' and '{}' both use {} {}", venue, it->second, owner, what, value));
std::vector< nlohmann::json > expand_instances(const nlohmann::json &config, std::string_view list_key, std::string_view venue, DefaultName &&default_name)
Expands a venue's config entry into one config per instance listed under list_key.
void claim(std::unordered_map< T, std::string > &claimed, const T &value, const std::string &owner, std::string_view what, std::string_view venue)
Records that instance owner uses value.