-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsjcpp_dto.cpp
More file actions
181 lines (133 loc) · 5.46 KB
/
Copy pathwsjcpp_dto.cpp
File metadata and controls
181 lines (133 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "wsjcpp_dto.h"
#include <wsjcpp_core.h>
// ---------------------------------------------------------------------
// WsjcppDefineFieldDto
WsjcppDefineFieldDto::WsjcppDefineFieldDto(
const std::string &sFieldTypeName,
const std::string &sFieldName,
const std::string &sFieldDescription
) {
m_sFieldTypeName = sFieldTypeName;
m_sFieldName = sFieldName;
// validate name - only lowercase and _
m_sFieldDescription = sFieldDescription;
}
// ---------------------------------------------------------------------
WsjcppDefineFieldDto & WsjcppDefineFieldDto::optional() {
m_sFieldRestrict = "optional";
return *this;
}
// ---------------------------------------------------------------------
WsjcppDefineFieldDto & WsjcppDefineFieldDto::required() {
m_sFieldRestrict = "required";
return *this;
}
// ---------------------------------------------------------------------
const std::string &WsjcppDefineFieldDto::getFieldTypeName() const {
return m_sFieldTypeName;
}
// ---------------------------------------------------------------------
const std::string &WsjcppDefineFieldDto::getFieldName() const {
return m_sFieldName;
}
// ---------------------------------------------------------------------
const std::string &WsjcppDefineFieldDto::getFieldRestrict() const {
return m_sFieldRestrict;
}
// ---------------------------------------------------------------------
const std::string &WsjcppDefineFieldDto::getFieldDescription() const {
return m_sFieldDescription;
}
// ---------------------------------------------------------------------
// WsjcppDtoString
WsjcppDtoString::WsjcppDtoString(const std::string &sName, const std::string &sDescription)
: WsjcppDefineFieldDto("string", sName, sDescription) {
}
// ---------------------------------------------------------------------
// WsjcppDtoBoolean
WsjcppDtoBoolean::WsjcppDtoBoolean(const std::string &sName, const std::string &sDescription)
: WsjcppDefineFieldDto("boolean", sName, sDescription) {
}
// ---------------------------------------------------------------------
// WsjcppDtoInteger
WsjcppDtoInteger::WsjcppDtoInteger(const std::string &sName, const std::string &sDescription)
: WsjcppDefineFieldDto("integer", sName, sDescription) {
}
// ---------------------------------------------------------------------
// WsjcppDto
WsjcppDto::WsjcppDto(const std::string &sTypeName, const std::string &sDescription) {
TAG = "DTO-" + sTypeName;
m_sTypeName = sTypeName;
m_sDescription = sDescription;
}
// ---------------------------------------------------------------------
const std::string &WsjcppDto::getTypeName() {
return m_sTypeName;
}
// ---------------------------------------------------------------------
const std::string &WsjcppDto::getDescription() {
return m_sDescription;
}
// ---------------------------------------------------------------------
bool WsjcppDto::setFieldStringValue(const std::string &sFieldName, const std::string &sFieldValue) {
WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName);
if (pField == nullptr) {
WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'");
return false;
}
// pField->isValid();
// TODO call validators
m_jsonReadyObject[sFieldName] = sFieldValue;
return true;
}
// ---------------------------------------------------------------------
bool WsjcppDto::setFieldIntegerValue(const std::string &sFieldName, int nValue) {
WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName);
if (pField == nullptr) {
WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'");
return false;
}
// TODO call validators
m_jsonReadyObject[sFieldName] = nValue;
return true;
}
// ---------------------------------------------------------------------
bool WsjcppDto::setFieldBooleanValue(const std::string &sFieldName, bool bValue) {
WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName);
if (pField == nullptr) {
WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'");
return false;
}
// TODO call validators
m_jsonReadyObject[sFieldName] = bValue;
return true;
}
// ---------------------------------------------------------------------
bool WsjcppDto::setFieldJsonValue(const std::string &sFieldName, const nlohmann::json &jsonFieldValue) {
WsjcppDefineFieldDto *pField = this->findFieldByName(sFieldName);
if (pField == nullptr) {
WsjcppLog::throw_err(TAG, "Object '" + m_sTypeName + "' has not field '" + sFieldName + "'");
return false;
}
// TODO call validators
m_jsonReadyObject[sFieldName] = jsonFieldValue;
return true;
}
// ---------------------------------------------------------------------
bool WsjcppDto::fillFromJson(nlohmann::json &jsonData, std::string &sError) {
return false;
}
// ---------------------------------------------------------------------
nlohmann::json WsjcppDto::toJson() {
return m_jsonReadyObject;
}
// ---------------------------------------------------------------------
WsjcppDefineFieldDto *WsjcppDto::findFieldByName(const std::string &sFieldName) {
for (int i = 0; i < m_vFields.size(); i++) {
if (m_vFields[i]->getFieldName() == sFieldName) {
return m_vFields[i];
}
}
return nullptr;
}
// ---------------------------------------------------------------------