node.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. #include "node.h"
  2. #include "node_io.h"
  3. #include <library/cpp/yson/writer.h>
  4. #include <util/generic/overloaded.h>
  5. #include <util/string/escape.h>
  6. #include <iostream>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. bool TNode::TNull::operator==(const TNull&) const {
  10. return true;
  11. }
  12. ////////////////////////////////////////////////////////////////////////////////
  13. bool TNode::TUndefined::operator==(const TUndefined&) const {
  14. return true;
  15. }
  16. ////////////////////////////////////////////////////////////////////////////////
  17. namespace NNodeCmp {
  18. bool IsComparableType(const TNode::EType type) {
  19. switch (type) {
  20. case TNode::String:
  21. case TNode::Int64:
  22. case TNode::Uint64:
  23. case TNode::Double:
  24. case TNode::Bool:
  25. case TNode::Null:
  26. case TNode::Undefined:
  27. return true;
  28. default:
  29. return false;
  30. }
  31. }
  32. bool operator<(const TNode& lhs, const TNode& rhs)
  33. {
  34. if (!lhs.GetAttributes().Empty() || !rhs.GetAttributes().Empty()) {
  35. ythrow TNode::TTypeError() << "Unsupported attributes comparison";
  36. }
  37. if (!IsComparableType(lhs.GetType()) || !IsComparableType(rhs.GetType())) {
  38. ythrow TNode::TTypeError() << "Unsupported types for comparison: " << lhs.GetType() << " with " << rhs.GetType();
  39. }
  40. if (lhs.GetType() != rhs.GetType()) {
  41. return lhs.GetType() < rhs.GetType();
  42. }
  43. switch (lhs.GetType()) {
  44. case TNode::String:
  45. return lhs.AsString() < rhs.AsString();
  46. case TNode::Int64:
  47. return lhs.AsInt64() < rhs.AsInt64();
  48. case TNode::Uint64:
  49. return lhs.AsUint64() < rhs.AsUint64();
  50. case TNode::Double:
  51. return lhs.AsDouble() < rhs.AsDouble();
  52. case TNode::Bool:
  53. return lhs.AsBool() < rhs.AsBool();
  54. case TNode::Null:
  55. case TNode::Undefined:
  56. return false;
  57. default:
  58. Y_ABORT("Unexpected type: %d", lhs.GetType());
  59. }
  60. }
  61. bool operator>(const TNode& lhs, const TNode& rhs)
  62. {
  63. return rhs < lhs;
  64. }
  65. bool operator<=(const TNode& lhs, const TNode& rhs)
  66. {
  67. return !(lhs > rhs);
  68. }
  69. bool operator>=(const TNode& lhs, const TNode& rhs)
  70. {
  71. return !(lhs < rhs);
  72. }
  73. } // namespace NNodeCmp
  74. ////////////////////////////////////////////////////////////////////////////////
  75. TNode::TNode()
  76. : Value_(TUndefined{})
  77. { }
  78. TNode::TNode(const char* s)
  79. : Value_(TString(s))
  80. { }
  81. TNode::TNode(TStringBuf s)
  82. : Value_(TString(s))
  83. { }
  84. TNode::TNode(std::string_view s)
  85. : Value_(TString(s))
  86. { }
  87. TNode::TNode(const std::string& s)
  88. : Value_(TString(s))
  89. { }
  90. TNode::TNode(TString s)
  91. : Value_(std::move(s))
  92. { }
  93. TNode::TNode(int i)
  94. : Value_(static_cast<i64>(i))
  95. { }
  96. TNode::TNode(unsigned int ui)
  97. : Value_(static_cast<ui64>(ui))
  98. { }
  99. TNode::TNode(long i)
  100. : Value_(static_cast<i64>(i))
  101. { }
  102. TNode::TNode(unsigned long ui)
  103. : Value_(static_cast<ui64>(ui))
  104. { }
  105. TNode::TNode(long long i)
  106. : Value_(static_cast<i64>(i))
  107. { }
  108. TNode::TNode(unsigned long long ui)
  109. : Value_(static_cast<ui64>(ui))
  110. { }
  111. TNode::TNode(double d)
  112. : Value_(d)
  113. { }
  114. TNode::TNode(bool b)
  115. : Value_(b)
  116. { }
  117. TNode::TNode(TMapType map)
  118. : Value_(std::move(map))
  119. { }
  120. TNode::TNode(const TNode& rhs)
  121. : TNode()
  122. {
  123. if (rhs.Attributes_) {
  124. CreateAttributes();
  125. *Attributes_ = *rhs.Attributes_;
  126. }
  127. Value_ = rhs.Value_;
  128. }
  129. TNode& TNode::operator=(const TNode& rhs)
  130. {
  131. if (this != &rhs) {
  132. TNode tmp = rhs;
  133. Move(std::move(tmp));
  134. }
  135. return *this;
  136. }
  137. TNode::TNode(TNode&& rhs) noexcept
  138. : TNode()
  139. {
  140. Move(std::move(rhs));
  141. }
  142. TNode& TNode::operator=(TNode&& rhs) noexcept
  143. {
  144. if (this != &rhs) {
  145. TNode tmp = std::move(rhs);
  146. Move(std::move(tmp));
  147. }
  148. return *this;
  149. }
  150. TNode::~TNode() = default;
  151. void TNode::Clear()
  152. {
  153. ClearAttributes();
  154. Value_ = TUndefined();
  155. }
  156. bool TNode::IsString() const
  157. {
  158. return std::holds_alternative<TString>(Value_);
  159. }
  160. bool TNode::IsInt64() const
  161. {
  162. return std::holds_alternative<i64>(Value_);
  163. }
  164. bool TNode::IsUint64() const
  165. {
  166. return std::holds_alternative<ui64>(Value_);
  167. }
  168. bool TNode::IsDouble() const
  169. {
  170. return std::holds_alternative<double>(Value_);
  171. }
  172. bool TNode::IsBool() const
  173. {
  174. return std::holds_alternative<bool>(Value_);
  175. }
  176. bool TNode::IsList() const
  177. {
  178. return std::holds_alternative<TListType>(Value_);
  179. }
  180. bool TNode::IsMap() const
  181. {
  182. return std::holds_alternative<TMapType>(Value_);
  183. }
  184. bool TNode::IsEntity() const
  185. {
  186. return IsNull();
  187. }
  188. bool TNode::IsNull() const
  189. {
  190. return std::holds_alternative<TNull>(Value_);
  191. }
  192. bool TNode::IsUndefined() const
  193. {
  194. return std::holds_alternative<TUndefined>(Value_);
  195. }
  196. bool TNode::HasValue() const
  197. {
  198. return !IsNull() && !IsUndefined();
  199. }
  200. bool TNode::Empty() const
  201. {
  202. switch (GetType()) {
  203. case String:
  204. return std::get<TString>(Value_).empty();
  205. case List:
  206. return std::get<TListType>(Value_).empty();
  207. case Map:
  208. return std::get<TMapType>(Value_).empty();
  209. default:
  210. ythrow TTypeError() << "Empty() called for type " << GetType();
  211. }
  212. }
  213. size_t TNode::Size() const
  214. {
  215. switch (GetType()) {
  216. case String:
  217. return std::get<TString>(Value_).size();
  218. case List:
  219. return std::get<TListType>(Value_).size();
  220. case Map:
  221. return std::get<TMapType>(Value_).size();
  222. default:
  223. ythrow TTypeError() << "Size() called for type " << GetType();
  224. }
  225. }
  226. TNode::EType TNode::GetType() const
  227. {
  228. return std::visit(TOverloaded{
  229. [](const TUndefined&) { return Undefined; },
  230. [](const TString&) { return String; },
  231. [](i64) { return Int64; },
  232. [](ui64) { return Uint64; },
  233. [](double) { return Double; },
  234. [](bool) { return Bool; },
  235. [](const TListType&) { return List; },
  236. [](const TMapType&) { return Map; },
  237. [](const TNull&) { return Null; }
  238. }, Value_);
  239. }
  240. const TString& TNode::AsString() const
  241. {
  242. CheckType(String);
  243. return std::get<TString>(Value_);
  244. }
  245. i64 TNode::AsInt64() const
  246. {
  247. CheckType(Int64);
  248. return std::get<i64>(Value_);
  249. }
  250. ui64 TNode::AsUint64() const
  251. {
  252. CheckType(Uint64);
  253. return std::get<ui64>(Value_);
  254. }
  255. double TNode::AsDouble() const
  256. {
  257. CheckType(Double);
  258. return std::get<double>(Value_);
  259. }
  260. bool TNode::AsBool() const
  261. {
  262. CheckType(Bool);
  263. return std::get<bool>(Value_);
  264. }
  265. const TNode::TListType& TNode::AsList() const
  266. {
  267. CheckType(List);
  268. return std::get<TListType>(Value_);
  269. }
  270. const TNode::TMapType& TNode::AsMap() const
  271. {
  272. CheckType(Map);
  273. return std::get<TMapType>(Value_);
  274. }
  275. TNode::TListType& TNode::AsList()
  276. {
  277. CheckType(List);
  278. return std::get<TListType>(Value_);
  279. }
  280. TNode::TMapType& TNode::AsMap()
  281. {
  282. CheckType(Map);
  283. return std::get<TMapType>(Value_);
  284. }
  285. const TString& TNode::UncheckedAsString() const noexcept
  286. {
  287. return std::get<TString>(Value_);
  288. }
  289. i64 TNode::UncheckedAsInt64() const noexcept
  290. {
  291. return std::get<i64>(Value_);
  292. }
  293. ui64 TNode::UncheckedAsUint64() const noexcept
  294. {
  295. return std::get<ui64>(Value_);
  296. }
  297. double TNode::UncheckedAsDouble() const noexcept
  298. {
  299. return std::get<double>(Value_);
  300. }
  301. bool TNode::UncheckedAsBool() const noexcept
  302. {
  303. return std::get<bool>(Value_);
  304. }
  305. const TNode::TListType& TNode::UncheckedAsList() const noexcept
  306. {
  307. return std::get<TListType>(Value_);
  308. }
  309. const TNode::TMapType& TNode::UncheckedAsMap() const noexcept
  310. {
  311. return std::get<TMapType>(Value_);
  312. }
  313. TNode::TListType& TNode::UncheckedAsList() noexcept
  314. {
  315. return std::get<TListType>(Value_);
  316. }
  317. TNode::TMapType& TNode::UncheckedAsMap() noexcept
  318. {
  319. return std::get<TMapType>(Value_);
  320. }
  321. TNode TNode::CreateList()
  322. {
  323. TNode node;
  324. node.Value_ = TListType{};
  325. return node;
  326. }
  327. TNode TNode::CreateList(TListType list)
  328. {
  329. TNode node;
  330. node.Value_ = std::move(list);
  331. return node;
  332. }
  333. TNode TNode::CreateMap()
  334. {
  335. TNode node;
  336. node.Value_ = TMapType{};
  337. return node;
  338. }
  339. TNode TNode::CreateMap(TMapType map)
  340. {
  341. TNode node;
  342. node.Value_ = std::move(map);
  343. return node;
  344. }
  345. TNode TNode::CreateEntity()
  346. {
  347. TNode node;
  348. node.Value_ = TNull{};
  349. return node;
  350. }
  351. const TNode& TNode::operator[](size_t index) const
  352. {
  353. CheckType(List);
  354. return std::get<TListType>(Value_)[index];
  355. }
  356. TNode& TNode::operator[](size_t index)
  357. {
  358. CheckType(List);
  359. return std::get<TListType>(Value_)[index];
  360. }
  361. const TNode& TNode::At(size_t index) const {
  362. CheckType(List);
  363. const auto& list = std::get<TListType>(Value_);
  364. if (index >= list.size()) {
  365. ythrow TLookupError() << "List out-of-range: requested index=" << index << ", but size=" << list.size();
  366. }
  367. return list[index];
  368. }
  369. TNode& TNode::At(size_t index) {
  370. CheckType(List);
  371. auto& list = std::get<TListType>(Value_);
  372. if (index >= list.size()) {
  373. ythrow TLookupError() << "List out-of-range: requested index=" << index << ", but size=" << list.size();
  374. }
  375. return list[index];
  376. }
  377. TNode& TNode::Add() &
  378. {
  379. AssureList();
  380. return std::get<TListType>(Value_).emplace_back();
  381. }
  382. TNode TNode::Add() &&
  383. {
  384. return std::move(Add());
  385. }
  386. TNode& TNode::Add(const TNode& node) &
  387. {
  388. AssureList();
  389. std::get<TListType>(Value_).emplace_back(node);
  390. return *this;
  391. }
  392. TNode TNode::Add(const TNode& node) &&
  393. {
  394. return std::move(Add(node));
  395. }
  396. TNode& TNode::Add(TNode&& node) &
  397. {
  398. AssureList();
  399. std::get<TListType>(Value_).emplace_back(std::move(node));
  400. return *this;
  401. }
  402. TNode TNode::Add(TNode&& node) &&
  403. {
  404. return std::move(Add(std::move(node)));
  405. }
  406. bool TNode::HasKey(const TStringBuf key) const
  407. {
  408. CheckType(Map);
  409. return std::get<TMapType>(Value_).contains(key);
  410. }
  411. TNode& TNode::operator()(const TString& key, const TNode& value) &
  412. {
  413. AssureMap();
  414. std::get<TMapType>(Value_)[key] = value;
  415. return *this;
  416. }
  417. TNode TNode::operator()(const TString& key, const TNode& value) &&
  418. {
  419. return std::move(operator()(key, value));
  420. }
  421. TNode& TNode::operator()(const TString& key, TNode&& value) &
  422. {
  423. AssureMap();
  424. std::get<TMapType>(Value_)[key] = std::move(value);
  425. return *this;
  426. }
  427. TNode TNode::operator()(const TString& key, TNode&& value) &&
  428. {
  429. return std::move(operator()(key, std::move(value)));
  430. }
  431. const TNode& TNode::operator[](const TStringBuf key) const
  432. {
  433. CheckType(Map);
  434. static TNode notFound;
  435. const auto& map = std::get<TMapType>(Value_);
  436. TMapType::const_iterator i = map.find(key);
  437. if (i == map.end()) {
  438. return notFound;
  439. } else {
  440. return i->second;
  441. }
  442. }
  443. TNode& TNode::operator[](const TStringBuf key)
  444. {
  445. AssureMap();
  446. return std::get<TMapType>(Value_)[key];
  447. }
  448. const TNode& TNode::At(const TStringBuf key) const {
  449. CheckType(Map);
  450. const auto& map = std::get<TMapType>(Value_);
  451. TMapType::const_iterator i = map.find(key);
  452. if (i == map.end()) {
  453. ythrow TLookupError() << "Cannot find key " << key;
  454. } else {
  455. return i->second;
  456. }
  457. }
  458. TNode& TNode::At(const TStringBuf key) {
  459. CheckType(Map);
  460. auto& map = std::get<TMapType>(Value_);
  461. TMapType::iterator i = map.find(key);
  462. if (i == map.end()) {
  463. ythrow TLookupError() << "Cannot find key " << key;
  464. } else {
  465. return i->second;
  466. }
  467. }
  468. const TString& TNode::ChildAsString(const TStringBuf key) const {
  469. const auto& node = At(key);
  470. try {
  471. return node.AsString();
  472. } catch (TTypeError& e) {
  473. e << ", during getting key=" << key;
  474. throw e;
  475. } catch (...) {
  476. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  477. }
  478. }
  479. i64 TNode::ChildAsInt64(const TStringBuf key) const {
  480. const auto& node = At(key);
  481. try {
  482. return node.AsInt64();
  483. } catch (TTypeError& e) {
  484. e << ", during getting key=" << key;
  485. throw e;
  486. } catch (...) {
  487. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  488. }
  489. }
  490. ui64 TNode::ChildAsUint64(const TStringBuf key) const {
  491. const auto& node = At(key);
  492. try {
  493. return node.AsUint64();
  494. } catch (TTypeError& e) {
  495. e << ", during getting key=" << key;
  496. throw e;
  497. } catch (...) {
  498. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  499. }
  500. }
  501. double TNode::ChildAsDouble(const TStringBuf key) const {
  502. const auto& node = At(key);
  503. try {
  504. return node.AsDouble();
  505. } catch (TTypeError& e) {
  506. e << ", during getting key=" << key;
  507. throw e;
  508. } catch (...) {
  509. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  510. }
  511. }
  512. bool TNode::ChildAsBool(const TStringBuf key) const {
  513. const auto& node = At(key);
  514. try {
  515. return node.AsBool();
  516. } catch (TTypeError& e) {
  517. e << ", during getting key=" << key;
  518. throw e;
  519. } catch (...) {
  520. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  521. }
  522. }
  523. const TNode::TListType& TNode::ChildAsList(const TStringBuf key) const {
  524. const auto& node = At(key);
  525. try {
  526. return node.AsList();
  527. } catch (TTypeError& e) {
  528. e << ", during getting key=" << key;
  529. throw e;
  530. } catch (...) {
  531. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  532. }
  533. }
  534. const TNode::TMapType& TNode::ChildAsMap(const TStringBuf key) const {
  535. const auto& node = At(key);
  536. try {
  537. return node.AsMap();
  538. } catch (TTypeError& e) {
  539. e << ", during getting key=" << key;
  540. throw e;
  541. } catch (...) {
  542. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  543. }
  544. }
  545. TNode::TListType& TNode::ChildAsList(const TStringBuf key) {
  546. auto& node = At(key);
  547. try {
  548. return node.AsList();
  549. } catch (TTypeError& e) {
  550. e << ", during getting key=" << key;
  551. throw e;
  552. } catch (...) {
  553. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  554. }
  555. }
  556. TNode::TMapType& TNode::ChildAsMap(const TStringBuf key) {
  557. auto& node = At(key);
  558. try {
  559. return node.AsMap();
  560. } catch (TTypeError& e) {
  561. e << ", during getting key=" << key;
  562. throw e;
  563. } catch (...) {
  564. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  565. }
  566. }
  567. const TString& TNode::ChildAsString(size_t index) const {
  568. const auto& node = At(index);
  569. try {
  570. return node.AsString();
  571. } catch (TTypeError& e) {
  572. e << ", during getting index=" << index;
  573. throw e;
  574. } catch (...) {
  575. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  576. }
  577. }
  578. i64 TNode::ChildAsInt64(size_t index) const {
  579. const auto& node = At(index);
  580. try {
  581. return node.AsInt64();
  582. } catch (TTypeError& e) {
  583. e << ", during getting index=" << index;
  584. throw e;
  585. } catch (...) {
  586. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  587. }
  588. }
  589. ui64 TNode::ChildAsUint64(size_t index) const {
  590. const auto& node = At(index);
  591. try {
  592. return node.AsUint64();
  593. } catch (TTypeError& e) {
  594. e << ", during getting index=" << index;
  595. throw e;
  596. } catch (...) {
  597. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  598. }
  599. }
  600. double TNode::ChildAsDouble(size_t index) const {
  601. const auto& node = At(index);
  602. try {
  603. return node.AsDouble();
  604. } catch (TTypeError& e) {
  605. e << ", during getting index=" << index;
  606. throw e;
  607. } catch (...) {
  608. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  609. }
  610. }
  611. bool TNode::ChildAsBool(size_t index) const {
  612. const auto& node = At(index);
  613. try {
  614. return node.AsBool();
  615. } catch (TTypeError& e) {
  616. e << ", during getting index=" << index;
  617. throw e;
  618. } catch (...) {
  619. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  620. }
  621. }
  622. const TNode::TListType& TNode::ChildAsList(size_t index) const {
  623. const auto& node = At(index);
  624. try {
  625. return node.AsList();
  626. } catch (TTypeError& e) {
  627. e << ", during getting index=" << index;
  628. throw e;
  629. } catch (...) {
  630. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  631. }
  632. }
  633. const TNode::TMapType& TNode::ChildAsMap(size_t index) const {
  634. const auto& node = At(index);
  635. try {
  636. return node.AsMap();
  637. } catch (TTypeError& e) {
  638. e << ", during getting index=" << index;
  639. throw e;
  640. } catch (...) {
  641. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  642. }
  643. }
  644. TNode::TListType& TNode::ChildAsList(size_t index) {
  645. auto& node = At(index);
  646. try {
  647. return node.AsList();
  648. } catch (TTypeError& e) {
  649. e << ", during getting index=" << index;
  650. throw e;
  651. } catch (...) {
  652. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  653. }
  654. }
  655. TNode::TMapType& TNode::ChildAsMap(size_t index) {
  656. auto& node = At(index);
  657. try {
  658. return node.AsMap();
  659. } catch (TTypeError& e) {
  660. e << ", during getting index=" << index;
  661. throw e;
  662. } catch (...) {
  663. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  664. }
  665. }
  666. bool TNode::HasAttributes() const
  667. {
  668. return Attributes_ && !Attributes_->Empty();
  669. }
  670. void TNode::ClearAttributes()
  671. {
  672. if (Attributes_) {
  673. Attributes_.Destroy();
  674. }
  675. }
  676. const TNode& TNode::GetAttributes() const
  677. {
  678. static TNode notFound = TNode::CreateMap();
  679. if (!Attributes_) {
  680. return notFound;
  681. }
  682. return *Attributes_;
  683. }
  684. TNode& TNode::Attributes()
  685. {
  686. if (!Attributes_) {
  687. CreateAttributes();
  688. }
  689. return *Attributes_;
  690. }
  691. void TNode::MoveWithoutAttributes(TNode&& rhs)
  692. {
  693. Value_ = std::move(rhs.Value_);
  694. rhs.Clear();
  695. }
  696. void TNode::Move(TNode&& rhs)
  697. {
  698. Value_ = std::move(rhs.Value_);
  699. Attributes_ = std::move(rhs.Attributes_);
  700. }
  701. void TNode::CheckType(EType type) const
  702. {
  703. Y_ENSURE_EX(GetType() == type,
  704. TTypeError() << "TNode type " << type << " expected, actual type " << GetType();
  705. );
  706. }
  707. void TNode::AssureMap()
  708. {
  709. if (std::holds_alternative<TUndefined>(Value_)) {
  710. Value_ = TMapType();
  711. } else {
  712. CheckType(Map);
  713. }
  714. }
  715. void TNode::AssureList()
  716. {
  717. if (std::holds_alternative<TUndefined>(Value_)) {
  718. Value_ = TListType();
  719. } else {
  720. CheckType(List);
  721. }
  722. }
  723. void TNode::CreateAttributes()
  724. {
  725. Attributes_ = MakeHolder<TNode>();
  726. Attributes_->Value_ = TMapType();
  727. }
  728. void TNode::Save(IOutputStream* out) const
  729. {
  730. NodeToYsonStream(*this, out, NYson::EYsonFormat::Binary);
  731. }
  732. void TNode::Load(IInputStream* in)
  733. {
  734. Clear();
  735. *this = NodeFromYsonStream(in, ::NYson::EYsonType::Node);
  736. }
  737. ////////////////////////////////////////////////////////////////////////////////
  738. bool operator==(const TNode& lhs, const TNode& rhs)
  739. {
  740. if (std::holds_alternative<TNode::TUndefined>(lhs.Value_) ||
  741. std::holds_alternative<TNode::TUndefined>(rhs.Value_))
  742. {
  743. // TODO: should try to remove this behaviour if nobody uses it.
  744. return false;
  745. }
  746. if (lhs.GetType() != rhs.GetType()) {
  747. return false;
  748. }
  749. if (lhs.Attributes_) {
  750. if (rhs.Attributes_) {
  751. if (*lhs.Attributes_ != *rhs.Attributes_) {
  752. return false;
  753. }
  754. } else {
  755. return false;
  756. }
  757. } else {
  758. if (rhs.Attributes_) {
  759. return false;
  760. }
  761. }
  762. return rhs.Value_ == lhs.Value_;
  763. }
  764. bool operator!=(const TNode& lhs, const TNode& rhs)
  765. {
  766. return !(lhs == rhs);
  767. }
  768. bool GetBool(const TNode& node)
  769. {
  770. if (node.IsBool()) {
  771. return node.AsBool();
  772. } else if (node.IsString()) {
  773. return node.AsString() == "true";
  774. } else {
  775. ythrow TNode::TTypeError()
  776. << "GetBool(): not a boolean or string type";
  777. }
  778. }
  779. void PrintTo(const TNode& node, std::ostream* out)
  780. {
  781. if (node.IsUndefined()) {
  782. (*out) << "NYT::TNode::Undefined";
  783. } else {
  784. (*out) << "NYT::TNode("
  785. << NodeToCanonicalYsonString(node)
  786. << ")";
  787. }
  788. }
  789. ////////////////////////////////////////////////////////////////////////////////
  790. } // namespace NYT