convert.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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(
  208. "Negative string length %v",
  209. length));
  210. }
  211. if (static_cast<i64>(input.Avail()) != length) {
  212. throw TYsonLiteralParseException(Format("Incorrect remaining string length: expected %v, got %v",
  213. length,
  214. input.Avail()));
  215. }
  216. TString result;
  217. result.ReserveAndResize(length);
  218. YT_VERIFY(static_cast<i64>(input.Read(result.Detach(), length)) == length);
  219. return result;
  220. }
  221. double ParseDoubleFromYsonString(const TYsonStringBuf& str)
  222. {
  223. YT_ASSERT(str.GetType() == EYsonType::Node);
  224. auto strBuf = str.AsStringBuf();
  225. TMemoryInput input(strBuf.data(), strBuf.length());
  226. char ch;
  227. if (!input.ReadChar(ch)) {
  228. throw TYsonLiteralParseException("Missing type marker");
  229. }
  230. if (ch != NDetail::DoubleMarker) {
  231. throw TYsonLiteralParseException(Format("Unexpected %v",
  232. FormatUnexpectedMarker(ch)));
  233. }
  234. if (input.Avail() != sizeof(double)) {
  235. throw TYsonLiteralParseException(Format("Incorrect remaining string length: expected %v, got %v",
  236. sizeof(double),
  237. input.Avail()));
  238. }
  239. double result;
  240. YT_VERIFY(input.Read(&result, sizeof(result)));
  241. return result;
  242. }
  243. } // namespace
  244. #define PARSE(type, underlyingType) \
  245. template <> \
  246. type ConvertFromYsonString<type>(const TYsonStringBuf& str) \
  247. { \
  248. try { \
  249. return CheckedIntegralCast<type>(Parse ## underlyingType ## FromYsonString(str)); \
  250. } catch (const std::exception& ex) { \
  251. throw TYsonLiteralParseException(ex, "Error parsing \"" #type "\" value from YSON"); \
  252. } \
  253. }
  254. PARSE(i8, Int64 )
  255. PARSE(i16, Int64 )
  256. PARSE(i32, Int64 )
  257. PARSE(i64, Int64 )
  258. PARSE(ui8, Uint64)
  259. PARSE(ui16, Uint64)
  260. PARSE(ui32, Uint64)
  261. PARSE(ui64, Uint64)
  262. #undef PARSE
  263. template <>
  264. TString ConvertFromYsonString<TString>(const TYsonStringBuf& str)
  265. {
  266. try {
  267. return ParseStringFromYsonString(str);
  268. } catch (const std::exception& ex) {
  269. throw TYsonLiteralParseException(ex, "Error parsing \"string\" value from YSON");
  270. }
  271. }
  272. template <>
  273. float ConvertFromYsonString<float>(const TYsonStringBuf& str)
  274. {
  275. try {
  276. return static_cast<float>(ParseDoubleFromYsonString(str));
  277. } catch (const std::exception& ex) {
  278. throw TYsonLiteralParseException(ex, "Error parsing \"float\" value from YSON");
  279. }
  280. }
  281. template <>
  282. double ConvertFromYsonString<double>(const TYsonStringBuf& str)
  283. {
  284. try {
  285. return ParseDoubleFromYsonString(str);
  286. } catch (const std::exception& ex) {
  287. throw TYsonLiteralParseException(ex, "Error parsing \"double\" value from YSON");
  288. }
  289. }
  290. template <>
  291. bool ConvertFromYsonString<bool>(const TYsonStringBuf& str)
  292. {
  293. try {
  294. YT_ASSERT(str.GetType() == EYsonType::Node);
  295. auto strBuf = str.AsStringBuf();
  296. TMemoryInput input(strBuf.data(), strBuf.length());
  297. char ch;
  298. if (!input.ReadChar(ch)) {
  299. throw TYsonLiteralParseException("Missing type marker");
  300. }
  301. if (ch != NDetail::TrueMarker && ch != NDetail::FalseMarker) {
  302. throw TYsonLiteralParseException(Format("Unexpected %v",
  303. FormatUnexpectedMarker(ch)));
  304. }
  305. return ch == NDetail::TrueMarker;
  306. } catch (const std::exception& ex) {
  307. throw TYsonLiteralParseException(ex, "Error parsing \"boolean\" value from YSON");
  308. }
  309. }
  310. template <>
  311. TInstant ConvertFromYsonString<TInstant>(const TYsonStringBuf& str)
  312. {
  313. try {
  314. return TInstant::ParseIso8601(ParseStringFromYsonString(str));
  315. } catch (const std::exception& ex) {
  316. throw TYsonLiteralParseException(ex, "Error parsing \"instant\" value from YSON");
  317. }
  318. }
  319. template <>
  320. TDuration ConvertFromYsonString<TDuration>(const TYsonStringBuf& str)
  321. {
  322. try {
  323. return TDuration::MilliSeconds(ParseUint64FromYsonString(str));
  324. } catch (const std::exception& ex) {
  325. throw TYsonLiteralParseException(ex, "Error parsing \"duration\" value from YSON");
  326. }
  327. }
  328. template <>
  329. TGuid ConvertFromYsonString<TGuid>(const TYsonStringBuf& str)
  330. {
  331. try {
  332. return TGuid::FromString(ParseStringFromYsonString(str));
  333. } catch (const std::exception& ex) {
  334. throw TYsonLiteralParseException(ex, "Error parsing \"guid\" value from YSON");
  335. }
  336. }
  337. ////////////////////////////////////////////////////////////////////////////////
  338. } // namespace NYT::NYson