Skip to content

Commit

Permalink
Prepare first version
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-kg committed May 24, 2020
1 parent 47eefe3 commit fcc18ee
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 32 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ addons:
# Build steps
script:
- ./build_simple.sh
- ./wsjcpp-dto
- cd unit-tests.wsjcpp
- ./build_simple.sh
- ./unit-tests
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ Helper classes for create C++ Data Transfer Object

## Integrate to your project

Include this files:

* src.wsjcpp/nlohmann_json/json.hpp
* src/wsjcpp_dto.h
* src/wsjcpp_dto.cpp

Or use a `wsjcpp`:
Use a `wsjcpp`:

```
$ wsjcpp install https://github.com/wsjcpp/wsjcpp-dto:master
```

## How to use:
Or include this files:

* src.wsjcpp/nlohmann_json/json.hpp
* src/wsjcpp_dto.h
* src/wsjcpp_dto.cpp

## How to use

```
class WsjcppDtoPerson : public WsjcppDefineDto {
class WsjcppDtoPerson : public WsjcppDto {
public:
WsjcppDtoPerson() : WsjcppDefineDto("person", "Person") {
// requireField<WsjcppDtoString>("name", "Name of person");
WsjcppDtoPerson() : WsjcppDto("person", "Person") {
requireField<WsjcppDtoString>("name", "Name of person");
optionalField<WsjcppDtoInteger>("age", "Age of person");
};
Expand Down
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include <wsjcpp_core.h>
#include <wsjcpp_dto.h>

class WsjcppDtoPerson : public WsjcppDefineDto {
class WsjcppDtoPerson : public WsjcppDto {
public:
WsjcppDtoPerson() : WsjcppDefineDto("person", "Person") {
WsjcppDtoPerson() : WsjcppDto("person", "Person") {
// requireField<WsjcppDtoString>("name", "Name of person");
requireField<WsjcppDtoString>("name", "Name of person");
optionalField<WsjcppDtoInteger>("age", "Age of person");
};
};

class WsjcppDtoUserProfile : public WsjcppDefineDto {
class WsjcppDtoUserProfile : public WsjcppDto {
public:
WsjcppDtoUserProfile() : WsjcppDefineDto("user_profile", "User Profile") {
WsjcppDtoUserProfile() : WsjcppDto("user_profile", "User Profile") {
// requireField<WsjcppDtoString>("name", "Name of person");
requireField<WsjcppDtoInteger>("userid", "User Id");
requireField<WsjcppDtoString>("", "Current Page");
Expand All @@ -26,9 +26,9 @@ class WsjcppDtoUserProfile : public WsjcppDefineDto {
};
};

class WsjcppDtoPersonsPage : public WsjcppDefineDto {
class WsjcppDtoPersonsPage : public WsjcppDto {
public:
WsjcppDtoPersonsPage() : WsjcppDefineDto("persons_page", "Persons page") {
WsjcppDtoPersonsPage() : WsjcppDto("persons_page", "Persons page") {
// requireField<WsjcppDtoString>("name", "Name of person");
requireField<WsjcppDtoInteger>("page", "Current Page");
requireField<WsjcppDtoInteger>("onpage", "Nomebr per page");
Expand Down
22 changes: 11 additions & 11 deletions src/wsjcpp_dto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,29 @@ WsjcppDtoInteger::WsjcppDtoInteger(const std::string &sName, const std::string &
}

// ---------------------------------------------------------------------
// WsjcppDefineDto
// WsjcppDto

WsjcppDefineDto::WsjcppDefineDto(const std::string &sTypeName, const std::string &sDescription) {
WsjcppDto::WsjcppDto(const std::string &sTypeName, const std::string &sDescription) {
TAG = "DTO-" + sTypeName;
m_sTypeName = sTypeName;
m_sDescription = sDescription;
}

// ---------------------------------------------------------------------

const std::string &WsjcppDefineDto::getTypeName() {
const std::string &WsjcppDto::getTypeName() {
return m_sTypeName;
}

// ---------------------------------------------------------------------

const std::string &WsjcppDefineDto::getDescription() {
const std::string &WsjcppDto::getDescription() {
return m_sDescription;
}

// ---------------------------------------------------------------------

bool WsjcppDefineDto::setFieldStringValue(const std::string &sFieldName, const std::string &sFieldValue) {
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 + "'");
Expand All @@ -117,7 +117,7 @@ bool WsjcppDefineDto::setFieldStringValue(const std::string &sFieldName, const s

// ---------------------------------------------------------------------

bool WsjcppDefineDto::setFieldIntegerValue(const std::string &sFieldName, int nValue) {
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 + "'");
Expand All @@ -130,7 +130,7 @@ bool WsjcppDefineDto::setFieldIntegerValue(const std::string &sFieldName, int nV

// ---------------------------------------------------------------------

bool WsjcppDefineDto::setFieldBooleanValue(const std::string &sFieldName, bool bValue) {
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 + "'");
Expand All @@ -143,7 +143,7 @@ bool WsjcppDefineDto::setFieldBooleanValue(const std::string &sFieldName, bool b

// ---------------------------------------------------------------------

bool WsjcppDefineDto::setFieldJsonValue(const std::string &sFieldName, const nlohmann::json &jsonFieldValue) {
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 + "'");
Expand All @@ -156,19 +156,19 @@ bool WsjcppDefineDto::setFieldJsonValue(const std::string &sFieldName, const nlo

// ---------------------------------------------------------------------

bool WsjcppDefineDto::fillFromJson(nlohmann::json &jsonData, std::string &sError) {
bool WsjcppDto::fillFromJson(nlohmann::json &jsonData, std::string &sError) {
return false;
}

// ---------------------------------------------------------------------

nlohmann::json WsjcppDefineDto::toJson() {
nlohmann::json WsjcppDto::toJson() {
return m_jsonReadyObject;
}

// ---------------------------------------------------------------------

WsjcppDefineFieldDto *WsjcppDefineDto::findFieldByName(const std::string &sFieldName) {
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];
Expand Down
7 changes: 3 additions & 4 deletions src/wsjcpp_dto.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class WsjcppDtoBoolean : public WsjcppDefineFieldDto {
};

// ---------------------------------------------------------------------
// WsjcppDefineDto - parent class for Data Transfer Objects
// WsjcppDto - parent class for Data Transfer Objects

class WsjcppDefineDto {
class WsjcppDto {
public:
WsjcppDefineDto(const std::string &sTypeName, const std::string &sDescription);
WsjcppDto(const std::string &sTypeName, const std::string &sDescription);
const std::string &getTypeName();
const std::string &getDescription();

Expand Down Expand Up @@ -115,7 +115,6 @@ class WsjcppDefineDto {
std::string m_sTypeName;
std::string m_sDescription;


std::vector<WsjcppDefineFieldDto *> m_vFields;
nlohmann::json m_jsonReadyObject;

Expand Down
4 changes: 4 additions & 0 deletions unit-tests.wsjcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ list (APPEND WSJCPP_SOURCES "../src.wsjcpp/nlohmann_json/json.hpp")

# wsjcpp-dto:v0.0.1
list (APPEND WSJCPP_INCLUDE_DIRS "../src")
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_dto.h")
list (APPEND WSJCPP_SOURCES "../src/wsjcpp_dto.cpp")

# unit-tests
list (APPEND WSJCPP_INCLUDE_DIRS "src")
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_dto_person.h")
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_dto_person.cpp")

include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)

Expand Down
40 changes: 40 additions & 0 deletions unit-tests.wsjcpp/src/unit_test_dto_person.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "unit_test_dto_person.h"
#include <vector>
#include <wsjcpp_core.h>
#include <wsjcpp_dto.h>

REGISTRY_WSJCPP_UNIT_TEST(UnitTestDtoPerson)

UnitTestDtoPerson::UnitTestDtoPerson()
: WsjcppUnitTestBase("UnitTestDtoPerson") {
}

// ---------------------------------------------------------------------

void UnitTestDtoPerson::init() {
// nothing
}

// ---------------------------------------------------------------------

class WsjcppDtoPerson : public WsjcppDto {
public:
WsjcppDtoPerson() : WsjcppDto("person", "Person") {
requireField<WsjcppDtoString>("name", "Name of person");
optionalField<WsjcppDtoInteger>("age", "Age of person");
};
};

// ---------------------------------------------------------------------

bool UnitTestDtoPerson::run() {
bool bTestSuccess = true;
WsjcppDtoPerson p;
p.setFieldStringValue("name", "John Smith");
p.setFieldIntegerValue("age", 21);
std::string sJsonGot = p.toJson().dump();
std::string sJsonExpected = "{\"age\":21,\"name\":\"John Smith\"}";
compareS(bTestSuccess, "WsjcppDtoPerson", sJsonGot, sJsonExpected);
return bTestSuccess;
}

15 changes: 15 additions & 0 deletions unit-tests.wsjcpp/src/unit_test_dto_person.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef UNIT_TEST_DTO_PERSON_H
#define UNIT_TEST_DTO_PERSON_H

#include <wsjcpp_unit_tests.h>

// Description: TODO
class UnitTestDtoPerson : public WsjcppUnitTestBase {
public:
UnitTestDtoPerson();
virtual void init();
virtual bool run();
};

#endif // UNIT_TEST_DTO_PERSON_H

17 changes: 17 additions & 0 deletions wsjcpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ version: "v0.0.1"
cmake_minimum_required: "3.0"
cmake_cxx_standard: "11"
description: "Helper classes for create C++ Data Transfer Object "

authors:
- name: "Evgenii Sopov"
email: "mrseakg@gmail.com"

origins:
- address: "https://sea-kg.com/wsjcpp-package-registry/"
type: "package-registry"

keywords:
- "c++"

dependencies:
- name: "wsjcpp-core"
version: "v0.1.5"
Expand All @@ -22,3 +26,16 @@ dependencies:
url: "https://github.com/nlohmann/json:develop"
origin: "https://github.com/"
installation-dir: "./src.wsjcpp/nlohmann_json"

unit-tests:
cases:
- name: "DtoPerson"
description: "Test DtoPerson"

distribution:
- source-file: "src/wsjcpp_dto.h"
target-file: "wsjcpp_dto.h"
type: "source-code"
- source-file: "src/wsjcpp_dto.cpp"
target-file: "wsjcpp_dto.cpp"
type: "source-code"

0 comments on commit fcc18ee

Please sign in to comment.