summaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/json2/as_json_node.h
blob: 82c51802433e98cc93b8ab2ac13897782020777f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once

#include "resource.h"

#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_helpers.h>
#include <yql/essentials/minikql/dom/node.h>
#include <yql/essentials/minikql/dom/json.h>

namespace NJson2Udf {
using namespace NKikimr;
using namespace NUdf;
using namespace NYql;
using namespace NDom;

template <typename TSource>
class TAsJsonNode: public TBoxedValue {
public:
    TAsJsonNode(TSourcePosition pos)
        : Pos_(pos)
    {
    }

    static TStringRef Name();

    static bool DeclareSignature(
        const TStringRef& name,
        TType* userType,
        IFunctionTypeInfoBuilder& builder,
        bool typesOnly) {
        Y_UNUSED(userType);
        if (name != Name()) {
            return false;
        }

        auto optionalSourceType = builder.Optional()->Item<TSource>().Build();
        auto resourceType = builder.Resource(JSON_NODE_RESOURCE_NAME);
        builder.Args()
            ->Add(optionalSourceType)
            .Done()
            .Returns(resourceType);

        if (!typesOnly) {
            builder.Implementation(new TAsJsonNode<TSource>(builder.GetSourcePosition()));
        }

        builder.IsStrict();
        return true;
    }

private:
    const size_t MaxParseErrors_ = 10;

    static TUnboxedValue Interpret(const TUnboxedValue& sourceValue, const IValueBuilder* valueBuilder);

    TUnboxedValue Run(
        const IValueBuilder* valueBuilder,
        const TUnboxedValuePod* args) const final {
        Y_UNUSED(valueBuilder);
        try {
            if (!args[0].HasValue()) {
                return MakeEntity();
            }
            return Interpret(args[0], valueBuilder);
        } catch (const std::exception& e) {
            UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).c_str());
        }
    }

    TSourcePosition Pos_;
};

template <>
TStringRef TAsJsonNode<TUtf8>::Name() {
    return TStringRef::Of("Utf8AsJsonNode");
}

template <>
TUnboxedValue TAsJsonNode<TUtf8>::Interpret(const TUnboxedValue& sourceValue, const IValueBuilder* valueBuilder) {
    return MakeString(sourceValue.AsStringRef(), valueBuilder);
}

template <>
TStringRef TAsJsonNode<double>::Name() {
    return TStringRef::Of("DoubleAsJsonNode");
}

template <>
TUnboxedValue TAsJsonNode<double>::Interpret(const TUnboxedValue& sourceValue, const IValueBuilder* valueBuilder) {
    Y_UNUSED(valueBuilder);
    return MakeDouble(sourceValue.Get<double>());
}

template <>
TStringRef TAsJsonNode<bool>::Name() {
    return TStringRef::Of("BoolAsJsonNode");
}

template <>
TUnboxedValue TAsJsonNode<bool>::Interpret(const TUnboxedValue& sourceValue, const IValueBuilder* valueBuilder) {
    Y_UNUSED(valueBuilder);
    return MakeBool(sourceValue.Get<bool>());
}

template <>
TStringRef TAsJsonNode<TJson>::Name() {
    return TStringRef::Of("JsonAsJsonNode");
}

template <>
TUnboxedValue TAsJsonNode<TJson>::Interpret(const TUnboxedValue& sourceValue, const IValueBuilder* valueBuilder) {
    return TryParseJsonDom(sourceValue.AsStringRef(), valueBuilder);
}
} // namespace NJson2Udf