Commit 73da235b authored by Volker Krause's avatar Volker Krause
Browse files

Encapsulate JSON-LD conversion in GenericExtractor::Result

Should allow us to avoid some back-and-forth conversion in the extraction
process eventually.
parent 0b0fa901
......@@ -470,7 +470,8 @@ void ExtractorEnginePrivate::extractDocument()
// if we didn't find anything, let's use whatever the generic extractors found
if (m_result.empty()) {
for (const auto &gr : m_genericResults) {
std::copy(gr.result.begin(), gr.result.end(), std::back_inserter(m_result));
const auto r = gr.jsonLdResult();
std::copy(r.begin(), r.end(), std::back_inserter(m_result));
}
}
}
......@@ -486,12 +487,12 @@ void ExtractorEnginePrivate::extractGeneric()
m_genericResults = m_genericPdfExtractor.extract(m_pdfDoc.get());
}
else if (m_pass) {
m_genericResults.emplace_back(GenericExtractor::Result{{GenericPkPassExtractor::extract(m_pass.get(), m_context->m_senderDate)}});
m_genericResults.emplace_back(GenericPkPassExtractor::extract(m_pass.get(), m_context->m_senderDate));
}
else if (!m_text.isEmpty()) {
if (IataBcbpParser::maybeIataBcbp(m_text)) {
const auto res = IataBcbpParser::parse(m_text, m_context->m_senderDate.date());
m_genericResults.emplace_back(GenericExtractor::Result{JsonLdDocument::toJson(res), m_text});
m_genericResults.emplace_back(GenericExtractor::Result{res, m_text});
}
}
else if (!m_data.isEmpty()) {
......@@ -540,7 +541,7 @@ void ExtractorEnginePrivate::determineExtractors()
for (const auto &genericResult : m_genericResults) {
// check if generic extractors identified documents we have custom extractors for
m_repo.extractorsForJsonLd(genericResult.result, m_extractors);
m_repo.extractorsForJsonLd(genericResult.jsonLdResult(), m_extractors);
// check the unrecognized (vendor-specific) barcodes, if any
m_repo.extractorsForBarcode(genericResult.barcode().toString(), m_extractors);
}
......@@ -605,8 +606,8 @@ void ExtractorEnginePrivate::extractCustomForGenericResults()
for (const auto &genericResult : m_genericResults) {
// expose genericResult content to custom extractors via Context object
m_context->m_barcode = genericResult.barcode();
if (!genericResult.result.empty()) {
m_context->m_data = m_engine.toScriptValue(genericResult.result);
if (!genericResult.jsonLdResult().isEmpty()) {
m_context->m_data = m_engine.toScriptValue(genericResult.jsonLdResult());
}
m_context->m_pdfPageNum = genericResult.pageNumber();
......@@ -617,7 +618,8 @@ void ExtractorEnginePrivate::extractCustomForGenericResults()
// if this didn't find something, take the generic extractor result as-is
if (prevResults == m_result.size()) {
std::copy(genericResult.result.begin(), genericResult.result.end(), std::back_inserter(m_result));
const auto r = genericResult.jsonLdResult();
std::copy(r.begin(), r.end(), std::back_inserter(m_result));
}
}
}
......
......@@ -17,12 +17,20 @@
#include "genericextractor_p.h"
#include <KItinerary/JsonLdDocument>
using namespace KItinerary;
GenericExtractor::Result::Result() = default;
GenericExtractor::Result::Result(const QJsonArray &result, const QVariant &barcode)
: result(result)
: m_jsonLdResult(result)
, m_barcode(barcode)
{
}
GenericExtractor::Result::Result(const QVector<QVariant>& result, const QVariant& barcode)
: m_result(result)
, m_barcode(barcode)
{
}
......@@ -31,7 +39,7 @@ GenericExtractor::Result::~Result() = default;
bool KItinerary::GenericExtractor::Result::isEmpty() const
{
return result.isEmpty() && m_barcode.isNull();
return m_result.isEmpty() && m_jsonLdResult.isEmpty() && m_barcode.isNull();
}
QVariant GenericExtractor::Result::barcode() const
......@@ -48,3 +56,19 @@ void GenericExtractor::Result::setPageNumber(int pageNum)
{
m_pageNum = pageNum;
}
QJsonArray KItinerary::GenericExtractor::Result::jsonLdResult() const
{
if (m_jsonLdResult.isEmpty()) {
m_jsonLdResult = JsonLdDocument::toJson(m_result);
}
return m_jsonLdResult;
}
QVector<QVariant> KItinerary::GenericExtractor::Result::result() const
{
if (m_result.isEmpty()) {
m_result = JsonLdDocument::fromJson(m_jsonLdResult);
}
return m_result;
}
......@@ -20,6 +20,7 @@
#include <QJsonArray>
#include <QVariant>
#include <QVector>
namespace KItinerary {
......@@ -27,12 +28,15 @@ namespace KItinerary {
namespace GenericExtractor
{
/** Generic extraction result. */
/** Generic extraction result.
* This can represent results both in JSON-LD serialized form and in QVariant decoded form.
*/
class Result
{
public:
Result();
explicit Result(const QJsonArray &result, const QVariant &barcode = {});
explicit Result(const QVector<QVariant> &result, const QVariant &barcode = {});
~Result();
/** Checks if there is any relevant result set in here. */
......@@ -47,10 +51,14 @@ public:
int pageNumber() const;
void setPageNumber(int pageNum);
public: // TODO
QJsonArray result; // JSON-LD data extracted from this document or page
/** JSON-LD data extracted from this document or page. */
QJsonArray jsonLdResult() const;
/** Result in decoded form. */
QVector<QVariant> result() const;
private:
mutable QJsonArray m_jsonLdResult;
mutable QVector<QVariant> m_result;
QVariant m_barcode;
int m_pageNum = -1;
};
......
......@@ -149,7 +149,7 @@ GenericExtractor::Result GenericPdfExtractor::extractImage(const PdfImage &img,
}
}
return GenericExtractor::Result{{}, s.isEmpty() ? b.isEmpty() ? QVariant() : QVariant(b) : QVariant(s)};
return GenericExtractor::Result{QJsonArray(), s.isEmpty() ? b.isEmpty() ? QVariant() : QVariant(b) : QVariant(s)};
}
bool GenericPdfExtractor::maybeBarcode(const PdfImage &img, BarcodeDecoder::BarcodeTypes hint)
......
......@@ -190,7 +190,7 @@ static QDateTime iataContextDate(KPkPass::Pass *pass, const QDateTime &context)
return pass->relevantDate().addDays(-1); // go a bit back, to compensate for unknown departure timezone at this point
}
QJsonObject GenericPkPassExtractor::extract(KPkPass::Pass *pass, const QDateTime &contextDate)
GenericExtractor::Result GenericPkPassExtractor::extract(KPkPass::Pass *pass, const QDateTime &contextDate)
{
QJsonObject result;
if (auto boardingPass = qobject_cast<KPkPass::BoardingPass*>(pass)) {
......@@ -211,7 +211,7 @@ QJsonObject GenericPkPassExtractor::extract(KPkPass::Pass *pass, const QDateTime
result.insert(QStringLiteral("@type"), QLatin1String("EventReservation"));
break;
default:
return result;
return {};
}
}
......@@ -284,5 +284,5 @@ QJsonObject GenericPkPassExtractor::extract(KPkPass::Pass *pass, const QDateTime
result.insert(QStringLiteral("pkpassSerialNumber"), pass->serialNumber());
}
return result;
return GenericExtractor::Result(QJsonArray({result}));
}
......@@ -20,6 +20,8 @@
#ifndef KITINERARY_GENERICPKPASSEXTRACTOR_P_H
#define KITINERARY_GENERICPKPASSEXTRACTOR_P_H
#include "genericextractor_p.h"
namespace KPkPass {
class Pass;
}
......@@ -32,7 +34,7 @@ namespace KItinerary {
/** Generic extractor for PkPass files. */
namespace GenericPkPassExtractor
{
QJsonObject extract(KPkPass::Pass *pass, const QDateTime &contextDate);
GenericExtractor::Result extract(KPkPass::Pass *pass, const QDateTime &contextDate);
}
}
......
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