From 6522dc5e9449b94b48370ec4a927f7d87c716662 Mon Sep 17 00:00:00 2001 From: ng201 Date: Tue, 11 May 2021 19:04:37 +0100 Subject: [PATCH] POPPY-0078: Model builder added. Bug fixed. --- src/core/CoreUtils.h | 55 +++++++++++++++++++++ src/core/DevRegistry/abstracts/JsonModels.h | 18 +++---- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/core/CoreUtils.h b/src/core/CoreUtils.h index 107c04b..db31bfc 100644 --- a/src/core/CoreUtils.h +++ b/src/core/CoreUtils.h @@ -28,6 +28,9 @@ #include "http/crate/Request.h" #include "http/crate/Response.h" +#include "gason/gason.h" + + class ErrorResponse { public: @@ -150,4 +153,56 @@ namespace CoreUtils { } + +template +class ModelBuilder { + + private: + + gason::JsonAllocator allocator; + gason::JsonValue root; + + union storage_t { + unsigned char dummy; + T value; + + constexpr storage_t() noexcept : dummy{} {} + + constexpr storage_t(T &&value) noexcept : value{ std::move(value) } {} + constexpr storage_t(const T &&value) : value{ value } {} + + ~storage_t(){} + } storage; + + bool error = false; + + public: + + const T& getModel() const noexcept { + return storage.value; + } + + template + static auto parse_with(std::string &&str) { + ModelBuilder builder; + + gason::JsonParseStatus status = gason::jsonParse(str, builder.root, builder.allocator); + if (status != gason::JSON_PARSE_OK) { + builder.error = true; + return builder; + } + + // generate the root + builder.storage.value = B::build(builder.root); + return builder; + } + + operator bool() const noexcept { + return error; + } + +}; + + + #endif /* _CORE_COREUTILS_H_ */ diff --git a/src/core/DevRegistry/abstracts/JsonModels.h b/src/core/DevRegistry/abstracts/JsonModels.h index db1f8ca..6a49647 100644 --- a/src/core/DevRegistry/abstracts/JsonModels.h +++ b/src/core/DevRegistry/abstracts/JsonModels.h @@ -115,7 +115,7 @@ namespace JsonModels { bool error = false; ///< Whether there was a parsing error. bool valid = false; ///< Whether the structure is valid. - bool device = false; ///< Whether the device is given. + //bool device = false; ///< Whether the device is given. public: @@ -136,29 +136,29 @@ namespace JsonModels { if (!node.isObject()) return dev; - dev.device = true; + //dev.device = true; if (auto node = root.child("address")) { if (!node.isString()) return dev; - dev.address = node.toString(); + dev.device.address = node.toString(); dev.valid = true; } if (auto node = root.child("deviceName")) { if (!node.isString()) return dev; - dev.dev_name = node.toString(); + dev.device.dev_name = node.toString(); dev.valid = true; } if (auto node = root.child("macAddress")) { if (!node.isString()) return dev; - dev.mac_address = node.toString(); + dev.device.mac_address = node.toString(); dev.valid = true; } if (auto node = root.child("authenticationInfo")) { if (!node.isString()) return dev; - dev.auth_info = node.toString(); + dev.device.auth_info = node.toString(); dev.valid = true; } } @@ -195,11 +195,11 @@ namespace JsonModels { } const char* validate() const { - if (dev_name == nullptr) + if (device.dev_name == nullptr) return "Device name must have value."; - if (mac_address == nullptr) + if (device.mac_address == nullptr) return "Device MAC address must have value."; - if (address == nullptr) + if (device.address == nullptr) return "Device address must have value."; return nullptr;