diff options
author | monster <monster@yandex-team.ru> | 2022-02-10 16:47:19 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:19 +0300 |
commit | b23c1d7a8015c2006a148fd93b84cdeb0aee17a3 (patch) | |
tree | 9814fbd1c3effac9b8377c5d604b367b14e2db55 /library/cpp/yson/node | |
parent | dd76ae1f6213d065375ab296699f764faafbe5bd (diff) | |
download | ydb-b23c1d7a8015c2006a148fd93b84cdeb0aee17a3.tar.gz |
Restoring authorship annotation for <monster@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/yson/node')
-rw-r--r-- | library/cpp/yson/node/node.cpp | 222 | ||||
-rw-r--r-- | library/cpp/yson/node/node.h | 116 | ||||
-rw-r--r-- | library/cpp/yson/node/node_builder.cpp | 174 | ||||
-rw-r--r-- | library/cpp/yson/node/node_builder.h | 48 | ||||
-rw-r--r-- | library/cpp/yson/node/node_io.cpp | 66 | ||||
-rw-r--r-- | library/cpp/yson/node/node_io.h | 22 | ||||
-rw-r--r-- | library/cpp/yson/node/node_visitor.cpp | 178 | ||||
-rw-r--r-- | library/cpp/yson/node/node_visitor.h | 58 |
8 files changed, 442 insertions, 442 deletions
diff --git a/library/cpp/yson/node/node.cpp b/library/cpp/yson/node/node.cpp index a533e34fa6..b39e070718 100644 --- a/library/cpp/yson/node/node.cpp +++ b/library/cpp/yson/node/node.cpp @@ -1,5 +1,5 @@ #include "node.h" - + #include "node_io.h" #include <library/cpp/yson/writer.h> @@ -13,15 +13,15 @@ namespace NYT { bool TNode::TNull::operator==(const TNull&) const { return true; } - -//////////////////////////////////////////////////////////////////////////////// - + +//////////////////////////////////////////////////////////////////////////////// + bool TNode::TUndefined::operator==(const TUndefined&) const { return true; } - + //////////////////////////////////////////////////////////////////////////////// - + namespace NNodeCmp { bool IsComparableType(const TNode::EType type) { @@ -94,15 +94,15 @@ bool operator>=(const TNode& lhs, const TNode& rhs) TNode::TNode() : Value_(TUndefined{}) { } - + TNode::TNode(const char* s) : Value_(TString(s)) { } - + TNode::TNode(TStringBuf s) : Value_(TString(s)) { } - + TNode::TNode(std::string_view s) : Value_(TString(s)) { } @@ -114,12 +114,12 @@ TNode::TNode(const std::string& s) TNode::TNode(TString s) : Value_(std::move(s)) { } - + TNode::TNode(int i) : Value_(static_cast<i64>(i)) { } - - + + TNode::TNode(unsigned int ui) : Value_(static_cast<ui64>(ui)) { } @@ -127,27 +127,27 @@ TNode::TNode(unsigned int ui) TNode::TNode(long i) : Value_(static_cast<i64>(i)) { } - + TNode::TNode(unsigned long ui) : Value_(static_cast<ui64>(ui)) { } - + TNode::TNode(long long i) : Value_(static_cast<i64>(i)) { } - + TNode::TNode(unsigned long long ui) : Value_(static_cast<ui64>(ui)) { } - + TNode::TNode(double d) : Value_(d) { } - + TNode::TNode(bool b) : Value_(b) { } - + TNode::TNode(TMapType map) : Value_(std::move(map)) { } @@ -161,39 +161,39 @@ TNode::TNode(const TNode& rhs) } Value_ = rhs.Value_; } - + TNode& TNode::operator=(const TNode& rhs) { if (this != &rhs) { TNode tmp = rhs; Move(std::move(tmp)); - } + } return *this; } - + TNode::TNode(TNode&& rhs) noexcept : TNode() { Move(std::move(rhs)); } - + TNode& TNode::operator=(TNode&& rhs) noexcept { if (this != &rhs) { TNode tmp = std::move(rhs); Move(std::move(tmp)); - } + } return *this; } - + TNode::~TNode() = default; - + void TNode::Clear() { ClearAttributes(); Value_ = TUndefined(); } - + bool TNode::IsString() const { return std::holds_alternative<TString>(Value_); @@ -203,37 +203,37 @@ bool TNode::IsInt64() const { return std::holds_alternative<i64>(Value_); } - + bool TNode::IsUint64() const { return std::holds_alternative<ui64>(Value_); } - + bool TNode::IsDouble() const { return std::holds_alternative<double>(Value_); } - + bool TNode::IsBool() const { return std::holds_alternative<bool>(Value_); } - + bool TNode::IsList() const { return std::holds_alternative<TListType>(Value_); } - + bool TNode::IsMap() const { return std::holds_alternative<TMapType>(Value_); } - + bool TNode::IsEntity() const { return IsNull(); } - + bool TNode::IsNull() const { return std::holds_alternative<TNull>(Value_); @@ -260,9 +260,9 @@ bool TNode::Empty() const return std::get<TMapType>(Value_).empty(); default: ythrow TTypeError() << "Empty() called for type " << GetType(); - } + } } - + size_t TNode::Size() const { switch (GetType()) { @@ -274,9 +274,9 @@ size_t TNode::Size() const return std::get<TMapType>(Value_).size(); default: ythrow TTypeError() << "Size() called for type " << GetType(); - } + } } - + TNode::EType TNode::GetType() const { return std::visit(TOverloaded{ @@ -291,61 +291,61 @@ TNode::EType TNode::GetType() const [](const TNull&) { return Null; } }, Value_); } - + const TString& TNode::AsString() const { CheckType(String); return std::get<TString>(Value_); } - + i64 TNode::AsInt64() const { CheckType(Int64); return std::get<i64>(Value_); } - + ui64 TNode::AsUint64() const { CheckType(Uint64); return std::get<ui64>(Value_); } - + double TNode::AsDouble() const { CheckType(Double); return std::get<double>(Value_); } - + bool TNode::AsBool() const { CheckType(Bool); return std::get<bool>(Value_); } - + const TNode::TListType& TNode::AsList() const { CheckType(List); return std::get<TListType>(Value_); } - + const TNode::TMapType& TNode::AsMap() const { CheckType(Map); return std::get<TMapType>(Value_); } - + TNode::TListType& TNode::AsList() { CheckType(List); return std::get<TListType>(Value_); } - + TNode::TMapType& TNode::AsMap() { CheckType(Map); return std::get<TMapType>(Value_); } - + const TString& TNode::UncheckedAsString() const noexcept { return std::get<TString>(Value_); @@ -397,7 +397,7 @@ TNode TNode::CreateList() node.Value_ = TListType{}; return node; } - + TNode TNode::CreateList(TListType list) { TNode node; @@ -411,7 +411,7 @@ TNode TNode::CreateMap() node.Value_ = TMapType{}; return node; } - + TNode TNode::CreateMap(TMapType map) { TNode node; @@ -425,19 +425,19 @@ TNode TNode::CreateEntity() node.Value_ = TNull{}; return node; } - + const TNode& TNode::operator[](size_t index) const { CheckType(List); return std::get<TListType>(Value_)[index]; } - + TNode& TNode::operator[](size_t index) { CheckType(List); return std::get<TListType>(Value_)[index]; } - + const TNode& TNode::At(size_t index) const { CheckType(List); const auto& list = std::get<TListType>(Value_); @@ -461,7 +461,7 @@ TNode& TNode::Add() & AssureList(); return std::get<TListType>(Value_).emplace_back(); } - + TNode TNode::Add() && { return std::move(Add()); @@ -473,7 +473,7 @@ TNode& TNode::Add(const TNode& node) & std::get<TListType>(Value_).emplace_back(node); return *this; } - + TNode TNode::Add(const TNode& node) && { return std::move(Add(node)); @@ -485,7 +485,7 @@ TNode& TNode::Add(TNode&& node) & std::get<TListType>(Value_).emplace_back(std::move(node)); return *this; } - + TNode TNode::Add(TNode&& node) && { return std::move(Add(std::move(node))); @@ -496,14 +496,14 @@ bool TNode::HasKey(const TStringBuf key) const CheckType(Map); return std::get<TMapType>(Value_).contains(key); } - + TNode& TNode::operator()(const TString& key, const TNode& value) & { AssureMap(); std::get<TMapType>(Value_)[key] = value; return *this; } - + TNode TNode::operator()(const TString& key, const TNode& value) && { return std::move(operator()(key, value)); @@ -515,7 +515,7 @@ TNode& TNode::operator()(const TString& key, TNode&& value) & std::get<TMapType>(Value_)[key] = std::move(value); return *this; } - + TNode TNode::operator()(const TString& key, TNode&& value) && { return std::move(operator()(key, std::move(value))); @@ -531,15 +531,15 @@ const TNode& TNode::operator[](const TStringBuf key) const return notFound; } else { return i->second; - } + } } - + TNode& TNode::operator[](const TStringBuf key) { AssureMap(); return std::get<TMapType>(Value_)[key]; } - + const TNode& TNode::At(const TStringBuf key) const { CheckType(Map); const auto& map = std::get<TMapType>(Value_); @@ -782,68 +782,68 @@ bool TNode::HasAttributes() const { return Attributes_ && !Attributes_->Empty(); } - + void TNode::ClearAttributes() { if (Attributes_) { Attributes_.Destroy(); - } + } } - + const TNode& TNode::GetAttributes() const { static TNode notFound = TNode::CreateMap(); if (!Attributes_) { return notFound; - } + } return *Attributes_; } - + TNode& TNode::Attributes() { if (!Attributes_) { CreateAttributes(); - } + } return *Attributes_; } - + void TNode::MoveWithoutAttributes(TNode&& rhs) { Value_ = std::move(rhs.Value_); rhs.Clear(); } - + void TNode::Move(TNode&& rhs) { Value_ = std::move(rhs.Value_); Attributes_ = std::move(rhs.Attributes_); } - + void TNode::CheckType(EType type) const { Y_ENSURE_EX(GetType() == type, TTypeError() << "TNode type " << type << " expected, actual type " << GetType(); ); } - + void TNode::AssureMap() { if (std::holds_alternative<TUndefined>(Value_)) { Value_ = TMapType(); } else { CheckType(Map); - } + } } - + void TNode::AssureList() { if (std::holds_alternative<TUndefined>(Value_)) { Value_ = TListType(); } else { CheckType(List); - } + } } - + void TNode::CreateAttributes() { Attributes_ = MakeHolder<TNode>(); @@ -862,54 +862,54 @@ void TNode::Load(IInputStream* in) } //////////////////////////////////////////////////////////////////////////////// - + bool operator==(const TNode& lhs, const TNode& rhs) { if (std::holds_alternative<TNode::TUndefined>(lhs.Value_) || std::holds_alternative<TNode::TUndefined>(rhs.Value_)) - { + { // TODO: should try to remove this behaviour if nobody uses it. return false; - } - + } + if (lhs.GetType() != rhs.GetType()) { - return false; - } - - if (lhs.Attributes_) { - if (rhs.Attributes_) { + return false; + } + + if (lhs.Attributes_) { + if (rhs.Attributes_) { if (*lhs.Attributes_ != *rhs.Attributes_) { return false; } - } else { - return false; - } - } else { - if (rhs.Attributes_) { - return false; - } - } - + } else { + return false; + } + } else { + if (rhs.Attributes_) { + return false; + } + } + return rhs.Value_ == lhs.Value_; -} - +} + bool operator!=(const TNode& lhs, const TNode& rhs) -{ - return !(lhs == rhs); -} - +{ + return !(lhs == rhs); +} + bool GetBool(const TNode& node) -{ - if (node.IsBool()) { - return node.AsBool(); - } else if (node.IsString()) { - return node.AsString() == "true"; - } else { +{ + if (node.IsBool()) { + return node.AsBool(); + } else if (node.IsString()) { + return node.AsString() == "true"; + } else { ythrow TNode::TTypeError() << "GetBool(): not a boolean or string type"; - } -} - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT + } +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yson/node/node.h b/library/cpp/yson/node/node.h index bc7609e6da..5f90f95df0 100644 --- a/library/cpp/yson/node/node.h +++ b/library/cpp/yson/node/node.h @@ -1,11 +1,11 @@ -#pragma once - +#pragma once + #include <util/generic/bt_exception.h> #include <util/generic/cast.h> -#include <util/generic/hash.h> +#include <util/generic/hash.h> #include <util/generic/variant.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> #include <util/generic/ylimits.h> #include <util/string/cast.h> @@ -14,23 +14,23 @@ class IInputStream; class IOutputStream; - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -class TNode -{ -public: + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +class TNode +{ +public: class TLookupError : public TWithBackTrace<yexception> { }; - class TTypeError + class TTypeError : public TWithBackTrace<yexception> - { }; - - enum EType { + { }; + + enum EType { Undefined = 0 /*"undefined"*/, // NOTE: string representation of all node types @@ -43,20 +43,20 @@ public: List = 6 /*"list_node"*/, Map = 7 /*"map_node"*/, Null = 8 /*"null"*/, - }; - + }; + using TListType = TVector<TNode>; using TMapType = THashMap<TString, TNode>; - + private: struct TNull { bool operator==(const TNull&) const; }; - + struct TUndefined { bool operator==(const TUndefined&) const; }; - + using TValue = std::variant< bool, i64, @@ -68,9 +68,9 @@ private: TNull, TUndefined >; - + public: - + TNode(); TNode(const char* s); TNode(TStringBuf s); @@ -78,7 +78,7 @@ public: explicit TNode(const std::string& s); TNode(TString s); TNode(int i); - + //this case made speccially for prevent mess cast of EType into TNode through TNode(int) constructor //usual case of error SomeNode == TNode::Undefined <-- SomeNode indeed will be compared with TNode(0) without this method //correct way is SomeNode.GetType() == TNode::Undefined @@ -102,17 +102,17 @@ public: TNode(double d); TNode(bool b); TNode(TMapType map); - + TNode(const TNode& rhs); TNode& operator=(const TNode& rhs); - + TNode(TNode&& rhs) noexcept; TNode& operator=(TNode&& rhs) noexcept; - + ~TNode(); - + void Clear(); - + bool IsString() const; bool IsInt64() const; bool IsUint64() const; @@ -127,7 +127,7 @@ public: bool IsUndefined() const; // Returns true if TNode is neither Null, nor Undefined bool HasValue() const; - + template<typename T> bool IsOfType() const noexcept; @@ -136,9 +136,9 @@ public: bool Empty() const; size_t Size() const; - + EType GetType() const; - + const TString& AsString() const; i64 AsInt64() const; ui64 AsUint64() const; @@ -148,7 +148,7 @@ public: const TMapType& AsMap() const; TListType& AsList(); TMapType& AsMap(); - + const TString& UncheckedAsString() const noexcept; i64 UncheckedAsInt64() const noexcept; ui64 UncheckedAsUint64() const noexcept; @@ -180,31 +180,31 @@ public: static TNode CreateMap(); static TNode CreateMap(TMapType map); static TNode CreateEntity(); - + const TNode& operator[](size_t index) const; TNode& operator[](size_t index); const TNode& At(size_t index) const; TNode& At(size_t index); - + TNode& Add() &; TNode Add() &&; TNode& Add(const TNode& node) &; TNode Add(const TNode& node) &&; TNode& Add(TNode&& node) &; TNode Add(TNode&& node) &&; - + bool HasKey(const TStringBuf key) const; TNode& operator()(const TString& key, const TNode& value) &; TNode operator()(const TString& key, const TNode& value) &&; TNode& operator()(const TString& key, TNode&& value) &; TNode operator()(const TString& key, TNode&& value) &&; - + const TNode& operator[](const TStringBuf key) const; TNode& operator[](const TStringBuf key); const TNode& At(const TStringBuf key) const; TNode& At(const TStringBuf key); - + // map getters // works the same way like simple getters const TString& ChildAsString(const TStringBuf key) const; @@ -254,42 +254,42 @@ public: T& ChildAs(size_t index); - // attributes + // attributes bool HasAttributes() const; void ClearAttributes(); const TNode& GetAttributes() const; TNode& Attributes(); - + void MoveWithoutAttributes(TNode&& rhs); - + // Serialize TNode using binary yson format. // Methods for ysaveload. void Save(IOutputStream* output) const; void Load(IInputStream* input); -private: +private: void Move(TNode&& rhs); - + void CheckType(EType type) const; - + void AssureMap(); void AssureList(); - + void CreateAttributes(); - -private: + +private: TValue Value_; THolder<TNode> Attributes_; - - friend bool operator==(const TNode& lhs, const TNode& rhs); - friend bool operator!=(const TNode& lhs, const TNode& rhs); -}; - + + friend bool operator==(const TNode& lhs, const TNode& rhs); + friend bool operator!=(const TNode& lhs, const TNode& rhs); +}; + bool operator==(const TNode& lhs, const TNode& rhs); bool operator!=(const TNode& lhs, const TNode& rhs); - + bool GetBool(const TNode& node); - + inline bool TNode::IsArithmetic() const { return IsInt64() || IsUint64() || IsDouble() || IsBool(); } @@ -508,8 +508,8 @@ inline const T& TNode::As() const { return std::get<T>(Value_); } -//////////////////////////////////////////////////////////////////////////////// - +//////////////////////////////////////////////////////////////////////////////// + namespace NNodeCmp { bool operator<(const TNode& lhs, const TNode& rhs); bool operator<=(const TNode& lhs, const TNode& rhs); @@ -520,4 +520,4 @@ namespace NNodeCmp { //////////////////////////////////////////////////////////////////////////////// -} // namespace NYT +} // namespace NYT diff --git a/library/cpp/yson/node/node_builder.cpp b/library/cpp/yson/node/node_builder.cpp index b21bc4502e..b4431bc77a 100644 --- a/library/cpp/yson/node/node_builder.cpp +++ b/library/cpp/yson/node/node_builder.cpp @@ -1,96 +1,96 @@ -#include "node_builder.h" - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -TNodeBuilder::TNodeBuilder(TNode* node) -{ - Stack_.push(node); -} - +#include "node_builder.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +TNodeBuilder::TNodeBuilder(TNode* node) +{ + Stack_.push(node); +} + void TNodeBuilder::OnStringScalar(TStringBuf value) -{ - AddNode(value, true); -} - -void TNodeBuilder::OnInt64Scalar(i64 value) -{ - AddNode(value, true); -} - -void TNodeBuilder::OnUint64Scalar(ui64 value) -{ - AddNode(value, true); -} - -void TNodeBuilder::OnDoubleScalar(double value) -{ - AddNode(value, true); -} - -void TNodeBuilder::OnBooleanScalar(bool value) -{ - AddNode(value, true); -} - -void TNodeBuilder::OnEntity() -{ - AddNode(TNode::CreateEntity(), true); -} - -void TNodeBuilder::OnBeginList() -{ - AddNode(TNode::CreateList(), false); -} - -void TNodeBuilder::OnListItem() -{ - Stack_.push(&Stack_.top()->Add()); -} - -void TNodeBuilder::OnEndList() -{ - Stack_.pop(); -} - -void TNodeBuilder::OnBeginMap() -{ - AddNode(TNode::CreateMap(), false); -} - +{ + AddNode(value, true); +} + +void TNodeBuilder::OnInt64Scalar(i64 value) +{ + AddNode(value, true); +} + +void TNodeBuilder::OnUint64Scalar(ui64 value) +{ + AddNode(value, true); +} + +void TNodeBuilder::OnDoubleScalar(double value) +{ + AddNode(value, true); +} + +void TNodeBuilder::OnBooleanScalar(bool value) +{ + AddNode(value, true); +} + +void TNodeBuilder::OnEntity() +{ + AddNode(TNode::CreateEntity(), true); +} + +void TNodeBuilder::OnBeginList() +{ + AddNode(TNode::CreateList(), false); +} + +void TNodeBuilder::OnListItem() +{ + Stack_.push(&Stack_.top()->Add()); +} + +void TNodeBuilder::OnEndList() +{ + Stack_.pop(); +} + +void TNodeBuilder::OnBeginMap() +{ + AddNode(TNode::CreateMap(), false); +} + void TNodeBuilder::OnKeyedItem(TStringBuf key) -{ +{ Stack_.push(&(*Stack_.top())[TString(key)]); -} - -void TNodeBuilder::OnEndMap() -{ - Stack_.pop(); -} - -void TNodeBuilder::OnBeginAttributes() -{ - Stack_.push(&Stack_.top()->Attributes()); -} - -void TNodeBuilder::OnEndAttributes() -{ - Stack_.pop(); -} - +} + +void TNodeBuilder::OnEndMap() +{ + Stack_.pop(); +} + +void TNodeBuilder::OnBeginAttributes() +{ + Stack_.push(&Stack_.top()->Attributes()); +} + +void TNodeBuilder::OnEndAttributes() +{ + Stack_.pop(); +} + void TNodeBuilder::OnNode(TNode node) { AddNode(std::move(node), true); } -void TNodeBuilder::AddNode(TNode value, bool pop) -{ +void TNodeBuilder::AddNode(TNode value, bool pop) +{ Stack_.top()->MoveWithoutAttributes(std::move(value)); - if (pop) - Stack_.pop(); -} - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT + if (pop) + Stack_.pop(); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yson/node/node_builder.h b/library/cpp/yson/node/node_builder.h index 35bb256a5e..69800016e0 100644 --- a/library/cpp/yson/node/node_builder.h +++ b/library/cpp/yson/node/node_builder.h @@ -1,23 +1,23 @@ -#pragma once - +#pragma once + #include "node.h" - + #include <library/cpp/json/json_reader.h> #include <library/cpp/yson/consumer.h> -#include <util/generic/stack.h> - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -class TNodeBuilder +#include <util/generic/stack.h> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +class TNodeBuilder : public ::NYson::TYsonConsumerBase -{ -public: - TNodeBuilder(TNode* node); - +{ +public: + TNodeBuilder(TNode* node); + void OnStringScalar(TStringBuf) override; void OnInt64Scalar(i64) override; void OnUint64Scalar(ui64) override; @@ -33,14 +33,14 @@ public: void OnBeginAttributes() override; void OnEndAttributes() override; void OnNode(TNode node); - -private: + +private: TStack<TNode*> Stack_; - -private: - inline void AddNode(TNode node, bool pop); -}; - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT + +private: + inline void AddNode(TNode node, bool pop); +}; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yson/node/node_io.cpp b/library/cpp/yson/node/node_io.cpp index ea53e4d8b7..294a7f7217 100644 --- a/library/cpp/yson/node/node_io.cpp +++ b/library/cpp/yson/node/node_io.cpp @@ -1,8 +1,8 @@ #include "node_io.h" - -#include "node_builder.h" -#include "node_visitor.h" - + +#include "node_builder.h" +#include "node_visitor.h" + #include <library/cpp/yson/json/json_writer.h> #include <library/cpp/yson/parser.h> #include <library/cpp/yson/writer.h> @@ -11,13 +11,13 @@ #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_value.h> -#include <util/stream/input.h> -#include <util/stream/output.h> -#include <util/stream/str.h> +#include <util/stream/input.h> +#include <util/stream/output.h> +#include <util/stream/str.h> #include <util/stream/mem.h> - -namespace NYT { - + +namespace NYT { + static void WalkJsonTree(const NJson::TJsonValue& jsonValue, NJson::TJsonCallbacks* callbacks) { using namespace NJson; @@ -68,20 +68,20 @@ static void WalkJsonTree(const NJson::TJsonValue& jsonValue, NJson::TJsonCallbac static TNode CreateEmptyNodeByType(::NYson::EYsonType type) { - TNode result; - switch (type) { + TNode result; + switch (type) { case ::NYson::EYsonType::ListFragment: - result = TNode::CreateList(); - break; + result = TNode::CreateList(); + break; case ::NYson::EYsonType::MapFragment: - result = TNode::CreateMap(); - break; - default: - break; - } + result = TNode::CreateMap(); + break; + default: + break; + } return result; } - + TNode NodeFromYsonString(const TStringBuf input, ::NYson::EYsonType type) { TMemoryInput stream(input); @@ -106,19 +106,19 @@ TNode NodeFromYsonStream(IInputStream* input, ::NYson::EYsonType type) { TNode result = CreateEmptyNodeByType(type); - TNodeBuilder builder(&result); + TNodeBuilder builder(&result); ::NYson::TYsonParser parser(&builder, input, type); - parser.Parse(); - return result; -} - + parser.Parse(); + return result; +} + void NodeToYsonStream(const TNode& node, IOutputStream* output, NYson::EYsonFormat format) -{ +{ ::NYson::TYsonWriter writer(output, format); - TNodeVisitor visitor(&writer); - visitor.Visit(node); -} - + TNodeVisitor visitor(&writer); + visitor.Visit(node); +} + void NodeToCanonicalYsonStream(const TNode& node, IOutputStream* output, NYson::EYsonFormat format) { ::NYson::TYsonWriter writer(output, format); @@ -149,6 +149,6 @@ TNode NodeFromJsonValue(const NJson::TJsonValue& input) return result; } -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yson/node/node_io.h b/library/cpp/yson/node/node_io.h index 5b63243317..2ad23b658f 100644 --- a/library/cpp/yson/node/node_io.h +++ b/library/cpp/yson/node/node_io.h @@ -1,22 +1,22 @@ -#pragma once - +#pragma once + #include "node.h" #include <library/cpp/yson/public.h> - + namespace NJson { class TJsonValue; } // namespace NJson -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + // Parse TNode from string in YSON format TNode NodeFromYsonString(const TStringBuf input, ::NYson::EYsonType type = ::NYson::EYsonType::Node); // Serialize TNode to string in one of YSON formats with random order of maps' keys (don't use in tests) TString NodeToYsonString(const TNode& node, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text); - + // Same as the latter, but maps' keys are sorted lexicographically (to be used in tests) TString NodeToCanonicalYsonString(const TNode& node, ::NYson::EYsonFormat format = ::NYson::EYsonFormat::Text); @@ -35,6 +35,6 @@ TNode NodeFromJsonString(const TStringBuf input); // Convert TJsonValue to TNode TNode NodeFromJsonValue(const NJson::TJsonValue& input); -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yson/node/node_visitor.cpp b/library/cpp/yson/node/node_visitor.cpp index 824d934867..899fbfa02a 100644 --- a/library/cpp/yson/node/node_visitor.cpp +++ b/library/cpp/yson/node/node_visitor.cpp @@ -1,12 +1,12 @@ -#include "node_visitor.h" - +#include "node_visitor.h" + #include <util/generic/algorithm.h> #include <util/string/printf.h> -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + namespace { template <typename Fun> @@ -31,122 +31,122 @@ void Iterate(const TNode::TMapType& nodeMap, bool sortByKey, Fun action) //////////////////////////////////////////////////////////////////////////////// TNodeVisitor::TNodeVisitor(NYson::IYsonConsumer* consumer, bool sortMapKeys) - : Consumer_(consumer) + : Consumer_(consumer) , SortMapKeys_(sortMapKeys) -{ } - -void TNodeVisitor::Visit(const TNode& node) -{ - VisitAny(node); -} - -void TNodeVisitor::VisitAny(const TNode& node) -{ - if (node.HasAttributes()) { - Consumer_->OnBeginAttributes(); +{ } + +void TNodeVisitor::Visit(const TNode& node) +{ + VisitAny(node); +} + +void TNodeVisitor::VisitAny(const TNode& node) +{ + if (node.HasAttributes()) { + Consumer_->OnBeginAttributes(); Iterate(node.GetAttributes().AsMap(), SortMapKeys_, [&](const std::pair<TString, TNode>& item) { - Consumer_->OnKeyedItem(item.first); + Consumer_->OnKeyedItem(item.first); if (item.second.IsUndefined()) { ythrow TNode::TTypeError() << "unable to visit attribute value of type " << TNode::EType::Undefined << "; attribute name: `" << item.first << '\'' ; } - VisitAny(item.second); + VisitAny(item.second); }); - Consumer_->OnEndAttributes(); - } - - switch (node.GetType()) { + Consumer_->OnEndAttributes(); + } + + switch (node.GetType()) { case TNode::String: - VisitString(node); - break; + VisitString(node); + break; case TNode::Int64: - VisitInt64(node); - break; + VisitInt64(node); + break; case TNode::Uint64: - VisitUint64(node); - break; + VisitUint64(node); + break; case TNode::Double: - VisitDouble(node); - break; + VisitDouble(node); + break; case TNode::Bool: - VisitBool(node); - break; + VisitBool(node); + break; case TNode::List: VisitList(node.AsList()); - break; + break; case TNode::Map: VisitMap(node.AsMap()); - break; + break; case TNode::Null: - VisitEntity(); - break; + VisitEntity(); + break; case TNode::Undefined: ythrow TNode::TTypeError() << "unable to visit TNode of type " << node.GetType(); default: Y_FAIL("Unexpected type: %d", node.GetType()); - } -} - -void TNodeVisitor::VisitString(const TNode& node) -{ - Consumer_->OnStringScalar(node.AsString()); -} - -void TNodeVisitor::VisitInt64(const TNode& node) -{ - Consumer_->OnInt64Scalar(node.AsInt64()); -} - -void TNodeVisitor::VisitUint64(const TNode& node) -{ - Consumer_->OnUint64Scalar(node.AsUint64()); -} - -void TNodeVisitor::VisitDouble(const TNode& node) -{ - Consumer_->OnDoubleScalar(node.AsDouble()); -} - -void TNodeVisitor::VisitBool(const TNode& node) -{ - Consumer_->OnBooleanScalar(node.AsBool()); -} - + } +} + +void TNodeVisitor::VisitString(const TNode& node) +{ + Consumer_->OnStringScalar(node.AsString()); +} + +void TNodeVisitor::VisitInt64(const TNode& node) +{ + Consumer_->OnInt64Scalar(node.AsInt64()); +} + +void TNodeVisitor::VisitUint64(const TNode& node) +{ + Consumer_->OnUint64Scalar(node.AsUint64()); +} + +void TNodeVisitor::VisitDouble(const TNode& node) +{ + Consumer_->OnDoubleScalar(node.AsDouble()); +} + +void TNodeVisitor::VisitBool(const TNode& node) +{ + Consumer_->OnBooleanScalar(node.AsBool()); +} + void TNodeVisitor::VisitList(const TNode::TListType& nodeList) -{ - Consumer_->OnBeginList(); +{ + Consumer_->OnBeginList(); size_t index = 0; for (const auto& item : nodeList) { - Consumer_->OnListItem(); + Consumer_->OnListItem(); if (item.IsUndefined()) { ythrow TNode::TTypeError() << "unable to visit list node child of type " << TNode::EType::Undefined << "; list index: " << index; } - VisitAny(item); + VisitAny(item); ++index; - } - Consumer_->OnEndList(); -} - + } + Consumer_->OnEndList(); +} + void TNodeVisitor::VisitMap(const TNode::TMapType& nodeMap) -{ - Consumer_->OnBeginMap(); +{ + Consumer_->OnBeginMap(); Iterate(nodeMap, SortMapKeys_, [&](const std::pair<TString, TNode>& item) { - Consumer_->OnKeyedItem(item.first); + Consumer_->OnKeyedItem(item.first); if (item.second.IsUndefined()) { ythrow TNode::TTypeError() << "unable to visit map node child of type " << TNode::EType::Undefined << "; map key: `" << item.first << '\'' ; } - VisitAny(item.second); + VisitAny(item.second); }); - Consumer_->OnEndMap(); -} - -void TNodeVisitor::VisitEntity() -{ - Consumer_->OnEntity(); -} - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT + Consumer_->OnEndMap(); +} + +void TNodeVisitor::VisitEntity() +{ + Consumer_->OnEntity(); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yson/node/node_visitor.h b/library/cpp/yson/node/node_visitor.h index d4eb8e5667..db25832309 100644 --- a/library/cpp/yson/node/node_visitor.h +++ b/library/cpp/yson/node/node_visitor.h @@ -1,37 +1,37 @@ -#pragma once - +#pragma once + #include "node.h" #include <library/cpp/yson/consumer.h> - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -class TNodeVisitor -{ -public: + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +class TNodeVisitor +{ +public: TNodeVisitor(NYson::IYsonConsumer* consumer, bool sortMapKeys = false); - - void Visit(const TNode& node); + + void Visit(const TNode& node); void VisitMap(const TNode::TMapType& nodeMap); void VisitList(const TNode::TListType& nodeMap); - -private: + +private: NYson::IYsonConsumer* Consumer_; bool SortMapKeys_; - -private: - void VisitAny(const TNode& node); - - void VisitString(const TNode& node); - void VisitInt64(const TNode& node); - void VisitUint64(const TNode& node); - void VisitDouble(const TNode& node); - void VisitBool(const TNode& node); - void VisitEntity(); -}; - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT + +private: + void VisitAny(const TNode& node); + + void VisitString(const TNode& node); + void VisitInt64(const TNode& node); + void VisitUint64(const TNode& node); + void VisitDouble(const TNode& node); + void VisitBool(const TNode& node); + void VisitEntity(); +}; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT |