convert.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "convert.h"
  2. #include "format.h"
  3. #include <library/cpp/yt/assert/assert.h>
  4. #include <library/cpp/yt/string/format.h>
  5. #include <library/cpp/yt/coding/varint.h>
  6. #include <library/cpp/yt/misc/cast.h>
  7. #include <array>
  8. #include <util/stream/mem.h>
  9. namespace NYT::NYson {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. template <>
  12. TYsonString ConvertToYsonString<i8>(const i8& value)
  13. {
  14. return ConvertToYsonString(static_cast<i64>(value));
  15. }
  16. template <>
  17. TYsonString ConvertToYsonString<i32>(const i32& value)
  18. {
  19. return ConvertToYsonString(static_cast<i64>(value));
  20. }
  21. template <>
  22. TYsonString ConvertToYsonString<i64>(const i64& value)
  23. {
  24. std::array<char, 1 + MaxVarInt64Size> buffer;
  25. auto* ptr = buffer.data();
  26. *ptr++ = NDetail::Int64Marker;
  27. ptr += WriteVarInt64(ptr, value);
  28. return TYsonString(TStringBuf(buffer.data(), ptr - buffer.data()));
  29. }
  30. template <>
  31. TYsonString ConvertToYsonString<ui8>(const ui8& value)
  32. {
  33. return ConvertToYsonString(static_cast<ui64>(value));
  34. }
  35. template <>
  36. TYsonString ConvertToYsonString<ui32>(const ui32& value)
  37. {
  38. return ConvertToYsonString(static_cast<ui64>(value));
  39. }
  40. template <>
  41. TYsonString ConvertToYsonString<ui64>(const ui64& value)
  42. {
  43. std::array<char, 1 + MaxVarInt64Size> buffer;
  44. auto* ptr = buffer.data();
  45. *ptr++ = NDetail::Uint64Marker;
  46. ptr += WriteVarUint64(ptr, value);
  47. return TYsonString(TStringBuf(buffer.data(), ptr - buffer.data()));
  48. }
  49. template <>
  50. TYsonString ConvertToYsonString<TString>(const TString& value)
  51. {
  52. return ConvertToYsonString(static_cast<TStringBuf>(value));
  53. }
  54. struct TConvertStringToYsonStringTag
  55. { };
  56. template <>
  57. TYsonString ConvertToYsonString<TStringBuf>(const TStringBuf& value)
  58. {
  59. auto buffer = TSharedMutableRef::Allocate<TConvertStringToYsonStringTag>(
  60. 1 + MaxVarInt64Size + value.length(),
  61. {.InitializeStorage = false});
  62. auto* ptr = buffer.Begin();
  63. *ptr++ = NDetail::StringMarker;
  64. ptr += WriteVarInt64(ptr, static_cast<i64>(value.length()));
  65. ::memcpy(ptr, value.data(), value.length());
  66. ptr += value.length();
  67. return TYsonString(buffer.Slice(buffer.Begin(), ptr));
  68. }
  69. TYsonString ConvertToYsonString(const char* value)
  70. {
  71. return ConvertToYsonString(TStringBuf(value));
  72. }
  73. template <>
  74. TYsonString ConvertToYsonString<float>(const float& value)
  75. {
  76. return ConvertToYsonString(static_cast<double>(value));
  77. }
  78. template <>
  79. TYsonString ConvertToYsonString<double>(const double& value)
  80. {
  81. std::array<char, 1 + sizeof(double)> buffer;
  82. auto* ptr = buffer.data();
  83. *ptr++ = NDetail::DoubleMarker;
  84. ::memcpy(ptr, &value, sizeof(value));
  85. ptr += sizeof(value);
  86. return TYsonString(TStringBuf(buffer.data(), ptr - buffer.data()));
  87. }
  88. template <>
  89. TYsonString ConvertToYsonString<bool>(const bool& value)
  90. {
  91. char ch = value ? NDetail::TrueMarker : NDetail::FalseMarker;
  92. return TYsonString(TStringBuf(&ch, 1));
  93. }
  94. template <>
  95. TYsonString ConvertToYsonString<TInstant>(const TInstant& value)
  96. {
  97. return ConvertToYsonString(value.ToString());
  98. }
  99. template <>
  100. TYsonString ConvertToYsonString<TDuration>(const TDuration& value)
  101. {
  102. return ConvertToYsonString(value.MilliSeconds());
  103. }
  104. template <>
  105. TYsonString ConvertToYsonString<TGuid>(const TGuid& value)
  106. {
  107. std::array<char, MaxGuidStringSize> guidBuffer;
  108. auto guidLength = WriteGuidToBuffer(guidBuffer.data(), value) - guidBuffer.data();
  109. std::array<char, 1 + MaxVarInt64Size + MaxGuidStringSize> ysonBuffer;
  110. auto* ptr = ysonBuffer.data();
  111. *ptr++ = NDetail::StringMarker;
  112. ptr += WriteVarInt64(ptr, static_cast<i64>(guidLength));
  113. ::memcpy(ptr, guidBuffer.data(), guidLength);
  114. ptr += guidLength;
  115. return TYsonString(TStringBuf(ysonBuffer.data(), ptr - ysonBuffer.data()));
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. namespace {
  119. TString FormatUnexpectedMarker(char ch)
  120. {
  121. switch (ch) {
  122. case NDetail::BeginListSymbol:
  123. return "list";
  124. case NDetail::BeginMapSymbol:
  125. return "map";
  126. case NDetail::BeginAttributesSymbol:
  127. return "attributes";
  128. case NDetail::EntitySymbol:
  129. return "\"entity\" literal";
  130. case NDetail::StringMarker:
  131. return "\"string\" literal";
  132. case NDetail::Int64Marker:
  133. return "\"int64\" literal";
  134. case NDetail::DoubleMarker:
  135. return "\"double\" literal";
  136. case NDetail::FalseMarker:
  137. case NDetail::TrueMarker:
  138. return "\"boolean\" literal";
  139. case NDetail::Uint64Marker:
  140. return "\"uint64\" literal";
  141. default:
  142. return Format("unexpected symbol %qv", ch);
  143. }
  144. }
  145. i64 ParseInt64FromYsonString(const TYsonStringBuf& str)
  146. {
  147. YT_ASSERT(str.GetType() == EYsonType::Node);
  148. auto strBuf = str.AsStringBuf();
  149. TMemoryInput input(strBuf.data(), strBuf.length());
  150. char ch;
  151. if (!input.ReadChar(ch)) {
  152. throw TYsonLiteralParseException("Missing type marker");
  153. }
  154. if (ch != NDetail::Int64Marker) {
  155. throw TYsonLiteralParseException(Format("Unexpected %v",
  156. FormatUnexpectedMarker(ch)));
  157. }
  158. i64 result;
  159. try {
  160. ReadVarInt64(&input, &result);
  161. } catch (const std::exception& ex) {
  162. throw TYsonLiteralParseException(ex, "Failed to decode \"int64\" value");
  163. }
  164. return result;
  165. }
  166. ui64 ParseUint64FromYsonString(const TYsonStringBuf& str)
  167. {
  168. YT_ASSERT(str.GetType() == EYsonType::Node);
  169. auto strBuf = str.AsStringBuf();
  170. TMemoryInput input(strBuf.data(), strBuf.length());
  171. char ch;
  172. if (!input.ReadChar(ch)) {
  173. throw TYsonLiteralParseException("Missing type marker");
  174. }
  175. if (ch != NDetail::Uint64Marker) {
  176. throw TYsonLiteralParseException(Format("Unexpected %v",
  177. FormatUnexpectedMarker(ch)));
  178. }
  179. ui64 result;
  180. try {
  181. ReadVarUint64(&input, &result);
  182. } catch (const std::exception& ex) {
  183. throw TYsonLiteralParseException(ex, "Failed to decode \"uint64\" value");
  184. }
  185. return result;
  186. }
  187. TString ParseStringFromYsonString(const TYsonStringBuf& str)
  188. {
  189. YT_ASSERT(str.GetType() == EYsonType::Node);
  190. auto strBuf = str.AsStringBuf();
  191. TMemoryInput input(strBuf.data(), strBuf.length());
  192. char ch;
  193. if (!input.ReadChar(ch)) {
  194. throw TYsonLiteralParseException("Missing type marker");
  195. }
  196. if (ch != NDetail::StringMarker) {
  197. throw TYsonLiteralParseException(Format("Unexpected %v",
  198. FormatUnexpectedMarker(ch)));
  199. }
  200. i64 length;
  201. try {
  202. ReadVarInt64(&input, &length);
  203. } catch (const std::exception& ex) {
  204. throw TYsonLiteralParseException(ex, "Failed to decode string length");
  205. }
  206. if (length < 0) {
  207. throw TYsonLiteralParseException(Format("Negative string length ",
  208. length));
  209. }
  210. if (static_cast<i64>(input.Avail()) != length) {
  211. throw TYsonLiteralParseException(Format("Incorrect remaining string length: expected %v, got %v",
  212. length,
  213. input.Avail()));
  214. }
  215. TString result;
  216. result.ReserveAndResize(length);
  217. YT_VERIFY(static_cast<i64>(input.Read(result.Detach(), length)) == length);
  218. return result;
  219. }
  220. double ParseDoubleFromYsonString(const TYsonStringBuf& str)
  221. {
  222. YT_ASSERT(str.GetType() == EYsonType::Node);
  223. auto strBuf = str.AsStringBuf();
  224. TMemoryInput input(strBuf.data(), strBuf.length());
  225. char ch;
  226. if (!input.ReadChar(ch)) {
  227. throw TYsonLiteralParseException("Missing type marker");
  228. }
  229. if (ch != NDetail::DoubleMarker) {
  230. throw TYsonLiteralParseException(Format("Unexpected %v",
  231. FormatUnexpectedMarker(ch)));
  232. }
  233. if (input.Avail() != sizeof(double)) {
  234. throw TYsonLiteralParseException(Format("Incorrect remaining string length: expected %v, got %v",
  235. sizeof(double),
  236. input.Avail()));
  237. }
  238. double result;
  239. YT_VERIFY(input.Read(&result, sizeof(result)));
  240. return result;
  241. }
  242. } // namespace
  243. #define PARSE(type, underlyingType) \
  244. template <> \
  245. type ConvertFromYsonString<type>(const TYsonStringBuf& str) \
  246. { \
  247. try { \
  248. return CheckedIntegralCast<type>(Parse ## underlyingType ## FromYsonString(str)); \
  249. } catch (const std::exception& ex) { \
  250. throw TYsonLiteralParseException(ex, "Error parsing \"" #type "\" value from YSON"); \
  251. } \
  252. }
  253. PARSE(i8, Int64 )
  254. PARSE(i16, Int64 )
  255. PARSE(i32, Int64 )
  256. PARSE(i64, Int64 )
  257. PARSE(ui8, Uint64)
  258. PARSE(ui16, Uint64)
  259. PARSE(ui32, Uint64)
  260. PARSE(ui64, Uint64)
  261. #undef PARSE
  262. template <>
  263. TString ConvertFromYsonString<TString>(const TYsonStringBuf& str)
  264. {
  265. try {
  266. return ParseStringFromYsonString(str);
  267. } catch (const std::exception& ex) {
  268. throw TYsonLiteralParseException(ex, "Error parsing \"string\" value from YSON");
  269. }
  270. }
  271. template <>
  272. float ConvertFromYsonString<float>(const TYsonStringBuf& str)
  273. {
  274. try {
  275. return static_cast<float>(ParseDoubleFromYsonString(str));
  276. } catch (const std::exception& ex) {
  277. throw TYsonLiteralParseException(ex, "Error parsing \"float\" value from YSON");
  278. }
  279. }
  280. template <>
  281. double ConvertFromYsonString<double>(const TYsonStringBuf& str)
  282. {
  283. try {
  284. return ParseDoubleFromYsonString(str);
  285. } catch (const std::exception& ex) {
  286. throw TYsonLiteralParseException(ex, "Error parsing \"double\" value from YSON");
  287. }
  288. }
  289. template <>
  290. bool ConvertFromYsonString<bool>(const TYsonStringBuf& str)
  291. {
  292. try {
  293. YT_ASSERT(str.GetType() == EYsonType::Node);
  294. auto strBuf = str.AsStringBuf();
  295. TMemoryInput input(strBuf.data(), strBuf.length());
  296. char ch;
  297. if (!input.ReadChar(ch)) {
  298. throw TYsonLiteralParseException("Missing type marker");
  299. }
  300. if (ch != NDetail::TrueMarker && ch != NDetail::FalseMarker) {
  301. throw TYsonLiteralParseException(Format("Unexpected %v",
  302. FormatUnexpectedMarker(ch)));
  303. }
  304. return ch == NDetail::TrueMarker;
  305. } catch (const std::exception& ex) {
  306. throw TYsonLiteralParseException(ex, "Error parsing \"boolean\" value from YSON");
  307. }
  308. }
  309. template <>
  310. TInstant ConvertFromYsonString<TInstant>(const TYsonStringBuf& str)
  311. {
  312. try {
  313. return TInstant::ParseIso8601(ParseStringFromYsonString(str));
  314. } catch (const std::exception& ex) {
  315. throw TYsonLiteralParseException(ex, "Error parsing \"instant\" value from YSON");
  316. }
  317. }
  318. template <>
  319. TDuration ConvertFromYsonString<TDuration>(const TYsonStringBuf& str)
  320. {
  321. try {
  322. return TDuration::MilliSeconds(ParseUint64FromYsonString(str));
  323. } catch (const std::exception& ex) {
  324. throw TYsonLiteralParseException(ex, "Error parsing \"duration\" value from YSON");
  325. }
  326. }
  327. template <>
  328. TGuid ConvertFromYsonString<TGuid>(const TYsonStringBuf& str)
  329. {
  330. try {
  331. return TGuid::FromString(ParseStringFromYsonString(str));
  332. } catch (const std::exception& ex) {
  333. throw TYsonLiteralParseException(ex, "Error parsing \"guid\" value from YSON");
  334. }
  335. }
  336. ////////////////////////////////////////////////////////////////////////////////
  337. } // namespace NYT::NYson