author | František Kučera <franta-hg@frantovo.cz> |
Sat, 05 Dec 2020 19:48:52 +0100 | |
branch | v_0 |
changeset 6 | 67dc7aaa5ed7 |
parent 5 | 399380ea9750 |
child 7 | 1b6deb1c8720 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
0 | 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 <cstdlib> |
|
20 |
#include <iostream> |
|
21 |
#include <memory> |
|
22 |
#include <vector> |
|
23 |
||
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
#include <yaml.h> |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
25 |
|
0 | 26 |
#include <relpipe/writer/RelationalWriter.h> |
27 |
#include <relpipe/writer/typedefs.h> |
|
28 |
||
29 |
namespace relpipe { |
|
30 |
namespace in { |
|
31 |
namespace yaml { |
|
32 |
||
33 |
class YAMLCommand { |
|
34 |
private: |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
35 |
using string_t = relpipe::writer::string_t; |
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
36 |
using RelpipeWriterException = relpipe::writer::RelpipeWriterException; |
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
37 |
|
2
d68192f0e960
convert YAML to relations
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
38 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // YAML strings are in UTF-8 |
0 | 39 |
|
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
class YAMLEvent { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
private: |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
yaml_event_t event; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
public: |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
YAMLEvent(yaml_event_t event) : event(event) { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
virtual ~YAMLEvent() { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
yaml_event_delete(&event); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
const yaml_event_type_t getType() const { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
return event.type; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
56 |
const string_t getTypeName() const { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
return findTypeName(event.type); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
/** Just for debugging and error handling */ |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
61 |
static const string_t findTypeName(const yaml_event_type_t eventType) { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
if (eventType == YAML_NO_EVENT) return L"NO"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
else if (eventType == YAML_STREAM_START_EVENT) return L"STREAM_START"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
else if (eventType == YAML_STREAM_END_EVENT) return L"STREAM_END"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
else if (eventType == YAML_DOCUMENT_START_EVENT) return L"DOCUMENT_START"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
else if (eventType == YAML_DOCUMENT_END_EVENT) return L"DOCUMENT_END"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
else if (eventType == YAML_ALIAS_EVENT) return L"ALIAS"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
else if (eventType == YAML_SCALAR_EVENT) return L"SCALAR"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
else if (eventType == YAML_SEQUENCE_START_EVENT) return L"SEQUENCE_START"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
else if (eventType == YAML_SEQUENCE_END_EVENT) return L"SEQUENCE_END"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
else if (eventType == YAML_MAPPING_START_EVENT) return L"MAPPING_START"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
else if (eventType == YAML_MAPPING_END_EVENT) return L"MAPPING_END"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
else return L"UNKNOWN"; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
74 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
|
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
const yaml_event_t* getEvent() const { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
return &event; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
YAMLEvent(const YAMLEvent&) = delete; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
YAMLEvent& operator=(const YAMLEvent&) = delete; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
}; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
84 |
class YAMLParser { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
private: |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
yaml_parser_t yamlParser; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
std::istream* input = nullptr; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
static int readFromInput(void* instance, unsigned char* buffer, size_t size, size_t* length) { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
std::istream* input = ((YAMLParser*) instance)->input; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
input->read((char*) buffer, size); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
*length = input->gcount(); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
return (input->good() || input->eof()) ? 1 : 0; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
public: |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
YAMLParser() { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
yaml_parser_initialize(&yamlParser); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
101 |
virtual ~YAMLParser() { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
102 |
yaml_parser_delete(&yamlParser); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
103 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
104 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
105 |
YAMLParser(const YAMLParser&) = delete; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
106 |
YAMLParser& operator=(const YAMLParser&) = delete; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
107 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
108 |
void setInput(std::istream* input) { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
109 |
this->input = input; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
110 |
yaml_parser_set_input(&yamlParser, readFromInput, (void*) this); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
111 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
112 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
113 |
YAMLEvent* next() { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
114 |
yaml_event_t event; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
115 |
int result = yaml_parser_parse(&yamlParser, &event); |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
116 |
return result == 1 && event.type != yaml_event_type_e::YAML_NO_EVENT ? new YAMLEvent(event) : nullptr; // 1 = OK in yaml.h; YAML_NO_EVENT = end |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
117 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
118 |
}; |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
119 |
|
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
120 |
using YAMLEvent_p = std::shared_ptr<YAMLEvent>; |
4 | 121 |
|
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
122 |
YAMLParser parser; |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
std::shared_ptr<relpipe::writer::RelationalWriter> writer; |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
124 |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
125 |
string_t relationName; |
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
126 |
std::vector<string_t> record; |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
std::vector<relpipe::writer::AttributeMetadata> attributesMetadata; |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
128 |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
129 |
string_t y2s(const yaml_char_t* value) { |
2
d68192f0e960
convert YAML to relations
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
130 |
return value ? convertor.from_bytes((const char*) value) : L""; |
d68192f0e960
convert YAML to relations
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
131 |
} |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
132 |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
133 |
string_t fetchScalarValue(YAMLEvent_p event) { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
134 |
if (event->getType() == YAML_SCALAR_EVENT) return y2s(event->getEvent()->data.scalar.value); |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
135 |
else throw RelpipeWriterException(L"Invalid YAML structure: expected SCALAR but got: " + event->getTypeName()); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
136 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
137 |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
138 |
void consumeEvent(const yaml_event_type_t expectedEventType, string_t expectedScalarValue = L"") { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
139 |
YAMLEvent_p event = YAMLEvent_p(parser.next()); |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
140 |
if (!event) throw RelpipeWriterException(L"Invalid YAML structure: missing event: " + YAMLEvent::findTypeName(expectedEventType)); |
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
141 |
if (event->getType() != expectedEventType) throw RelpipeWriterException(L"Invalid YAML structure: expected event: " + YAMLEvent::findTypeName(expectedEventType) + L", but got: " + event->getTypeName()); |
6
67dc7aaa5ed7
rather fail-fast than hide mistakes (might be e.g. a typo in the source YAML file)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
142 |
if (expectedEventType == YAML_SCALAR_EVENT && expectedScalarValue.size() && expectedScalarValue != fetchScalarValue(event)) throw RelpipeWriterException(L"Invalid YAML structure: expected scalar value: „" + expectedScalarValue + L"“, but got „" + fetchScalarValue(event) + L"“"); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
144 |
|
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
145 |
string_t consumeScalarEvent() { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
146 |
YAMLEvent_p event = YAMLEvent_p(parser.next()); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
147 |
if (event && event->getType() == YAML_SCALAR_EVENT) return fetchScalarValue(event); |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
148 |
else throw RelpipeWriterException(L"Invalid YAML structure: expected SCALAR, but got: " + event->getTypeName()); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
149 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
150 |
|
4 | 151 |
bool until(const yaml_event_type_t until, YAMLEvent_p& event) { |
152 |
event = YAMLEvent_p(parser.next()); |
|
153 |
return event && event->getType() != until; |
|
154 |
} |
|
155 |
||
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
156 |
void processRelation() { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
157 |
YAMLEvent_p event = YAMLEvent_p(parser.next()); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
158 |
if (event->getType() == YAML_MAPPING_START_EVENT) processRelationWithMetadata(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
159 |
else if (event->getType() == YAML_SEQUENCE_START_EVENT)processRelationWithoutMetadata(); |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
160 |
else throw RelpipeWriterException(L"Invalid YAML structure: expected MAPPING or SEQUENCE, but got: " + event->getTypeName()); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
161 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
162 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
163 |
void processRelationWithMetadata() { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
164 |
consumeEvent(YAML_SCALAR_EVENT, L"attribute-metadata"); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
165 |
consumeEvent(YAML_SEQUENCE_START_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
166 |
|
4 | 167 |
for (YAMLEvent_p event; until(YAML_SEQUENCE_END_EVENT, event);) { |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
168 |
if (event->getType() != YAML_MAPPING_START_EVENT) throw RelpipeWriterException(L"Invalid YAML structure: expected MAPPING (attribute-metadata), but got: " + event->getTypeName()); |
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
169 |
string_t name; |
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
170 |
string_t type = L"string"; |
4 | 171 |
for (YAMLEvent_p event; until(YAML_MAPPING_END_EVENT, event);) { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
172 |
auto key = fetchScalarValue(event); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
173 |
auto value = consumeScalarEvent(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
174 |
if (key == L"name") name = value; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
175 |
else if (key == L"type") type = value; |
6
67dc7aaa5ed7
rather fail-fast than hide mistakes (might be e.g. a typo in the source YAML file)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
176 |
else throw RelpipeWriterException(L"Invalid YAML structure: expected „name“ or „type“ but got: " + key); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
177 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
178 |
attributesMetadata.push_back({name, writer->toTypeId(type)}); |
2
d68192f0e960
convert YAML to relations
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
179 |
} |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
180 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
181 |
writer->startRelation(relationName, attributesMetadata, true); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
182 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
183 |
YAMLEvent_p event = YAMLEvent_p(parser.next()); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
184 |
if (event->getType() == YAML_SCALAR_EVENT && fetchScalarValue(event) == L"record") { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
185 |
consumeEvent(YAML_SEQUENCE_START_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
186 |
processRecords(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
187 |
consumeEvent(YAML_MAPPING_END_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
188 |
} else if (event->getType() == YAML_MAPPING_END_EVENT) { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
189 |
// empty relation, no records |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
190 |
} else { |
6
67dc7aaa5ed7
rather fail-fast than hide mistakes (might be e.g. a typo in the source YAML file)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
191 |
RelpipeWriterException(L"Invalid YAML structure: expected „record“ or MAPPING_END, but got: " + event->getTypeName()); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
192 |
} |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
193 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
194 |
|
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
195 |
void processRelationWithoutMetadata() { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
196 |
// First record: |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
197 |
consumeEvent(YAML_MAPPING_START_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
198 |
record.clear(); |
4 | 199 |
for (YAMLEvent_p event; until(YAML_MAPPING_END_EVENT, event);) { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
200 |
auto name = fetchScalarValue(event); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
201 |
auto value = consumeScalarEvent(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
202 |
attributesMetadata.push_back({name, relpipe::writer::TypeId::STRING}); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
203 |
record.push_back(value); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
204 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
205 |
writer->startRelation(relationName, attributesMetadata, true); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
206 |
for (auto value : record) writer->writeAttribute(value); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
207 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
208 |
// Following records: |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
209 |
processRecords(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
210 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
211 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
212 |
void processRecords() { |
4 | 213 |
for (YAMLEvent_p event; until(YAML_SEQUENCE_END_EVENT, event);) { |
5
399380ea9750
simplify types (namespaces)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
214 |
if (event->getType() != YAML_MAPPING_START_EVENT) throw RelpipeWriterException(L"Invalid YAML structure: expected MAPPING (record), but got: " + event->getTypeName()); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
215 |
record.clear(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
216 |
record.resize(attributesMetadata.size()); |
4 | 217 |
for (YAMLEvent_p event; until(YAML_MAPPING_END_EVENT, event);) { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
218 |
auto name = fetchScalarValue(event); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
219 |
auto value = consumeScalarEvent(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
220 |
for (int i = 0; i < attributesMetadata.size(); i++) { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
221 |
if (name == attributesMetadata[i].attributeName) { |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
222 |
record[i] = value; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
223 |
break; |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
224 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
225 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
226 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
227 |
for (auto value : record) writer->writeAttribute(value); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
228 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
229 |
} |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
230 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
231 |
|
0 | 232 |
public: |
233 |
||
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
234 |
YAMLCommand() { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
235 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
236 |
|
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
237 |
virtual ~YAMLCommand() { |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
238 |
} |
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
239 |
|
0 | 240 |
void process(std::istream& input, std::shared_ptr<relpipe::writer::RelationalWriter> writer) { |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
241 |
parser.setInput(&input); |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
242 |
this->writer = writer; |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
243 |
|
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
244 |
consumeEvent(YAML_STREAM_START_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
245 |
consumeEvent(YAML_DOCUMENT_START_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
246 |
consumeEvent(YAML_MAPPING_START_EVENT); |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
247 |
|
4 | 248 |
for (YAMLEvent_p event; until(YAML_MAPPING_END_EVENT, event);) { |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
249 |
relationName = fetchScalarValue(event); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
250 |
attributesMetadata.clear(); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
251 |
processRelation(); |
1
8d2d8f4077af
OOP wrapper for yaml.h + list event types
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
252 |
} |
3
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
253 |
|
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
254 |
consumeEvent(YAML_DOCUMENT_END_EVENT); |
64f8f8792470
support also attribute metadata (i.e. also different attribute types than mere strings) and empty relations (no records)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
255 |
consumeEvent(YAML_STREAM_END_EVENT); |
0 | 256 |
} |
257 |
}; |
|
258 |
||
259 |
} |
|
260 |
} |
|
261 |
} |