forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcel_function_registry.cc
More file actions
112 lines (93 loc) · 3.6 KB
/
Copy pathcel_function_registry.cc
File metadata and controls
112 lines (93 loc) · 3.6 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "eval/public/cel_function_registry.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
absl::Status CelFunctionRegistry::Register(
std::unique_ptr<CelFunction> function) {
const CelFunctionDescriptor& descriptor = function->descriptor();
if (DescriptorRegistered(descriptor)) {
return absl::Status(
absl::StatusCode::kAlreadyExists,
"CelFunction with specified parameters already registered");
}
auto& overloads = functions_[descriptor.name()];
overloads.static_overloads.push_back(std::move(function));
return absl::OkStatus();
}
absl::Status CelFunctionRegistry::RegisterLazyFunction(
const CelFunctionDescriptor& descriptor,
std::unique_ptr<CelFunctionProvider> factory) {
if (DescriptorRegistered(descriptor)) {
return absl::Status(
absl::StatusCode::kAlreadyExists,
"CelFunction with specified parameters already registered");
}
auto& overloads = functions_[descriptor.name()];
LazyFunctionEntry entry = std::make_unique<LazyFunctionEntry::element_type>(
descriptor, std::move(factory));
overloads.lazy_overloads.push_back(std::move(entry));
return absl::OkStatus();
}
std::vector<const CelFunction*> CelFunctionRegistry::FindOverloads(
absl::string_view name, bool receiver_style,
const std::vector<CelValue::Type>& types) const {
std::vector<const CelFunction*> matched_funcs;
auto overloads = functions_.find(std::string(name));
if (overloads == functions_.end()) {
return matched_funcs;
}
for (const auto& func_ptr : overloads->second.static_overloads) {
if (func_ptr->descriptor().ShapeMatches(receiver_style, types)) {
matched_funcs.push_back(func_ptr.get());
}
}
return matched_funcs;
}
std::vector<const CelFunctionProvider*> CelFunctionRegistry::FindLazyOverloads(
absl::string_view name, bool receiver_style,
const std::vector<CelValue::Type>& types) const {
std::vector<const CelFunctionProvider*> matched_funcs;
auto overloads = functions_.find(std::string(name));
if (overloads == functions_.end()) {
return matched_funcs;
}
for (const LazyFunctionEntry& entry : overloads->second.lazy_overloads) {
if (entry->first.ShapeMatches(receiver_style, types)) {
matched_funcs.push_back(entry->second.get());
}
}
return matched_funcs;
}
absl::node_hash_map<std::string, std::vector<const CelFunctionDescriptor*>>
CelFunctionRegistry::ListFunctions() const {
absl::node_hash_map<std::string, std::vector<const CelFunctionDescriptor*>>
descriptor_map;
for (const auto& entry : functions_) {
std::vector<const CelFunctionDescriptor*> descriptors;
const RegistryEntry& function_entry = entry.second;
descriptors.reserve(function_entry.static_overloads.size() +
function_entry.lazy_overloads.size());
for (const auto& func : function_entry.static_overloads) {
descriptors.push_back(&func->descriptor());
}
for (const LazyFunctionEntry& func : function_entry.lazy_overloads) {
descriptors.push_back(&func->first);
}
descriptor_map[entry.first] = std::move(descriptors);
}
return descriptor_map;
}
bool CelFunctionRegistry::DescriptorRegistered(
const CelFunctionDescriptor& descriptor) const {
return !(FindOverloads(descriptor.name(), descriptor.receiver_style(),
descriptor.types())
.empty()) ||
!(FindLazyOverloads(descriptor.name(), descriptor.receiver_style(),
descriptor.types())
.empty());
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google