Skip to content

Commit 3e54cae

Browse files
committed
Fixed #7 Added new methods get*Value
1 parent 2444e17 commit 3e54cae

12 files changed

+119
-60
lines changed

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ int main(int argc, char* argv[]) {
1616
}
1717
WsjcppYaml yaml;
1818
std::string sError;
19-
if (!yaml.loadFromFile("./wsjcpp.yml", sError)) {
19+
std::string sFilePath = "./unit-tests.wsjcpp/data-tests/read-file/example-voiting-app/docker-compose.yml";
20+
if (!yaml.loadFromFile(sFilePath, sError)) {
2021
WsjcppLog::err(TAG, "Could not read data from file: " + sError);
2122
return -1;
2223
}
2324

24-
if (!yaml.saveToFile("./wsjcpp.yml")) {
25+
if (!yaml.saveToFile(sFilePath)) {
2526
WsjcppLog::err(TAG, "Could not save data to file");
2627
return -1;
2728
}

src/wsjcpp_yaml.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include "wsjcpp_yaml.h"
33
#include <wsjcpp_core.h>
4+
#include <cstdlib>
45

56
// ---------------------------------------------------------------------
67
// WsjcppYamlPlaceInFile
@@ -497,6 +498,7 @@ bool WsjcppYamlItem::isValue() {
497498
// ---------------------------------------------------------------------
498499

499500
std::string WsjcppYamlItem::getValue() {
501+
WsjcppLog::warn(TAG, "getValue is deprecated try getStringValue, getBoolValue, getLongValue, getIntValue...");
500502
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
501503
WsjcppLog::throw_err(TAG, "getValue, Element must be value for " + this->getForLogFormat());
502504
}
@@ -505,6 +507,55 @@ std::string WsjcppYamlItem::getValue() {
505507

506508
// ---------------------------------------------------------------------
507509

510+
std::string WsjcppYamlItem::getStringValue() {
511+
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
512+
WsjcppLog::throw_err(TAG, "getStringValue, Element must be value for " + this->getForLogFormat());
513+
}
514+
return m_sValue;
515+
}
516+
517+
// ---------------------------------------------------------------------
518+
519+
bool WsjcppYamlItem::getBoolValue() {
520+
std::string sValue = getStringValue();
521+
sValue = WsjcppCore::toLower(sValue);
522+
if (sValue == "yes" || sValue == "true") {
523+
return true;
524+
} else if (sValue == "no" || sValue == "false") {
525+
return false;
526+
} else {
527+
WsjcppLog::throw_err(TAG, "getBoolValue, Element must be bool expected with ignore case like"
528+
"['yes','no','true','false'] for " + this->getForLogFormat());
529+
}
530+
return false;
531+
}
532+
533+
// ---------------------------------------------------------------------
534+
535+
long WsjcppYamlItem::getLongValue() {
536+
std::string sValue = getStringValue();
537+
sValue = WsjcppCore::toLower(sValue);
538+
long nValue = std::atol(sValue.c_str());
539+
if (std::to_string(nValue) != sValue) {
540+
WsjcppLog::throw_err(TAG, "getLongValue, Element must be long or int but have a string" + this->getForLogFormat());
541+
}
542+
return nValue;
543+
}
544+
545+
// ---------------------------------------------------------------------
546+
547+
int WsjcppYamlItem::getIntValue() {
548+
std::string sValue = getStringValue();
549+
sValue = WsjcppCore::toLower(sValue);
550+
int nValue = std::atoi(sValue.c_str());
551+
if (std::to_string(nValue) != sValue) {
552+
WsjcppLog::throw_err(TAG, "getLongValue, Element must be int but have a string" + this->getForLogFormat());
553+
}
554+
return nValue;
555+
}
556+
557+
// ---------------------------------------------------------------------
558+
508559
void WsjcppYamlItem::setValue(const std::string &sValue, WsjcppYamlQuotes nQuotes) {
509560
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
510561
WsjcppLog::throw_err(TAG, "setValue, Element must be value for " + this->getForLogFormat());

src/wsjcpp_yaml.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ class WsjcppYamlItem { // TODO: rename to node
129129
bool removeElement(int i);
130130

131131
bool isValue();
132-
std::string getValue();
132+
133+
std::string getValue(); // deprecated
134+
std::string getStringValue(); // simular 'getValue'
135+
bool getBoolValue();
136+
long getLongValue();
137+
int getIntValue();
138+
133139
void setValue(const std::string &sValue, WsjcppYamlQuotes nQuotes = WSJCPP_YAML_QUOTES_NONE);
134140
WsjcppYamlQuotes getValueQuotes();
135141

unit-tests.wsjcpp/src/unit_test_read_yaml.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ void UnitTestReadYaml::executeTest() {
4343
compare("has networks", yaml.getRoot()->hasElement("networks"), true);
4444
compare("has services", yaml.getRoot()->hasElement("services"), true);
4545

46-
compare("version-value", yaml.getRoot()->getElement("version")->getValue(), "3");
46+
compare("version-value", yaml.getRoot()->getElement("version")->getStringValue(), "3");
47+
compare("version-value", yaml.getRoot()->getElement("version")->getIntValue(), 3); // wrong or not ?
4748

4849
// TODO unit test source code here
4950
}

unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ void UnitTestRemoveElementInArray::executeTest() {
5252

5353
WsjcppYamlItem *pArr1 = yaml.getRoot()->getElement("arr1");
5454
compare("arr1 len", pArr1->getLength(), 3);
55-
compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getValue(), "i1");
56-
compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getValue(), "i2");
57-
compare("arr1 name2 ", pArr1->getElement(2)->getElement("name")->getValue(), "i3");
55+
compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getStringValue(), "i1");
56+
compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getStringValue(), "i2");
57+
compare("arr1 name2 ", pArr1->getElement(2)->getElement("name")->getStringValue(), "i3");
5858

5959
pArr1->removeElement(1);
6060

6161
compare("arr1 len", pArr1->getLength(), 2);
62-
compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getValue(), "i1");
63-
compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getValue(), "i3");
62+
compare("arr1 name0 ", pArr1->getElement(0)->getElement("name")->getStringValue(), "i1");
63+
compare("arr1 name1 ", pArr1->getElement(1)->getElement("name")->getStringValue(), "i3");
6464
}
6565

6666
// ---------------------------------------------------------------------

unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ void UnitTestYamlParserAll::executeTest() {
7272
}
7373

7474
WsjcppYamlItem *pItem = nullptr;
75-
compare("test10", yaml.getRoot()->getElement("test10")->getValue(), "one");
76-
compare("test20", yaml.getRoot()->getElement("test20")->getValue(), "two");
75+
compare("test10", yaml.getRoot()->getElement("test10")->getStringValue(), "one");
76+
compare("test20", yaml.getRoot()->getElement("test20")->getStringValue(), "two");
7777

7878
pItem = yaml.getRoot()->getElement("array30");
7979
compare("array30_length", pItem->getLength(), 3);
8080

8181
pItem = yaml.getRoot()->getElement("array30")->getElement(0);
82-
compare("test30_value", pItem->getValue(), "one31");
82+
compare("test30_value", pItem->getStringValue(), "one31");
8383
compare("test30_comment", pItem->getComment(), "this field for test array30");
8484
pItem = yaml.getRoot()->getElement("array30")->getElement(1);
85-
compare("test40_value", pItem->getValue(), "two32");
85+
compare("test40_value", pItem->getStringValue(), "two32");
8686
compare("test40_comment", pItem->getComment(), "");
8787

8888
pItem = yaml.getRoot()->getElement("array40");
@@ -92,10 +92,10 @@ void UnitTestYamlParserAll::executeTest() {
9292
compare("array50_length", pItem->getLength(), 2);
9393

9494
pItem = yaml.getRoot()->getElement("map60")->getElement("test70");
95-
compare("test70_value", pItem->getValue(), "opa1");
95+
compare("test70_value", pItem->getStringValue(), "opa1");
9696

9797
pItem = yaml.getRoot()->getElement("map60")->getElement("test80");
98-
compare("test80_comment", pItem->getValue(), "opa2");
98+
compare("test80_comment", pItem->getStringValue(), "opa2");
9999

100100
std::string sSaved2 = "";
101101
if (compare("saving yaml", yaml.saveToString(sSaved2), true)) {

unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void UnitTestYamlParserArrayIncludedMap::executeTest() {
5757

5858
WsjcppYamlItem *pItem = nullptr;
5959

60-
compare("param1-value", yaml.getRoot()->getElement("param1")->getValue(), "none value1");
60+
compare("param1-value", yaml.getRoot()->getElement("param1")->getStringValue(), "none value1");
6161
compare("param1-line", yaml.getRoot()->getElement("param1")->getPlaceInFile().getLine(), "param1: none value1 # it's value for something # olala ");
6262
compare("param1-original-number-of-line", yaml.getRoot()->getElement("param1")->getPlaceInFile().getNumberOfLine(), 1);
6363
compare("param1-comment", yaml.getRoot()->getElement("param1")->getComment(), "it's value for something # olala");
@@ -66,28 +66,28 @@ void UnitTestYamlParserArrayIncludedMap::executeTest() {
6666
compare("array-test2-comment", yaml.getRoot()->getElement("array-test2")->getComment(), "some comment 2");
6767

6868
pItem = yaml.getRoot()->getElement("array-test2")->getElement(0);
69-
compare("array-test2-element0-value", pItem->getValue(), "value21");
69+
compare("array-test2-element0-value", pItem->getStringValue(), "value21");
7070
compare("array-test2-element0-comment", pItem->getComment(), "comment v21");
7171

7272
pItem = yaml.getRoot()->getElement("array-test2")->getElement(1);
73-
compare("array-test2-element1-value", yaml["array-test2"][1].getValue(), "value22");
73+
compare("array-test2-element1-value", yaml["array-test2"][1].getStringValue(), "value22");
7474
compare("array-test2-element1-comment", yaml["array-test2"][1].getComment(), "comment v22");
7575

7676
pItem = yaml.getRoot()->getElement("array-test2")->getElement(2);
77-
compare("array-test2-element2-value", pItem->getValue(), "true");
77+
compare("array-test2-element2-value", pItem->getStringValue(), "true");
7878
compare("array-test2-element2-comment", pItem->getComment(), "comment true");
7979

8080
compare("array-and-map-length", yaml["array-and-map"].getLength(), 2);
8181

8282
pItem = yaml.getRoot()->getElement("array-and-map")->getElement(0);
83-
compare("array-and-map-element0-value", pItem->getElement("submap-param1")->getValue(), "v01");
84-
compare("array-and-map-element0-value", pItem->getElement("submap-param2")->getValue(), "v02");
83+
compare("array-and-map-element0-value", pItem->getElement("submap-param1")->getStringValue(), "v01");
84+
compare("array-and-map-element0-value", pItem->getElement("submap-param2")->getStringValue(), "v02");
8585

8686
pItem = yaml.getRoot()->getElement("array-and-map")->getElement(1);
87-
compare("array-and-map-element1-value", pItem->getElement("submap-param1")->getValue(), "v11");
88-
compare("array-and-map-element1-value", pItem->getElement("submap-param2")->getValue(), "v12");
87+
compare("array-and-map-element1-value", pItem->getElement("submap-param1")->getStringValue(), "v11");
88+
compare("array-and-map-element1-value", pItem->getElement("submap-param2")->getStringValue(), "v12");
8989

90-
compare("param2-value", yaml.getRoot()->getElement("param2")->getValue(), "v2");
90+
compare("param2-value", yaml.getRoot()->getElement("param2")->getStringValue(), "v2");
9191

9292
std::string sSaved = "";
9393
if (compare("save yaml", yaml.saveToString(sSaved), true)) {

unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,29 @@ void UnitTestYamlParserComments::executeTest() {
5959

6060
// TODO: .findLine(0)
6161

62-
compare("param1", yaml["param1"].getValue(), "value1");
62+
compare("param1", yaml["param1"].getStringValue(), "value1");
6363
compare("param1", yaml["param1"].getComment(), "comment 2 # comment");
6464

65-
compare("param2", yaml["param2"].getValue(), "value2");
65+
compare("param2", yaml["param2"].getStringValue(), "value2");
6666
compare("param2", yaml["param2"].getComment(), "some \"comment 3\"");
6767

6868

6969
compare("array1-comment", yaml["array1"].getComment(), "comment 5");
7070
compare("array1-length", yaml["array1"].getLength(), 2);
71-
compare("array1-element0-value", yaml["array1"][0].getValue(), "val1");
71+
compare("array1-element0-value", yaml["array1"][0].getStringValue(), "val1");
7272
compare("array1-element0-comment", yaml["array1"][0].getComment(), "comment 6");
7373

7474
// TODO: .findLine(7)
7575

76-
compare("array1-element1-value", yaml["array1"][1].getValue(), "val2");
76+
compare("array1-element1-value", yaml["array1"][1].getStringValue(), "val2");
7777
compare("array1-element1-comment", yaml["array1"][1].getComment(), "comment 8");
7878

7979
compare("map1-comment", yaml["map1"].getComment(), "comment 9");
8080
compare("map1-p1-comment", yaml["map1"]["p1"].getComment(), "comment 10");
8181
compare("map1-p2-comment", yaml["map1"]["p2"].getComment(), "comment 12");
8282

8383

84-
// compare("param2", yaml.getRoot()->getElement("param2")->getValue(), "value2");
84+
// compare("param2", yaml.getRoot()->getElement("param2")->getStringValue(), "value2");
8585
// compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2");
8686

8787
std::string sSaved = "";

unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@ void UnitTestYamlParserHierarchicalMap::executeTest() {
6565
WsjcppYamlItem *pItem = nullptr;
6666

6767
pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map111");
68-
compare("param1111", pItem->getElement("param1111")->getValue(), "v1111");
69-
compare("param1112", pItem->getElement("param1112")->getValue(), "v1112");
68+
compare("param1111", pItem->getElement("param1111")->getStringValue(), "v1111");
69+
compare("param1112", pItem->getElement("param1112")->getStringValue(), "v1112");
7070

7171
pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map112");
72-
compare("param1121", pItem->getElement("param1121")->getValue(), "v1121");
73-
compare("param1122", pItem->getElement("param1122")->getValue(), "v1122");
72+
compare("param1121", pItem->getElement("param1121")->getStringValue(), "v1121");
73+
compare("param1122", pItem->getElement("param1122")->getStringValue(), "v1122");
7474

7575
pItem = yaml.getRoot()->getElement("map1")->getElement("map11")->getElement("map113");
76-
compare("param1131", pItem->getElement("param1131")->getValue(), "v1131");
77-
compare("param1132", pItem->getElement("param1132")->getValue(), "v1132");
76+
compare("param1131", pItem->getElement("param1131")->getStringValue(), "v1131");
77+
compare("param1132", pItem->getElement("param1132")->getStringValue(), "v1132");
7878

7979
pItem = yaml.getRoot()->getElement("map1")->getElement("map12");
80-
compare("param121", pItem->getElement("param121")->getValue(), "v121");
81-
compare("param122", pItem->getElement("param122")->getValue(), "v122");
80+
compare("param121", pItem->getElement("param121")->getStringValue(), "v121");
81+
compare("param122", pItem->getElement("param122")->getStringValue(), "v122");
8282

8383
pItem = yaml.getRoot()->getElement("map1")->getElement("map12")->getElement("map123");
84-
compare("param1231", pItem->getElement("param1231")->getValue(), "v1231");
85-
compare("param1232", pItem->getElement("param1232")->getValue(), "v1232");
84+
compare("param1231", pItem->getElement("param1231")->getStringValue(), "v1231");
85+
compare("param1232", pItem->getElement("param1232")->getStringValue(), "v1232");
8686

87-
compare("param2", yaml.getRoot()->getElement("param2")->getValue(), "v2");
87+
compare("param2", yaml.getRoot()->getElement("param2")->getStringValue(), "v2");
8888
compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2");
8989

9090
std::string sSaved = "";

unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ void UnitTestYamlParserQuotes::executeTest() {
5757

5858
// TODO: .findLine(0)
5959

60-
compare("param1", yaml["param1"].getValue(), "value1");
60+
compare("param1", yaml["param1"].getStringValue(), "value1");
6161
compare("param1", yaml["param1"].getComment(), "v1");
6262

63-
compare("param2", yaml["param2"].getValue(), " #$!!!value2");
63+
compare("param2", yaml["param2"].getStringValue(), " #$!!!value2");
6464
compare("param2", yaml["param2"].getComment(), "val 2");
6565

66-
compare(" param3 olala", yaml[" param3 olala"].getValue(), "val 3");
66+
compare(" param3 olala", yaml[" param3 olala"].getStringValue(), "val 3");
6767
compare(" param3 olala", yaml[" param3 olala"].getComment(), "val 3***");
6868

69-
compare("param4 val", yaml["param4"].getValue(), " #$!!!value4");
70-
compare("param4 comment", yaml["param4"].getValue(), " #$!!!value4");
69+
compare("param4 val", yaml["param4"].getStringValue(), " #$!!!value4");
70+
compare("param4 comment", yaml["param4"].getStringValue(), " #$!!!value4");
7171

72-
compare("url-value", yaml["url"].getValue(), "https://github.com/wsjcpp/wsjcpp-yaml");
73-
compare("issues-value", yaml["issues"].getValue(), "https://github.com/wsjcpp/wsjcpp-yaml/issues");
74-
compare("empty-value", yaml["empty"].getValue(), "");
72+
compare("url-value", yaml["url"].getStringValue(), "https://github.com/wsjcpp/wsjcpp-yaml");
73+
compare("issues-value", yaml["issues"].getStringValue(), "https://github.com/wsjcpp/wsjcpp-yaml/issues");
74+
compare("empty-value", yaml["empty"].getStringValue(), "");
7575

76-
compare("array-element0-value", yaml["array"][0].getValue(), "https://github.com/wsjcpp/wsjcpp-core:v0.0.1");
76+
compare("array-element0-value", yaml["array"][0].getStringValue(), "https://github.com/wsjcpp/wsjcpp-core:v0.0.1");
7777

7878
std::string sSaved = "";
7979
compare("save yaml", yaml.saveToString(sSaved), true);

0 commit comments

Comments
 (0)