forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathternary_step.cc
More file actions
81 lines (64 loc) · 2.11 KB
/
Copy pathternary_step.cc
File metadata and controls
81 lines (64 loc) · 2.11 KB
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
#include "eval/eval/ternary_step.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "eval/eval/expression_step_base.h"
#include "eval/public/cel_builtins.h"
#include "eval/public/cel_value.h"
#include "eval/public/unknown_attribute_set.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
namespace {
class TernaryStep : public ExpressionStepBase {
public:
// Constructs FunctionStep that uses overloads specified.
TernaryStep(int64_t expr_id) : ExpressionStepBase(expr_id) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
};
absl::Status TernaryStep::Evaluate(ExecutionFrame* frame) const {
// Must have 3 or more values on the stack.
if (!frame->value_stack().HasEnough(3)) {
return absl::Status(absl::StatusCode::kInternal, "Value stack underflow");
}
// Create Span object that contains input arguments to the function.
auto args = frame->value_stack().GetSpan(3);
CelValue value;
const CelValue& condition = args.at(0);
// As opposed to regular functions, ternary treats unknowns or errors on the
// condition (arg0) as blocking. If we get an error or unknown then we
// ignore the other arguments and forward the condition as the result.
if (frame->enable_unknowns()) {
// Check if unknown?
if (condition.IsUnknownSet()) {
frame->value_stack().Pop(2);
return absl::OkStatus();
}
}
if (condition.IsError()) {
frame->value_stack().Pop(2);
return absl::OkStatus();
}
CelValue result;
if (!condition.IsBool()) {
result = CreateNoMatchingOverloadError(frame->arena(), builtin::kTernary);
} else if (condition.BoolOrDie()) {
result = args.at(1);
} else {
result = args.at(2);
}
frame->value_stack().Pop(args.size());
frame->value_stack().Push(result);
return absl::OkStatus();
}
} // namespace
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateTernaryStep(
int64_t expr_id) {
std::unique_ptr<ExpressionStep> step =
absl::make_unique<TernaryStep>(expr_id);
return step;
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google