yql_codec_results.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <library/cpp/yson/writer.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/string/cast.h>
  5. namespace NYql {
  6. namespace NResult {
  7. // we should not write numbers as numbers ever
  8. // write numbers as strings except Yson value where we use restricted dialect
  9. // write bool as bool
  10. // write null as entity
  11. class TYsonResultWriter {
  12. public:
  13. static constexpr TStringBuf VoidString = "Void";
  14. public:
  15. explicit TYsonResultWriter(NYson::TYsonConsumerBase& writer)
  16. : Writer(writer)
  17. {
  18. }
  19. void OnVoid() {
  20. Writer.OnStringScalar(VoidString);
  21. }
  22. void OnNull() {
  23. Writer.OnEntity();
  24. }
  25. void OnEmptyList() {
  26. Writer.OnBeginList();
  27. Writer.OnEndList();
  28. }
  29. void OnEmptyDict() {
  30. Writer.OnBeginList();
  31. Writer.OnEndList();
  32. }
  33. void OnEntity() {
  34. Writer.OnEntity();
  35. }
  36. // numbers
  37. void OnInt64Scalar(i64 value) {
  38. WriteNumberAsString(value);
  39. }
  40. void OnUint64Scalar(ui64 value) {
  41. WriteNumberAsString(value);
  42. }
  43. void OnFloatScalar(float value) {
  44. Writer.OnStringScalar(::FloatToString(value));
  45. }
  46. void OnDoubleScalar(double value) {
  47. Writer.OnStringScalar(::FloatToString(value));
  48. }
  49. void OnBooleanScalar(bool value) {
  50. Writer.OnBooleanScalar(value);
  51. }
  52. // strings
  53. void OnStringScalar(TStringBuf value);
  54. void OnUtf8StringScalar(TStringBuf value) {
  55. Writer.OnStringScalar(value);
  56. }
  57. // list construction
  58. void OnBeginList() {
  59. Writer.OnBeginList();
  60. }
  61. void OnListItem() {
  62. Writer.OnListItem();
  63. }
  64. void OnEndList() {
  65. Writer.OnEndList();
  66. }
  67. void OnBeginMap() {
  68. Writer.OnBeginMap();
  69. }
  70. void OnKeyedItem(const TStringBuf& key) {
  71. Writer.OnKeyedItem(key);
  72. }
  73. void OnEndMap() {
  74. Writer.OnEndMap();
  75. }
  76. void OnBeginAttributes() {
  77. Writer.OnBeginAttributes();
  78. }
  79. void OnEndAttributes() {
  80. Writer.OnEndAttributes();
  81. }
  82. void OnRaw(TStringBuf ysonNode, NYT::NYson::EYsonType type) {
  83. Writer.OnRaw(ysonNode, type);
  84. }
  85. private:
  86. template <typename T>
  87. void WriteNumberAsString(T value) {
  88. Writer.OnStringScalar(::ToString(value));
  89. }
  90. private:
  91. NYson::TYsonConsumerBase& Writer;
  92. };
  93. }
  94. }