compile_path.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include "resource.h"
  3. #include <yql/essentials/public/udf/udf_value.h>
  4. #include <yql/essentials/public/udf/udf_helpers.h>
  5. namespace NJson2Udf {
  6. using namespace NKikimr;
  7. using namespace NUdf;
  8. using namespace NYql;
  9. class TCompilePath: public TBoxedValue {
  10. public:
  11. TCompilePath(TSourcePosition pos)
  12. : Pos_(pos)
  13. {
  14. }
  15. static const TStringRef& Name() {
  16. static auto name = TStringRef::Of("CompilePath");
  17. return name;
  18. }
  19. static bool DeclareSignature(
  20. const TStringRef& name,
  21. TType* userType,
  22. IFunctionTypeInfoBuilder& builder,
  23. bool typesOnly) {
  24. Y_UNUSED(userType);
  25. if (name != Name()) {
  26. return false;
  27. }
  28. auto resourceType = builder.Resource(JSONPATH_RESOURCE_NAME);
  29. builder.Args()
  30. ->Add<NUdf::TUtf8>()
  31. .Done()
  32. .Returns(resourceType);
  33. if (!typesOnly) {
  34. builder.Implementation(new TCompilePath(builder.GetSourcePosition()));
  35. }
  36. return true;
  37. }
  38. private:
  39. const size_t MaxParseErrors = 10;
  40. TUnboxedValue Run(
  41. const IValueBuilder* valueBuilder,
  42. const TUnboxedValuePod* args) const final {
  43. Y_UNUSED(valueBuilder);
  44. try {
  45. TIssues issues;
  46. const auto jsonPath = NJsonPath::ParseJsonPath(args[0].AsStringRef(), issues, MaxParseErrors);
  47. if (!issues.Empty()) {
  48. ythrow yexception() << "Error parsing jsonpath:" << Endl << issues.ToString();
  49. }
  50. return TUnboxedValuePod(new TJsonPathResource(jsonPath));
  51. } catch (const std::exception& e) {
  52. UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
  53. }
  54. }
  55. TSourcePosition Pos_;
  56. };
  57. }