aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yaml
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2023-06-15 18:24:59 +0300
committerDaniil Cherednik <dan.cherednik@gmail.com>2023-06-15 18:26:33 +0300
commit068d4453cf9fc68c875eee73f5c637bb076f6a71 (patch)
treeda3e83fdb9488ea08faa39d8b41916744f9acad7 /library/cpp/yaml
parent7e7de263d4acbc6eacf92b618bcb5f9049bccc9b (diff)
downloadydb-068d4453cf9fc68c875eee73f5c637bb076f6a71.tar.gz
Create stable-23-2 branch
x-stable-origin-commit: 3fd4d58117c143ed9e6b21813ccd9e507d2cd4d3
Diffstat (limited to 'library/cpp/yaml')
-rw-r--r--library/cpp/yaml/fyamlcpp/fyamlcpp.cpp142
-rw-r--r--library/cpp/yaml/fyamlcpp/fyamlcpp.h23
-rw-r--r--library/cpp/yaml/fyamlcpp/fyamlcpp_ut.cpp371
3 files changed, 523 insertions, 13 deletions
diff --git a/library/cpp/yaml/fyamlcpp/fyamlcpp.cpp b/library/cpp/yaml/fyamlcpp/fyamlcpp.cpp
index b8d8b17596..d006001b9e 100644
--- a/library/cpp/yaml/fyamlcpp/fyamlcpp.cpp
+++ b/library/cpp/yaml/fyamlcpp/fyamlcpp.cpp
@@ -155,18 +155,6 @@ enum EEmitterCfgFlags {
Default = FYECF_DEFAULT,
};
-enum class ENodeStyle {
- Any = FYNS_ANY,
- Flow = FYNS_FLOW,
- Block = FYNS_BLOCK,
- Plain = FYNS_PLAIN,
- SingleQuoted = FYNS_SINGLE_QUOTED,
- DoubleQuoted = FYNS_DOUBLE_QUOTED,
- Literal = FYNS_LITERAL,
- Folded = FYNS_FOLDED,
- Alias = FYNS_ALIAS,
-};
-
enum class ENodeWalkFlags {
DontFollow = FYNWF_DONT_FOLLOW,
Follow = FYNWF_FOLLOW,
@@ -330,6 +318,115 @@ TString TNodeRef::Scalar() const {
return TString(text, size);
}
+TMark TNodeRef::BeginMark() const {
+ ENSURE_NODE_NOT_EMPTY(Node_);
+ std::unique_ptr<fy_document_iterator, void(*)(fy_document_iterator*)> it(
+ fy_document_iterator_create(),
+ &fy_document_iterator_destroy);
+ fy_document_iterator_node_start(it.get(), Node_);
+ auto deleter = [&](fy_event* fye){ fy_document_iterator_event_free(it.get(), fye); };
+ std::unique_ptr<fy_event, decltype(deleter)> ev(
+ fy_document_iterator_body_next(it.get()),
+ deleter);
+ auto* mark = fy_event_start_mark(ev.get());
+
+ if (!mark) {
+ ythrow yexception() << "can't get begin mark for a node";
+ }
+
+ return TMark{
+ mark->input_pos,
+ mark->line,
+ mark->column,
+ };
+}
+
+bool IsComplexType(ENodeType type) {
+ return type == ENodeType::Mapping || type == ENodeType::Sequence;
+}
+
+fy_event_type GetOpenEventType(ENodeType type) {
+ switch(type) {
+ case ENodeType::Mapping:
+ return FYET_MAPPING_START;
+ case ENodeType::Sequence:
+ return FYET_SEQUENCE_START;
+ default:
+ Y_FAIL("Not a brackets type");
+ }
+}
+
+fy_event_type GetCloseEventType(ENodeType type) {
+ switch(type) {
+ case ENodeType::Mapping:
+ return FYET_MAPPING_END;
+ case ENodeType::Sequence:
+ return FYET_SEQUENCE_END;
+ default:
+ Y_FAIL("Not a brackets type");
+ }
+}
+
+TMark TNodeRef::EndMark() const {
+ ENSURE_NODE_NOT_EMPTY(Node_);
+ std::unique_ptr<fy_document_iterator, void(*)(fy_document_iterator*)> it(
+ fy_document_iterator_create(),
+ &fy_document_iterator_destroy);
+ fy_document_iterator_node_start(it.get(), Node_);
+
+ auto deleter = [&](fy_event* fye){ fy_document_iterator_event_free(it.get(), fye); };
+ std::unique_ptr<fy_event, decltype(deleter)> prevEv(
+ nullptr,
+ deleter);
+ std::unique_ptr<fy_event, decltype(deleter)> ev(
+ fy_document_iterator_body_next(it.get()),
+ deleter);
+
+ if (IsComplexType(Type())) {
+ int openBrackets = 0;
+ if (ev->type == GetOpenEventType(Type())) {
+ ++openBrackets;
+ }
+ if (ev->type == GetCloseEventType(Type())) {
+ --openBrackets;
+ }
+ while (ev->type != GetCloseEventType(Type()) || openBrackets != 0) {
+ std::unique_ptr<fy_event, decltype(deleter)> cur(
+ fy_document_iterator_body_next(it.get()),
+ deleter);
+ if (cur == nullptr) {
+ break;
+ }
+ if (cur->type == GetOpenEventType(Type())) {
+ ++openBrackets;
+ }
+ if (cur->type == GetCloseEventType(Type())) {
+ --openBrackets;
+ }
+ if (fy_event_get_node_style(cur.get()) != FYNS_BLOCK) {
+ prevEv.reset(ev.release());
+ ev.reset(cur.release());
+ }
+ }
+ }
+
+ auto* mark = fy_event_end_mark(ev.get());
+
+ if (!mark && prevEv) {
+ mark = fy_event_end_mark(prevEv.get());
+ }
+
+ if (!mark) {
+ ythrow yexception() << "can't get end mark for a node";
+ }
+
+ return TMark{
+ mark->input_pos,
+ mark->line,
+ mark->column,
+ };
+}
+
TMapping TNodeRef::Map() const {
ENSURE_NODE_NOT_EMPTY(Node_);
Y_ENSURE_EX(fy_node_is_mapping(Node_), TFyamlEx() << "Node is not Mapping: " << Path());
@@ -400,6 +497,16 @@ std::unique_ptr<char, void(*)(char*)> TNodeRef::EmitToCharArray() const {
return res;
}
+void TNodeRef::SetStyle(ENodeStyle style) {
+ ENSURE_NODE_NOT_EMPTY(Node_);
+ fy_node_set_style(Node_, (enum fy_node_style)style);
+}
+
+ENodeStyle TNodeRef::Style() const {
+ ENSURE_NODE_NOT_EMPTY(Node_);
+ return (ENodeStyle)fy_node_get_style(Node_);
+}
+
void TNodeRef::SetUserData(NDetail::IBasicUserData* data) {
ENSURE_NODE_NOT_EMPTY(Node_);
fy_node_set_meta(Node_, data);
@@ -429,6 +536,12 @@ TNodeRef TNodePairRef::Key() const {
return TNodeRef(fy_node_pair_key(Pair_));
}
+int TNodePairRef::Index(const TNodeRef& node) const {
+ ENSURE_NODE_NOT_EMPTY(node);
+ ENSURE_NODE_NOT_EMPTY(Pair_);
+ return fy_node_mapping_get_pair_index(node.Node_, Pair_);
+}
+
void TNodePairRef::SetKey(const TNodeRef& node) {
ENSURE_NODE_NOT_EMPTY(Pair_);
ENSURE_NODE_NOT_EMPTY(node);
@@ -542,6 +655,9 @@ void TMapping::Remove(const TNodePairRef& toRemove) {
ENSURE_NODE_NOT_EMPTY(Node_);
ENSURE_NODE_NOT_EMPTY(toRemove);
NDetail::RethrowOnError(fy_node_mapping_remove(Node_, toRemove.Pair_), Node_);
+ fy_node_free(fy_node_pair_key(toRemove.Pair_));
+ fy_node_free(fy_node_pair_value(toRemove.Pair_));
+ free(toRemove.Pair_);
}
TMappingIterator TMapping::Remove(const TMappingIterator& toRemove) {
@@ -819,7 +935,7 @@ std::unique_ptr<char, void(*)(char*)> TDocument::EmitToCharArray() const {
std::unique_ptr<char, void(*)(char*)> res(
fy_emit_document_to_string(
Document_.get(),
- (fy_emitter_cfg_flags)(FYECF_DEFAULT | FYECF_OUTPUT_COMMENTS)), &NDetail::FreeChar);
+ (fy_emitter_cfg_flags)(FYECF_DEFAULT | FYECF_MODE_PRETTY | FYECF_OUTPUT_COMMENTS)), &NDetail::FreeChar);
return res;
}
diff --git a/library/cpp/yaml/fyamlcpp/fyamlcpp.h b/library/cpp/yaml/fyamlcpp/fyamlcpp.h
index bcf9362d73..6df7b244c6 100644
--- a/library/cpp/yaml/fyamlcpp/fyamlcpp.h
+++ b/library/cpp/yaml/fyamlcpp/fyamlcpp.h
@@ -15,6 +15,7 @@ struct fy_document;
struct fy_diag;
struct fy_document_iterator;
struct fy_node_pair;
+extern "C" struct fy_node *fy_node_buildf(struct fy_document *fyd, const char *fmt, ...);
namespace NFyaml {
@@ -32,6 +33,18 @@ enum class ENodeType {
Mapping,
};
+enum class ENodeStyle {
+ Any = -1,
+ Flow,
+ Block,
+ Plain,
+ SingleQuoted,
+ DoubleQuoted,
+ Literal,
+ Folded,
+ Alias,
+};
+
namespace NDetail {
class IBasicUserData {
@@ -137,6 +150,10 @@ public:
TString Scalar() const;
+ TMark BeginMark() const;
+
+ TMark EndMark() const;
+
void Insert(const TNodeRef& node);
bool Empty() const { return Node_ == nullptr; }
@@ -155,6 +172,10 @@ public:
std::unique_ptr<char, void(*)(char*)> EmitToCharArray() const;
+ void SetStyle(ENodeStyle style);
+
+ ENodeStyle Style() const;
+
protected:
fy_node* Node_ = nullptr;
@@ -198,6 +219,8 @@ public:
TNodeRef Key() const;
+ int Index(const TNodeRef& node) const;
+
void SetKey(const TNodeRef& node);
TNodeRef Value() const;
diff --git a/library/cpp/yaml/fyamlcpp/fyamlcpp_ut.cpp b/library/cpp/yaml/fyamlcpp/fyamlcpp_ut.cpp
index ffe1d4368c..2f6e14138c 100644
--- a/library/cpp/yaml/fyamlcpp/fyamlcpp_ut.cpp
+++ b/library/cpp/yaml/fyamlcpp/fyamlcpp_ut.cpp
@@ -11,6 +11,16 @@ Y_UNIT_TEST_SUITE(FYamlCpp) {
UNIT_ASSERT_EQUAL((int)NFyaml::ENodeType::Scalar, FYNT_SCALAR);
UNIT_ASSERT_EQUAL((int)NFyaml::ENodeType::Sequence, FYNT_SEQUENCE);
UNIT_ASSERT_EQUAL((int)NFyaml::ENodeType::Mapping, FYNT_MAPPING);
+
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Any, FYNS_ANY);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Flow, FYNS_FLOW);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Block, FYNS_BLOCK);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Plain, FYNS_PLAIN);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::SingleQuoted, FYNS_SINGLE_QUOTED);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::DoubleQuoted, FYNS_DOUBLE_QUOTED);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Literal, FYNS_LITERAL);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Folded, FYNS_FOLDED);
+ UNIT_ASSERT_EQUAL((int)NFyaml::ENodeStyle::Alias, FYNS_ALIAS);
}
Y_UNIT_TEST(ErrorHandling) {
@@ -153,4 +163,365 @@ x: b
UNIT_ASSERT_VALUES_EQUAL(seq[1].Map().at("b").Sequence().at(1).Scalar(), "2");
UNIT_ASSERT_VALUES_EQUAL(seq[1].Map().at("b").Sequence().at(2).Scalar(), "3");
}
+
+ Y_UNIT_TEST(SimpleScalarMark) {
+ auto check = [](const TString& str, const NFyaml::TNodeRef& node) {
+ auto pos = str.find("123");
+ auto endPos = pos + strlen("123");
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+ UNIT_ASSERT_VALUES_EQUAL(begin, pos);
+ UNIT_ASSERT_VALUES_EQUAL(end, endPos);
+ };
+
+ {
+ TString str = R"(obj: 123)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(obj: 123 # test)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+# test
+obj: 123 # test
+# test
+)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+---
+obj: 123
+...
+)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+a: foo
+test: [{obj: 123}]
+b: bar
+ )";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("test").Sequence().at(0).Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(obj: '123')";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(obj: '123' # test)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+# test
+obj: '123' # test
+# test
+)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+---
+obj: '123'
+...
+)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+a: foo
+test: [{obj: "123"}]
+b: bar
+ )";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("test").Sequence().at(0).Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(obj: "123")";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(obj: "123" # test)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+# test
+obj: "123" # test
+# test
+)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+---
+obj: "123"
+...
+)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+a: foo
+test: [{obj: "123"}]
+b: bar
+ )";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("test").Sequence().at(0).Map().at("obj");
+ check(str, node);
+ }
+
+ {
+ TString str = R"(
+a: foo
+test: [{obj: !!int "123"}]
+b: bar
+ )";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("test").Sequence().at(0).Map().at("obj");
+ check(str, node);
+ }
+
+ }
+
+ Y_UNIT_TEST(MultilineScalarMark) {
+ {
+ TString str = R"(obj: >+2
+ some
+ multiline
+
+ scalar with couple words)";
+ auto doc = NFyaml::TDocument::Parse(str);
+ auto node = doc.Root().Map().at("obj");
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+ UNIT_ASSERT_VALUES_EQUAL(begin, 9);
+ UNIT_ASSERT_VALUES_EQUAL(end, 55);
+ }
+ }
+
+ Y_UNIT_TEST(MapMark) {
+ {
+ TString str = R"(
+a: foo
+map: !!map
+ internal_map1: {}
+ internal_map2:
+ internal_map3:
+ internal_map4:
+ value: 1
+ internal_map5: {
+ internal_map6: {test1: 1, test2: 2},
+ internal_map7: {
+ value: 1
+ }
+ }
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("map");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 21);
+ UNIT_ASSERT_VALUES_EQUAL(end, 246);
+ }
+
+ {
+ TString str = R"(
+a: foo
+map: !!map # comment
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("map");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 11);
+ UNIT_ASSERT_VALUES_EQUAL(end, 11);
+ }
+
+ {
+ TString str = R"(
+a: foo
+map: {} # comment
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("map");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 13);
+ UNIT_ASSERT_VALUES_EQUAL(end, 15);
+ }
+
+ {
+ TString str = R"(
+a: foo
+map:
+ value: 1
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("map");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 15);
+ UNIT_ASSERT_VALUES_EQUAL(end, 23);
+ }
+ }
+
+ Y_UNIT_TEST(SequenceMark) {
+ {
+ TString str = R"(
+a: foo
+seq: !!seq
+- internal_map1: {}
+- internal_seq2:
+ - internal_seq3:
+ - internal_seq4:
+ value: 1
+ - internal_seq5: [
+ internal_seq6: [{test1: 1}, {test2: 2}],
+ internal_seq7: [
+ {value: 1}
+ ]
+ ]
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("seq");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 19);
+ UNIT_ASSERT_VALUES_EQUAL(end, 252);
+ }
+
+ {
+ TString str = R"(
+a: foo
+seq: !!seq # comment
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("seq");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 11);
+ UNIT_ASSERT_VALUES_EQUAL(end, 11);
+ }
+
+ {
+ TString str = R"(
+a: foo
+seq: [] # comment
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("seq");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 13);
+ UNIT_ASSERT_VALUES_EQUAL(end, 15);
+ }
+
+ {
+ TString str = R"(
+a: foo
+seq:
+- value: 1
+# comment
+c: bar
+ )";
+
+ auto doc = NFyaml::TDocument::Parse(str);
+
+ auto node = doc.Root().Map().at("seq");
+
+ auto begin = node.BeginMark().InputPos;
+ auto end = node.EndMark().InputPos;
+
+ UNIT_ASSERT_VALUES_EQUAL(begin, 13);
+ UNIT_ASSERT_VALUES_EQUAL(end, 23);
+ }
+ }
+
}