You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.8 KiB
70 lines
2.8 KiB
4 weeks ago
|
// Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.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, see <https://www.gnu.org/licenses/>.
|
||
|
|
||
|
#include "distribution.h"
|
||
|
#include <iostream>
|
||
|
#include <nlohmann/json.hpp>
|
||
|
|
||
|
distribution::distribution()
|
||
|
: name(""),
|
||
|
description(""),
|
||
|
displayname(""),
|
||
|
title(""),
|
||
|
summary(""),
|
||
|
domain_name(""),
|
||
|
lp(nullptr) {}
|
||
|
|
||
|
distribution::~distribution() {}
|
||
|
|
||
|
std::optional<distribution> distribution::parse(const std::string& json_data) {
|
||
|
distribution d;
|
||
|
d.parse_json(json_data);
|
||
|
return d;
|
||
|
}
|
||
|
|
||
|
void distribution::parse_json(const std::string& json_data) {
|
||
|
try {
|
||
|
auto data = nlohmann::json::parse(json_data);
|
||
|
|
||
|
// Map JSON keys to lambda functions
|
||
|
std::map<std::string, std::function<void(const nlohmann::json&)>> json_map = {
|
||
|
{"name", [this](const nlohmann::json& val) { name = val.get<std::string>(); }},
|
||
|
{"description", [this](const nlohmann::json& val) { description = val.get<std::string>(); }},
|
||
|
{"displayname", [this](const nlohmann::json& val) { displayname = val.get<std::string>(); }},
|
||
|
{"title", [this](const nlohmann::json& val) { title = val.get<std::string>(); }},
|
||
|
{"summary", [this](const nlohmann::json& val) { summary = val.get<std::string>(); }},
|
||
|
{"domain_name", [this](const nlohmann::json& val) { domain_name = val.get<std::string>(); }},
|
||
|
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }},
|
||
|
{"web_link", [this](const nlohmann::json& val) { web_link = val.get<std::string>(); }},
|
||
|
{"translationgroup_link", [this](const nlohmann::json& val) { translationgroup_link = val.get<std::string>(); }}
|
||
|
};
|
||
|
|
||
|
// Process JSON keys dynamically
|
||
|
for (auto& [key, value] : data.items()) {
|
||
|
if (json_map.find(key) != json_map.end()) {
|
||
|
json_map[key](value);
|
||
|
json_map.erase(key);
|
||
|
}
|
||
|
}
|
||
|
} catch (const nlohmann::json::parse_error& e) {
|
||
|
std::cerr << "JSON parse error in distribution::parse_json: " << e.what() << std::endl;
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void distribution::set_lp(launchpad* lp_ptr) {
|
||
|
lp = lp_ptr;
|
||
|
}
|