forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_adapter.h
More file actions
409 lines (358 loc) · 13.2 KB
/
Copy pathfunction_adapter.h
File metadata and controls
409 lines (358 loc) · 13.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Definitions for template helpers to wrap C++ functions as CEL extension
// function implementations.
#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_FUNCTION_ADAPTER_H_
#define THIRD_PARTY_CEL_CPP_RUNTIME_FUNCTION_ADAPTER_H_
#include <functional>
#include <memory>
#include <vector>
#include "absl/functional/bind_front.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/function.h"
#include "base/function_descriptor.h"
#include "common/kind.h"
#include "common/value.h"
#include "internal/status_macros.h"
#include "runtime/internal/function_adapter.h"
#include "runtime/register_function_helper.h"
namespace cel {
namespace runtime_internal {
template <typename T>
struct AdaptedTypeTraits {
using AssignableType = T;
static T ToArg(AssignableType v) { return v; }
};
// Specialization for cref parameters without forcing a temporary copy of the
// underlying handle argument.
template <>
struct AdaptedTypeTraits<const Value&> {
using AssignableType = const Value*;
static std::reference_wrapper<const Value> ToArg(AssignableType v) {
return *v;
}
};
template <>
struct AdaptedTypeTraits<const StringValue&> {
using AssignableType = const StringValue*;
static std::reference_wrapper<const StringValue> ToArg(AssignableType v) {
return *v;
}
};
template <>
struct AdaptedTypeTraits<const BytesValue&> {
using AssignableType = const BytesValue*;
static std::reference_wrapper<const BytesValue> ToArg(AssignableType v) {
return *v;
}
};
// Partial specialization for other cases.
//
// These types aren't referenceable since they aren't actually
// represented as alternatives in the underlying variant.
//
// This still requires an implicit copy and corresponding ref-count increase.
template <typename T>
struct AdaptedTypeTraits<const T&> {
using AssignableType = T;
static T ToArg(AssignableType v) { return v; }
};
template <typename... Args>
struct KindAdderImpl;
template <typename Arg, typename... Args>
struct KindAdderImpl<Arg, Args...> {
static void AddTo(std::vector<cel::Kind>& args) {
args.push_back(AdaptedKind<Arg>());
KindAdderImpl<Args...>::AddTo(args);
}
};
template <>
struct KindAdderImpl<> {
static void AddTo(std::vector<cel::Kind>& args) {}
};
template <typename... Args>
struct KindAdder {
static std::vector<cel::Kind> Kinds() {
std::vector<cel::Kind> args;
KindAdderImpl<Args...>::AddTo(args);
return args;
}
};
template <typename T>
struct ApplyReturnType {
using type = absl::StatusOr<T>;
};
template <typename T>
struct ApplyReturnType<absl::StatusOr<T>> {
using type = absl::StatusOr<T>;
};
template <int N, typename Arg, typename... Args>
struct IndexerImpl {
using type = typename IndexerImpl<N - 1, Args...>::type;
};
template <typename Arg, typename... Args>
struct IndexerImpl<0, Arg, Args...> {
using type = Arg;
};
template <int N, typename... Args>
struct Indexer {
static_assert(N < sizeof...(Args) && N >= 0);
using type = typename IndexerImpl<N, Args...>::type;
};
template <int N, typename... Args>
struct ApplyHelper {
template <typename T, typename Op>
static typename ApplyReturnType<T>::type Apply(
Op&& op, absl::Span<const Value> input) {
constexpr int idx = sizeof...(Args) - N;
using Arg = typename Indexer<idx, Args...>::type;
using ArgTraits = AdaptedTypeTraits<Arg>;
typename ArgTraits::AssignableType arg_i;
CEL_RETURN_IF_ERROR(HandleToAdaptedVisitor{input[idx]}(&arg_i));
return ApplyHelper<N - 1, Args...>::template Apply<T>(
absl::bind_front(std::forward<Op>(op), ArgTraits::ToArg(arg_i)), input);
}
};
template <typename... Args>
struct ApplyHelper<0, Args...> {
template <typename T, typename Op>
static typename ApplyReturnType<T>::type Apply(
Op&& op, absl::Span<const Value> input) {
return op();
}
};
} // namespace runtime_internal
// Adapter class for generating CEL extension functions from a two argument
// function. Generates an implementation of the cel::Function interface that
// calls the function to wrap.
//
// Extension functions must distinguish between recoverable errors (error that
// should participate in CEL's error pruning) and unrecoverable errors (a non-ok
// absl::Status that stops evaluation). The function to wrap may return
// StatusOr<T> to propagate a Status, or return a Value with an Error
// value to introduce a CEL error.
//
// To introduce an extension function that may accept any kind of CEL value as
// an argument, the wrapped function should use a Value<Handle> parameter and
// check the type of the argument at evaluation time.
//
// Supported CEL to C++ type mappings:
// bool -> bool
// double -> double
// uint -> uint64_t
// int -> int64_t
// timestamp -> absl::Time
// duration -> absl::Duration
//
// Complex types may be referred to by cref or value.
// To return these, users should return a Value.
// any/dyn -> Value, const Value&
// string -> StringValue | const StringValue&
// bytes -> BytesValue | const BytesValue&
// list -> ListValue | const ListValue&
// map -> MapValue | const MapValue&
// struct -> StructValue | const StructValue&
// null -> NullValue | const NullValue&
//
// To intercept error and unknown arguments, users must use a non-strict
// overload with all arguments typed as any and check the kind of the
// Value argument.
//
// Example Usage:
// double SquareDifference(ValueManager&, double x, double y) {
// return x * x - y * y;
// }
//
// {
// RuntimeBuilder builder;
// // Initialize Expression builder with built-ins as needed.
//
// CEL_RETURN_IF_ERROR(
// builder.function_registry().Register(
// BinaryFunctionAdapter<double, double, double>::CreateDescriptor(
// "sq_diff", /*receiver_style=*/false),
// BinaryFunctionAdapter<double, double, double>::WrapFunction(
// &SquareDifference)));
//
//
// // Alternative shorthand
// auto status = BinaryFunctionAdapter<double, double, double>::
// RegisterGlobalOverload(
// "sq_diff",
// &SquareDifference,
// builder.function_registry());
// CEL_RETURN_IF_ERROR(status);
// }
//
// example CEL expression:
// sq_diff(4, 3) == 7 [true]
//
template <typename T, typename U, typename V>
class BinaryFunctionAdapter
: public RegisterHelper<BinaryFunctionAdapter<T, U, V>> {
public:
using FunctionType = std::function<T(ValueManager&, U, V)>;
static std::unique_ptr<cel::Function> WrapFunction(FunctionType fn) {
return std::make_unique<BinaryFunctionImpl>(std::move(fn));
}
static FunctionDescriptor CreateDescriptor(absl::string_view name,
bool receiver_style,
bool is_strict = true) {
return FunctionDescriptor(name, receiver_style,
{runtime_internal::AdaptedKind<U>(),
runtime_internal::AdaptedKind<V>()},
is_strict);
}
private:
class BinaryFunctionImpl : public cel::Function {
public:
explicit BinaryFunctionImpl(FunctionType fn) : fn_(std::move(fn)) {}
absl::StatusOr<Value> Invoke(const FunctionEvaluationContext& context,
absl::Span<const Value> args) const override {
using Arg1Traits = runtime_internal::AdaptedTypeTraits<U>;
using Arg2Traits = runtime_internal::AdaptedTypeTraits<V>;
if (args.size() != 2) {
return absl::InvalidArgumentError(
"unexpected number of arguments for binary function");
}
typename Arg1Traits::AssignableType arg1;
typename Arg2Traits::AssignableType arg2;
CEL_RETURN_IF_ERROR(
runtime_internal::HandleToAdaptedVisitor{args[0]}(&arg1));
CEL_RETURN_IF_ERROR(
runtime_internal::HandleToAdaptedVisitor{args[1]}(&arg2));
if constexpr (std::is_same_v<T, Value> ||
std::is_same_v<T, absl::StatusOr<Value>>) {
return fn_(context.value_factory(), Arg1Traits::ToArg(arg1),
Arg2Traits::ToArg(arg2));
} else {
T result = fn_(context.value_factory(), Arg1Traits::ToArg(arg1),
Arg2Traits::ToArg(arg2));
return runtime_internal::AdaptedToHandleVisitor{}(std::move(result));
}
}
private:
BinaryFunctionAdapter::FunctionType fn_;
};
};
// Adapter class for generating CEL extension functions from a one argument
// function.
//
// See documentation for Binary Function adapter for general recommendations.
//
// Example Usage:
// double Invert(ValueManager&, double x) {
// return 1 / x;
// }
//
// {
// std::unique_ptr<CelExpressionBuilder> builder;
//
// CEL_RETURN_IF_ERROR(
// builder->GetRegistry()->Register(
// UnaryFunctionAdapter<double, double>::CreateDescriptor("inv",
// /*receiver_style=*/false),
// UnaryFunctionAdapter<double, double>::WrapFunction(&Invert)));
// }
// // example CEL expression
// inv(4) == 1/4 [true]
template <typename T, typename U>
class UnaryFunctionAdapter : public RegisterHelper<UnaryFunctionAdapter<T, U>> {
public:
using FunctionType = std::function<T(ValueManager&, U)>;
static std::unique_ptr<cel::Function> WrapFunction(FunctionType fn) {
return std::make_unique<UnaryFunctionImpl>(std::move(fn));
}
static FunctionDescriptor CreateDescriptor(absl::string_view name,
bool receiver_style,
bool is_strict = true) {
return FunctionDescriptor(name, receiver_style,
{runtime_internal::AdaptedKind<U>()}, is_strict);
}
private:
class UnaryFunctionImpl : public cel::Function {
public:
explicit UnaryFunctionImpl(FunctionType fn) : fn_(std::move(fn)) {}
absl::StatusOr<Value> Invoke(const FunctionEvaluationContext& context,
absl::Span<const Value> args) const override {
using ArgTraits = runtime_internal::AdaptedTypeTraits<U>;
if (args.size() != 1) {
return absl::InvalidArgumentError(
"unexpected number of arguments for unary function");
}
typename ArgTraits::AssignableType arg1;
CEL_RETURN_IF_ERROR(
runtime_internal::HandleToAdaptedVisitor{args[0]}(&arg1));
if constexpr (std::is_same_v<T, Value> ||
std::is_same_v<T, absl::StatusOr<Value>>) {
return fn_(context.value_factory(), ArgTraits::ToArg(arg1));
} else {
T result = fn_(context.value_factory(), ArgTraits::ToArg(arg1));
return runtime_internal::AdaptedToHandleVisitor{}(std::move(result));
}
}
private:
FunctionType fn_;
};
};
// Generic adapter class for generating CEL extension functions from an
// n-argument function. Prefer using the Binary and Unary versions. They are
// simpler and cover most use cases.
//
// See documentation for Binary Function adapter for general recommendations.
template <typename T, typename... Args>
class VariadicFunctionAdapter
: public RegisterHelper<VariadicFunctionAdapter<T, Args...>> {
public:
using FunctionType = std::function<T(ValueManager&, Args...)>;
static std::unique_ptr<cel::Function> WrapFunction(FunctionType fn) {
return std::make_unique<VariadicFunctionImpl>(std::move(fn));
}
static FunctionDescriptor CreateDescriptor(absl::string_view name,
bool receiver_style,
bool is_strict = true) {
return FunctionDescriptor(name, receiver_style,
runtime_internal::KindAdder<Args...>::Kinds(),
is_strict);
}
private:
class VariadicFunctionImpl : public cel::Function {
public:
explicit VariadicFunctionImpl(FunctionType fn) : fn_(std::move(fn)) {}
absl::StatusOr<Value> Invoke(const FunctionEvaluationContext& context,
absl::Span<const Value> args) const override {
if (args.size() != sizeof...(Args)) {
return absl::InvalidArgumentError(
absl::StrCat("unexpected number of arguments for variadic(",
sizeof...(Args), ") function"));
}
CEL_ASSIGN_OR_RETURN(
T result,
(runtime_internal::ApplyHelper<sizeof...(Args), Args...>::
template Apply<T>(
absl::bind_front(fn_, std::ref(context.value_factory())),
args)));
return runtime_internal::AdaptedToHandleVisitor{}(std::move(result));
}
private:
FunctionType fn_;
};
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_RUNTIME_FUNCTION_ADAPTER_H_