-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcel_function.cc
More file actions
83 lines (67 loc) · 2.54 KB
/
cel_function.cc
File metadata and controls
83 lines (67 loc) · 2.54 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
#include "eval/public/cel_function.h"
#include <cstddef>
#include <vector>
#include "absl/base/nullability.h"
#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "common/value.h"
#include "eval/internal/interop.h"
#include "eval/public/cel_value.h"
#include "internal/status_macros.h"
#include "runtime/activation_interface.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace google::api::expr::runtime {
using ::cel::Value;
using ::cel::interop_internal::ToLegacyValue;
bool CelFunction::MatchArguments(absl::Span<const CelValue> arguments) const {
auto types_size = descriptor().types().size();
if (types_size != arguments.size()) {
return false;
}
for (size_t i = 0; i < types_size; i++) {
const auto& value = arguments[i];
CelValue::Type arg_type = descriptor().types()[i];
if (value.type() != arg_type && arg_type != CelValue::Type::kAny) {
return false;
}
}
return true;
}
bool CelFunction::MatchArguments(absl::Span<const cel::Value> arguments) const {
auto types_size = descriptor().types().size();
if (types_size != arguments.size()) {
return false;
}
for (size_t i = 0; i < types_size; i++) {
const auto& value = arguments[i];
CelValue::Type arg_type = descriptor().types()[i];
if (value->kind() != arg_type && arg_type != CelValue::Type::kAny) {
return false;
}
}
return true;
}
absl::StatusOr<Value> CelFunction::Invoke(
absl::Span<const cel::Value> arguments,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena,
const cel::ActivationInterface* absl_nullable activation) const {
std::vector<CelValue> legacy_args;
legacy_args.reserve(arguments.size());
// Users shouldn't be able to create expressions that call registered
// functions with unconvertible types, but it's possible to create an AST that
// can trigger this by making an unexpected call on a value that the
// interpreter expects to only be used with internal program steps.
for (const auto& arg : arguments) {
CEL_ASSIGN_OR_RETURN(legacy_args.emplace_back(),
ToLegacyValue(arena, arg, true));
}
CelValue legacy_result;
CEL_RETURN_IF_ERROR(Evaluate(legacy_args, &legacy_result, arena));
return cel::interop_internal::LegacyValueToModernValueOrDie(
arena, legacy_result, /*unchecked=*/true);
}
} // namespace google::api::expr::runtime