3
* @author Michael Fetisov (fetisov.michael@bmstu.ru)
8
* @copyright Copyright (c) 2022
12
#include "simodo/setup/SetupJsonBuilder.h"
13
#include "simodo/setup/SetupStructure.h"
14
#include "simodo/variable/json/Serialization.h"
15
#include "simodo/inout/convert/functions.h"
17
namespace simodo::setup
19
SetupDescription SetupJsonBuilder::build(const std::string & setup_file)
22
variable::Value value;
24
_errors = variable::loadJson(setup_file, value);
29
if (value.type() != variable::ValueType::Object) {
30
/// \todo Тут и далее поменять на более понятное описание ошибки
31
_errors = "Недопустимый формат #1";
35
std::shared_ptr<variable::Object> setup_object = value.getObject();
37
const variable::Value & brief_value = setup_object->find(u"brief");
38
const variable::Value & description_value = setup_object->find(u"description");
39
const variable::Value & setup_value = setup_object->find(u"setup");
41
/// \todo Разделить проверки с подробным описанием
42
if (brief_value.type() != variable::ValueType::String
43
|| description_value.type() != variable::ValueType::String
44
|| setup_value.type() != variable::ValueType::Array) {
45
_errors = "Недопустимый формат #2";
49
res.brief = inout::toU8(brief_value.getString());
50
res.description = inout::toU8(description_value.getString());
52
std::shared_ptr<variable::Array> setup_array = setup_value.getArray();
54
for(const variable::Value & v : setup_array->values()) {
55
if (v.type() != variable::ValueType::Object) {
56
_errors = "Недопустимый формат #3";
60
std::shared_ptr<variable::Object> param_object = v.getObject();
62
const variable::Value & param_id_value = param_object->find(u"id");
63
const variable::Value & param_name_value = param_object->find(u"name");
64
const variable::Value & param_brief_value = param_object->find(u"brief");
65
const variable::Value & param_description_value = param_object->find(u"description");
66
const variable::Value & param_value = param_object->find(u"value");
68
/// \todo Разделить проверки с подробным описанием
69
if (param_id_value.type() != variable::ValueType::String
70
|| param_name_value.type() != variable::ValueType::String
71
|| param_brief_value.type() != variable::ValueType::String
72
|| param_description_value.type() != variable::ValueType::String) {
73
_errors = "Недопустимый формат #4";
77
SetupStructure param_struct;
79
param_struct.id = inout::toU8(param_id_value.getString());
80
param_struct.name = inout::toU8(param_name_value.getString());
81
param_struct.brief = inout::toU8(param_brief_value.getString());
82
param_struct.description = inout::toU8(param_description_value.getString());
83
param_struct.value = param_value;
85
res.setup.push_back(param_struct);