parent
e8fbd606e8
commit
302c72250b
@ -0,0 +1,88 @@
|
|||||||
|
// 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;
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
#ifndef ARCHIVE_PERMISSION_H
|
||||||
|
#define ARCHIVE_PERMISSION_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <optional>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
class launchpad;
|
||||||
|
|
||||||
|
#ifndef LAUNCHPAD_API
|
||||||
|
#ifdef BUILDING_LAUNCHPAD
|
||||||
|
#define LAUNCHPAD_API __attribute__((visibility("default")))
|
||||||
|
#else
|
||||||
|
#define LAUNCHPAD_API
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class LAUNCHPAD_API archive_permission {
|
||||||
|
public:
|
||||||
|
archive_permission();
|
||||||
|
~archive_permission();
|
||||||
|
|
||||||
|
static std::optional<archive_permission> parse(const std::string& json_data);
|
||||||
|
|
||||||
|
void set_lp(launchpad* lp_ptr);
|
||||||
|
|
||||||
|
// Public member variables (fields)
|
||||||
|
// Writable fields
|
||||||
|
std::string archive_link;
|
||||||
|
std::string component_name;
|
||||||
|
std::string date_created;
|
||||||
|
std::string distro_series_name;
|
||||||
|
std::string distroseries_link;
|
||||||
|
bool explicit_;
|
||||||
|
std::string package_set_name;
|
||||||
|
std::string permission;
|
||||||
|
std::string person_link;
|
||||||
|
std::string pocket;
|
||||||
|
std::string source_package_name;
|
||||||
|
|
||||||
|
// Read-only fields
|
||||||
|
std::string http_etag;
|
||||||
|
std::string resource_type_link;
|
||||||
|
std::string self_link;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void parse_json(const std::string& json_data);
|
||||||
|
launchpad* lp;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ARCHIVE_PERMISSION_H
|
Loading…
Reference in new issue