module.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "type_builder.h"
  3. #include "value_builder.h"
  4. #include <yql/essentials/public/udf/udf_value.h>
  5. #include <yql/essentials/public/udf/udf_registrator.h>
  6. #include <google/protobuf/message.h>
  7. #include <google/protobuf/text_format.h>
  8. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  9. namespace NYql {
  10. namespace NUdf {
  11. class TProtobufBase : public IUdfModule {
  12. public:
  13. void CleanupOnTerminate() const override;
  14. void GetAllFunctions(IFunctionsSink& sink) const override;
  15. void BuildFunctionTypeInfo(
  16. const TStringRef& name,
  17. TType* userType,
  18. const TStringRef& typeConfig,
  19. ui32 flags,
  20. IFunctionTypeInfoBuilder& builder) const override;
  21. protected:
  22. virtual const NProtoBuf::Descriptor* GetDescriptor() const = 0;
  23. virtual TProtobufValue* CreateValue(const TProtoInfo& info, bool asText) const = 0;
  24. virtual TProtobufSerialize* CreateSerialize(const TProtoInfo& info, bool asText) const = 0;
  25. };
  26. template <typename T>
  27. class TProtobufModule : public TProtobufBase {
  28. class TValue : public TProtobufValue {
  29. public:
  30. TValue(const TProtoInfo& info, bool asText)
  31. : TProtobufValue(info)
  32. , AsText_(asText)
  33. {
  34. }
  35. TAutoPtr<NProtoBuf::Message> Parse(const TStringBuf& data) const override {
  36. TAutoPtr<T> proto(new T);
  37. if (AsText_) {
  38. NProtoBuf::io::ArrayInputStream si(data.data(), data.size());
  39. if (!NProtoBuf::TextFormat::Parse(&si, proto.Get())) {
  40. ythrow yexception() << "can't parse text protobuf";
  41. }
  42. } else {
  43. if (!proto->ParseFromArray(data.data(), data.size())) {
  44. ythrow yexception() << "can't parse binary protobuf";
  45. }
  46. }
  47. return proto.Release();
  48. }
  49. private:
  50. const bool AsText_;
  51. };
  52. class TSerialize : public TProtobufSerialize {
  53. public:
  54. TSerialize(const TProtoInfo& info, bool asText)
  55. : TProtobufSerialize(info)
  56. , AsText_(asText)
  57. {
  58. }
  59. TMaybe<TString> Serialize(const NProtoBuf::Message& proto) const override {
  60. TString result;
  61. if (AsText_) {
  62. if (!NProtoBuf::TextFormat::PrintToString(proto, &result)) {
  63. ythrow yexception() << "can't serialize prototext message";
  64. }
  65. } else {
  66. result.ReserveAndResize(proto.ByteSize());
  67. if (!proto.SerializeToArray(result.begin(), result.size())) {
  68. ythrow yexception() << "can't serialize protobin message";
  69. }
  70. }
  71. return result;
  72. }
  73. TAutoPtr<NProtoBuf::Message> MakeProto() const override {
  74. return TAutoPtr<NProtoBuf::Message>(new T);
  75. }
  76. private:
  77. const bool AsText_;
  78. };
  79. private:
  80. const NProtoBuf::Descriptor* GetDescriptor() const override {
  81. return T::descriptor();
  82. }
  83. TProtobufValue* CreateValue(const TProtoInfo& info, bool asText) const override {
  84. return new TValue(info, asText);
  85. }
  86. TProtobufSerialize* CreateSerialize(const TProtoInfo& info, bool asText) const override {
  87. return new TSerialize(info, asText);
  88. }
  89. };
  90. } // namespace NUdf
  91. } // namespace NYql