aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yaml
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/yaml
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/yaml')
-rw-r--r--library/cpp/yaml/as/tstring.h27
-rw-r--r--library/cpp/yaml/as/ut/tstring_test.cpp59
-rw-r--r--library/cpp/yaml/as/ut/ya.make14
-rw-r--r--library/cpp/yaml/as/ya.make13
-rw-r--r--library/cpp/yaml/ya.make4
5 files changed, 117 insertions, 0 deletions
diff --git a/library/cpp/yaml/as/tstring.h b/library/cpp/yaml/as/tstring.h
new file mode 100644
index 0000000000..1037e46706
--- /dev/null
+++ b/library/cpp/yaml/as/tstring.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <contrib/libs/yaml-cpp/include/yaml-cpp/yaml.h>
+
+#include <util/generic/string.h>
+
+namespace YAML {
+ template <>
+ inline TString Node::as<TString>() const {
+ const auto& converted = as<std::string>();
+ return TString(converted.c_str(), converted.size());
+ }
+
+ template <>
+ struct convert<TString> {
+ static Node encode(const TString& rhs) {
+ return Node(std::string(rhs));
+ }
+
+ static bool decode(const Node& node, TString& rhs) {
+ if (!node.IsScalar())
+ return false;
+ rhs = node.Scalar();
+ return true;
+ }
+ };
+}
diff --git a/library/cpp/yaml/as/ut/tstring_test.cpp b/library/cpp/yaml/as/ut/tstring_test.cpp
new file mode 100644
index 0000000000..2ad4f6c572
--- /dev/null
+++ b/library/cpp/yaml/as/ut/tstring_test.cpp
@@ -0,0 +1,59 @@
+#include <library/cpp/testing/unittest/registar.h>
+#include <library/cpp/yaml/as/tstring.h>
+
+#include <contrib/libs/yaml-cpp/include/yaml-cpp/yaml.h>
+
+#include <array>
+
+Y_UNIT_TEST_SUITE(YamlTstringTest) {
+ Y_UNIT_TEST(As) {
+ const auto* key1 = "key1";
+ const auto* key2 = "key2";
+ const TString string = "string";
+ const TString anotherString = "another string";
+
+ YAML::Node map;
+ map[key1] = std::string(string);
+
+ UNIT_ASSERT_VALUES_EQUAL(map[key1].as<TString>(), string);
+ UNIT_ASSERT_VALUES_EQUAL(map[key1].as<TString>(anotherString), string);
+
+ UNIT_ASSERT_VALUES_EQUAL(map[key2].as<TString>(anotherString), anotherString);
+
+ UNIT_ASSERT_EXCEPTION(map[key1].as<int>(), YAML::BadConversion);
+
+ UNIT_ASSERT_EXCEPTION(map[key2].as<int>(), YAML::BadConversion);
+ UNIT_ASSERT_EXCEPTION(map[key2].as<TString>(), YAML::BadConversion);
+ }
+
+ Y_UNIT_TEST(Convert) {
+ const TString string("string");
+ auto node = YAML::Node(std::string(string));
+
+ UNIT_ASSERT_VALUES_EQUAL(node.Scalar(), YAML::convert<TString>::encode(string).Scalar());
+
+ TString buffer;
+ YAML::convert<TString>::decode(node, buffer);
+ UNIT_ASSERT_VALUES_EQUAL(string, buffer);
+ }
+
+ Y_UNIT_TEST(NullCharactersInString) {
+ const std::array<char, 3> characters = {{'a', '\0', 'b'}};
+ const std::string testString(characters.data(), characters.size());
+ const TString refString(characters.data(), characters.size());
+ const std::string key = "key";
+
+ YAML::Node node;
+ node[key] = testString;
+
+ UNIT_ASSERT_VALUES_EQUAL(node[key].as<TString>(), refString);
+ UNIT_ASSERT_VALUES_EQUAL(node[key].as<TString>(TString("fallback")), refString);
+
+ auto stringNode = YAML::Node(testString);
+ UNIT_ASSERT_VALUES_EQUAL(stringNode.Scalar(), YAML::convert<TString>::encode(refString).Scalar());
+
+ TString buffer;
+ YAML::convert<TString>::decode(stringNode, buffer);
+ UNIT_ASSERT_VALUES_EQUAL(refString, buffer);
+ }
+}
diff --git a/library/cpp/yaml/as/ut/ya.make b/library/cpp/yaml/as/ut/ya.make
new file mode 100644
index 0000000000..4eb394dd54
--- /dev/null
+++ b/library/cpp/yaml/as/ut/ya.make
@@ -0,0 +1,14 @@
+UNITTEST()
+
+OWNER(g:crypta)
+
+SRCS(
+ tstring_test.cpp
+)
+
+PEERDIR(
+ contrib/libs/yaml-cpp
+ library/cpp/yaml/as
+)
+
+END()
diff --git a/library/cpp/yaml/as/ya.make b/library/cpp/yaml/as/ya.make
new file mode 100644
index 0000000000..80d60b957b
--- /dev/null
+++ b/library/cpp/yaml/as/ya.make
@@ -0,0 +1,13 @@
+LIBRARY()
+
+OWNER(g:crypta)
+
+PEERDIR(
+ contrib/libs/yaml-cpp
+)
+
+END()
+
+RECURSE_FOR_TESTS(
+ ut
+)
diff --git a/library/cpp/yaml/ya.make b/library/cpp/yaml/ya.make
new file mode 100644
index 0000000000..e9689fe65b
--- /dev/null
+++ b/library/cpp/yaml/ya.make
@@ -0,0 +1,4 @@
+RECURSE(
+ as
+ scheme
+)