Commit ac7350c2 authored by EXT Arnaud Clère's avatar EXT Arnaud Clère

Use QVariant error context

Refactored isErrorFiltered using a QVariant context() method
parent cebbddff
......@@ -235,13 +235,13 @@ public:
QCborVisitor(QCborValue* v) : cbor(v) { Q_ASSERT(v); }
void reset(QCborValue* v) { cbor=v; Q_ASSERT(v); steps.resize(0); }
QAsciiData currentPath() const {
QVariant context() const {
QByteArray path;
Q_FOREACH(Step s, steps) {
if (!s.key.isNull()) { path.append('{').append( s.key.utf8() ); }
else { path.append('[').append(QByteArray::number(s.idx )); }
}
return QAsciiData(path);
return path;
}
// Shortcuts
......@@ -268,7 +268,6 @@ protected:
bool tryAny() { return true; }
bool isValid() const noexcept { return cbor; }
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter ? errorFilter(e, QString(currentPath().latin1()).append(context)) : false; }
private:
const QCborValue& current(int outer=0) const { Q_ASSERT(0<=outer); return steps.size()-outer <= 0 ? *cbor : steps[steps.size()-outer-1].item; }
......@@ -419,7 +418,7 @@ protected:
return leaveContainer(); }
bool isValid() const noexcept { return const_cast<QCborReader*>(this)->lastError()==QCborError::NoError; }
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter && errorFilter(e, QString("at %1 ").arg(currentOffset()).append(context)); }
QVariant context() const { return currentOffset(); }
private:
void skipTag() { if (isTag()) next(); }
bool getNumber(double& d) { skipTag();
......
......@@ -105,13 +105,13 @@ public:
QJsonVisitor(const QJsonValue* v) : json(v) { Q_ASSERT(v); }
void reset(QJsonValue* v) { json=v; Q_ASSERT(v); steps.resize(0); }
QAsciiData currentPath() const {
QVariant context() const {
QByteArray path;
Q_FOREACH(Step s, steps) {
if (!s.key.isNull()) { path.append('{').append( s.key.utf8() ); }
else { path.append('[').append(QByteArray::number(s.idx )); }
}
return QAsciiData(path);
return path;
}
// Shortcuts
......@@ -137,7 +137,6 @@ protected:
bool tryAny() { return true; }
bool isValid() const noexcept { return true; }
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter ? errorFilter(e, QString(currentPath().latin1()).append(context)) : false; }
private:
const QJsonValue& current(int outer=0) const { Q_ASSERT(0<=outer && json); return steps.size()-outer <= 0 ? *json : steps[steps.size()-outer-1].item; }
......@@ -388,7 +387,7 @@ protected:
return true; }
bool isValid() const noexcept { return io; }
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter ? errorFilter(e, QString("at %1:%2 ").arg(line).arg(column).append(context)) : false; }
QVariant context() const { return QVariantList{line,column}; }
private:
CachedNumber getNumber() {
if (cachedNumber!=None) return cachedNumber;
......
......@@ -616,10 +616,15 @@ protected:
QAbstractValue* itemBind() { return static_cast<QAbstractValue*>(&r); }
void setItemData(QUtf8Data u) { io.buffer() = u.utf8(); io.seek(0); r.reset(&io); }
QString path(QModelIndex current) const {
return current.isValid() ? path(current.parent())+QString("/%1,%2").arg(current.row()).arg(current.column()) : "";
QVariantList path(QModelIndex current) const {
if (current.isValid()) {
auto p = path(current.parent());
p.append(QVariantList{current.row(),current.column()});
return p;
}
return QVariantList();
}
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter ? errorFilter(e, QString("at %1 %2").arg(path(parent)).arg(context)) : false; }
QVariant context() const { return path(parent); }
QBuffer io;
TItemReader r;
......
......@@ -106,11 +106,11 @@ class QSettingsReader : public QAbstractValueReader
public:
QSettingsReader(QSettings* s) : settings(s) { Q_ASSERT(s); levels.push(Level(qBindExpectedItem)); }
QString currentPath() const {
QVariant context() const {
QString path;
for (auto&& l : levels) {
if (l.key.isNull()) { path.append('/').append(l.key.latin1()); }
else { path.append('/').append(QString::number(l.idx)); }
if (l.key.isNull()) { path.append('/').append(QString::number(l.idx)); }
else { path.append('/').append(l.key.latin1()); }
}
return path;
}
......@@ -148,7 +148,6 @@ protected:
bool tryAny() { return true; }
bool isValid() const noexcept { return settings; }
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter && errorFilter(e, QString("at %1 %2").arg(currentPath(), context)); }
private:
template<typename T>
bool set(T& t) { QVariant v = settings->value(key()); if (v.convert(qMetaTypeId<T>())) { t = v.value<T>(); return true; } return false; }
......
......@@ -113,7 +113,7 @@ extern QIdentifierLiteral qmColor;
enum QValueMode { Invalid=0, Read=1, Write=2 }; //!< Specifies QTransmogrifier::zap traversal and processing (the design would support other QValueMode like Append or Diff)
using QValueErrorFilter = std::function<bool(QIdentifierLiteral,QString)>;
using QValueErrorFilter = std::function<bool(QIdentifierLiteral,QVariant)>;
#include <QtCore/qvariant.h>
//#include <QtCore/qfloat16.h>
......@@ -205,7 +205,8 @@ struct QAbstractValue {
errorFilter = newFilter;
return previousHandler;
}
virtual bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter && errorFilter(e, context); }
virtual bool isErrorFiltered(QIdentifierLiteral e, QVariant context = QVariant()) const { auto c = this->context(); return errorFilter && errorFilter(e, c.isNull() ? context : context.isNull() ? c : QVariantList({context, this->context()})); }
virtual QVariant context() const { return QVariant(); }
protected:
QValueErrorFilter errorFilter = nullptr;
};
......
......@@ -122,13 +122,13 @@ public:
QVariantVisitor(const QVariant* v) : value(v) { Q_ASSERT(v); }
void reset(const QVariant* v) { value=v; Q_ASSERT(v); levels.resize(0); }
QAsciiData currentPath() const {
QVariant context() const {
QByteArray path;
Q_FOREACH(Level l, levels) {
if (l.key.isNull()) { path.append('{').append( l.key.utf8() ); }
else { path.append('[').append(QByteArray::number(l.idx )); }
}
return QAsciiData(path);
return path;
}
protected:
// TODO Support _meta to be able to cache and restitute all metadata as well as data+datatype
......@@ -177,7 +177,6 @@ protected:
bool tryAny () { return true; }
bool isValid() const noexcept { return value; }
bool isErrorFiltered(QIdentifierLiteral e, QString context = QString()) const { return errorFilter ? errorFilter(e, QString(currentPath().latin1()).append(context)) : false; }
private:
const QVariant* current(unsigned outer=0) const { return unsigned(levels.size())-outer <= 0 ? value : &(levels[unsigned(levels.size())-outer-1].item); }
......
......@@ -401,8 +401,10 @@ int main(int argc, char *argv[])
qRegisterMetaTypeStreamOperators<QUtf8Data>();
QStringList errors;
auto handler = [&errors](QIdentifierLiteral error, QString context) {
errors.append(context.append(error.latin1()));
auto handler = [&errors](QIdentifierLiteral error, QVariant context) {
QByteArray ba;
TextWriter(&ba).bind(context);
errors.append(ba.append(':').append(error.latin1()));
return true;
};
......@@ -1147,8 +1149,10 @@ void doGuiExample() {
persons[1].phones.append(Phone{Phone::Office,"112"});
QStringList errors;
auto handler = [&errors](QIdentifierLiteral name, QString context){
errors.append(context.append(name.latin1()));
auto handler = [&errors](QIdentifierLiteral error, QVariant context){
QByteArray ba;
TextWriter(&ba).bind(context);
errors.append(ba.append(':').append(error.latin1()));
return true;
};
......
......@@ -56,7 +56,7 @@ Bindable |
Bindable>Cbor |bf647479706502666e756d6265726b2b34342031323334353637ff
Bindable>Json |{"type":2,"number":"+44 1234567"}
Person-Json |================================================================================
Json>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | at 0:1 IgnoredCharacter, at 0:15 IgnoredCharacter, at 0:23 IgnoredCharacter, at 0:40 IgnoredCharacter, at 0:58 IgnoredCharacter, ExpectedDecimal, at 0:102 IgnoredCharacter, at 0:102 commentsIgnoredItem, at 0:102 UnexpectedEnd, at 0:103 UnexpectedValue, at 0:104 Stopped
Json>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | 0 1:IgnoredCharacter, 0 15:IgnoredCharacter, 0 23:IgnoredCharacter, 0 40:IgnoredCharacter, 0 58:IgnoredCharacter, 0x0000000001:ExpectedDecimal, 0 102:IgnoredCharacter, comments [ 0 102]:IgnoredItem, 0 102:UnexpectedEnd, 0 103:UnexpectedValue, 0 104:Stopped
P>Json |{"names":["John","Doe"],"height":1.7500000000000002,"age":-1,"phones":[],"comments":"","children":[]}
Json>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] |
P>JsonValue |{"age":-1,"children":[],"comments":"","height":1.7500000000000004,"names":["John","Doe"],"phones":[]}
......@@ -65,7 +65,7 @@ Json>JsonValue|{"age":-1,"children":[],"comments":"","height":1.7500000000000004
JsonValue>Json|{"age":-1,"children":[],"comments":"","height":1.7500000000000004,"names":["John","Doe"],"phones":[]}
Json>Cbor |bf656e616d65739f644a6f686e63446f65ff66686569676874fa3fe0000063616765206670686f6e65739fff68636f6d6d656e747360686368696c6472656e9fffff
Person-Cbor |================================================================================
Cbor>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | at 41 commentsIgnoredItem, at 41 UnexpectedEnd, at 58 UnexpectedValue, at 58 Stopped
Cbor>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | comments 41:IgnoredItem, 41:UnexpectedEnd, 58:UnexpectedValue, 58:Stopped
P>Cbor |bf656e616d65739f644a6f686e63446f65ff66686569676874fb3ffc00000000000063616765206670686f6e65738068636f6d6d656e747360686368696c6472656e80ff
Cbor>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] |
QCborStream>P |(Person) names:[ John Doe] height:1.75 age:-1 phones:[] comments: children:[] | true
......@@ -76,5 +76,5 @@ CborValue>Cbor|a6656e616d657382644a6f686e63446f6566686569676874fb3ffc00000000000
Cbor>Json |{"names":["John","Doe"],"height":1.75,"age":-1,"phones":[],"comments":"","children":[]} |
Person-Settings|================================================================================
P>Settings |(Person) names:[ John Doe] height:1.75 age:18 phones:[] comments:unicode is likely U+01  + U+1F  + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ children:[]
Settings>P |(Person) names:[ John Doe] height:1.75 age:18 phones:[] comments:unicode is likely U+01  + U+1F  + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ children:[] | at /-1/ namesIgnoredItem, at /-1/ UnexpectedEnd, at /-1 UnexpectedValue
Settings>P |(Person) names:[ John Doe] height:1.75 age:18 phones:[] comments:unicode is likely U+01  + U+1F  + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ children:[] | names /ExpectedItem/-1:IgnoredItem, /ExpectedItem/-1:UnexpectedEnd, /ExpectedItem:UnexpectedValue
P>Settings |QBind
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