author | František Kučera <franta-hg@frantovo.cz> |
Sat, 27 Nov 2021 23:15:17 +0100 | |
branch | v_0 |
changeset 3 | 1e9a07d8a406 |
parent 2 | 6839c85a9621 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <iostream> |
|
23 |
#include <sstream> |
|
24 |
#include <locale> |
|
25 |
#include <codecvt> |
|
26 |
||
27 |
#include <relpipe/common/type/typedefs.h> |
|
28 |
#include <relpipe/reader/TypeId.h> |
|
29 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
30 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
31 |
||
32 |
namespace relpipe { |
|
33 |
namespace out { |
|
34 |
namespace yaml { |
|
35 |
||
36 |
class YAMLHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
37 |
private: |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
const char ESC = '\\'; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
const char Q = '"'; |
0 | 40 |
std::ostream& output; |
41 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // we generate YAML always in UTF-8 like XML? |
|
42 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes; |
|
43 |
size_t currentAttributeIndex = 0; |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
bool firstRecord; |
0 | 45 |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
std::string escape(const relpipe::common::type::StringX& value) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
std::stringstream result; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
result.put(Q); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
for (char ch : convertor.to_bytes(value)) { |
2
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
if (ch == ESC) result.put(ESC).put(ch); |
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
else if (ch == Q) result.put(ESC).put(ch); |
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
52 |
else if (ch == '\n') result.put(ESC).put('n'); |
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
else if (ch == '\r') result.put(ESC).put('r'); |
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
else result.put(ch); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
} |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
result.put(Q); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
return result.str(); |
0 | 58 |
} |
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
|
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
std::string indent(int level) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
std::stringstream result; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
for (int i = 0; i < level; i++) result << " "; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
return result.str(); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
} |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
|
0 | 66 |
public: |
67 |
||
68 |
YAMLHandler(std::ostream& output) : output(output) { |
|
69 |
} |
|
70 |
||
71 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
72 |
currentAttributes = attributes; |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
currentAttributeIndex = 0; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
output << escape(name); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
output << ":" << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
output << indent(1) << R"("attribute-metadata":)" << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
for (auto am : attributes) output << indent(2) << R"(- "name": )" << escape(am.getAttributeName()) << std::endl << indent(2) << R"( "type": )" << escape(am.getTypeName()) << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
firstRecord = true; |
0 | 79 |
} |
80 |
||
81 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
if (firstRecord) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
output << indent(1) << R"("record":)" << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
84 |
firstRecord = false; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
} |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
|
0 | 87 |
if (currentAttributeIndex % currentAttributes.size() == 0) { |
88 |
currentAttributeIndex = 0; |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
output << indent(2) << "- "; |
0 | 90 |
} else { |
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
output << indent(2) << " "; |
0 | 92 |
} |
93 |
||
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
output << escape(currentAttributes[currentAttributeIndex].getAttributeName()) << ": "; |
2
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
95 |
|
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
if (currentAttributes[currentAttributeIndex].getTypeId() == relpipe::reader::TypeId::INTEGER) output << convertor.to_bytes(value) << std::endl; |
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
else if (currentAttributes[currentAttributeIndex].getTypeId() == relpipe::reader::TypeId::BOOLEAN) output << convertor.to_bytes(value) << std::endl; |
6839c85a9621
do not quote nor escape integers and booleans,
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
else output << escape(value) << std::endl; |
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
|
0 | 100 |
currentAttributeIndex++; |
101 |
} |
|
102 |
||
103 |
void endOfPipe() { |
|
104 |
output.flush(); |
|
105 |
} |
|
106 |
||
107 |
}; |
|
108 |
||
109 |
} |
|
110 |
} |
|
111 |
} |