-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathconst_value_step.cc
More file actions
91 lines (76 loc) · 3 KB
/
Copy pathconst_value_step.cc
File metadata and controls
91 lines (76 loc) · 3 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
82
83
84
85
86
87
88
89
90
91
#include "eval/eval/const_value_step.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "absl/status/statusor.h"
#include "absl/time/time.h"
#include "base/ast_internal.h"
#include "eval/eval/compiler_constant_step.h"
#include "eval/eval/expression_step_base.h"
#include "eval/internal/interop.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::ast::internal::Constant;
class ConstValueStep : public ExpressionStepBase {
public:
ConstValueStep(const Constant& expr, int64_t expr_id, bool comes_from_ast)
: ExpressionStepBase(expr_id, comes_from_ast),
const_expr_(expr),
value_(ConvertConstant(const_expr_)) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
// Maintain a copy of the source constant to avoid lifecycle dependence on the
// ast after planning.
cel::ast::internal::Constant const_expr_;
cel::Handle<cel::Value> value_;
};
absl::Status ConstValueStep::Evaluate(ExecutionFrame* frame) const {
frame->value_stack().Push(value_);
return absl::OkStatus();
}
} // namespace
cel::Handle<cel::Value> ConvertConstant(
const cel::ast::internal::Constant& const_expr) {
struct {
cel::Handle<cel::Value> operator()(
const cel::ast::internal::NullValue& value) {
return cel::interop_internal::CreateNullValue();
}
cel::Handle<cel::Value> operator()(bool value) {
return cel::interop_internal::CreateBoolValue(value);
}
cel::Handle<cel::Value> operator()(int64_t value) {
return cel::interop_internal::CreateIntValue(value);
}
cel::Handle<cel::Value> operator()(uint64_t value) {
return cel::interop_internal::CreateUintValue(value);
}
cel::Handle<cel::Value> operator()(double value) {
return cel::interop_internal::CreateDoubleValue(value);
}
cel::Handle<cel::Value> operator()(const std::string& value) {
return cel::interop_internal::CreateStringValueFromView(value);
}
cel::Handle<cel::Value> operator()(const cel::ast::internal::Bytes& value) {
return cel::interop_internal::CreateBytesValueFromView(value.bytes);
}
cel::Handle<cel::Value> operator()(const absl::Duration duration) {
return cel::interop_internal::CreateDurationValue(duration);
}
cel::Handle<cel::Value> operator()(const absl::Time timestamp) {
return cel::interop_internal::CreateTimestampValue(timestamp);
}
} handler;
return absl::visit(handler, const_expr.constant_kind());
}
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateConstValueStep(
cel::Handle<cel::Value> value, int64_t expr_id, bool comes_from_ast) {
return std::make_unique<CompilerConstantStep>(std::move(value), expr_id,
comes_from_ast);
}
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateConstValueStep(
const Constant& value, int64_t expr_id, bool comes_from_ast) {
return std::make_unique<ConstValueStep>(value, expr_id, comes_from_ast);
}
} // namespace google::api::expr::runtime