Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/core/CoreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "http/crate/Request.h"
#include "http/crate/Response.h"

#include "gason/gason.h"


class ErrorResponse {

public:
Expand Down Expand Up @@ -150,4 +153,56 @@ namespace CoreUtils {

}


template<typename T>
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<typename B>
static auto parse_with(std::string &&str) {
ModelBuilder<T> 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_ */
18 changes: 9 additions & 9 deletions src/core/DevRegistry/abstracts/JsonModels.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down