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.h
More file actions
90 lines (77 loc) · 3.56 KB
/
Copy pathcel_function_registry.h
File metadata and controls
90 lines (77 loc) · 3.56 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
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_FUNCTION_REGISTRY_H_
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_FUNCTION_REGISTRY_H_
#include "absl/container/node_hash_map.h"
#include "absl/types/span.h"
#include "eval/public/cel_function.h"
#include "eval/public/cel_function_provider.h"
#include "eval/public/cel_options.h"
#include "eval/public/cel_value.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
// CelFunctionRegistry class allows to register builtin or custom
// CelFunction handlers with it and look them up when creating
// CelExpression objects from Expr ASTs.
class CelFunctionRegistry {
public:
CelFunctionRegistry() {}
~CelFunctionRegistry() {}
// Register CelFunction object. Object ownership is
// passed to registry.
// Function registration should be performed prior to
// CelExpression creation.
absl::Status Register(std::unique_ptr<CelFunction> function);
// Register a lazily provided function. CelFunctionProvider is used to get
// a CelFunction ptr at evaluation time. The registry takes ownership of the
// factory.
absl::Status RegisterLazyFunction(
const CelFunctionDescriptor& descriptor,
std::unique_ptr<CelFunctionProvider> factory);
// Register a lazily provided function. This overload uses a default provider
// that delegates to the activation at evaluation time.
absl::Status RegisterLazyFunction(const CelFunctionDescriptor& descriptor) {
return RegisterLazyFunction(descriptor, CreateActivationFunctionProvider());
}
// Find subset of CelFunction that match overload conditions
// As types may not be available during expression compilation,
// further narrowing of this subset will happen at evaluation stage.
// name - the name of CelFunction;
// receiver_style - indicates whether function has receiver style;
// types - argument types. If type is not known during compilation,
// DYN value should be passed.
std::vector<const CelFunction*> FindOverloads(
absl::string_view name, bool receiver_style,
const std::vector<CelValue::Type>& types) const;
// Find subset of CelFunction providers that match overload conditions
// As types may not be available during expression compilation,
// further narrowing of this subset will happen at evaluation stage.
// name - the name of CelFunction;
// receiver_style - indicates whether function has receiver style;
// types - argument types. If type is not known during compilation,
// DYN value should be passed.
std::vector<const CelFunctionProvider*> FindLazyOverloads(
absl::string_view name, bool receiver_style,
const std::vector<CelValue::Type>& types) const;
// Retrieve list of registered function descriptors. This includes both
// static and lazy functions.
absl::node_hash_map<std::string, std::vector<const CelFunctionDescriptor*>>
ListFunctions() const;
private:
// Returns whether the descriptor is registered in either as a lazy funtion or
// in the static functions.
bool DescriptorRegistered(const CelFunctionDescriptor& descriptor) const;
using StaticFunctionEntry = std::unique_ptr<CelFunction>;
using LazyFunctionEntry = std::unique_ptr<
std::pair<CelFunctionDescriptor, std::unique_ptr<CelFunctionProvider>>>;
struct RegistryEntry {
std::vector<StaticFunctionEntry> static_overloads;
std::vector<LazyFunctionEntry> lazy_overloads;
};
absl::node_hash_map<std::string, RegistryEntry> functions_;
};
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google
#endif // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_FUNCTION_REGISTRY_H_