Commit 145f5228 authored by Volker Krause's avatar Volker Krause
Browse files

Allow to configure acceptable top-level types in the result validator

parent d7250af1
......@@ -18,6 +18,7 @@
#include <extractorvalidator.h>
#include <KItinerary/JsonLdDocument>
#include <KItinerary/Reservation>
#include <QDebug>
#include <QFile>
......@@ -51,7 +52,14 @@ private Q_SLOTS:
QEXPECT_FAIL("", "not implemented yet", Continue);
QVERIFY(v.isValidElement(input.at(0)));
// TODO test type filtering
v.setAcceptedTypes<FlightReservation, TrainReservation>();
input = JsonLdDocument::fromJson(QJsonDocument::fromJson(readFile(SOURCE_DIR "/mergedata/cancellation.lhs.json")).array());
QCOMPARE(input.size(), 1);
QVERIFY(v.isValidElement(input.at(0)));
v.setAcceptedTypes<LodgingReservation, TrainReservation>();
QVERIFY(!v.isValidElement(input.at(0)));
v.setAcceptedTypes();
QVERIFY(v.isValidElement(input.at(0)));
}
};
......
......@@ -41,6 +41,7 @@
#include <KItinerary/Place>
#include <KItinerary/RentalCar>
#include <KItinerary/Reservation>
#include <KItinerary/Taxi>
#include <KItinerary/Ticket>
#include <KItinerary/TrainTrip>
#include <KItinerary/Visit>
......@@ -64,6 +65,28 @@ using namespace KItinerary;
ExtractorPostprocessor::ExtractorPostprocessor()
: d(new ExtractorPostprocessorPrivate)
{
// configure the default set of accepted types, for backward compatibility
d->m_validator.setAcceptedTypes<
FlightReservation,
TrainReservation,
BusReservation,
RentalCarReservation,
TaxiReservation,
EventReservation,
FoodEstablishmentReservation,
LodgingReservation,
// reservationFor types
Flight,
TrainTrip,
BusTrip,
RentalCar,
Taxi,
Event,
TouristAttractionVisit,
FoodEstablishment,
// PBI types
LocalBusiness
>();
}
ExtractorPostprocessor::ExtractorPostprocessor(ExtractorPostprocessor &&) noexcept = default;
......
......@@ -35,12 +35,23 @@ using namespace KItinerary;
namespace KItinerary {
class ExtractorValidatorPrivate {
public:
bool isSupportedTopLevelType(const QVariant &elem) const;
std::vector<const QMetaObject*> m_acceptedTypes;
};
}
ExtractorValidator::ExtractorValidator() = default;
ExtractorValidator::ExtractorValidator() :
d(new ExtractorValidatorPrivate)
{}
ExtractorValidator::~ExtractorValidator() = default;
void ExtractorValidator::setAcceptedTypes(std::vector<const QMetaObject*> &&accptedTypes)
{
d->m_acceptedTypes = std::move(accptedTypes);
}
static bool filterElement(const QVariant &elem);
static bool filterLodgingReservation(const LodgingReservation &res)
......@@ -151,45 +162,21 @@ static bool filterElement(const QVariant &elem)
return true;
}
// TODO this default is merely to retain behavior compat for now
// eventually this should be configured by the user to the subset they need
static const QMetaObject* supported_type_table[] = {
// reservation types
&FlightReservation::staticMetaObject,
&TrainReservation::staticMetaObject,
&BusReservation::staticMetaObject,
&RentalCarReservation::staticMetaObject,
&TaxiReservation::staticMetaObject,
&EventReservation::staticMetaObject,
&FoodEstablishmentReservation::staticMetaObject,
&LodgingReservation::staticMetaObject,
// reservationFor types
&Flight::staticMetaObject,
&TrainTrip::staticMetaObject,
&BusTrip::staticMetaObject,
&RentalCar::staticMetaObject,
&Taxi::staticMetaObject,
&Event::staticMetaObject,
&TouristAttractionVisit::staticMetaObject,
&FoodEstablishment::staticMetaObject,
// PBI types
&LocalBusiness::staticMetaObject,
};
static bool isSupportedTopLevelType(const QVariant &elem)
bool ExtractorValidatorPrivate::isSupportedTopLevelType(const QVariant &elem) const
{
if (m_acceptedTypes.empty()) { // nothing configured, we accept everything
return true;
}
auto mo = QMetaType::metaObjectForType(elem.userType());
if (!mo) {
qCDebug(ValidatorLog) << "Element discared due to non-gadget top-level type:" << elem.typeName();
return false;
}
while (mo) {
for (const auto &t : supported_type_table) {
if (t == mo) {
return true;
}
const auto it = std::find(m_acceptedTypes.begin(), m_acceptedTypes.end(), mo);
if (it != m_acceptedTypes.end()) {
return true;
}
mo = mo->superClass();
}
......@@ -199,7 +186,7 @@ static bool isSupportedTopLevelType(const QVariant &elem)
bool ExtractorValidator::isValidElement(const QVariant &elem)
{
// check this is an allowed top-level type
if (!isSupportedTopLevelType(elem)) {
if (!d->isSupportedTopLevelType(elem)) {
qCDebug(ValidatorLog) << "Element discarded due to unsupported top-level type:" << elem.typeName();
return false;
}
......
......@@ -21,7 +21,9 @@
#include "kitinerary_export.h"
#include <memory>
#include <vector>
class QMetaObject;
class QVariant;
namespace KItinerary {
......@@ -30,6 +32,7 @@ class ExtractorValidatorPrivate;
/**
* Validates extractor results.
* Used to discard incomplete or otherwise invalid data.
* @since 20.08
*/
class KITINERARY_EXPORT ExtractorValidator
{
......@@ -45,6 +48,18 @@ public:
*/
bool isValidElement(const QVariant &elem);
/** Sets the list of supported top-level types that should be accepted.
* Providing an empty set of types will accept all top-level types.
* Instances of types inheriting from accepted types are also accepted.
* Default is to accept all types.
*/
void setAcceptedTypes(std::vector<const QMetaObject*> &&accptedTypes);
/** Convenience overload of setAcceptedTypes(). */
template <typename ...Args> inline void setAcceptedTypes()
{
setAcceptedTypes({&Args::staticMetaObject...});
}
private:
std::unique_ptr<ExtractorValidatorPrivate> d;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment