module.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "module.h"
  2. namespace NYql {
  3. namespace NUdf {
  4. using namespace NProtoBuf;
  5. void TProtobufBase::CleanupOnTerminate() const {
  6. }
  7. void TProtobufBase::GetAllFunctions(IFunctionsSink& sink) const {
  8. sink.Add(TStringRef::Of("Parse"));
  9. sink.Add(TStringRef::Of("ParseText"));
  10. sink.Add(TStringRef::Of("Serialize"));
  11. sink.Add(TStringRef::Of("SerializeText"));
  12. }
  13. void TProtobufBase::BuildFunctionTypeInfo(
  14. const TStringRef& name,
  15. TType* userType,
  16. const TStringRef& typeConfig,
  17. ui32 flags,
  18. IFunctionTypeInfoBuilder& builder) const
  19. {
  20. Y_UNUSED(userType);
  21. Y_UNUSED(typeConfig);
  22. try {
  23. TProtoInfo typeInfo;
  24. ProtoTypeBuild(GetDescriptor(),
  25. EEnumFormat::Number,
  26. ERecursionTraits::Fail, true, builder, &typeInfo);
  27. auto stringType = builder.SimpleType<char*>();
  28. if ((TStringRef::Of("Serialize") == name) || (TStringRef::Of("SerializeText") == name)) {
  29. // function signature:
  30. // String Serialize(Protobuf value)
  31. builder.Returns(stringType)
  32. .Args()->Add(typeInfo.StructType)
  33. .Flags(ICallablePayload::TArgumentFlags::AutoMap)
  34. .Done();
  35. } else {
  36. // function signature:
  37. // Protobuf Parse(String value)
  38. builder.Returns(typeInfo.StructType)
  39. .Args()->Add(stringType)
  40. .Flags(ICallablePayload::TArgumentFlags::AutoMap)
  41. .Done();
  42. }
  43. if (TStringRef::Of("Serialize") == name) {
  44. if ((flags & TFlags::TypesOnly) == 0) {
  45. builder.Implementation(this->CreateSerialize(typeInfo, false));
  46. }
  47. }
  48. if (TStringRef::Of("SerializeText") == name) {
  49. if ((flags & TFlags::TypesOnly) == 0) {
  50. builder.Implementation(this->CreateSerialize(typeInfo, true));
  51. }
  52. }
  53. if (TStringRef::Of("Parse") == name) {
  54. if ((flags & TFlags::TypesOnly) == 0) {
  55. builder.Implementation(this->CreateValue(typeInfo, false));
  56. }
  57. }
  58. if (TStringRef::Of("ParseText") == name) {
  59. if ((flags & TFlags::TypesOnly) == 0) {
  60. builder.Implementation(this->CreateValue(typeInfo, true));
  61. }
  62. }
  63. } catch (...) {
  64. builder.SetError(CurrentExceptionMessage());
  65. }
  66. }
  67. } // namespace NUdf
  68. } // namespace NYql