Skip to content

Commit 0049019

Browse files
committed
Prepare first
1 parent d28cd6d commit 0049019

File tree

4 files changed

+315
-15
lines changed

4 files changed

+315
-15
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.wsjcpp/*
22
wsjcpp-dto
33
tmp/*
4+
.logs/*
5+
.vscode/*
46

57
# Prerequisites
68
*.d

src/main.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22
#include <iostream>
33
#include <algorithm>
44
#include <wsjcpp_core.h>
5+
#include <wsjcpp_dto.h>
6+
7+
class WsjcppDtoPerson : public WsjcppDefineDto {
8+
public:
9+
WsjcppDtoPerson() : WsjcppDefineDto("person", "Person") {
10+
// requireField<WsjcppDtoString>("name", "Name of person");
11+
requireField<WsjcppDtoString>("name", "Name of person");
12+
optionalField<WsjcppDtoInteger>("age", "Age of person");
13+
};
14+
};
15+
16+
class WsjcppDtoUserProfile : public WsjcppDefineDto {
17+
public:
18+
WsjcppDtoUserProfile() : WsjcppDefineDto("user_profile", "User Profile") {
19+
// requireField<WsjcppDtoString>("name", "Name of person");
20+
requireField<WsjcppDtoInteger>("userid", "User Id");
21+
requireField<WsjcppDtoString>("", "Current Page");
22+
// requireField<WsjcppDtoPerson>("person", "Total");
23+
// TODO array
24+
// requireField<WsjcppDtoString>("name", "Name of person");
25+
// optionalField<WsjcppDtoPerson>("age", "Age of person");
26+
};
27+
};
28+
29+
class WsjcppDtoPersonsPage : public WsjcppDefineDto {
30+
public:
31+
WsjcppDtoPersonsPage() : WsjcppDefineDto("persons_page", "Persons page") {
32+
// requireField<WsjcppDtoString>("name", "Name of person");
33+
requireField<WsjcppDtoInteger>("page", "Current Page");
34+
requireField<WsjcppDtoInteger>("onpage", "Nomebr per page");
35+
requireField<WsjcppDtoInteger>("total", "Total");
36+
// TODO array
37+
// requireField<WsjcppDtoString>("name", "Name of person");
38+
// optionalField<WsjcppDtoPerson>("age", "Age of person");
39+
};
40+
};
541

642
int main(int argc, const char* argv[]) {
743
std::string TAG = "MAIN";
@@ -13,6 +49,11 @@ int main(int argc, const char* argv[]) {
1349
WsjcppLog::setPrefixLogFile("wsjcpp");
1450
WsjcppLog::setLogDirectory(".logs");
1551
// TODO your code here
52+
53+
WsjcppDtoPerson p;
54+
p.setFieldStringValue("name", "value");
55+
p.setFieldIntegerValue("age", 1);
56+
std::cout << p.toJson().dump() << std::endl;
1657
return 0;
1758
}
1859

src/wsjcpp_dto.cpp

Lines changed: 162 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,181 @@
11

22
#include "wsjcpp_dto.h"
33

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;
656
}
757

858
// ---------------------------------------------------------------------
59+
// WsjcppDtoString
60+
61+
WsjcppDtoString::WsjcppDtoString(const std::string &sName, const std::string &sDescription)
62+
: WsjcppDefineFieldDto("string", sName, sDescription) {
963

10-
std::string WsjcppDto::getObjectTypeName() {
11-
return m_sObjectTypeName;
1264
}
1365

1466
// ---------------------------------------------------------------------
67+
// WsjcppDtoBoolean
68+
69+
WsjcppDtoBoolean::WsjcppDtoBoolean(const std::string &sName, const std::string &sDescription)
70+
: WsjcppDefineFieldDto("boolean", sName, sDescription) {
1571

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) {
17160
return false;
18161
}
19162

20163
// ---------------------------------------------------------------------
21164

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;
25178
}
26179

180+
27181
// ---------------------------------------------------------------------

src/wsjcpp_dto.h

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,124 @@
22
#define WSJCPP_DTO_H
33

44
#include <string>
5+
#include <vector>
56
#include <json.hpp>
67

7-
/*!
8-
* WsjcppDto - parent class for Data Transfer Objects
9-
* */
8+
// ---------------------------------------------------------------------
9+
// WsjcppDefineFieldDto
10+
11+
class WsjcppDefineFieldDto {
12+
public:
13+
WsjcppDefineFieldDto(
14+
const std::string &sFieldTypeName,
15+
const std::string &sFieldName,
16+
const std::string &sFieldDescription
17+
);
18+
WsjcppDefineFieldDto();
19+
WsjcppDefineFieldDto & optional();
20+
WsjcppDefineFieldDto & required();
21+
22+
const std::string &getFieldTypeName() const;
23+
const std::string &getFieldName() const;
24+
const std::string &getFieldRestrict() const;
25+
const std::string &getFieldDescription() const;
26+
27+
bool isRequired();
28+
29+
// WsjcppMemberDto &addValidator(WsjcppValidatorStringBase *pValidatorStringBase);
30+
// const std::vector<WsjcppValidatorStringBase *> &listOfValidators();
31+
32+
private:
33+
std::string m_sFieldTypeName;
34+
std::string m_sFieldName;
35+
std::string m_sFieldRestrict;
36+
std::string m_sFieldDescription;
37+
38+
// std::vector<WsjcppValidatorStringBase *> m_vValidatorsString;
39+
};
40+
41+
// ---------------------------------------------------------------------
42+
// WsjcppDtoString
43+
44+
class WsjcppDtoString : public WsjcppDefineFieldDto {
45+
public:
46+
WsjcppDtoString(const std::string &sName, const std::string &sDescription);
47+
};
48+
49+
// ---------------------------------------------------------------------
50+
// WsjcppDtoInteger
51+
52+
class WsjcppDtoInteger : public WsjcppDefineFieldDto {
53+
public:
54+
WsjcppDtoInteger(const std::string &sName, const std::string &sDescription);
55+
};
56+
57+
// ---------------------------------------------------------------------
58+
// WsjcppDtoBoolean
59+
60+
class WsjcppDtoBoolean : public WsjcppDefineFieldDto {
61+
public:
62+
WsjcppDtoBoolean(const std::string &sName, const std::string &sDescription);
63+
};
64+
65+
// ---------------------------------------------------------------------
66+
// WsjcppDefineDto - parent class for Data Transfer Objects
1067

11-
class WsjcppDto {
68+
class WsjcppDefineDto {
1269
public:
13-
WsjcppDto(const std::string &sObjectTypeName);
14-
std::string getObjectTypeName();
70+
WsjcppDefineDto(const std::string &sTypeName, const std::string &sDescription);
71+
const std::string &getTypeName();
72+
const std::string &getDescription();
73+
74+
template<typename T>
75+
T &requireField(const std::string &sName, const std::string &sDescription) {
76+
T *pField = new T(sName, sDescription);
77+
pField->required();
78+
m_vFields.push_back(pField);
79+
WsjcppDefineFieldDto *pAddedField = m_vFields[m_vFields.size()-1];
80+
return *((T *)pAddedField);
81+
}
82+
83+
template<typename T>
84+
T &optionalField(const std::string &sName, const std::string &sDescription) {
85+
T *pField = new T(sName, sDescription);
86+
pField->optional();
87+
m_vFields.push_back(pField);
88+
WsjcppDefineFieldDto *pAddedField = m_vFields[m_vFields.size()-1];
89+
return *((T *)pAddedField);
90+
}
91+
92+
// TODO check duplicates
93+
/*CmdInputDef pStringDef(sName, sDescription);
94+
pStringDef.string_().required();
95+
m_vInputs.push_back(pStringDef);
96+
return m_vInputs[m_vInputs.size()-1];*/
97+
98+
/*WsjcppMemberDto &requireStringMember(const std::string &sName, const std::string &sDescription);
99+
WsjcppMemberDto &optionalStringMember(const std::string &sName, const std::string &sDescription);
100+
WsjcppMemberDto &requireIntegerMember(const std::string &sName, const std::string &sDescription);
101+
WsjcppMemberDto &optionalIntegerMember(const std::string &sName, const std::string &sDescription);
102+
WsjcppMemberDto &requireBooleanMember(const std::string &sName, const std::string &sDescription);
103+
WsjcppMemberDto &optionalBooleanMember(const std::string &sName, const std::string &sDescription);
104+
*/
105+
bool setFieldStringValue(const std::string &sFieldName, const std::string &sValue);
106+
bool setFieldIntegerValue(const std::string &sFieldName, int nValue);
107+
bool setFieldBooleanValue(const std::string &sFieldName, bool bValue);
108+
bool setFieldJsonValue(const std::string &sFieldName, const nlohmann::json &jsonValue);
109+
15110
bool fillFromJson(nlohmann::json &jsonData, std::string &sError);
16111
nlohmann::json toJson();
17112

18113
private:
19-
std::string m_sObjectTypeName;
114+
std::string TAG;
115+
std::string m_sTypeName;
116+
std::string m_sDescription;
117+
118+
119+
std::vector<WsjcppDefineFieldDto *> m_vFields;
120+
nlohmann::json m_jsonReadyObject;
121+
122+
WsjcppDefineFieldDto *findFieldByName(const std::string &sFieldName);
20123
};
21124

22125
#endif // WSJCPP_DTO_H

0 commit comments

Comments
 (0)