node.h 15 KB

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