convert.cpp 11 KB

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