node.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #pragma once
  2. #include <util/generic/bt_exception.h>
  3. #include <util/generic/cast.h>
  4. #include <util/generic/hash.h>
  5. #include <util/generic/variant.h>
  6. #include <util/generic/vector.h>
  7. #include <util/generic/yexception.h>
  8. #include <util/generic/ylimits.h>
  9. #include <util/string/cast.h>
  10. #include <cmath>
  11. #include <variant>
  12. class IInputStream;
  13. class IOutputStream;
  14. namespace NYT {
  15. ////////////////////////////////////////////////////////////////////////////////
  16. class TNode
  17. {
  18. public:
  19. class TLookupError
  20. : public TWithBackTrace<yexception>
  21. { };
  22. class TTypeError
  23. : public TWithBackTrace<yexception>
  24. { };
  25. enum EType {
  26. Undefined = 0 /*"undefined"*/,
  27. // NOTE: string representation of all node types
  28. // are compatible with server node type (except `Undefined' which is missing on server).
  29. String = 1 /*"string_node"*/,
  30. Int64 = 2 /*"int64_node"*/,
  31. Uint64 = 3 /*"uint64_node"*/,
  32. Double = 4 /*"double_node"*/,
  33. Bool = 5 /*"boolean_node"*/,
  34. List = 6 /*"list_node"*/,
  35. Map = 7 /*"map_node"*/,
  36. Null = 8 /*"null"*/,
  37. };
  38. using TListType = TVector<TNode>;
  39. using TMapType = THashMap<TString, TNode>;
  40. private:
  41. struct TNull {
  42. bool operator==(const TNull&) const;
  43. };
  44. struct TUndefined {
  45. bool operator==(const TUndefined&) const;
  46. };
  47. using TValue = std::variant<
  48. bool,
  49. i64,
  50. ui64,
  51. double,
  52. TString,
  53. TListType,
  54. TMapType,
  55. TNull,
  56. TUndefined
  57. >;
  58. public:
  59. TNode();
  60. TNode(const char* s);
  61. TNode(TStringBuf s);
  62. explicit TNode(std::string_view s);
  63. explicit TNode(const std::string& s);
  64. TNode(TString s);
  65. TNode(int i);
  66. //this case made speccially for prevent mess cast of EType into TNode through TNode(int) constructor
  67. //usual case of error SomeNode == TNode::Undefined <-- SomeNode indeed will be compared with TNode(0) without this method
  68. //correct way is SomeNode.GetType() == TNode::Undefined
  69. template<class T = EType>
  70. Y_FORCE_INLINE TNode(EType)
  71. {
  72. static_assert(!std::is_same<T, EType>::value, "looks like a mistake, may be you forget .GetType()");
  73. }
  74. //this case made speccially for prevent mess cast of T* into TNode through implicit bool ctr
  75. template<class T = int>
  76. Y_FORCE_INLINE TNode(const T*) : TNode() {
  77. static_assert(!std::is_same<T,T>::value, "looks like a mistake, and pointer have converted to bool");
  78. }
  79. TNode(unsigned int ui);
  80. TNode(long i);
  81. TNode(unsigned long ui);
  82. TNode(long long i);
  83. TNode(unsigned long long ui);
  84. TNode(double d);
  85. TNode(bool b);
  86. TNode(TMapType map);
  87. TNode(const TNode& rhs);
  88. TNode& operator=(const TNode& rhs);
  89. TNode(TNode&& rhs) noexcept;
  90. TNode& operator=(TNode&& rhs) noexcept;
  91. ~TNode();
  92. void Clear();
  93. bool IsString() const;
  94. bool IsInt64() const;
  95. bool IsUint64() const;
  96. bool IsDouble() const;
  97. bool IsBool() const;
  98. bool IsList() const;
  99. bool IsMap() const;
  100. // `IsEntity' is deprecated use `IsNull' instead.
  101. bool IsEntity() const;
  102. bool IsNull() const;
  103. bool IsUndefined() const;
  104. // Returns true if TNode is neither Null, nor Undefined
  105. bool HasValue() const;
  106. template<typename T>
  107. bool IsOfType() const noexcept;
  108. // Int64, Uint64, Double, or Bool
  109. bool IsArithmetic() const;
  110. bool Empty() const;
  111. size_t Size() const;
  112. EType GetType() const;
  113. const TString& AsString() const;
  114. i64 AsInt64() const;
  115. ui64 AsUint64() const;
  116. double AsDouble() const;
  117. bool AsBool() const;
  118. const TListType& AsList() const;
  119. const TMapType& AsMap() const;
  120. TListType& AsList();
  121. TMapType& AsMap();
  122. const TString& UncheckedAsString() const noexcept;
  123. i64 UncheckedAsInt64() const noexcept;
  124. ui64 UncheckedAsUint64() const noexcept;
  125. double UncheckedAsDouble() const noexcept;
  126. bool UncheckedAsBool() const noexcept;
  127. const TListType& UncheckedAsList() const noexcept;
  128. const TMapType& UncheckedAsMap() const noexcept;
  129. TListType& UncheckedAsList() noexcept;
  130. TMapType& UncheckedAsMap() noexcept;
  131. // integer types cast
  132. // makes overflow checks
  133. template<typename T>
  134. T IntCast() const;
  135. // integers <-> double <-> string
  136. // makes overflow checks
  137. template<typename T>
  138. T ConvertTo() const;
  139. template<typename T>
  140. T& As();
  141. template<typename T>
  142. const T& As() const;
  143. static TNode CreateList();
  144. static TNode CreateList(TListType list);
  145. static TNode CreateMap();
  146. static TNode CreateMap(TMapType map);
  147. static TNode CreateEntity();
  148. const TNode& operator[](size_t index) const;
  149. TNode& operator[](size_t index);
  150. const TNode& At(size_t index) const;
  151. TNode& At(size_t index);
  152. TNode& Add() &;
  153. TNode Add() &&;
  154. TNode& Add(const TNode& node) &;
  155. TNode Add(const TNode& node) &&;
  156. TNode& Add(TNode&& node) &;
  157. TNode Add(TNode&& node) &&;
  158. bool HasKey(const TStringBuf key) const;
  159. TNode& operator()(const TString& key, const TNode& value) &;
  160. TNode operator()(const TString& key, const TNode& value) &&;
  161. TNode& operator()(const TString& key, TNode&& value) &;
  162. TNode operator()(const TString& key, TNode&& value) &&;
  163. const TNode& operator[](const TStringBuf key) const;
  164. TNode& operator[](const TStringBuf key);
  165. const TNode& At(const TStringBuf key) const;
  166. TNode& At(const TStringBuf key);
  167. // map getters
  168. // works the same way like simple getters
  169. const TString& ChildAsString(const TStringBuf key) const;
  170. i64 ChildAsInt64(const TStringBuf key) const;
  171. ui64 ChildAsUint64(const TStringBuf key) const;
  172. double ChildAsDouble(const TStringBuf key) const;
  173. bool ChildAsBool(const TStringBuf key) const;
  174. const TListType& ChildAsList(const TStringBuf key) const;
  175. const TMapType& ChildAsMap(const TStringBuf key) const;
  176. TListType& ChildAsList(const TStringBuf key);
  177. TMapType& ChildAsMap(const TStringBuf key);
  178. template<typename T>
  179. T ChildIntCast(const TStringBuf key) const;
  180. template<typename T>
  181. T ChildConvertTo(const TStringBuf key) const;
  182. template<typename T>
  183. const T& ChildAs(const TStringBuf key) const;
  184. template<typename T>
  185. T& ChildAs(const TStringBuf key);
  186. // list getters
  187. // works the same way like simple getters
  188. const TString& ChildAsString(size_t index) const;
  189. i64 ChildAsInt64(size_t index) const;
  190. ui64 ChildAsUint64(size_t index) const;
  191. double ChildAsDouble(size_t index) const;
  192. bool ChildAsBool(size_t index) const;
  193. const TListType& ChildAsList(size_t index) const;
  194. const TMapType& ChildAsMap(size_t index) const;
  195. TListType& ChildAsList(size_t index);
  196. TMapType& ChildAsMap(size_t index);
  197. template<typename T>
  198. T ChildIntCast(size_t index) const;
  199. template<typename T>
  200. T ChildConvertTo(size_t index) const;
  201. template<typename T>
  202. const T& ChildAs(size_t index) const;
  203. template<typename T>
  204. T& ChildAs(size_t index);
  205. // attributes
  206. bool HasAttributes() const;
  207. void ClearAttributes();
  208. const TNode& GetAttributes() const;
  209. TNode& Attributes();
  210. void MoveWithoutAttributes(TNode&& rhs);
  211. // Serialize TNode using binary yson format.
  212. // Methods for ysaveload.
  213. void Save(IOutputStream* output) const;
  214. void Load(IInputStream* input);
  215. private:
  216. void Move(TNode&& rhs);
  217. void CheckType(EType type) const;
  218. void AssureMap();
  219. void AssureList();
  220. void CreateAttributes();
  221. private:
  222. TValue Value_;
  223. THolder<TNode> Attributes_;
  224. friend bool operator==(const TNode& lhs, const TNode& rhs);
  225. friend bool operator!=(const TNode& lhs, const TNode& rhs);
  226. };
  227. bool operator==(const TNode& lhs, const TNode& rhs);
  228. bool operator!=(const TNode& lhs, const TNode& rhs);
  229. bool GetBool(const TNode& node);
  230. /// Debug printer for gtest
  231. void PrintTo(const TNode& node, std::ostream* out);
  232. inline bool TNode::IsArithmetic() const {
  233. return IsInt64() || IsUint64() || IsDouble() || IsBool();
  234. }
  235. template<typename T>
  236. inline T TNode::IntCast() const {
  237. if constexpr (std::is_integral<T>::value) {
  238. try {
  239. switch (GetType()) {
  240. case TNode::Uint64:
  241. return SafeIntegerCast<T>(AsUint64());
  242. case TNode::Int64:
  243. return SafeIntegerCast<T>(AsInt64());
  244. default:
  245. ythrow TTypeError() << "IntCast() called for type " << GetType();
  246. }
  247. } catch(TBadCastException& exc) {
  248. ythrow TTypeError() << "TBadCastException during IntCast(): " << exc.what();
  249. }
  250. } else {
  251. static_assert(sizeof(T) != sizeof(T), "implemented only for std::is_integral types");
  252. }
  253. }
  254. template<typename T>
  255. inline T TNode::ConvertTo() const {
  256. if constexpr (std::is_integral<T>::value) {
  257. switch (GetType()) {
  258. case NYT::TNode::String:
  259. return ::FromString(AsString());
  260. case NYT::TNode::Int64:
  261. case NYT::TNode::Uint64:
  262. return IntCast<T>();
  263. case NYT::TNode::Double:
  264. if (AsDouble() < Min<T>() || AsDouble() > MaxFloor<T>() || !std::isfinite(AsDouble())) {
  265. ythrow TTypeError() << AsDouble() << " can't be converted to " << TypeName<T>();
  266. }
  267. return AsDouble();
  268. case NYT::TNode::Bool:
  269. return AsBool();
  270. case NYT::TNode::List:
  271. case NYT::TNode::Map:
  272. case NYT::TNode::Null:
  273. case NYT::TNode::Undefined:
  274. ythrow TTypeError() << "ConvertTo<" << TypeName<T>() << ">() called for type " << GetType();
  275. };
  276. } else {
  277. static_assert(sizeof(T) != sizeof(T), "should have template specialization");
  278. }
  279. }
  280. template<>
  281. inline TString TNode::ConvertTo<TString>() const {
  282. switch (GetType()) {
  283. case NYT::TNode::String:
  284. return AsString();
  285. case NYT::TNode::Int64:
  286. return ::ToString(AsInt64());
  287. case NYT::TNode::Uint64:
  288. return ::ToString(AsUint64());
  289. case NYT::TNode::Double:
  290. return ::ToString(AsDouble());
  291. case NYT::TNode::Bool:
  292. return ::ToString(AsBool());
  293. case NYT::TNode::List:
  294. case NYT::TNode::Map:
  295. case NYT::TNode::Null:
  296. case NYT::TNode::Undefined:
  297. ythrow TTypeError() << "ConvertTo<TString>() called for type " << GetType();
  298. }
  299. Y_UNREACHABLE();
  300. }
  301. template<>
  302. inline double TNode::ConvertTo<double>() const {
  303. switch (GetType()) {
  304. case NYT::TNode::String:
  305. return ::FromString(AsString());
  306. case NYT::TNode::Int64:
  307. return AsInt64();
  308. case NYT::TNode::Uint64:
  309. return AsUint64();
  310. case NYT::TNode::Double:
  311. return AsDouble();
  312. case NYT::TNode::Bool:
  313. return AsBool();
  314. case NYT::TNode::List:
  315. case NYT::TNode::Map:
  316. case NYT::TNode::Null:
  317. case NYT::TNode::Undefined:
  318. ythrow TTypeError() << "ConvertTo<double>() called for type " << GetType();
  319. }
  320. }
  321. template<>
  322. inline bool TNode::ConvertTo<bool>() const {
  323. switch (GetType()) {
  324. case NYT::TNode::String:
  325. return ::FromString(AsString());
  326. case NYT::TNode::Int64:
  327. return AsInt64();
  328. case NYT::TNode::Uint64:
  329. return AsUint64();
  330. case NYT::TNode::Double:
  331. return AsDouble();
  332. case NYT::TNode::Bool:
  333. return AsBool();
  334. case NYT::TNode::List:
  335. case NYT::TNode::Map:
  336. case NYT::TNode::Null:
  337. case NYT::TNode::Undefined:
  338. ythrow TTypeError() << "ConvertTo<bool>() called for type " << GetType();
  339. }
  340. }
  341. template<typename T>
  342. inline T TNode::ChildIntCast(const TStringBuf key) const {
  343. const auto& node = At(key);
  344. try {
  345. return node.IntCast<T>();
  346. } catch (TTypeError& e) {
  347. e << ", during getting key=" << key;
  348. throw e;
  349. } catch (...) {
  350. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  351. }
  352. }
  353. template<typename T>
  354. inline T TNode::ChildIntCast(size_t index) const {
  355. const auto& node = At(index);
  356. try {
  357. return node.IntCast<T>();
  358. } catch (TTypeError& e) {
  359. e << ", during getting index=" << index;
  360. throw e;
  361. } catch (...) {
  362. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  363. }
  364. }
  365. template<typename T>
  366. inline T TNode::ChildConvertTo(const TStringBuf key) const {
  367. const auto& node = At(key);
  368. try {
  369. return node.ConvertTo<T>();
  370. } catch (TTypeError& e) {
  371. e << ", during getting key=" << key;
  372. throw e;
  373. } catch (...) {
  374. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  375. }
  376. }
  377. template<typename T>
  378. inline T TNode::ChildConvertTo(size_t index) const {
  379. const auto& node = At(index);
  380. try {
  381. return node.ConvertTo<T>();
  382. } catch (TTypeError& e) {
  383. e << ", during getting index=" << index;
  384. throw e;
  385. } catch (...) {
  386. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  387. }
  388. }
  389. template<typename T>
  390. inline const T& TNode::ChildAs(const TStringBuf key) const {
  391. const auto& node = At(key);
  392. try {
  393. return node.As<T>();
  394. } catch (TTypeError& e) {
  395. e << ", during getting key=" << key;
  396. throw e;
  397. } catch (...) {
  398. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting key=" << key;
  399. }
  400. }
  401. template<typename T>
  402. inline const T& TNode::ChildAs(size_t index) const {
  403. const auto& node = At(index);
  404. try {
  405. return node.As<T>();
  406. } catch (TTypeError& e) {
  407. e << ", during getting index=" << index;
  408. throw e;
  409. } catch (...) {
  410. ythrow TTypeError() << CurrentExceptionMessage() << ", during getting index=" << index;
  411. }
  412. }
  413. template<typename T>
  414. inline T& TNode::ChildAs(const TStringBuf key) {
  415. return const_cast<T&>(static_cast<const TNode*>(this)->ChildAs<T>(key));
  416. }
  417. template<typename T>
  418. inline T& TNode::ChildAs(size_t index) {
  419. return const_cast<T&>(static_cast<const TNode*>(this)->ChildAs<T>(index));
  420. }
  421. template<typename T>
  422. inline bool TNode::IsOfType() const noexcept {
  423. return std::holds_alternative<T>(Value_);
  424. }
  425. template<typename T>
  426. inline T& TNode::As() {
  427. return std::get<T>(Value_);
  428. }
  429. template<typename T>
  430. inline const T& TNode::As() const {
  431. return std::get<T>(Value_);
  432. }
  433. ////////////////////////////////////////////////////////////////////////////////
  434. namespace NNodeCmp {
  435. bool operator<(const TNode& lhs, const TNode& rhs);
  436. bool operator<=(const TNode& lhs, const TNode& rhs);
  437. bool operator>(const TNode& lhs, const TNode& rhs);
  438. bool operator>=(const TNode& lhs, const TNode& rhs);
  439. bool IsComparableType(const TNode::EType type);
  440. }
  441. ////////////////////////////////////////////////////////////////////////////////
  442. } // namespace NYT