node_visitor.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "node_visitor.h"
  2. #include <util/generic/algorithm.h>
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. namespace {
  6. template <typename Fun>
  7. void Iterate(const TNode::TMapType& nodeMap, bool sortByKey, Fun action)
  8. {
  9. if (sortByKey) {
  10. TVector<TNode::TMapType::const_iterator> iterators;
  11. for (auto it = nodeMap.begin(); it != nodeMap.end(); ++it) {
  12. iterators.push_back(it);
  13. }
  14. SortBy(iterators, [](TNode::TMapType::const_iterator it) { return it->first; });
  15. for (const auto& it : iterators) {
  16. action(*it);
  17. }
  18. } else {
  19. ForEach(nodeMap.begin(), nodeMap.end(), action);
  20. }
  21. }
  22. } // namespace
  23. ////////////////////////////////////////////////////////////////////////////////
  24. TNodeVisitor::TNodeVisitor(NYson::IYsonConsumer* consumer, bool sortMapKeys)
  25. : Consumer_(consumer)
  26. , SortMapKeys_(sortMapKeys)
  27. { }
  28. void TNodeVisitor::Visit(const TNode& node)
  29. {
  30. VisitAny(node);
  31. }
  32. void TNodeVisitor::VisitAny(const TNode& node)
  33. {
  34. if (node.HasAttributes()) {
  35. Consumer_->OnBeginAttributes();
  36. Iterate(node.GetAttributes().AsMap(), SortMapKeys_, [&](const auto& item) {
  37. Consumer_->OnKeyedItem(item.first);
  38. if (item.second.IsUndefined()) {
  39. ythrow TNode::TTypeError() << "unable to visit attribute value of type "
  40. << TNode::EType::Undefined << "; attribute name: `" << item.first << '\'' ;
  41. }
  42. VisitAny(item.second);
  43. });
  44. Consumer_->OnEndAttributes();
  45. }
  46. switch (node.GetType()) {
  47. case TNode::String:
  48. VisitString(node);
  49. break;
  50. case TNode::Int64:
  51. VisitInt64(node);
  52. break;
  53. case TNode::Uint64:
  54. VisitUint64(node);
  55. break;
  56. case TNode::Double:
  57. VisitDouble(node);
  58. break;
  59. case TNode::Bool:
  60. VisitBool(node);
  61. break;
  62. case TNode::List:
  63. VisitList(node.AsList());
  64. break;
  65. case TNode::Map:
  66. VisitMap(node.AsMap());
  67. break;
  68. case TNode::Null:
  69. VisitEntity();
  70. break;
  71. case TNode::Undefined:
  72. ythrow TNode::TTypeError() << "unable to visit TNode of type " << node.GetType();
  73. default:
  74. Y_ABORT("Unexpected type: %d", node.GetType());
  75. }
  76. }
  77. void TNodeVisitor::VisitString(const TNode& node)
  78. {
  79. Consumer_->OnStringScalar(node.AsString());
  80. }
  81. void TNodeVisitor::VisitInt64(const TNode& node)
  82. {
  83. Consumer_->OnInt64Scalar(node.AsInt64());
  84. }
  85. void TNodeVisitor::VisitUint64(const TNode& node)
  86. {
  87. Consumer_->OnUint64Scalar(node.AsUint64());
  88. }
  89. void TNodeVisitor::VisitDouble(const TNode& node)
  90. {
  91. Consumer_->OnDoubleScalar(node.AsDouble());
  92. }
  93. void TNodeVisitor::VisitBool(const TNode& node)
  94. {
  95. Consumer_->OnBooleanScalar(node.AsBool());
  96. }
  97. void TNodeVisitor::VisitList(const TNode::TListType& nodeList)
  98. {
  99. Consumer_->OnBeginList();
  100. size_t index = 0;
  101. for (const auto& item : nodeList) {
  102. Consumer_->OnListItem();
  103. if (item.IsUndefined()) {
  104. ythrow TNode::TTypeError() << "unable to visit list node child of type "
  105. << TNode::EType::Undefined << "; list index: " << index;
  106. }
  107. VisitAny(item);
  108. ++index;
  109. }
  110. Consumer_->OnEndList();
  111. }
  112. void TNodeVisitor::VisitMap(const TNode::TMapType& nodeMap)
  113. {
  114. Consumer_->OnBeginMap();
  115. Iterate(nodeMap, SortMapKeys_, [&](const auto& item) {
  116. Consumer_->OnKeyedItem(item.first);
  117. if (item.second.IsUndefined()) {
  118. ythrow TNode::TTypeError() << "unable to visit map node child of type "
  119. << TNode::EType::Undefined << "; map key: `" << item.first << '\'' ;
  120. }
  121. VisitAny(item.second);
  122. });
  123. Consumer_->OnEndMap();
  124. }
  125. void TNodeVisitor::VisitEntity()
  126. {
  127. Consumer_->OnEntity();
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////
  130. } // namespace NYT