Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe22476

Browse files
jckingcopybara-github
authored andcommitted
Initial minimal implementation of Comprehensions V2
PiperOrigin-RevId: 704353242
1 parent 16a71c5 commit fe22476

19 files changed

Lines changed: 1907 additions & 44 deletions

common/expr_factory.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,32 @@ class ExprFactory {
317317
AccuVar accu_var, AccuInit accu_init,
318318
LoopCondition loop_condition, LoopStep loop_step,
319319
Result result) {
320+
return NewComprehension(id, std::move(iter_var), "", std::move(iter_range),
321+
std::move(accu_var), std::move(accu_init),
322+
std::move(loop_condition), std::move(loop_step),
323+
std::move(result));
324+
}
325+
326+
template <typename IterVar, typename IterVar2, typename IterRange,
327+
typename AccuVar, typename AccuInit, typename LoopCondition,
328+
typename LoopStep, typename Result,
329+
typename = std::enable_if_t<IsStringLike<IterVar>::value>,
330+
typename = std::enable_if_t<IsStringLike<IterVar2>::value>,
331+
typename = std::enable_if_t<IsExprLike<IterRange>::value>,
332+
typename = std::enable_if_t<IsStringLike<AccuVar>::value>,
333+
typename = std::enable_if_t<IsExprLike<AccuInit>::value>,
334+
typename = std::enable_if_t<IsExprLike<LoopStep>::value>,
335+
typename = std::enable_if_t<IsExprLike<LoopCondition>::value>,
336+
typename = std::enable_if_t<IsExprLike<Result>::value>>
337+
Expr NewComprehension(ExprId id, IterVar iter_var, IterVar2 iter_var2,
338+
IterRange iter_range, AccuVar accu_var,
339+
AccuInit accu_init, LoopCondition loop_condition,
340+
LoopStep loop_step, Result result) {
320341
Expr expr;
321342
expr.set_id(id);
322343
auto& comprehension_expr = expr.mutable_comprehension_expr();
323344
comprehension_expr.set_iter_var(std::move(iter_var));
345+
comprehension_expr.set_iter_var2(std::move(iter_var2));
324346
comprehension_expr.set_iter_range(std::move(iter_range));
325347
comprehension_expr.set_accu_var(std::move(accu_var));
326348
comprehension_expr.set_accu_init(std::move(accu_init));

common/values/error_value.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ bool IsNoSuchField(const ErrorValue& value);
156156

157157
bool IsNoSuchKey(const ErrorValue& value);
158158

159+
class ErrorValueReturn final {
160+
public:
161+
ErrorValueReturn() = default;
162+
163+
ErrorValue operator()(absl::Status status) const {
164+
return ErrorValue(std::move(status));
165+
}
166+
};
167+
159168
} // namespace cel
160169

161170
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_ERROR_VALUE_H_

conformance/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ cc_library(
7373
"//eval/public:cel_value",
7474
"//eval/public:transform_utility",
7575
"//extensions:bindings_ext",
76+
"//extensions:comprehensions_v2_functions",
77+
"//extensions:comprehensions_v2_macros",
7678
"//extensions:encoders",
7779
"//extensions:math_ext",
7880
"//extensions:math_ext_decls",

conformance/service.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#include "eval/public/cel_value.h"
6262
#include "eval/public/transform_utility.h"
6363
#include "extensions/bindings_ext.h"
64+
#include "extensions/comprehensions_v2_functions.h"
65+
#include "extensions/comprehensions_v2_macros.h"
6466
#include "extensions/encoders.h"
6567
#include "extensions/math_ext.h"
6668
#include "extensions/math_ext_decls.h"
@@ -256,6 +258,8 @@ absl::Status LegacyParse(const conformance::v1alpha1::ParseRequest& request,
256258
options.enable_optional_syntax = enable_optional_syntax;
257259
cel::MacroRegistry macros;
258260
CEL_RETURN_IF_ERROR(cel::RegisterStandardMacros(macros, options));
261+
CEL_RETURN_IF_ERROR(
262+
cel::extensions::RegisterComprehensionsV2Macros(macros, options));
259263
CEL_RETURN_IF_ERROR(cel::extensions::RegisterBindingsMacros(macros, options));
260264
CEL_RETURN_IF_ERROR(cel::extensions::RegisterMathMacros(macros, options));
261265
CEL_RETURN_IF_ERROR(cel::extensions::RegisterProtoMacros(macros, options));
@@ -334,6 +338,8 @@ class LegacyConformanceServiceImpl : public ConformanceServiceInterface {
334338
cel::expr::conformance::proto3::TestAllTypes::NestedEnum_descriptor());
335339
CEL_RETURN_IF_ERROR(
336340
RegisterBuiltinFunctions(builder->GetRegistry(), options));
341+
CEL_RETURN_IF_ERROR(cel::extensions::RegisterComprehensionsV2Functions(
342+
builder->GetRegistry(), options));
337343
CEL_RETURN_IF_ERROR(cel::extensions::RegisterEncodersFunctions(
338344
builder->GetRegistry(), options));
339345
CEL_RETURN_IF_ERROR(cel::extensions::RegisterStringsFunctions(
@@ -507,6 +513,8 @@ class ModernConformanceServiceImpl : public ConformanceServiceInterface {
507513
type_registry,
508514
cel::expr::conformance::proto3::TestAllTypes::NestedEnum_descriptor()));
509515

516+
CEL_RETURN_IF_ERROR(cel::extensions::RegisterComprehensionsV2Functions(
517+
builder.function_registry(), options));
510518
CEL_RETURN_IF_ERROR(cel::extensions::EnableOptionalTypes(builder));
511519
CEL_RETURN_IF_ERROR(cel::extensions::RegisterEncodersFunctions(
512520
builder.function_registry(), options));

0 commit comments

Comments
 (0)