Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdns: use std::variant #14727

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 53 additions & 53 deletions modules/lua2backend/lua2api2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ private:

typedef std::vector<std::pair<string, string>> lookup_context_t;

typedef std::vector<std::pair<int, std::vector<std::pair<string, boost::variant<bool, int, DNSName, string, QType>>>>> lookup_result_t;
typedef std::vector<std::pair<int, std::vector<std::pair<string, std::variant<bool, int, DNSName, string, QType>>>>> lookup_result_t;
typedef std::function<lookup_result_t(const QType& qtype, const DNSName& qname, int domain_id, const lookup_context_t& ctx)> lookup_call_t;

typedef boost::variant<bool, lookup_result_t> list_result_t;
typedef std::variant<bool, lookup_result_t> list_result_t;
typedef std::function<list_result_t(const DNSName& qname, int domain_id)> list_call_t;

typedef vector<pair<string, boost::variant<bool, long, string, vector<string>>>> domaininfo_result_t;
typedef boost::variant<bool, domaininfo_result_t> get_domaininfo_result_t;
typedef vector<pair<string, std::variant<bool, long, string, vector<string>>>> domaininfo_result_t;
typedef std::variant<bool, domaininfo_result_t> get_domaininfo_result_t;
typedef vector<pair<DNSName, domaininfo_result_t>> get_all_domains_result_t;
typedef std::function<get_domaininfo_result_t(const DNSName& domain)> get_domaininfo_call_t;
typedef std::function<get_all_domains_result_t()> get_all_domains_call_t;

typedef vector<pair<int, string>> domain_metadata_result_t;
typedef boost::variant<bool, domain_metadata_result_t> get_domain_metadata_result_t;
typedef boost::variant<bool, vector<pair<string, domain_metadata_result_t>>> get_all_domain_metadata_result_t;
typedef std::variant<bool, domain_metadata_result_t> get_domain_metadata_result_t;
typedef std::variant<bool, vector<pair<string, domain_metadata_result_t>>> get_all_domain_metadata_result_t;
typedef std::function<get_domain_metadata_result_t(const DNSName& domain, const string& kind)> get_domain_metadata_call_t;
typedef std::function<get_all_domain_metadata_result_t(const DNSName& domain)> get_all_domain_metadata_call_t;

typedef vector<pair<string, boost::variant<bool, int, string>>> keydata_result_t;
typedef boost::variant<bool, vector<pair<int, keydata_result_t>>> get_domain_keys_result_t;
typedef vector<pair<string, std::variant<bool, int, string>>> keydata_result_t;
typedef std::variant<bool, vector<pair<int, keydata_result_t>>> get_domain_keys_result_t;
typedef std::function<get_domain_keys_result_t(const DNSName& domain)> get_domain_keys_call_t;

typedef std::vector<std::pair<string, boost::variant<string, DNSName>>> before_and_after_names_result_t;
typedef boost::variant<bool, before_and_after_names_result_t> get_before_and_after_names_absolute_result_t;
typedef std::vector<std::pair<string, std::variant<string, DNSName>>> before_and_after_names_result_t;
typedef std::variant<bool, before_and_after_names_result_t> get_before_and_after_names_absolute_result_t;
typedef std::function<get_before_and_after_names_absolute_result_t(int id, const DNSName& qname)> get_before_and_after_names_absolute_call_t;

typedef std::function<void(int, long)> set_notified_call_t;
Expand Down Expand Up @@ -141,35 +141,35 @@ public:
DNSResourceRecord rec;
for (const auto& item : row.second) {
if (item.first == "type") {
if (item.second.which() == 1)
rec.qtype = QType(boost::get<int>(item.second));
else if (item.second.which() == 3)
rec.qtype = boost::get<string>(item.second);
else if (item.second.which() == 4)
rec.qtype = boost::get<QType>(item.second);
if (std::get<int>(item.second) == 1)
rec.qtype = QType(std::get<int>(item.second));
else if (std::get<int>(item.second) == 3)
rec.qtype = std::get<string>(item.second);
else if (std::get<int>(item.second) == 4)
rec.qtype = std::get<QType>(item.second);
else
throw PDNSException("Unsupported value for type");
}
else if (item.first == "name") {
if (item.second.which() == 3)
rec.qname = DNSName(boost::get<string>(item.second));
else if (item.second.which() == 2)
rec.qname = boost::get<DNSName>(item.second);
if (std::get<int>(item.second) == 3)
rec.qname = DNSName(std::get<string>(item.second));
else if (std::get<int>(item.second) == 2)
rec.qname = std::get<DNSName>(item.second);
else
throw PDNSException("Unsupported value for name");
}
else if (item.first == "domain_id")
rec.domain_id = boost::get<int>(item.second);
rec.domain_id = std::get<int>(item.second);
else if (item.first == "auth")
rec.auth = boost::get<bool>(item.second);
rec.auth = std::get<bool>(item.second);
else if (item.first == "last_modified")
rec.last_modified = static_cast<time_t>(boost::get<int>(item.second));
rec.last_modified = static_cast<time_t>(std::get<int>(item.second));
else if (item.first == "ttl")
rec.ttl = boost::get<int>(item.second);
rec.ttl = std::get<int>(item.second);
else if (item.first == "content")
rec.setContent(boost::get<string>(item.second));
rec.setContent(std::get<string>(item.second));
else if (item.first == "scopeMask")
rec.scopeMask = boost::get<int>(item.second);
rec.scopeMask = std::get<int>(item.second);
else
g_log << Logger::Warning << "Unsupported key '" << item.first << "' in lookup or list result" << endl;
}
Expand All @@ -193,10 +193,10 @@ public:
logCall("list", "target=" << target << ",domain_id=" << domain_id);
list_result_t result = f_list(target, domain_id);

if (result.which() == 0)
if (!std::get<bool>(result))
return false;

parseLookup(boost::get<lookup_result_t>(result));
parseLookup(std::get<lookup_result_t>(result));

return true;
}
Expand Down Expand Up @@ -255,20 +255,20 @@ public:
{
for (const auto& item : row) {
if (item.first == "account")
di.account = boost::get<string>(item.second);
di.account = std::get<string>(item.second);
else if (item.first == "last_check")
di.last_check = static_cast<time_t>(boost::get<long>(item.second));
di.last_check = static_cast<time_t>(std::get<long>(item.second));
else if (item.first == "masters")
for (const auto& primary : boost::get<vector<string>>(item.second))
for (const auto& primary : std::get<vector<string>>(item.second))
di.primaries.push_back(ComboAddress(primary, 53));
else if (item.first == "id")
di.id = static_cast<int>(boost::get<long>(item.second));
di.id = static_cast<int>(std::get<long>(item.second));
else if (item.first == "notified_serial")
di.notified_serial = static_cast<unsigned int>(boost::get<long>(item.second));
di.notified_serial = static_cast<unsigned int>(std::get<long>(item.second));
else if (item.first == "serial")
di.serial = static_cast<unsigned int>(boost::get<long>(item.second));
di.serial = static_cast<unsigned int>(std::get<long>(item.second));
else if (item.first == "kind")
di.kind = DomainInfo::stringToKind(boost::get<string>(item.second));
di.kind = DomainInfo::stringToKind(std::get<string>(item.second));
else
g_log << Logger::Warning << "Unsupported key '" << item.first << "' in domaininfo result" << endl;
}
Expand All @@ -293,11 +293,11 @@ public:
logCall("get_domaininfo", "domain=" << domain);
get_domaininfo_result_t result = f_get_domaininfo(domain);

if (result.which() == 0)
if (!std::get<bool>(result))
return false;

di.zone = domain;
parseDomainInfo(boost::get<domaininfo_result_t>(result), di);
parseDomainInfo(std::get<domaininfo_result_t>(result), di);

return true;
}
Expand All @@ -324,10 +324,10 @@ public:

logCall("get_all_domain_metadata", "name=" << name);
get_all_domain_metadata_result_t result = f_get_all_domain_metadata(name);
if (result.which() == 0)
if (!std::get<bool>(result))
return false;

for (const auto& row : boost::get<vector<pair<string, domain_metadata_result_t>>>(result)) {
for (const auto& row : std::get<vector<pair<string, domain_metadata_result_t>>>(result)) {
meta[row.first].clear();
for (const auto& item : row.second)
meta[row.first].push_back(item.second);
Expand All @@ -344,11 +344,11 @@ public:

logCall("get_domain_metadata", "name=" << name << ",kind=" << kind);
get_domain_metadata_result_t result = f_get_domain_metadata(name, kind);
if (result.which() == 0)
if (!std::get<bool>(result))
return false;

meta.clear();
for (const auto& item : boost::get<domain_metadata_result_t>(result))
for (const auto& item : std::get<domain_metadata_result_t>(result))
meta.push_back(item.second);

logResult("value=" << boost::algorithm::join(meta, ", "));
Expand All @@ -363,23 +363,23 @@ public:
logCall("get_domain_keys", "name=" << name);
get_domain_keys_result_t result = f_get_domain_keys(name);

if (result.which() == 0)
if (!std::get<bool>(result))
return false;

for (const auto& row : boost::get<vector<pair<int, keydata_result_t>>>(result)) {
for (const auto& row : std::get<vector<pair<int, keydata_result_t>>>(result)) {
DNSBackend::KeyData key;
key.published = true;
for (const auto& item : row.second) {
if (item.first == "content")
key.content = boost::get<string>(item.second);
key.content = std::get<string>(item.second);
else if (item.first == "id")
key.id = static_cast<unsigned int>(boost::get<int>(item.second));
key.id = static_cast<unsigned int>(std::get<int>(item.second));
else if (item.first == "flags")
key.flags = static_cast<unsigned int>(boost::get<int>(item.second));
key.flags = static_cast<unsigned int>(std::get<int>(item.second));
else if (item.first == "active")
key.active = boost::get<bool>(item.second);
key.active = std::get<bool>(item.second);
else if (item.first == "published")
key.published = boost::get<bool>(item.second);
key.published = std::get<bool>(item.second);
else
g_log << Logger::Warning << "[" << getPrefix() << "] Unsupported key '" << item.first << "' in keydata result" << endl;
}
Expand All @@ -398,20 +398,20 @@ public:
logCall("get_before_and_after_names_absolute", "id=<<" << id << ",qname=" << qname);
get_before_and_after_names_absolute_result_t result = f_get_before_and_after_names_absolute(id, qname);

if (result.which() == 0)
if (!std::get<bool>(result))
return false;

before_and_after_names_result_t row = boost::get<before_and_after_names_result_t>(result);
before_and_after_names_result_t row = std::get<before_and_after_names_result_t>(result);
if (row.size() != 3) {
g_log << Logger::Error << "Invalid result from dns_get_before_and_after_names_absolute, expected array with 3 items, got " << row.size() << "item(s)" << endl;
return false;
}
for (const auto& item : row) {
DNSName value;
if (item.second.which() == 0)
value = DNSName(boost::get<string>(item.second));
if (!std::get<string>(item.second).empty())
value = DNSName(std::get<string>(item.second));
else
value = DNSName(boost::get<DNSName>(item.second));
value = DNSName(std::get<DNSName>(item.second));
if (item.first == "unhashed")
unhashed = value;
else if (item.first == "before")
Expand Down
8 changes: 4 additions & 4 deletions pdns/lua-auth4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ bool AuthLua4::axfrfilter(const ComboAddress& remote, const DNSName& zone, const
DNSResourceRecord rec;

const auto& map = row.second;
rec.qtype = QType(boost::get<unsigned int>(map.at("qtype")));
rec.qname = DNSName(boost::get<std::string>(map.at("qname")));
rec.qtype = QType(std::get<unsigned int>(map.at("qtype")));
rec.qname = DNSName(std::get<std::string>(map.at("qname")));
rec.qname.makeUsLowerCase();
if (map.count("ttl")) {
rec.ttl = boost::get<unsigned int>(map.at("ttl"));
rec.ttl = std::get<unsigned int>(map.at("ttl"));
}
rec.setContent(boost::get<std::string>(map.at("content")));
rec.setContent(std::get<std::string>(map.at("content")));

out.push_back(rec);
}
Expand Down
2 changes: 1 addition & 1 deletion pdns/lua-auth4.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private:
};

typedef std::function<bool(const UpdatePolicyQuery&)> luacall_update_policy_t;
typedef std::function<std::tuple<int, std::unordered_map<int, std::unordered_map<std::string,boost::variant<unsigned int,std::string> > > >(const ComboAddress&, const DNSName&, const DNSResourceRecord&)> luacall_axfr_filter_t;
typedef std::function<std::tuple<int, std::unordered_map<int, std::unordered_map<std::string, std::variant<unsigned int, std::string>>>>(const ComboAddress&, const DNSName&, const DNSResourceRecord&)> luacall_axfr_filter_t;
typedef std::function<bool(DNSPacket*)> luacall_prequery_t;

luacall_update_policy_t d_update_policy;
Expand Down
38 changes: 19 additions & 19 deletions pdns/lua-base4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,25 @@ void BaseLua4::prepareContext() {

// cas_t
d_lw->writeFunction("newCAS", []{ return cas_t(); });
d_lw->registerFunction<void(cas_t::*)(boost::variant<string,ComboAddress, vector<pair<unsigned int,string> > >)>("add",
[](cas_t& cas, const boost::variant<string,ComboAddress,vector<pair<unsigned int,string> > >& in)
{
try {
if(auto s = boost::get<string>(&in)) {
cas.insert(ComboAddress(*s));
}
else if(auto v = boost::get<vector<pair<unsigned int, string> > >(&in)) {
for(const auto& str : *v)
cas.insert(ComboAddress(str.second));
}
else
cas.insert(boost::get<ComboAddress>(in));
}
catch(std::exception& e) {
SLOG(g_log <<Logger::Error<<e.what()<<endl,
g_slog->withName("lua")->error(Logr::Error, e.what(), "Exception in newCAS", "exception", Logging::Loggable("std::exception")));
}
});
d_lw->registerFunction<void (cas_t::*)(std::variant<string, ComboAddress, vector<pair<unsigned int, string>>>)>("add",
[](cas_t& cas, const std::variant<string, ComboAddress, vector<pair<unsigned int, string>>>& in) {
try {
auto s = std::get<string>(in);
if (!s.empty()) {
cas.emplace(s);
}
else if (auto v = std::get<vector<pair<unsigned int, string>>>(in); !v.empty()) {
for (const auto& str : v)
cas.emplace(str.second);
}
else
cas.insert(std::get<ComboAddress>(in));
}
catch (std::exception& e) {
SLOG(g_log << Logger::Error << e.what() << endl,
g_slog->withName("lua")->error(Logr::Error, e.what(), "Exception in newCAS", "exception", Logging::Loggable("std::exception")));
}
});
d_lw->registerFunction<bool(cas_t::*)(const ComboAddress&)>("check",[](const cas_t& cas, const ComboAddress&ca) { return cas.count(ca)>0; });

// QType
Expand Down
5 changes: 2 additions & 3 deletions pdns/lua-base4.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "namespaces.hh"
#include <boost/variant/variant.hpp>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Expand All @@ -24,7 +23,7 @@ protected:
virtual void postPrepareContext() = 0;
virtual void postLoad() = 0;
typedef vector<pair<string, int>> in_t;
vector<pair<string, boost::variant<int, in_t, struct timeval*>>> d_pd;
typedef vector<pair<string, boost::variant<string, bool, int, double>>> Features;
vector<pair<string, std::variant<int, in_t, struct timeval*>>> d_pd;
typedef vector<pair<string, std::variant<string, bool, int, double>>> Features;
virtual void getFeatures(Features&);
};
Loading
Loading