Skip to content

Commit db87a3b

Browse files
committed
Fixed empty lines
1 parent 3ca6b38 commit db87a3b

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -505,29 +505,43 @@ std::string WsjcppYamlItem::toString(std::string sIntent) {
505505
}
506506
sRet += "# " + m_sComment;
507507
}
508+
} else if (this->isUndefined()) {
509+
for (int i = 0; i < m_vObjects.size(); i++) {
510+
if (m_vObjects[i]->isEmpty()) {
511+
sRet += "\n";
512+
} else {
513+
WsjcppLog::warn(TAG, "Undefined element conatins something else");
514+
}
515+
// sRet += std::to_string(m_vObjects.size());
516+
}
517+
return sRet;
508518
} else if (this->isEmpty()) {
509519
if (m_sComment.length() > 0) {
510520
sRet += sIntent + "# " + m_sComment;
511521
}
522+
// sRet += "\n";
523+
return sRet;
512524
} else if (this->isArray()) {
513525
for (int i = 0; i < m_vObjects.size(); i++) {
514526
WsjcppYamlItem *pItem = m_vObjects[i];
515527
if (pItem->isEmpty()) {
516-
sRet += sIntent + pItem->toString();
528+
sRet += pItem->toString(sIntent) + "\n";
529+
// sRet += "\n";
517530
} else if (pItem->isMap()) {
518531
std::string s = pItem->toString(sIntent + " ");
519532
WsjcppCore::trim(s);
520533
sRet += sIntent + "- " + s;
534+
sRet += "\n";
521535
} else {
522536
sRet += sIntent + "- " + pItem->toString();
537+
sRet += "\n";
523538
}
524-
sRet += "\n";
525539
}
526540
} else if (this->isMap()) {
527541
for (int i = 0; i < m_vObjects.size(); i++) {
528542
WsjcppYamlItem *pItem = m_vObjects[i];
529543
if (pItem->isEmpty() ) {
530-
sRet += sIntent + pItem->toString();
544+
sRet += pItem->toString(sIntent);
531545
sRet += "\n";
532546
} else if (pItem->isArray() || pItem->isMap()) {
533547
if (pItem->getNameQuotes() == WSJCPP_YAML_QUOTES_DOUBLE) {
@@ -543,12 +557,18 @@ std::string WsjcppYamlItem::toString(std::string sIntent) {
543557
sRet += "\n";
544558
sRet += pItem->toString(sIntent + " ");
545559
} else {
560+
std::string sVal = pItem->toString();
561+
std::string sVal_ = sVal;
562+
sVal_ = WsjcppCore::trim(sVal_);
563+
if (sVal_.length() > 0) {
564+
sVal = " " + sVal;
565+
}
546566
if (pItem->getNameQuotes() == WSJCPP_YAML_QUOTES_DOUBLE) {
547-
sRet += sIntent + "\"" + pItem->getName() + "\": " + pItem->toString();
567+
sRet += sIntent + "\"" + pItem->getName() + "\":" + sVal;
548568
} else if (pItem->getNameQuotes() == WSJCPP_YAML_QUOTES_SINGLE) {
549-
sRet += sIntent + "\'" + pItem->getName() + "\': " + pItem->toString();
569+
sRet += sIntent + "\'" + pItem->getName() + "\':" + sVal;
550570
} else {
551-
sRet += sIntent + pItem->getName() + ": " + pItem->toString();
571+
sRet += sIntent + pItem->getName() + ":" + sVal;
552572
}
553573
sRet += "\n";
554574
}
@@ -1216,11 +1236,26 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
12161236
int nDiffIntent = nLineIntent - st.nIntent;
12171237

12181238
if (st.line.isEmptyLine()) {
1219-
WsjcppYamlItem *pItem = new WsjcppYamlItem(
1220-
st.pCurItem, st.placeInFile,
1221-
WsjcppYamlItemType::WSJCPP_YAML_ITEM_EMPTY
1222-
);
1223-
st.pCurItem->appendElement(pItem);
1239+
1240+
1241+
if (st.pCurItem != nullptr) {
1242+
1243+
if (st.pCurItem->isArray() || st.pCurItem->isMap() || st.pCurItem->isUndefined()) {
1244+
WsjcppYamlItem *pItem = new WsjcppYamlItem(
1245+
st.pCurItem, st.placeInFile,
1246+
WsjcppYamlItemType::WSJCPP_YAML_ITEM_EMPTY
1247+
);
1248+
st.pCurItem->appendElement(pItem);
1249+
} else if (st.pCurItem->getParent() != nullptr && (st.pCurItem->getParent()->isArray() || st.pCurItem->getParent()->isMap())) {
1250+
WsjcppYamlItem *pItem = new WsjcppYamlItem(
1251+
st.pCurItem->getParent(), st.placeInFile,
1252+
WsjcppYamlItemType::WSJCPP_YAML_ITEM_EMPTY
1253+
);
1254+
st.pCurItem->getParent()->appendElement(pItem);
1255+
} else {
1256+
WsjcppLog::throw_err(TAG, "Empty element can be added only to map or to array");
1257+
}
1258+
}
12241259
continue;
12251260
}
12261261

unit-tests.wsjcpp/data-tests/read-write-file/docker-compose.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
version: "3"
44

55
services:
6+
# service vote
67
vote:
78
build: ./vote
89
command: python app.py
@@ -14,14 +15,18 @@ services:
1415
- front-tier
1516
- back-tier
1617

18+
# service result
1719
result:
1820
build: ./result
1921
command: nodemon server.js
2022
volumes:
2123
- ./result:/app
2224
ports:
23-
- "5001:80"
25+
# ports forward
26+
- "5001:80" # some
27+
2428
- "5858:5858"
29+
2530
networks:
2631
- front-tier
2732
- back-tier

unit-tests.wsjcpp/parser_yaml.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import yaml
2+
3+
4+
a_yaml_file = open("./data-tests/read-write-file/docker-compose.yml")
5+
6+
parsed_yaml_file = yaml.load(a_yaml_file, Loader=yaml.FullLoader)
7+
8+
print(parsed_yaml_file)

unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void UnitTestYamlParserComments::executeTest() {
9494
"array1: # comment 5\n"
9595
" - val1 # comment 6\n"
9696
" # comment 7\n"
97-
" \n"
97+
"\n"
9898
" - val2 # comment 8\n"
9999
"map1: # comment 9\n"
100100
" p1: val 1 # comment 10\n"

0 commit comments

Comments
 (0)