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