forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcel_function.h
More file actions
99 lines (81 loc) · 3.52 KB
/
Copy pathcel_function.h
File metadata and controls
99 lines (81 loc) · 3.52 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
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_FUNCTION_H_
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_FUNCTION_H_
#include <vector>
#include "absl/types/span.h"
#include "eval/public/cel_value.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
// Type that describes CelFunction.
// This complex structure is needed for overloads support.
class CelFunctionDescriptor {
public:
CelFunctionDescriptor(const std::string& name, bool receiver_style,
const std::vector<CelValue::Type> types)
: name_(name), receiver_style_(receiver_style), types_(types) {}
// Function name.
const std::string& name() const { return name_; }
// Whether function is receiver style i.e. true means arg0.name(args[1:]...).
bool receiver_style() const { return receiver_style_; }
// The argmument types the function accepts.
const std::vector<CelValue::Type>& types() const { return types_; }
// Helper for matching a descriptor. This tests that the shape is the same --
// |other| accepts the same number and types of arguments and is the same call
// style).
bool ShapeMatches(const CelFunctionDescriptor& other) const {
return ShapeMatches(other.receiver_style(), other.types());
}
bool ShapeMatches(bool receiver_style,
const std::vector<CelValue::Type>& types) const;
private:
std::string name_;
bool receiver_style_;
std::vector<CelValue::Type> types_;
};
// CelFunction is a handler that represents single
// CEL function.
// CelFunction provides Evaluate() method, that performs
// evaluation of the function. CelFunction instances provide
// descriptors that contain function information:
// - name
// - is function receiver style (e.f(g) vs f(e,g))
// - amount of arguments and their types.
// Function overloads are resolved based on their arguments and
// receiver style.
class CelFunction {
public:
// Build CelFunction from descriptor
explicit CelFunction(const CelFunctionDescriptor& descriptor)
: descriptor_(descriptor) {}
// Non-copyable
CelFunction(const CelFunction& other) = delete;
CelFunction& operator=(const CelFunction& other) = delete;
virtual ~CelFunction() {}
// Evaluates CelValue based on arguments supplied.
// If result content is to be allocated (e.g. string concatenation),
// arena parameter must be used as allocation manager.
// Provides resulting value in *result, returns evaluation success/failure.
// Methods should discriminate between internal evaluator errors, that
// makes further evaluation impossible or unreasonable (example - argument
// type or number mismatch) and business logic errors (example division by
// zero). When former happens, error Status is returned and *result is
// not changed. In case of business logic error, returned Status is Ok, and
// error is provided as CelValue - wrapped CelError in *result.
virtual absl::Status Evaluate(absl::Span<const CelValue> arguments,
CelValue* result,
google::protobuf::Arena* arena) const = 0;
// Determines whether instance of CelFunction is applicable to
// arguments supplied.
// Method is called during runtime.
bool MatchArguments(absl::Span<const CelValue> arguments) const;
// CelFunction descriptor
const CelFunctionDescriptor& descriptor() const { return descriptor_; }
private:
CelFunctionDescriptor descriptor_;
};
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google
#endif // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_FUNCTION_H_