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.

89 lines
4.1 KiB

// 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 "archive_permission.h"
#include "launchpad.h"
#include <iostream>
#include <map>
#include <functional>
archive_permission::archive_permission()
: explicit_(false),
lp(nullptr) {}
archive_permission::~archive_permission() {}
std::optional<archive_permission> archive_permission::parse(const std::string& json_data) {
archive_permission ap;
ap.parse_json(json_data);
return ap;
}
void archive_permission::parse_json(const std::string& json_data) {
if (json_data.empty()) {
std::cerr << "Error: Empty JSON data for archive_permission::parse_json." << std::endl;
return;
}
try {
auto data = nlohmann::json::parse(json_data);
// Map JSON keys to lambdas to parse
std::map<std::string, std::function<void(const nlohmann::json&)>> json_map = {
{"archive_link", [this](const nlohmann::json& val) { archive_link = val.get<std::string>(); }},
{"component_name", [this](const nlohmann::json& val) { component_name = val.get<std::string>(); }},
{"date_created", [this](const nlohmann::json& val) { date_created = val.get<std::string>(); }},
{"distro_series_name", [this](const nlohmann::json& val) { distro_series_name = val.get<std::string>(); }},
{"distroseries_link", [this](const nlohmann::json& val) { distroseries_link = val.get<std::string>(); }},
{"explicit", [this](const nlohmann::json& val) { explicit_ = val.get<bool>(); }},
{"package_set_name", [this](const nlohmann::json& val) { package_set_name = val.get<std::string>(); }},
{"permission", [this](const nlohmann::json& val) { permission = val.get<std::string>(); }},
{"person_link", [this](const nlohmann::json& val) { person_link = val.get<std::string>(); }},
{"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }},
{"source_package_name", [this](const nlohmann::json& val) { source_package_name = val.get<std::string>(); }}
};
std::map<std::string, std::function<void(const nlohmann::json&)>> readonly_map = {
{"http_etag", [this](const nlohmann::json& val) { http_etag = val.get<std::string>(); }},
{"resource_type_link", [this](const nlohmann::json& val) { resource_type_link = val.get<std::string>(); }},
{"self_link", [this](const nlohmann::json& val) { self_link = val.get<std::string>(); }}
};
for (auto& [key, value] : data.items()) {
try {
if (json_map.find(key) != json_map.end()) {
json_map[key](value);
json_map.erase(key);
} else if (readonly_map.find(key) != readonly_map.end()) {
readonly_map[key](value);
readonly_map.erase(key);
}
} catch (const std::exception& e) {
// If parsing fails for a field, just continue
continue;
}
}
} catch (const nlohmann::json::parse_error& e) {
std::cerr << "JSON parse error in archive_permission::parse_json: " << e.what() << std::endl;
std::cerr << "Input JSON was: " << json_data << std::endl;
} catch (const std::exception& e) {
std::cerr << "Unexpected error during JSON parsing: " << e.what() << std::endl;
}
}
void archive_permission::set_lp(launchpad* lp_ptr) {
lp = lp_ptr;
}