-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathYamlReader.hpp
More file actions
326 lines (281 loc) · 9.06 KB
/
Copy pathYamlReader.hpp
File metadata and controls
326 lines (281 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
// SPDX-FileCopyrightText: 2025 Vector Informatik GmbH
//
// SPDX-License-Identifier: MIT
#include <string>
#include <memory>
#include <optional>
#include <sstream>
#include <map>
#include <vector>
#include "rapidyaml.hpp"
#include "ParticipantConfiguration.hpp"
#include "YamlParserUtils.hpp"
namespace VSilKit {
template <typename Impl>
class BasicYamlReader
{
protected:
ryml::Parser& _parser;
ryml::ConstNodeRef _node;
public:
BasicYamlReader(ryml::Parser& parser_, ryml::ConstNodeRef node_)
: _parser(parser_)
, _node(node_)
{
}
public:
template <typename T, typename std::enable_if_t<!std::is_integral_v<T>, bool> = true>
void OptionalRead(T& val, const std::string& name)
{
auto&& child = GetChildSafe(name);
if (child.IsValid())
{
child.Read(val);
}
}
void OptionalRead(bool& val, const std::string& name)
{
auto&& child = GetChildSafe(name);
if (child.IsValid())
{
child.Read(val);
}
}
template <typename T>
void OptionalRead(std::optional<T>& val, const std::string& name)
{
auto&& child = GetChildSafe(name);
if (child.IsValid())
{
T tmp{};
child.Read(tmp);
val = std::move(tmp); // needs a proper setter to set "has_value"
}
}
template <typename T, typename std::enable_if_t<std::is_integral_v<T>, bool> = true>
void OptionalRead(T& val, const std::string& name)
{
auto tmp = ryml::fmt::overflow_checked(val);
(void)_node.get_if(ryml::to_csubstr(name), &tmp);
}
template <typename ConfigT>
void OptionalRead_deprecated_alternative(ConfigT& value, const std::string& fieldName,
std::initializer_list<std::string> deprecatedFieldNames)
{
if (!(IsMap() || IsSequence()))
{
return;
}
auto hasChild = [this](auto&& name) {
auto&& c = GetChildSafe(name);
return c.IsValid();
};
std::vector<std::string> presentDeprecatedFieldNames;
std::copy_if(deprecatedFieldNames.begin(), deprecatedFieldNames.end(),
std::back_inserter(presentDeprecatedFieldNames), hasChild);
if (HasKey(fieldName) && presentDeprecatedFieldNames.size() >= 1)
{
std::stringstream ss;
ss << "The key \"" << fieldName << "\" and the deprecated alternatives";
for (const auto& deprecatedFieldName : presentDeprecatedFieldNames)
{
ss << " \"" << deprecatedFieldName << "\"";
}
ss << " are present.";
throw SilKit::ConfigurationError{ss.str()};
}
if (presentDeprecatedFieldNames.size() >= 2)
{
std::stringstream ss;
ss << "The deprecated keys";
for (const auto& deprecatedFieldName : presentDeprecatedFieldNames)
{
ss << " \"" << deprecatedFieldName << "\"";
}
ss << " are present.";
throw SilKit::ConfigurationError{ss.str()};
}
OptionalRead(value, fieldName);
for (const auto& deprecatedFieldName : deprecatedFieldNames)
{
OptionalRead(value, deprecatedFieldName);
}
}
public:
template <typename T>
void ReadKeyValue(T& value, const std::string& name)
{
auto&& child = GetChildSafe(name);
if (!child.IsValid())
{
std::ostringstream s;
s << "missing key: " << name;
throw MakeConfigurationError(s.str());
}
child.Read(value);
}
public:
template <typename T>
void Read(T& value)
{
_node >> value;
}
template <typename Rep, typename Period>
void Read(std::chrono::duration<Rep, Period>& obj)
{
Rep value{};
auto tmp = ryml::fmt::overflow_checked(value);
Read(tmp);
obj = std::chrono::milliseconds{value};
}
template <typename T>
void Read(std::vector<T>& val)
{
for (auto&& i : _node.cchildren())
{
T element{};
MakeImpl(i).Read(element);
val.emplace_back(std::move(element));
}
}
protected:
bool IsValid() const
{
return !_node.invalid();
}
bool IsMap() const
{
return _node.is_map();
}
bool IsScalar() const
{
return _node.is_val() || _node.is_keyval();
}
bool IsSequence() const
{
return _node.is_seq();
}
bool IsExistingString(const char* str) const
{
if (!_node.is_val())
{
return false;
}
return _node.val() == ryml::to_csubstr(str);
}
bool IsEmpty() const
{
return _node.empty();
}
bool IsString(const char* string) const
{
return IsScalar() && (_node.val() == ryml::to_csubstr(string));
}
bool HasKey(const std::string& name) const
{
return HasKey(_node, name);
}
static bool HasKey(ryml::ConstNodeRef node, const std::string& name)
{
return node.is_map() && !node.find_child(ryml::to_csubstr(name)).invalid();
}
auto MakeConfigurationError(const std::string_view message) const -> SilKit::ConfigurationError
{
const auto location = _parser.location(_node);
return VSilKit::MakeConfigurationError(location, message);
}
auto GetChildSafe(const std::string& name) const -> Impl
{
if (HasKey(name))
{
return MakeImpl(_node.find_child(ryml::to_csubstr(name)));
}
if (IsSequence())
{
for (const auto& child : _node.cchildren())
{
if (child.is_container() && HasKey(child, name))
{
return MakeImpl(child.find_child(ryml::to_csubstr(name)));
}
}
}
return MakeImpl({});
}
auto MakeImpl(ryml::ConstNodeRef node_) const -> Impl
{
return Impl{_parser, node_};
}
private:
auto AsImpl() -> Impl&
{
return static_cast<Impl&>(*this);
}
auto AsImpl() const -> const Impl&
{
return static_cast<const Impl&>(*this);
}
};
struct YamlReader : BasicYamlReader<YamlReader>
{
using BasicYamlReader::BasicYamlReader;
using BasicYamlReader::Read;
protected: // Parsing utils
template <typename T>
void ReadController(T& obj)
{
ReadKeyValue(obj.name, "Name");
OptionalRead(obj.network, "Network");
OptionalRead(obj.useTraceSinks, "UseTraceSinks");
OptionalRead(obj.replay, "Replay");
}
public:
void Read(SilKit::Services::MatchingLabel& value);
void Read(SilKit::Services::MatchingLabel::Kind& value);
void Read(SilKit::Services::Logging::Level& obj);
void Read(SilKit::Services::Logging::Topic& obj);
void Read(SilKit::Services::Flexray::FlexrayClusterParameters& obj);
void Read(SilKit::Services::Flexray::FlexrayNodeParameters& obj);
void Read(SilKit::Services::Flexray::FlexrayTxBufferConfig& obj);
void Read(SilKit::Services::Flexray::FlexrayChannel& obj);
void Read(SilKit::Services::Flexray::FlexrayClockPeriod& obj);
void Read(SilKit::Services::Flexray::FlexrayTransmissionMode& obj);
void Read(SilKit::Config::Sink::Type& obj);
void Read(SilKit::Config::Sink::Format& obj);
void Read(SilKit::Config::Sink& obj);
void Read(SilKit::Config::Logging& obj);
void Read(SilKit::Config::MetricsSink::Type& obj);
void Read(SilKit::Config::MetricsSink& obj);
void Read(SilKit::Config::Metrics& obj);
void Read(SilKit::Config::MdfChannel& obj);
void Read(SilKit::Config::Replay& obj);
void Read(SilKit::Config::Replay::Direction& obj);
void Read(SilKit::Config::CanController& obj);
void Read(SilKit::Config::LinController& obj);
void Read(SilKit::Config::EthernetController& obj);
void Read(SilKit::Config::FlexrayController& obj);
void Read(SilKit::Config::Label::Kind& obj);
void Read(SilKit::Config::Label& obj);
void Read(SilKit::Config::DataPublisher& obj);
void Read(SilKit::Config::DataSubscriber& obj);
void Read(SilKit::Config::RpcServer& obj);
void Read(SilKit::Config::RpcClient& obj);
void Read(SilKit::Config::Tracing& obj);
void Read(SilKit::Config::TraceSink& obj);
void Read(SilKit::Config::TraceSink::Type& obj);
void Read(SilKit::Config::TraceSource& obj);
void Read(SilKit::Config::TraceSource::Type& obj);
void Read(SilKit::Config::Extensions& obj);
void Read(SilKit::Config::Middleware& obj);
void Read(SilKit::Config::Includes& obj);
void Read(SilKit::Config::Aggregation& obj);
void Read(SilKit::Config::TimeSynchronization& obj);
void Read(SilKit::Config::Experimental& obj);
void Read(SilKit::Config::ParticipantConfiguration& obj);
void Read(SilKit::Config::HealthCheck& obj);
//Registry
void Read(SilKitRegistry::Config::V1::Experimental& obj);
void Read(SilKitRegistry::Config::V1::RegistryConfiguration& obj);
};
} // namespace VSilKit