forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYaml.cpp
More file actions
488 lines (414 loc) · 12.9 KB
/
Copy pathYaml.cpp
File metadata and controls
488 lines (414 loc) · 12.9 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include <pch.h>
#include "winget/Yaml.h"
#include "YamlWrapper.h"
#include "AppInstallerErrors.h"
#include "AppInstallerLogging.h"
#include "AppInstallerStrings.h"
namespace AppInstaller::YAML
{
using namespace std::string_view_literals;
namespace
{
Node s_globalInvalidNode;
std::string_view GetExceptionTypeStringView(Exception::Type type)
{
switch (type)
{
case Exception::Type::None:
return "None"sv;
case Exception::Type::Memory:
return "Memory"sv;
case Exception::Type::Reader:
return "Reader"sv;
case Exception::Type::Scanner:
return "Scanner"sv;
case Exception::Type::Parser:
return "Parser"sv;
case Exception::Type::Composer:
return "Composer"sv;
case Exception::Type::Writer:
return "Writer"sv;
case Exception::Type::Emitter:
return "Emitter"sv;
}
return "Unknown"sv;
}
void OutputExceptionHeader(std::ostringstream& out, Exception::Type type)
{
out << "[YAML:" << GetExceptionTypeStringView(type) << "] ";
}
void OutputMark(std::ostringstream& out, const Mark& mark)
{
out << "[line " << mark.line << "; col " << mark.column << ']';
}
}
Exception::Exception(Type type) :
wil::ResultException(APPINSTALLER_CLI_ERROR_LIBYAML_ERROR)
{
std::ostringstream out;
OutputExceptionHeader(out, type);
if (type == Type::Memory)
{
out << "Unable to (re)allocate memory";
}
else
{
out << "An unknown error occurred";
}
m_what = out.str();
}
Exception::Exception(Type type, const char* problem, size_t offset, int value) :
wil::ResultException(APPINSTALLER_CLI_ERROR_LIBYAML_ERROR)
{
std::ostringstream out;
OutputExceptionHeader(out, type);
out << (problem ? problem : "Unexplained error");
if (value != -1)
{
out << " [" << value << ']';
}
out << " at " << offset;
m_what = out.str();
}
Exception::Exception(Type type, const char* problem, const Mark& problemMark, const char* context, const Mark& contextMark) :
wil::ResultException(APPINSTALLER_CLI_ERROR_LIBYAML_ERROR)
{
std::ostringstream out;
OutputExceptionHeader(out, type);
if (context)
{
out << context << ' ';
OutputMark(out, contextMark);
out << ' ' << (problem ? problem : "unexplained error");
}
else
{
out << (problem ? problem : "Unexplained error");
}
out << ' ';
OutputMark(out, problemMark);
m_what = out.str();
}
Exception::Exception(Type type, const char* problem) :
wil::ResultException(APPINSTALLER_CLI_ERROR_LIBYAML_ERROR)
{
std::ostringstream out;
OutputExceptionHeader(out, type);
out << (problem ? problem : "Unexplained error");
m_what = out.str();
}
const char* Exception::what() const noexcept
{
return m_what.c_str();
}
Node::Node(Type type, std::string tag, const YAML::Mark& mark) :
m_type(type), m_tag(std::move(tag)), m_mark(mark)
{
if (m_type == Type::Sequence)
{
m_sequence = decltype(m_sequence)::value_type{};
}
else if (m_type == Type::Mapping)
{
m_mapping = decltype(m_mapping)::value_type{};
}
}
void Node::SetScalar(std::string value)
{
Require(Type::Scalar);
m_scalar = std::move(value);
}
bool Node::operator<(const Node& other) const
{
Require(Type::Scalar);
other.Require(Type::Scalar);
return this->m_scalar < other.m_scalar;
}
Node& Node::operator[](std::string_view key)
{
Require(Type::Mapping);
auto itrs = m_mapping->equal_range(key);
if (itrs.first == itrs.second)
{
return s_globalInvalidNode;
}
Node& result = itrs.first->second;
THROW_HR_IF(APPINSTALLER_CLI_ERROR_YAML_DUPLICATE_MAPPING_KEY, ++itrs.first != itrs.second);
return result;
}
const Node& Node::operator[](std::string_view key) const
{
Require(Type::Mapping);
auto itrs = m_mapping->equal_range(key);
if (itrs.first == itrs.second)
{
return s_globalInvalidNode;
}
const Node& result = itrs.first->second;
THROW_HR_IF(APPINSTALLER_CLI_ERROR_YAML_DUPLICATE_MAPPING_KEY, ++itrs.first != itrs.second);
return result;
}
Node& Node::operator[](size_t index)
{
Require(Type::Sequence);
return m_sequence.value()[index];
}
const Node& Node::operator[](size_t index) const
{
Require(Type::Sequence);
return m_sequence.value()[index];
}
size_t Node::size() const
{
switch (m_type)
{
case Type::Invalid:
case Type::None:
case Type::Scalar:
return 0;
case Type::Sequence:
return m_sequence->size();
case Type::Mapping:
return m_mapping->size();
}
THROW_HR(E_UNEXPECTED);
}
const std::vector<Node>& Node::Sequence() const
{
Require(Type::Sequence);
return m_sequence.value();
}
const std::multimap<Node, Node>& Node::Mapping() const
{
Require(Type::Mapping);
return m_mapping.value();
}
void Node::Require(Type type) const
{
THROW_HR_IF(APPINSTALLER_CLI_ERROR_YAML_INVALID_OPERATION, m_type != type);
}
std::string Node::as_dispatch(std::string*) const
{
return m_scalar;
}
int64_t Node::as_dispatch(int64_t*) const
{
return std::stoll(m_scalar);
}
int Node::as_dispatch(int*) const
{
// To allow HResult representation
return static_cast<int>(std::stoll(m_scalar, 0, 0));
}
bool Node::as_dispatch(bool*) const
{
if (Utility::CaseInsensitiveEquals(m_scalar, "true"))
{
return true;
}
else if (Utility::CaseInsensitiveEquals(m_scalar, "false"))
{
return false;
}
else
{
THROW_HR(APPINSTALLER_CLI_ERROR_YAML_INVALID_DATA);
}
}
Node Load(std::string_view input)
{
Wrapper::Parser parser(input);
Wrapper::Document document = parser.Load();
if (document.HasRoot())
{
return document.GetRoot();
}
else
{
return {};
}
}
Node Load(const std::string& input)
{
return Load(static_cast<std::string_view>(input));
}
Node Load(std::istream& input)
{
Wrapper::Parser parser(input);
Wrapper::Document document = parser.Load();
if (document.HasRoot())
{
return document.GetRoot();
}
else
{
return {};
}
}
Node Load(const std::filesystem::path& input)
{
std::ifstream stream(input, std::ios_base::in | std::ios_base::binary);
THROW_LAST_ERROR_IF(stream.fail());
return Load(stream);
}
Emitter::Emitter() :
m_document(std::make_unique<Wrapper::Document>(true))
{
SetAllowedInputs<InputType::BeginMap, InputType::BeginSeq>();
}
Emitter::Emitter(Emitter&&) noexcept = default;
Emitter& Emitter::operator=(Emitter&&) noexcept = default;
Emitter::~Emitter() = default;
Emitter& Emitter::operator<<(EmitterEvent event)
{
switch (event)
{
case AppInstaller::YAML::BeginSeq:
{
CheckInput(InputType::BeginSeq);
int id = m_document->AddSequence();
AppendNode(id);
m_containers.emplace(id, false);
SetAllowedInputsForContainer();
break;
}
case AppInstaller::YAML::EndSeq:
CheckInput(InputType::EndSeq);
m_containers.pop();
SetAllowedInputsForContainer();
break;
case AppInstaller::YAML::BeginMap:
{
CheckInput(InputType::BeginMap);
int id = m_document->AddMapping();
AppendNode(id);
m_containers.emplace(id, true);
SetAllowedInputsForContainer();
break;
}
case AppInstaller::YAML::EndMap:
CheckInput(InputType::EndMap);
m_containers.pop();
SetAllowedInputsForContainer();
break;
case AppInstaller::YAML::Key:
CheckInput(InputType::Key);
m_scalarInfo = InputType::Key;
SetAllowedInputs<InputType::Scalar>();
break;
case AppInstaller::YAML::Value:
CheckInput(InputType::Value);
m_scalarInfo = InputType::Value;
SetAllowedInputs<InputType::Scalar, InputType::BeginMap, InputType::BeginSeq>();
break;
default:
THROW_HR(E_UNEXPECTED);
}
return *this;
}
Emitter& Emitter::operator<<(std::string_view value)
{
CheckInput(InputType::Scalar);
int id = m_document->AddScalar(value);
if (!m_scalarInfo)
{
// Part of a sequence
AppendNode(id);
// No change to allowed inputs
}
else if (m_scalarInfo.value() == InputType::Key)
{
m_keyId = id;
m_scalarInfo = std::nullopt;
SetAllowedInputs<InputType::Value, InputType::BeginMap, InputType::BeginSeq>();
}
else if (m_scalarInfo.value() == InputType::Value)
{
// Mapping pair complete
AppendNode(id);
m_scalarInfo = std::nullopt;
SetAllowedInputsForContainer();
}
else
{
THROW_HR(APPINSTALLER_CLI_ERROR_YAML_INVALID_EMITTER_STATE);
}
return *this;
}
Emitter& Emitter::operator<<(int64_t value)
{
std::ostringstream stream;
stream << value;
return operator<<(stream.str());
}
Emitter& Emitter::operator<<(bool value)
{
return operator<<(value ? "true"sv : "false"sv);
}
std::string Emitter::str()
{
std::ostringstream stream;
Wrapper::Emitter emitter(stream);
emitter.Dump(*m_document);
emitter.Flush();
return stream.str();
}
void Emitter::Emit(std::ostream& out)
{
Wrapper::Emitter emitter(out);
emitter.Dump(*m_document);
emitter.Flush();
}
void Emitter::AppendNode(int id)
{
if (!m_containers.empty())
{
ContainerInfo& ci = m_containers.top();
if (ci.IsMapping)
{
THROW_HR_IF(APPINSTALLER_CLI_ERROR_YAML_INVALID_EMITTER_STATE, !m_keyId);
m_document->AppendMappingPair(ci.Id, m_keyId.value(), id);
m_keyId = std::nullopt;
}
else
{
m_document->AppendSequenceItem(ci.Id, id);
}
}
}
size_t Emitter::GetInputBitmask(InputType type)
{
return static_cast<size_t>(1) << static_cast<size_t>(type);
}
void Emitter::CheckInput(InputType type)
{
if ((m_allowedInputs & GetInputBitmask(type)) == 0)
{
AICLI_LOG(YAML, Error, << "Invalid emitter input [0x" <<
std::hex << std::setw(2) << std::setfill('0') << GetInputBitmask(type) << "], expected one of [0x" <<
std::hex << std::setw(2) << std::setfill('0') << m_allowedInputs << "]");
THROW_HR(APPINSTALLER_CLI_ERROR_YAML_INVALID_EMITTER_STATE);
}
}
void Emitter::SetAllowedInputsForContainer()
{
if (m_containers.empty())
{
m_allowedInputs = 0;
}
else
{
if (m_containers.top().IsMapping)
{
SetAllowedInputs<InputType::Key, InputType::EndMap>();
}
else
{
SetAllowedInputs<InputType::Scalar, InputType::BeginMap, InputType::BeginSeq, InputType::EndSeq>();
}
}
}
}