-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcel_function.cc
More file actions
73 lines (59 loc) · 2.08 KB
/
Copy pathcel_function.cc
File metadata and controls
73 lines (59 loc) · 2.08 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
#include "eval/public/cel_function.h"
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/function.h"
#include "eval/internal/interop.h"
#include "extensions/protobuf/memory_manager.h"
#include "internal/status_macros.h"
#include "google/protobuf/arena.h"
namespace google::api::expr::runtime {
using ::cel::FunctionEvaluationContext;
using ::cel::Handle;
using ::cel::Value;
using ::cel::extensions::ProtoMemoryManager;
using ::cel::interop_internal::ModernValueToLegacyValueOrDie;
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::Handle<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<Handle<Value>> CelFunction::Invoke(
const FunctionEvaluationContext& context,
absl::Span<const Handle<Value>> arguments) const {
google::protobuf::Arena* arena = ProtoMemoryManager::CastToProtoArena(
context.value_factory().memory_manager());
std::vector<CelValue> legacy_args = ModernValueToLegacyValueOrDie(
context.value_factory().memory_manager(), arguments, 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