aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/purecalc/common/inspect_input.cpp
blob: 9ca56da5decb0a5a2d1273cd4a8b04e5a3c212f2 (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
#include "inspect_input.h"

#include <yql/essentials/core/yql_expr_type_annotation.h>

namespace NYql::NPureCalc {
    bool TryFetchInputIndexFromSelf(const TExprNode& node, TExprContext& ctx, ui32 inputsCount, ui32& result) {
        TIssueScopeGuard issueSope(ctx.IssueManager, [&]() {
            return MakeIntrusive<TIssue>(ctx.GetPosition(node.Pos()), TStringBuilder() << "At function: " << node.Content());
        });

        if (!EnsureArgsCount(node, 1, ctx)) {
            return false;
        }

        if (!EnsureAtom(*node.Child(0), ctx)) {
            return false;
        }

        if (!TryFromString(node.Child(0)->Content(), result)) {
            auto message = TStringBuilder() << "Index " << TString{node.Child(0)->Content()}.Quote() << " isn't UI32";
            ctx.AddError(TIssue(ctx.GetPosition(node.Child(0)->Pos()), std::move(message)));
            return false;
        }

        if (result >= inputsCount) {
            auto message = TStringBuilder() << "Invalid input index: " << result << " is out of range [0;" << inputsCount << ")";
            ctx.AddError(TIssue(ctx.GetPosition(node.Child(0)->Pos()), std::move(message)));
            return false;
        }

        return true;
    }
}