Skip to content

Commit 99142ec

Browse files
apolukhinrobot-piglet
authored andcommitted
feat universal: fix yaml::Value error detection
Tests: протестировано CI --- Pull Request resolved: #1167 Co-authored-by: antoshkka <antoshkka@userver.tech> Co-authored-by: antoshkka <antoshkka@userver.tech> commit_hash:cf8e4eb6b04357a4cff1952a70a3b60286d6f31c
1 parent 85fec25 commit 99142ec

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

conanfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def requirements(self):
118118
self.requires('libev/[^4.33]')
119119
self.requires('openssl/[>=1.1 <4]')
120120
self.requires('rapidjson/[>=cci.20230929 <cci.20230930]', transitive_headers=True)
121-
self.requires('yaml-cpp/[^0.8.0]')
121+
self.requires('yaml-cpp/[>=0.8.0 <=0.9.0]')
122122
self.requires('zlib/[^1.3]')
123123
self.requires('zstd/[^1.5]')
124124
self.requires('icu/[>=74.1 <77]', force=True)

universal/src/formats/yaml/value.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "exttypes.hpp"
1212
#include "string_view_support.hpp"
1313

14-
USERVER_NAMESPACE_BEGIN
15-
16-
namespace formats::yaml {
17-
1814
namespace {
1915

2016
// Helper structure for YAML conversions. YAML has built in conversion logic and
@@ -32,6 +28,41 @@ struct IsConvertibleChecker {
3228
}
3329
};
3430

31+
} // anonymous namespace
32+
33+
namespace YAML {
34+
35+
// Reverting harm done by https://github.com/jbeder/yaml-cpp/commit/96f5c887f373ac483844c51cfc9a3621002314f0
36+
// to detect if the conversion is successful without throwing an exception
37+
template <typename T>
38+
requires std::integral<T> || std::floating_point<T>
39+
struct as_if<T, IsConvertibleChecker<T> > {
40+
explicit as_if(const Node& node_in)
41+
: node(node_in)
42+
{}
43+
const Node& node;
44+
45+
T operator()(const IsConvertibleChecker<T>& fallback) const {
46+
if (!node.m_pNode) {
47+
return fallback;
48+
}
49+
50+
T t;
51+
if (convert<T>::decode(node, t)) {
52+
return t;
53+
}
54+
return fallback;
55+
}
56+
};
57+
58+
} // namespace YAML
59+
60+
USERVER_NAMESPACE_BEGIN
61+
62+
namespace formats::yaml {
63+
64+
namespace {
65+
3566
auto MakeMissingNode() { return YAML::Node{}[0]; }
3667

3768
// yaml-cpp allows to parse quoted and typed strings into bool and numeric

0 commit comments

Comments
 (0)