value.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include <yql/essentials/types/binary_json/read.h>
  3. #include <yql/essentials/public/udf/udf_value.h>
  4. #include <util/generic/maybe.h>
  5. #include <variant>
  6. namespace NYql::NJsonPath {
  7. enum class EValueType {
  8. Bool = 0,
  9. Number = 1,
  10. String = 2,
  11. Null = 4,
  12. Object = 5,
  13. Array = 6,
  14. };
  15. struct TEmptyMarker {
  16. };
  17. class TValue;
  18. class TArrayIterator {
  19. public:
  20. TArrayIterator();
  21. explicit TArrayIterator(const NUdf::TUnboxedValue& iterator);
  22. explicit TArrayIterator(NUdf::TUnboxedValue&& iterator);
  23. explicit TArrayIterator(const NKikimr::NBinaryJson::TArrayIterator& iterator);
  24. explicit TArrayIterator(NKikimr::NBinaryJson::TArrayIterator&& iterator);
  25. bool Next(TValue& value);
  26. private:
  27. std::variant<TEmptyMarker, NUdf::TUnboxedValue, NKikimr::NBinaryJson::TArrayIterator> Iterator;
  28. };
  29. class TObjectIterator {
  30. public:
  31. TObjectIterator();
  32. explicit TObjectIterator(const NUdf::TUnboxedValue& iterator);
  33. explicit TObjectIterator(NUdf::TUnboxedValue&& iterator);
  34. explicit TObjectIterator(const NKikimr::NBinaryJson::TObjectIterator& iterator);
  35. explicit TObjectIterator(NKikimr::NBinaryJson::TObjectIterator&& iterator);
  36. bool Next(TValue& key, TValue& value);
  37. private:
  38. std::variant<TEmptyMarker, NUdf::TUnboxedValue, NKikimr::NBinaryJson::TObjectIterator> Iterator;
  39. };
  40. class TValue {
  41. public:
  42. TValue();
  43. explicit TValue(const NUdf::TUnboxedValue& value);
  44. explicit TValue(NUdf::TUnboxedValue&& value);
  45. explicit TValue(const NKikimr::NBinaryJson::TEntryCursor& value);
  46. explicit TValue(NKikimr::NBinaryJson::TEntryCursor&& value);
  47. explicit TValue(const NKikimr::NBinaryJson::TContainerCursor& value);
  48. explicit TValue(NKikimr::NBinaryJson::TContainerCursor&& value);
  49. EValueType GetType() const;
  50. bool Is(EValueType type) const;
  51. bool IsBool() const;
  52. bool IsNumber() const;
  53. bool IsString() const;
  54. bool IsNull() const;
  55. bool IsObject() const;
  56. bool IsArray() const;
  57. // Scalar value methods
  58. double GetNumber() const;
  59. bool GetBool() const;
  60. const TStringBuf GetString() const;
  61. ui32 GetSize() const;
  62. // Array methods
  63. TValue GetElement(ui32 index) const;
  64. TArrayIterator GetArrayIterator() const;
  65. // Object methods
  66. TMaybe<TValue> Lookup(const TStringBuf key) const;
  67. TObjectIterator GetObjectIterator() const;
  68. NUdf::TUnboxedValue ConvertToUnboxedValue(const NUdf::IValueBuilder* valueBuilder) const;
  69. private:
  70. void UnpackInnerValue();
  71. std::variant<NUdf::TUnboxedValue, NKikimr::NBinaryJson::TEntryCursor, NKikimr::NBinaryJson::TContainerCursor> Value;
  72. };
  73. }