convert.cpp 11 KB

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