This commit is contained in:
parent
1dcd862a60
commit
f14020493b
|
@ -37,7 +37,7 @@
|
|||
</ImportGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
|
||||
<QtInstall>5.12.11_msvc2017_64</QtInstall>
|
||||
<QtModules>core;network</QtModules>
|
||||
<QtModules>core;xml;network</QtModules>
|
||||
<QtBuildConfig>debug</QtBuildConfig>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
|
||||
|
@ -94,14 +94,14 @@
|
|||
<ItemGroup>
|
||||
<ClCompile Include="dispatch.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="msgs_types.cpp" />
|
||||
<ClCompile Include="validate_depict.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="cmds.h" />
|
||||
<ClInclude Include="cmds_basic.h" />
|
||||
<ClInclude Include="data_types.h" />
|
||||
<ClInclude Include="dispatch.h" />
|
||||
<ClInclude Include="msgs_types.h" />
|
||||
<ClInclude Include="validate_depict.h" />
|
||||
<ClInclude Include="validate_impl.h" />
|
||||
<ClInclude Include="validate.h" />
|
||||
<ClInclude Include="validate_basic.h" />
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
<ClCompile Include="dispatch.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="msgs_types.cpp">
|
||||
<ClCompile Include="validate_depict.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
|
@ -46,9 +46,6 @@
|
|||
<ClInclude Include="data_types.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="msgs_types.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="validate.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
@ -58,5 +55,8 @@
|
|||
<ClInclude Include="validate_basic.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="validate_depict.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -4,63 +4,90 @@
|
|||
#include <memory>
|
||||
|
||||
namespace datas {
|
||||
class IDataComplex {
|
||||
public:
|
||||
virtual ~IDataComplex() = default;
|
||||
|
||||
/**
|
||||
* @brief 是否空对象.
|
||||
*
|
||||
* \return 结果
|
||||
*/
|
||||
virtual bool isEmpty() const = 0;
|
||||
|
||||
/**
|
||||
* @brief 是否Array.
|
||||
*
|
||||
* \return
|
||||
*/
|
||||
virtual bool isArray() const = 0;
|
||||
|
||||
/**
|
||||
* @brief 是否Object.
|
||||
*
|
||||
* \return
|
||||
*/
|
||||
virtual bool isObject() const = 0;
|
||||
};
|
||||
|
||||
class IDataArray;
|
||||
class IDataObject {
|
||||
class IDataObject : public datas::IDataComplex {
|
||||
public:
|
||||
virtual ~IDataObject() = default;
|
||||
|
||||
/**
|
||||
* @brief 是否空对象.
|
||||
*
|
||||
* \return 结果
|
||||
* @brief 构建一个新的Array对象.
|
||||
*
|
||||
* \return
|
||||
*/
|
||||
virtual bool isEmpty() const = 0;
|
||||
virtual std::shared_ptr<IDataArray> newArray() const = 0;;
|
||||
/**
|
||||
* @brief 当前对象是否是Array.
|
||||
*
|
||||
* \return 结果
|
||||
* @brief 构建一个新的Object.
|
||||
*
|
||||
* \return
|
||||
*/
|
||||
virtual bool isArray() const = 0;
|
||||
virtual std::shared_ptr<IDataObject> newObject() const = 0;
|
||||
/**
|
||||
* @brief 转换成Array.
|
||||
* @brief 获取子对象.
|
||||
*
|
||||
* \param key
|
||||
* \return
|
||||
*/
|
||||
virtual std::shared_ptr<IDataArray> toArray() = 0;
|
||||
virtual std::shared_ptr<datas::IDataComplex> getChild(const QString &key) const = 0;
|
||||
virtual void setChild(const QString &key, std::shared_ptr<datas::IDataComplex> obj) = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @brief 指定字段是值类型.
|
||||
*
|
||||
* \param key
|
||||
* \return
|
||||
*/
|
||||
virtual bool isValueField(const QString& key) const = 0;
|
||||
|
||||
virtual bool getBool(const QString& key) const = 0;
|
||||
virtual double getDouble(const QString& key) const = 0;
|
||||
virtual int32_t getInt32(const QString& key) const = 0;
|
||||
virtual int64_t getInt64(const QString& key) const = 0;
|
||||
virtual QString getString(const QString& key) const = 0;
|
||||
/**
|
||||
* @brief 获取指定对象,若无指定对象则返回一个不关联的空对象.
|
||||
*
|
||||
* \param key
|
||||
* \return
|
||||
*/
|
||||
virtual std::shared_ptr<IDataObject> getObject(const QString& key) const = 0;
|
||||
|
||||
virtual void setBool(const QString& key, bool value) = 0;
|
||||
virtual void setDouble(const QString& key, double value) = 0;
|
||||
virtual void setInt32(const QString& key, int32_t value) = 0;
|
||||
virtual void setInt64(const QString& key, int64_t value) = 0;
|
||||
virtual void setString(const QString& key, const QString& value) = 0;
|
||||
virtual void setObject(const QString& key, std::shared_ptr<IDataObject> value) = 0;
|
||||
|
||||
virtual QString toText() const = 0;
|
||||
virtual void loadFrom(const QString &text) = 0;
|
||||
};
|
||||
class IDataArray : public IDataObject {
|
||||
class IDataArray : public datas::IDataComplex {
|
||||
public:
|
||||
virtual ~IDataArray() = default;
|
||||
|
||||
virtual bool isValid() const = 0;
|
||||
|
||||
virtual int32_t size() const = 0;
|
||||
virtual std::shared_ptr<IDataObject> get(int index) const = 0;
|
||||
virtual void insert(int index, std::shared_ptr<IDataObject> value) = 0;
|
||||
virtual void append(std::shared_ptr<IDataObject> value) = 0;
|
||||
virtual std::shared_ptr<datas::IDataComplex> get(int index) const = 0;
|
||||
virtual void insert(int index, std::shared_ptr<datas::IDataComplex> value) = 0;
|
||||
virtual void append(std::shared_ptr<datas::IDataComplex> value) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
#include "msgs_types.h"
|
||||
|
||||
using namespace msgs;
|
||||
|
||||
QByteArray getMessage(const datas::IDataObject& obj) {
|
||||
auto content = obj.toText().toUtf8();
|
||||
auto header = QString(u8"Content-Length:%1\n").arg(content.length());
|
||||
|
||||
auto u8_content = header.toUtf8();
|
||||
u8_content.append(content);
|
||||
return u8_content;
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include "data_types.h"
|
||||
|
||||
namespace msgs {
|
||||
QByteArray getMessage(const datas::IDataObject &obj);
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
#include "validate_depict.h"
|
||||
|
||||
using namespace datas;
|
||||
using namespace depict;
|
||||
|
||||
depict::ValidateDocObject::ValidateDocObject(QJsonObject obj) : _object_bind(obj) { }
|
||||
|
||||
depict::ValidateDocObject::ValidateDocObject() { }
|
||||
|
||||
bool ValidateDocObject::isEmpty() const {
|
||||
return _object_bind.isEmpty();
|
||||
}
|
||||
|
||||
bool ValidateDocObject::isArray() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ValidateDocObject::isObject() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<IDataArray> ValidateDocObject::newArray() const {
|
||||
return std::shared_ptr<IDataArray>();
|
||||
}
|
||||
|
||||
std::shared_ptr<IDataObject> ValidateDocObject::newObject() const {
|
||||
return std::shared_ptr<IDataObject>();
|
||||
}
|
||||
|
||||
std::shared_ptr<IDataComplex> ValidateDocObject::getChild(const QString& key) const {
|
||||
auto t_obj = _object_bind.value(key);
|
||||
if (t_obj.isArray())
|
||||
return std::make_shared<ValidateDocArray>(t_obj.toArray());
|
||||
if (t_obj.isObject())
|
||||
return std::make_shared<ValidateDocObject>(t_obj.toObject());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ValidateDocObject::setChild(const QString& key, std::shared_ptr<IDataComplex> obj) {
|
||||
if (obj->isArray()) {
|
||||
this->_object_bind[key] = std::dynamic_pointer_cast<ValidateDocArray>(obj)->_array_bind;
|
||||
}
|
||||
else if (obj->isObject()) {
|
||||
this->_object_bind[key] = std::dynamic_pointer_cast<ValidateDocObject>(obj)->_object_bind;
|
||||
}
|
||||
}
|
||||
|
||||
bool ValidateDocObject::isValueField(const QString& key) const {
|
||||
auto val = _object_bind.value(key);
|
||||
if (val.isArray() || val.isObject())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ValidateDocObject::getBool(const QString& key) const {
|
||||
return _object_bind.value(key).toBool();
|
||||
}
|
||||
|
||||
double ValidateDocObject::getDouble(const QString& key) const {
|
||||
return _object_bind.value(key).toDouble();
|
||||
}
|
||||
|
||||
int32_t ValidateDocObject::getInt32(const QString& key) const {
|
||||
return _object_bind.value(key).toInt();
|
||||
}
|
||||
|
||||
#include <QVariant>
|
||||
int64_t ValidateDocObject::getInt64(const QString& key) const {
|
||||
return _object_bind.value(key).toVariant().toLongLong();
|
||||
}
|
||||
|
||||
QString ValidateDocObject::getString(const QString& key) const {
|
||||
return _object_bind.value(key).toString();
|
||||
}
|
||||
|
||||
void ValidateDocObject::setBool(const QString& key, bool value) {
|
||||
_object_bind[key] = value;
|
||||
}
|
||||
|
||||
void ValidateDocObject::setDouble(const QString& key, double value) {
|
||||
_object_bind[key] = value;
|
||||
}
|
||||
|
||||
void ValidateDocObject::setInt32(const QString& key, int32_t value) {
|
||||
_object_bind[key] = value;
|
||||
}
|
||||
|
||||
void ValidateDocObject::setInt64(const QString& key, int64_t value) {
|
||||
_object_bind[key] = value;
|
||||
}
|
||||
|
||||
void ValidateDocObject::setString(const QString& key, const QString& value) {
|
||||
_object_bind[key] = value;
|
||||
}
|
||||
|
||||
#include <QJsonDocument>
|
||||
QString ValidateDocObject::toText() const {
|
||||
return QString::fromUtf8(QJsonDocument(_object_bind).toJson(QJsonDocument::JsonFormat::Compact));
|
||||
}
|
||||
|
||||
void ValidateDocObject::loadFrom(const QString& text) {
|
||||
auto doc = QJsonDocument::fromJson(text.toUtf8());
|
||||
_object_bind = doc.object();
|
||||
}
|
||||
|
||||
depict::ValidateDocArray::ValidateDocArray(QJsonArray array) : _array_bind(array) { }
|
||||
|
||||
depict::ValidateDocArray::ValidateDocArray() { }
|
||||
|
||||
bool depict::ValidateDocArray::isEmpty() const {
|
||||
return !_array_bind.size();
|
||||
}
|
||||
|
||||
bool depict::ValidateDocArray::isArray() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool depict::ValidateDocArray::isObject() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t depict::ValidateDocArray::size() const {
|
||||
return _array_bind.size();
|
||||
}
|
||||
|
||||
std::shared_ptr<IDataComplex> depict::ValidateDocArray::get(int index) const {
|
||||
auto val = _array_bind.at(index);
|
||||
if (val.isArray())
|
||||
return std::make_shared<ValidateDocArray>(val.toArray());
|
||||
|
||||
if (val.isObject())
|
||||
return std::make_shared<ValidateDocObject>(val.toObject());
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void depict::ValidateDocArray::insert(int index, std::shared_ptr<IDataComplex> value) {
|
||||
if (value->isArray())
|
||||
_array_bind.insert(index, std::dynamic_pointer_cast<ValidateDocArray>(value)->_array_bind);
|
||||
if (value->isObject())
|
||||
_array_bind.insert(index, std::dynamic_pointer_cast<ValidateDocObject>(value)->_object_bind);
|
||||
}
|
||||
|
||||
void depict::ValidateDocArray::append(std::shared_ptr<IDataComplex> value) {
|
||||
if (value->isArray())
|
||||
_array_bind.append(std::dynamic_pointer_cast<ValidateDocArray>(value)->_array_bind);
|
||||
if (value->isObject())
|
||||
_array_bind.append(std::dynamic_pointer_cast<ValidateDocObject>(value)->_object_bind);
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
#pragma once
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include "data_types.h"
|
||||
|
||||
namespace depict {
|
||||
class ValidateDocArray;
|
||||
class ValidateDocObject : public datas::IDataObject {
|
||||
friend class ValidateDocArray;
|
||||
private:
|
||||
QJsonObject _object_bind;
|
||||
|
||||
public:
|
||||
ValidateDocObject(QJsonObject obj);
|
||||
ValidateDocObject();
|
||||
|
||||
// ͨ¹ý IDataObject ¼Ì³Ð
|
||||
bool isEmpty() const override;
|
||||
|
||||
bool isArray() const override;
|
||||
|
||||
bool isObject() const override;
|
||||
|
||||
std::shared_ptr<datas::IDataArray> newArray() const override;
|
||||
|
||||
std::shared_ptr<datas::IDataObject> newObject() const override;
|
||||
|
||||
std::shared_ptr<datas::IDataComplex> getChild(const QString& key) const override;
|
||||
|
||||
void setChild(const QString& key, std::shared_ptr<datas::IDataComplex> obj) override;
|
||||
|
||||
bool isValueField(const QString& key) const override;
|
||||
|
||||
bool getBool(const QString& key) const override;
|
||||
|
||||
double getDouble(const QString& key) const override;
|
||||
|
||||
int32_t getInt32(const QString& key) const override;
|
||||
|
||||
int64_t getInt64(const QString& key) const override;
|
||||
|
||||
QString getString(const QString& key) const override;
|
||||
|
||||
void setBool(const QString& key, bool value) override;
|
||||
|
||||
void setDouble(const QString& key, double value) override;
|
||||
|
||||
void setInt32(const QString& key, int32_t value) override;
|
||||
|
||||
void setInt64(const QString& key, int64_t value) override;
|
||||
|
||||
void setString(const QString& key, const QString& value) override;
|
||||
|
||||
QString toText() const override;
|
||||
|
||||
void loadFrom(const QString& text) override;
|
||||
|
||||
};
|
||||
|
||||
class ValidateDocArray : public datas::IDataArray {
|
||||
friend class ValidateDocObject;
|
||||
private:
|
||||
QJsonArray _array_bind;
|
||||
|
||||
public:
|
||||
ValidateDocArray(QJsonArray array);
|
||||
ValidateDocArray();
|
||||
|
||||
// ͨ¹ý IDataArray ¼Ì³Ð
|
||||
bool isEmpty() const override;
|
||||
bool isArray() const override;
|
||||
bool isObject() const override;
|
||||
int32_t size() const override;
|
||||
std::shared_ptr<IDataComplex> get(int index) const override;
|
||||
void insert(int index, std::shared_ptr<IDataComplex> value) override;
|
||||
void append(std::shared_ptr<IDataComplex> value) override;
|
||||
};
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue