From f8cdaa98d15cf684372c44607a6a582897f38ce2 Mon Sep 17 00:00:00 2001 From: CEL Dev Team Date: Fri, 13 Nov 2026 05:09:27 -0800 Subject: [PATCH] No public description PiperOrigin-RevId: 1020948789 --- eval/compiler/flat_expr_builder.cc | 4 + eval/compiler/flat_expr_builder_test.cc | 193 +++++ eval/eval/select_step.cc | 182 +++-- eval/eval/select_step_test.cc | 384 +++++++++ eval/public/structs/BUILD | 1 + .../cel_proto_wrap_value_to_message.cc | 36 +- .../cel_proto_wrap_value_to_message_test.cc | 32 + runtime/standard/BUILD | 71 +- runtime/standard/arithmetic_functions.cc | 355 ++++----- runtime/standard/comparison_functions.cc | 348 ++++---- runtime/standard/container_functions.cc | 108 ++- .../container_membership_functions.cc | 444 +++++------ runtime/standard/equality_functions.cc | 393 +++------ runtime/standard/logical_functions.cc | 80 +- runtime/standard/regex_functions.cc | 56 +- runtime/standard/string_functions.cc | 184 +++-- runtime/standard/time_functions.cc | 726 ++++++----------- runtime/standard/type_conversion_functions.cc | 754 +++++++++--------- 18 files changed, 2313 insertions(+), 2038 deletions(-) diff --git a/eval/compiler/flat_expr_builder.cc b/eval/compiler/flat_expr_builder.cc index 3626937e9..b026857ff 100644 --- a/eval/compiler/flat_expr_builder.cc +++ b/eval/compiler/flat_expr_builder.cc @@ -963,6 +963,10 @@ class FlatExprVisitor : public cel::AstVisitor { if (options_.enable_typed_field_access) { std::optional operand_type = extension_context_.GetResolvedType(select_expr.operand().id()); + if (enable_optional_types_ && operand_type.has_value() && + operand_type->IsOptional()) { + operand_type = operand_type->GetOptional().GetParameter(); + } if (operand_type.has_value() && operand_type->IsStruct()) { struct_type = operand_type->GetStruct(); if (struct_type.has_value()) { diff --git a/eval/compiler/flat_expr_builder_test.cc b/eval/compiler/flat_expr_builder_test.cc index fe1a2ee71..b74d1071d 100644 --- a/eval/compiler/flat_expr_builder_test.cc +++ b/eval/compiler/flat_expr_builder_test.cc @@ -3844,6 +3844,199 @@ TEST(FlatExprBuilderTest, BindInlinesConstantAndFoldsMainExpression) { ASSERT_OK_AND_ASSIGN(CelValue result, cel_expr->Evaluate(activation, &arena)); EXPECT_THAT(result, test::IsCelInt64(16)); } + +class OptionalWrapFunction final : public cel::Function { + public: + explicit OptionalWrapFunction(bool empty) : empty_(empty) {} + + absl::StatusOr Invoke( + absl::Span args, + const InvokeContext& context) const override { + if (empty_) { + return cel::OptionalValue::None(); + } + return cel::OptionalValue::Of(args[0], context.arena()); + } + + private: + bool empty_; +}; + +class OptionalUnwrapOrSentinelFunction final : public cel::Function { + public: + absl::StatusOr Invoke( + absl::Span args, + const InvokeContext& context) const override { + if (!args[0].IsOptional()) { + return args[0]; + } + const cel::OptionalValue& opt = args[0].GetOptional(); + if (!opt.HasValue()) { + return cel::StringValue::Literal("optional.none"); + } + return opt.Value(); + } +}; + +TEST(FlatExprBuilderTest, ChainedTypedSelectOptionalAndHas) { + std::shared_ptr env = + NewTestingRuntimeEnv(); + cel::RuntimeOptions options; + options.enable_typed_field_access = true; + CelExpressionBuilderFlatImpl builder(env, options); + ASSERT_THAT(RegisterBuiltinFunctions(builder.GetRegistry()), IsOk()); + ASSERT_THAT(builder.GetRegistry()->Register( + cel::FunctionDescriptor("make_opt", false, {cel::Kind::kAny}), + std::make_unique(/*empty=*/false)), + IsOk()); + ASSERT_THAT(builder.GetRegistry()->Register( + cel::FunctionDescriptor("make_none", false, {}), + std::make_unique(/*empty=*/true)), + IsOk()); + ASSERT_THAT( + builder.GetRegistry()->Register( + cel::FunctionDescriptor("unwrap_opt", false, {cel::Kind::kAny}), + std::make_unique()), + IsOk()); + builder.flat_expr_builder().enable_optional_types(); + + auto make_optional_msg_type = [](std::string msg_name) { + return cel::TypeSpec(cel::AbstractType( + "optional_type", + {cel::TypeSpec(cel::MessageTypeSpec(std::move(msg_name)))})); + }; + + auto build_select_chain = + [&](bool start_with_none, + bool test_only) -> absl::StatusOr { + cel::Expr ident_or_call; + if (start_with_none) { + ident_or_call.set_id(1); + ident_or_call.mutable_call_expr().set_function("make_none"); + } else { + ident_or_call.set_id(1); + auto& call = ident_or_call.mutable_call_expr(); + call.set_function("make_opt"); + auto& arg = call.mutable_args().emplace_back(); + arg.set_id(10); + arg.mutable_ident_expr().set_name("message"); + } + + cel::Expr select1; + select1.set_id(2); + select1.mutable_select_expr().set_field("single_nested_message"); + select1.mutable_select_expr().set_operand(std::move(ident_or_call)); + + cel::Expr select2; + select2.set_id(3); + select2.mutable_select_expr().set_field("bb"); + select2.mutable_select_expr().set_test_only(test_only); + select2.mutable_select_expr().set_operand(std::move(select1)); + + cel::Expr root; + if (test_only) { + root = std::move(select2); + } else { + root.set_id(4); + root.mutable_call_expr().set_function("unwrap_opt"); + root.mutable_call_expr().mutable_args().push_back(std::move(select2)); + } + + cel::Ast::TypeMap type_map; + type_map[1] = + make_optional_msg_type("cel.expr.conformance.proto3.TestAllTypes"); + type_map[2] = make_optional_msg_type( + "cel.expr.conformance.proto3.TestAllTypes.NestedMessage"); + if (!start_with_none) { + type_map[10] = cel::TypeSpec( + cel::MessageTypeSpec("cel.expr.conformance.proto3.TestAllTypes")); + } + if (test_only) { + type_map[3] = cel::TypeSpec(cel::PrimitiveType::kBool); + } else { + type_map[3] = cel::TypeSpec(cel::AbstractType( + "optional_type", {cel::TypeSpec(cel::PrimitiveType::kInt64)})); + } + + auto ast = std::make_unique(std::move(root), cel::SourceInfo(), + cel::Ast::ReferenceMap(), + std::move(type_map), "0"); + return builder.flat_expr_builder().CreateExpressionImpl(std::move(ast), + nullptr); + }; + + google::protobuf::Arena arena; + { + ASSERT_OK_AND_ASSIGN(FlatExpression flat_expr, + build_select_chain(/*start_with_none=*/false, + /*test_only=*/false)); + CelExpressionFlatImpl cel_expr(env, std::move(flat_expr)); + + cel::expr::conformance::proto3::TestAllTypes message; + { + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena)); + EXPECT_THAT(result, test::IsCelString("optional.none")); + } + message.mutable_single_nested_message()->set_bb(55); + { + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena)); + EXPECT_THAT(result, test::IsCelInt64(55)); + } + } + { + ASSERT_OK_AND_ASSIGN(FlatExpression flat_expr, + build_select_chain(/*start_with_none=*/true, + /*test_only=*/false)); + CelExpressionFlatImpl cel_expr(env, std::move(flat_expr)); + Activation activation; + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena)); + EXPECT_THAT(result, test::IsCelString("optional.none")); + } + { + ASSERT_OK_AND_ASSIGN(FlatExpression flat_expr, + build_select_chain(/*start_with_none=*/false, + /*test_only=*/true)); + CelExpressionFlatImpl cel_expr(env, std::move(flat_expr)); + + cel::expr::conformance::proto3::TestAllTypes message; + { + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena)); + EXPECT_THAT(result, test::IsCelBool(false)); + } + message.mutable_single_nested_message()->set_bb(55); + { + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena)); + EXPECT_THAT(result, test::IsCelBool(true)); + } + } + { + ASSERT_OK_AND_ASSIGN(FlatExpression flat_expr, + build_select_chain(/*start_with_none=*/true, + /*test_only=*/true)); + CelExpressionFlatImpl cel_expr(env, std::move(flat_expr)); + Activation activation; + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena)); + EXPECT_THAT(result, test::IsCelBool(false)); + } +} } // namespace } // namespace google::api::expr::runtime diff --git a/eval/eval/select_step.cc b/eval/eval/select_step.cc index 8a70e3b82..f5082fba1 100644 --- a/eval/eval/select_step.cc +++ b/eval/eval/select_step.cc @@ -281,6 +281,48 @@ bool SupportsCachedFieldDescriptor( return true; } +// Attempts to unwrap `arg` into a `ParsedMessageValue` matching `descriptor` +// and `field_descriptor`. Sets `is_legacy` to true when `arg` is a legacy +// `CelValue` message wrapper so field wrapping preserves legacy interop. +absl::optional TryUnwrapTypedMessage( + const Value& arg, const google::protobuf::Descriptor* descriptor, + const google::protobuf::FieldDescriptor* field_descriptor, bool& is_legacy) { + if (auto unwrapped = arg.AsParsedMessage(); + unwrapped.has_value() && + SupportsCachedFieldDescriptor(*unwrapped, descriptor, field_descriptor)) { + is_legacy = false; + return *unwrapped; + } + if (const google::protobuf::Message* legacy_message = + cel::interop_internal::GetLegacyMessage(arg); + legacy_message != nullptr) { + cel::ParsedMessageValue parsed_message = + cel::UnsafeParsedMessageValue(legacy_message); + if (SupportsCachedFieldDescriptor(parsed_message, descriptor, + field_descriptor)) { + is_legacy = true; + return parsed_message; + } + } + return absl::nullopt; +} + +absl::Status GetTypedMessageField( + const cel::ParsedMessageValue& parsed_message, bool is_legacy, + const google::protobuf::FieldDescriptor* field_descriptor, + ProtoWrapperTypeOptions unboxing_option, ExecutionFrame* frame, + Value* result) { + if (is_legacy) { + return cel::interop_internal::WrapLegacyMessageField( + parsed_message.message(), field_descriptor, unboxing_option, + frame->descriptor_pool(), frame->message_factory(), frame->arena(), + result); + } + return parsed_message.GetField( + field_descriptor, unboxing_option, frame->descriptor_pool(), + frame->message_factory(), frame->arena(), result); +} + class ProtoSelectStep : public SelectStep { public: ProtoSelectStep(absl::string_view value, @@ -304,72 +346,68 @@ class ProtoSelectStep : public SelectStep { } const Value& arg = frame->value_stack().Peek(); - if (auto unwrapped = arg.AsParsedMessage(); - unwrapped.has_value() && - SupportsCachedFieldDescriptor(*unwrapped, descriptor_, - field_descriptor_)) { - EvaluateMessageFieldGet(frame, *unwrapped); + bool is_legacy = false; + if (auto parsed_message = TryUnwrapTypedMessage( + arg, descriptor_, field_descriptor_, is_legacy); + parsed_message.has_value()) { + if (CheckAttributeTrail(field_, frame)) { + return; + } + if (absl::Status status = GetTypedMessageField( + *parsed_message, is_legacy, field_descriptor_, unboxing_option_, + frame, &frame->value_stack().Peek()); + !status.ok()) { + frame->Abort(std::move(status)); + } return; - } else if (const google::protobuf::Message* legacy_message = - cel::interop_internal::GetLegacyMessage(arg); - legacy_message != nullptr) { - auto parsed_message = cel::UnsafeParsedMessageValue(legacy_message); - // A little unfortunate, but need to special case for legacy values so we - // can minimize back and forth interop conversions. - if (SupportsCachedFieldDescriptor(parsed_message, descriptor_, - field_descriptor_)) { - EvaluateMessageFieldGet(frame, legacy_message); + } + + if (enable_optional_types_ && arg.IsOptional()) { + const OptionalValue& optional_arg = arg.GetOptional(); + if (!optional_arg.HasValue()) { + if (CheckAttributeTrail(field_, frame)) { + return; + } + frame->value_stack().Peek() = OptionalValue::None(); + return; + } + Value unwrapped_opt; + optional_arg.Value(&unwrapped_opt); + if (auto parsed_message = TryUnwrapTypedMessage( + unwrapped_opt, descriptor_, field_descriptor_, is_legacy); + parsed_message.has_value()) { + if (CheckAttributeTrail(field_, frame)) { + return; + } + if (!parsed_message->HasField(field_descriptor_)) { + frame->value_stack().Peek() = OptionalValue::None(); + return; + } + Value field_result; + if (absl::Status status = GetTypedMessageField( + *parsed_message, is_legacy, field_descriptor_, unboxing_option_, + frame, &field_result); + !status.ok()) { + frame->Abort(std::move(status)); + return; + } + ABSL_DCHECK(!field_result.IsUnknown()); + frame->value_stack().Peek() = + OptionalValue::Of(std::move(field_result), frame->arena()); return; } } + // If we get an unexpected value type, fall back to the generic // implementation. SelectStep::Evaluate(frame); } private: - void EvaluateMessageFieldGet( - ExecutionFrame* frame, - const cel::ParsedMessageValue& parsed_message) const; - void EvaluateMessageFieldGet( - ExecutionFrame* frame, - const google::protobuf::Message* absl_nonnull legacy_message) const; - const google::protobuf::Descriptor* descriptor_; const google::protobuf::FieldDescriptor* field_descriptor_; }; -void ProtoSelectStep::EvaluateMessageFieldGet( - ExecutionFrame* frame, - const cel::ParsedMessageValue& parsed_message) const { - if (CheckAttributeTrail(field_, frame)) { - return; - } - if (absl::Status status = parsed_message.GetField( - field_descriptor_, unboxing_option_, frame->descriptor_pool(), - frame->message_factory(), frame->arena(), - &frame->value_stack().Peek()); - !status.ok()) { - frame->Abort(std::move(status)); - } -} - -void ProtoSelectStep::EvaluateMessageFieldGet( - ExecutionFrame* frame, - const google::protobuf::Message* absl_nonnull legacy_message) const { - ABSL_DCHECK(legacy_message != nullptr); - if (CheckAttributeTrail(field_, frame)) { - return; - } - if (absl::Status status = cel::interop_internal::WrapLegacyMessageField( - legacy_message, field_descriptor_, unboxing_option_, - frame->descriptor_pool(), frame->message_factory(), frame->arena(), - &frame->value_stack().Peek()); - !status.ok()) { - frame->Abort(std::move(status)); - } -} - class ProtoHasStep : public SelectStep { public: ProtoHasStep(absl::string_view field, bool enable_wrapper_type_null_unboxing, @@ -391,23 +429,33 @@ class ProtoHasStep : public SelectStep { } const Value& arg = frame->value_stack().Peek(); - if (auto unwrapped = arg.AsParsedMessage(); - unwrapped.has_value() && - SupportsCachedFieldDescriptor(*unwrapped, descriptor_, - field_descriptor_)) { - EvaluateHas(frame, *unwrapped); + bool is_legacy = false; + if (auto parsed_message = TryUnwrapTypedMessage( + arg, descriptor_, field_descriptor_, is_legacy); + parsed_message.has_value()) { + EvaluateHas(frame, *parsed_message); return; - } else if (const google::protobuf::Message* legacy_message = - cel::interop_internal::GetLegacyMessage(arg); - legacy_message != nullptr) { - cel::ParsedMessageValue parsed_message = - cel::UnsafeParsedMessageValue(legacy_message); - if (SupportsCachedFieldDescriptor(parsed_message, descriptor_, - field_descriptor_)) { - EvaluateHas(frame, parsed_message); + } + + if (enable_optional_types_ && arg.IsOptional()) { + const OptionalValue& optional_arg = arg.GetOptional(); + if (!optional_arg.HasValue()) { + if (CheckAttributeTrail(field_, frame)) { + return; + } + frame->value_stack().Peek() = cel::FalseValue(); + return; + } + Value unwrapped_opt; + optional_arg.Value(&unwrapped_opt); + if (auto parsed_message = TryUnwrapTypedMessage( + unwrapped_opt, descriptor_, field_descriptor_, is_legacy); + parsed_message.has_value()) { + EvaluateHas(frame, *parsed_message); return; } } + // If we get an unexpected value type, fall back to the generic // implementation. SelectStep::Evaluate(frame); @@ -447,17 +495,15 @@ absl::StatusOr> CreateTypedSelectStep( absl::string_view field, cel::StructType resolved_operand_type, cel::StructTypeField resolved_field, bool test_only, bool enable_wrapper_type_null_unboxing, bool enable_optional_types) { - if (!resolved_operand_type.IsMessage()) { + if (!resolved_operand_type.IsMessage() || !resolved_field.IsMessage()) { // The specialization only supports messages. Fallback to the generic // implementation for other types. - // TODO(uncreated-issue/89): support optional select and chaining. return CreateSelectStep(field, test_only, enable_wrapper_type_null_unboxing, enable_optional_types); } const google::protobuf::Descriptor* descriptor = resolved_operand_type.GetMessage().descriptor(); - ABSL_DCHECK(resolved_field.IsMessage()); const google::protobuf::FieldDescriptor* field_descriptor = resolved_field.GetMessage().descriptor(); diff --git a/eval/eval/select_step_test.cc b/eval/eval/select_step_test.cc index 8a89529b5..29ac2f388 100644 --- a/eval/eval/select_step_test.cc +++ b/eval/eval/select_step_test.cc @@ -20,6 +20,7 @@ #include "common/expr.h" #include "common/legacy_value.h" #include "common/type.h" +#include "common/value.h" #include "eval/eval/cel_expression_flat_impl.h" #include "eval/eval/evaluator_core.h" #include "eval/public/activation.h" @@ -1053,6 +1054,389 @@ TEST_F(SelectStepTest, TypedSelectStepUnknownPatternResolvesToUnknown) { ASSERT_TRUE(result.IsUnknownSet()); } +// Helper step to wrap the top-of-stack value in `cel::OptionalValue::Of(...)` +// while preserving its `AttributeTrail` for unknown attribute tests. +class WrapOptionalTestStep : public ExpressionStepLogic { + public: + void Evaluate(ExecutionFrame* frame) const override { + cel::Value& top = frame->value_stack().Peek(); + top = cel::OptionalValue::Of(std::move(top), frame->arena()); + } +}; + +// Helper step to unwrap `cel::OptionalValue` into a sentinel `StringValue` +// ("optional.none") or its held value so `CelExpressionFlatImpl::Evaluate` can +// return it as a `CelValue`. +class UnwrapOptionalTestStep : public ExpressionStepLogic { + public: + void Evaluate(ExecutionFrame* frame) const override { + cel::Value& top = frame->value_stack().Peek(); + if (!top.IsOptional()) { + return; + } + const cel::OptionalValue& opt = top.GetOptional(); + if (!opt.HasValue()) { + top = cel::StringValue::Literal("optional.none"); + return; + } + cel::Value inner; + opt.Value(&inner); + top = std::move(inner); + } +}; + +TEST_P(SelectStepConformanceTest, TypedHasStepTest) { + cel::StructType resolved_operand_type( + (cel::MessageType(TestAllTypes::descriptor()))); + const google::protobuf::FieldDescriptor* field_desc = + TestAllTypes::descriptor()->FindFieldByName("single_int64"); + ASSERT_NE(field_desc, nullptr); + cel::StructTypeField resolved_field((cel::MessageTypeField(field_desc))); + + ASSERT_OK_AND_ASSIGN( + auto step1, CreateTypedSelectStep( + "single_int64", resolved_operand_type, resolved_field, + /*test_only=*/true, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/false)); + + ExecutionPath path; + path.push_back(ExpressionStep::MakeIdentifierStep(0)); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step1))); + cel::RuntimeOptions options; + if (GetParam()) { + options.unknown_processing = cel::UnknownProcessingOptions::kAttributeOnly; + } + CelExpressionFlatImpl cel_expr( + env_, + FlatExpression(std::move(path), /*comprehension_slot_count=*/1, + env_->type_registry.GetComposedTypeProvider(), options, + /*arena=*/nullptr, IdentifierTable{{"message", 0}})); + + TestAllTypes message; + { + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.BoolOrDie()); + } + message.set_single_int64(42); + { + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsBool()); + EXPECT_TRUE(result.BoolOrDie()); + } +} + +TEST_P(SelectStepConformanceTest, ChainedTypedSelectStepTest) { + cel::StructType root_type((cel::MessageType(TestAllTypes::descriptor()))); + const google::protobuf::FieldDescriptor* nested_field_desc = + TestAllTypes::descriptor()->FindFieldByName("single_nested_message"); + ASSERT_NE(nested_field_desc, nullptr); + cel::StructTypeField nested_field((cel::MessageTypeField(nested_field_desc))); + + cel::StructType nested_type( + (cel::MessageType(TestAllTypes::NestedMessage::descriptor()))); + const google::protobuf::FieldDescriptor* bb_field_desc = + TestAllTypes::NestedMessage::descriptor()->FindFieldByName("bb"); + ASSERT_NE(bb_field_desc, nullptr); + cel::StructTypeField bb_field((cel::MessageTypeField(bb_field_desc))); + + ASSERT_OK_AND_ASSIGN( + auto step1, + CreateTypedSelectStep("single_nested_message", root_type, nested_field, + /*test_only=*/false, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/false)); + ASSERT_OK_AND_ASSIGN( + auto step2, + CreateTypedSelectStep("bb", nested_type, bb_field, + /*test_only=*/false, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/false)); + + ExecutionPath path; + path.push_back(ExpressionStep::MakeIdentifierStep(0)); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step1))); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step2))); + cel::RuntimeOptions options; + if (GetParam()) { + options.unknown_processing = cel::UnknownProcessingOptions::kAttributeOnly; + } + CelExpressionFlatImpl cel_expr( + env_, + FlatExpression(std::move(path), /*comprehension_slot_count=*/1, + env_->type_registry.GetComposedTypeProvider(), options, + /*arena=*/nullptr, IdentifierTable{{"message", 0}})); + + TestAllTypes message; + message.mutable_single_nested_message()->set_bb(99); + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + + ASSERT_OK_AND_ASSIGN(CelValue result, cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsInt64()); + EXPECT_EQ(result.Int64OrDie(), 99); +} + +TEST_P(SelectStepConformanceTest, TypedOptionalSelectStepTest) { + cel::StructType root_type((cel::MessageType(TestAllTypes::descriptor()))); + const google::protobuf::FieldDescriptor* nested_field_desc = + TestAllTypes::descriptor()->FindFieldByName("single_nested_message"); + ASSERT_NE(nested_field_desc, nullptr); + cel::StructTypeField nested_field((cel::MessageTypeField(nested_field_desc))); + + cel::StructType nested_type( + (cel::MessageType(TestAllTypes::NestedMessage::descriptor()))); + const google::protobuf::FieldDescriptor* bb_field_desc = + TestAllTypes::NestedMessage::descriptor()->FindFieldByName("bb"); + ASSERT_NE(bb_field_desc, nullptr); + cel::StructTypeField bb_field((cel::MessageTypeField(bb_field_desc))); + + auto make_chained_expr = + [&](bool start_with_none) -> absl::StatusOr { + ExecutionPath path; + if (start_with_none) { + path.push_back(ExpressionStep::MakeConstant(cel::OptionalValue::None())); + } else { + path.push_back(ExpressionStep::MakeIdentifierStep(0)); + path.push_back(ExpressionStep::MakeGenericStep( + std::make_unique(), -1, /*stack_delta=*/0)); + } + CEL_ASSIGN_OR_RETURN( + auto step1, + CreateTypedSelectStep("single_nested_message", root_type, nested_field, + /*test_only=*/false, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/true)); + CEL_ASSIGN_OR_RETURN( + auto step2, + CreateTypedSelectStep("bb", nested_type, bb_field, /*test_only=*/false, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/true)); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step1))); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step2))); + path.push_back(ExpressionStep::MakeGenericStep( + std::make_unique(), -1, /*stack_delta=*/0)); + + cel::RuntimeOptions options; + if (GetParam()) { + options.unknown_processing = + cel::UnknownProcessingOptions::kAttributeOnly; + } + return CelExpressionFlatImpl( + env_, + FlatExpression(std::move(path), /*comprehension_slot_count=*/1, + env_->type_registry.GetComposedTypeProvider(), options, + /*arena=*/nullptr, IdentifierTable{{"message", 0}})); + }; + + // Optional none propagates as optional.none. + { + ASSERT_OK_AND_ASSIGN(CelExpressionFlatImpl none_expr, + make_chained_expr(true)); + Activation activation; + ASSERT_OK_AND_ASSIGN(CelValue result, + none_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsString()); + EXPECT_EQ(result.StringOrDie().value(), "optional.none"); + } + + ASSERT_OK_AND_ASSIGN(CelExpressionFlatImpl cel_expr, + make_chained_expr(false)); + // Unset intermediate field returns optional.none. + { + TestAllTypes message; + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsString()); + EXPECT_EQ(result.StringOrDie().value(), "optional.none"); + } + // Present intermediate message with unset leaf field returns optional.none. + { + TestAllTypes message; + message.mutable_single_nested_message(); + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsString()); + EXPECT_EQ(result.StringOrDie().value(), "optional.none"); + } + // Present intermediate and leaf field returns optional.of(value). + { + TestAllTypes message; + message.mutable_single_nested_message()->set_bb(123); + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsInt64()); + EXPECT_EQ(result.Int64OrDie(), 123); + } + // Fallback when optional holds a legacy map instead of a proto message. + { + std::string nested_key = "single_nested_message"; + std::string bb_key = "bb"; + std::vector> inner_kvs{ + {CelValue::CreateString(&bb_key), CelValue::CreateInt64(77)}}; + ASSERT_OK_AND_ASSIGN(auto inner_map, + CreateContainerBackedMap(absl::MakeSpan(inner_kvs))); + std::vector> outer_kvs{ + {CelValue::CreateString(&nested_key), + CelValue::CreateMap(inner_map.get())}}; + ASSERT_OK_AND_ASSIGN(auto outer_map, + CreateContainerBackedMap(absl::MakeSpan(outer_kvs))); + Activation activation; + activation.InsertValue("message", CelValue::CreateMap(outer_map.get())); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsInt64()); + EXPECT_EQ(result.Int64OrDie(), 77); + } +} + +TEST_F(SelectStepTest, TypedOptionalSelectStepUnknownPatternResolvesToUnknown) { + cel::StructType root_type((cel::MessageType(TestAllTypes::descriptor()))); + const google::protobuf::FieldDescriptor* field_desc = + TestAllTypes::descriptor()->FindFieldByName("single_int64"); + ASSERT_NE(field_desc, nullptr); + cel::StructTypeField resolved_field((cel::MessageTypeField(field_desc))); + + ASSERT_OK_AND_ASSIGN( + auto step1, + CreateTypedSelectStep("single_int64", root_type, resolved_field, + /*test_only=*/false, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/true)); + + ExecutionPath path; + path.push_back(ExpressionStep::MakeIdentifierStep(0)); + path.push_back(ExpressionStep::MakeGenericStep( + std::make_unique(), -1, /*stack_delta=*/0)); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step1))); + path.push_back(ExpressionStep::MakeGenericStep( + std::make_unique(), -1, /*stack_delta=*/0)); + + cel::RuntimeOptions options; + options.unknown_processing = cel::UnknownProcessingOptions::kAttributeOnly; + CelExpressionFlatImpl cel_expr( + env_, + FlatExpression(std::move(path), /*comprehension_slot_count=*/1, + env_->type_registry.GetComposedTypeProvider(), options, + /*arena=*/nullptr, IdentifierTable{{"message", 0}})); + + TestAllTypes message; + message.set_single_int64(42); + std::vector unknown_patterns; + unknown_patterns.push_back(CelAttributePattern( + "message", {CelAttributeQualifierPattern::OfString("single_int64")})); + + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_THAT(activation.SetUnknownAttributePatterns(unknown_patterns), IsOk()); + + ASSERT_OK_AND_ASSIGN(CelValue result, cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsUnknownSet()); +} + +TEST_P(SelectStepConformanceTest, TypedOptionalHasStepTest) { + cel::StructType root_type((cel::MessageType(TestAllTypes::descriptor()))); + const google::protobuf::FieldDescriptor* nested_field_desc = + TestAllTypes::descriptor()->FindFieldByName("single_nested_message"); + ASSERT_NE(nested_field_desc, nullptr); + cel::StructTypeField nested_field((cel::MessageTypeField(nested_field_desc))); + + cel::StructType nested_type( + (cel::MessageType(TestAllTypes::NestedMessage::descriptor()))); + const google::protobuf::FieldDescriptor* bb_field_desc = + TestAllTypes::NestedMessage::descriptor()->FindFieldByName("bb"); + ASSERT_NE(bb_field_desc, nullptr); + cel::StructTypeField bb_field((cel::MessageTypeField(bb_field_desc))); + + auto make_has_expr = + [&](bool start_with_none) -> absl::StatusOr { + ExecutionPath path; + if (start_with_none) { + path.push_back(ExpressionStep::MakeConstant(cel::OptionalValue::None())); + } else { + path.push_back(ExpressionStep::MakeIdentifierStep(0)); + path.push_back(ExpressionStep::MakeGenericStep( + std::make_unique(), -1, /*stack_delta=*/0)); + } + CEL_ASSIGN_OR_RETURN( + auto step1, + CreateTypedSelectStep("single_nested_message", root_type, nested_field, + /*test_only=*/false, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/true)); + CEL_ASSIGN_OR_RETURN( + auto step2, + CreateTypedSelectStep("bb", nested_type, bb_field, /*test_only=*/true, + /*enable_wrapper_type_null_unboxing=*/false, + /*enable_optional_types=*/true)); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step1))); + path.push_back(ExpressionStep::MakeGenericStep(std::move(step2))); + + cel::RuntimeOptions options; + if (GetParam()) { + options.unknown_processing = + cel::UnknownProcessingOptions::kAttributeOnly; + } + return CelExpressionFlatImpl( + env_, + FlatExpression(std::move(path), /*comprehension_slot_count=*/1, + env_->type_registry.GetComposedTypeProvider(), options, + /*arena=*/nullptr, IdentifierTable{{"message", 0}})); + }; + + { + ASSERT_OK_AND_ASSIGN(CelExpressionFlatImpl none_expr, make_has_expr(true)); + Activation activation; + ASSERT_OK_AND_ASSIGN(CelValue result, + none_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.BoolOrDie()); + } + + ASSERT_OK_AND_ASSIGN(CelExpressionFlatImpl cel_expr, make_has_expr(false)); + { + TestAllTypes message; + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsBool()); + EXPECT_FALSE(result.BoolOrDie()); + } + { + TestAllTypes message; + message.mutable_single_nested_message()->set_bb(10); + Activation activation; + activation.InsertValue("message", + CelProtoWrapper::CreateMessage(&message, &arena_)); + ASSERT_OK_AND_ASSIGN(CelValue result, + cel_expr.Evaluate(activation, &arena_)); + ASSERT_TRUE(result.IsBool()); + EXPECT_TRUE(result.BoolOrDie()); + } +} + INSTANTIATE_TEST_SUITE_P(UnknownsEnabled, SelectStepConformanceTest, testing::Bool()); diff --git a/eval/public/structs/BUILD b/eval/public/structs/BUILD index 1eb7d0772..eea2da31b 100644 --- a/eval/public/structs/BUILD +++ b/eval/public/structs/BUILD @@ -128,6 +128,7 @@ cc_library( ], deps = [ "//eval/public:cel_value", + "//internal:json", "//internal:overflow", "//internal:proto_time_encoding", "//internal:status_macros", diff --git a/eval/public/structs/cel_proto_wrap_value_to_message.cc b/eval/public/structs/cel_proto_wrap_value_to_message.cc index 0b04e93d2..fb36bea92 100644 --- a/eval/public/structs/cel_proto_wrap_value_to_message.cc +++ b/eval/public/structs/cel_proto_wrap_value_to_message.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "google/protobuf/any.pb.h" #include "google/protobuf/duration.pb.h" @@ -34,6 +35,7 @@ #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "eval/public/cel_value.h" +#include "internal/json.h" #include "internal/overflow.h" #include "internal/proto_time_encoding.h" #include "internal/status_macros.h" @@ -41,7 +43,6 @@ #include "internal/well_known_types.h" #include "google/protobuf/arena.h" #include "google/protobuf/descriptor.h" -#include "google/protobuf/json/json.h" #include "google/protobuf/message.h" namespace google::api::expr::runtime::internal { @@ -63,8 +64,6 @@ using google::protobuf::Arena; using google::protobuf::Descriptor; using google::protobuf::Message; using google::protobuf::MessageFactory; -using google::protobuf::json::MessageToJsonString; -using google::protobuf::json::PrintOptions; // kMaxIntJSON is defined as the Number.MAX_SAFE_INTEGER value per EcmaScript 6. constexpr int64_t kMaxIntJSON = (1ll << 53) - 1; @@ -94,23 +93,28 @@ static bool IsFieldMaskProto(const google::protobuf::Descriptor* descriptor) { static std::optional GetFieldMaskJsonString( const google::protobuf::Message& message) { - // TODO(b/540507668): Refactor to pipe descriptor_pool through - // ValueFromValue to use internal::MessageToJson. - PrintOptions json_options; - std::string json_str; - auto status = MessageToJsonString(message, &json_str, json_options); + Value json_value; + const google::protobuf::Descriptor* descriptor = message.GetDescriptor(); + const google::protobuf::Reflection* reflection = message.GetReflection(); + if (descriptor == nullptr || reflection == nullptr) { + ABSL_LOG(ERROR) << "Failed to convert FieldMask to JSON: missing " + "descriptor or reflection"; + return std::nullopt; + } + const google::protobuf::DescriptorPool* pool = descriptor->file()->pool(); + google::protobuf::MessageFactory* factory = reflection->GetMessageFactory(); + if (pool == nullptr || factory == nullptr) { + ABSL_LOG(ERROR) << "Failed to convert FieldMask to JSON: missing " + "descriptor pool or message factory"; + return std::nullopt; + } + absl::Status status = + cel::internal::MessageToJson(message, pool, factory, &json_value); if (!status.ok()) { ABSL_LOG(ERROR) << "Failed to convert FieldMask to JSON: " << status; return std::nullopt; } - // If JSON marshalling is correct, we know we'll always get a plain - // JSON string value and it shouldn't contain any escapes that we need - // to interpret. - if (json_str.size() >= 2 && json_str.front() == '"' && - json_str.back() == '"') { - return json_str.substr(1, json_str.size() - 2); - } - return json_str; + return std::move(*json_value.mutable_string_value()); } struct IgnoreErrorAndReturnNullptr { diff --git a/eval/public/structs/cel_proto_wrap_value_to_message_test.cc b/eval/public/structs/cel_proto_wrap_value_to_message_test.cc index 40b9398e2..26238231b 100644 --- a/eval/public/structs/cel_proto_wrap_value_to_message_test.cc +++ b/eval/public/structs/cel_proto_wrap_value_to_message_test.cc @@ -505,6 +505,38 @@ TEST_F(CelProtoWrapValueToMessageTest, WrapFieldMaskToValue) { ExpectWrappedMessage(value, expected_message); } +TEST_F(CelProtoWrapValueToMessageTest, WrapFieldMaskSnakeCaseToValue) { + FieldMask field_mask; + ASSERT_TRUE(TextFormat::ParseFromString( + R"pb(paths: "foo_bar.baz_qux" paths: "some_field")pb", &field_mask)); + CelValue value = MessageToCelValue(&field_mask); + + Value expected_message; + ASSERT_TRUE(TextFormat::ParseFromString( + R"pb(string_value: "fooBar.bazQux,someField")pb", &expected_message)); + + ExpectWrappedMessage(value, expected_message); +} + +TEST_F(CelProtoWrapValueToMessageTest, WrapEmptyFieldMaskToValue) { + FieldMask field_mask; + CelValue value = MessageToCelValue(&field_mask); + + Value expected_message; + expected_message.set_string_value(""); + + ExpectWrappedMessage(value, expected_message); +} + +TEST_F(CelProtoWrapValueToMessageTest, WrapInvalidFieldMaskToValue) { + FieldMask field_mask; + field_mask.add_paths("Foo"); + CelValue value = MessageToCelValue(&field_mask); + + Value expected_message; + ExpectNotWrapped(value, expected_message); +} + TEST_F(CelProtoWrapValueToMessageTest, WrapMapWithFieldMaskToAny) { const std::string kField = "field_mask"; FieldMask field_mask; diff --git a/runtime/standard/BUILD b/runtime/standard/BUILD index d08ac2116..f78c02f57 100644 --- a/runtime/standard/BUILD +++ b/runtime/standard/BUILD @@ -33,14 +33,20 @@ cc_library( ], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:number", "//internal:status_macros", + "//runtime:function", "//runtime:function_registry", "//runtime:runtime_options", "@com_google_absl//absl/status", - "@com_google_absl//absl/time", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings:string_view", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:span", ], ) @@ -69,18 +75,19 @@ cc_library( ], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:number", "//internal:status_macros", + "//runtime:function", "//runtime:function_registry", - "//runtime:register_function_helper", "//runtime:runtime_options", - "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", - "@com_google_protobuf//:protobuf", + "@com_google_absl//absl/types:span", ], ) @@ -108,22 +115,23 @@ cc_library( hdrs = ["equality_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", "//common:value_kind", "//internal:number", "//internal:status_macros", + "//runtime:function", "//runtime:function_registry", - "//runtime:register_function_helper", "//runtime:runtime_options", "//runtime/internal:errors", "@com_google_absl//absl/base:nullability", - "@com_google_absl//absl/functional:function_ref", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/time", "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:span", "@com_google_protobuf//:protobuf", ], ) @@ -156,16 +164,17 @@ cc_library( ], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", "//internal:status_macros", "//runtime:function", "//runtime:function_registry", - "//runtime:register_function_helper", "//runtime:runtime_options", "//runtime/internal:errors", "@com_google_absl//absl/status", - "@com_google_absl//absl/strings", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/types:span", ], ) @@ -203,14 +212,18 @@ cc_library( hdrs = ["container_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:status_macros", + "//runtime:function", "//runtime:function_registry", "//runtime:runtime_options", "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/types:span", "@com_google_protobuf//:protobuf", ], ) @@ -235,8 +248,10 @@ cc_library( hdrs = ["type_conversion_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:overflow", "//internal:status_macros", "//internal:time", @@ -247,8 +262,8 @@ cc_library( "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", - "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/time", + "@com_google_absl//absl/types:span", "@com_google_protobuf//:protobuf", ], ) @@ -273,15 +288,19 @@ cc_library( hdrs = ["arithmetic_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:overflow", "//internal:status_macros", "//runtime:function", "//runtime:function_registry", "//runtime:runtime_options", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", ], ) @@ -305,8 +324,10 @@ cc_library( hdrs = ["time_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:overflow", "//internal:status_macros", "//runtime:function", @@ -316,7 +337,7 @@ cc_library( "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", + "@com_google_absl//absl/types:span", ], ) @@ -340,16 +361,18 @@ cc_library( hdrs = ["string_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", + "//common:value_kind", "//internal:status_macros", + "//runtime:function", "//runtime:function_registry", "//runtime:runtime_options", - "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", - "@com_google_protobuf//:protobuf", + "@com_google_absl//absl/types:span", ], ) @@ -373,7 +396,8 @@ cc_library( hdrs = ["regex_functions.h"], deps = [ "//base:builtins", - "//base:function_adapter", + "//common:function_descriptor", + "//common:kind", "//common:value", "//internal:re2_options", "//internal:status_macros", @@ -381,7 +405,8 @@ cc_library( "//runtime:function_registry", "//runtime:runtime_options", "@com_google_absl//absl/status", - "@com_google_absl//absl/strings", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/types:span", "@com_googlesource_code_re2//:re2", ], ) diff --git a/runtime/standard/arithmetic_functions.cc b/runtime/standard/arithmetic_functions.cc index c77dd1a12..e44a31e3f 100644 --- a/runtime/standard/arithmetic_functions.cc +++ b/runtime/standard/arithmetic_functions.cc @@ -16,12 +16,17 @@ #include #include +#include #include "absl/status/status.h" +#include "absl/status/statusor.h" #include "absl/strings/string_view.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "internal/overflow.h" #include "internal/status_macros.h" #include "runtime/function.h" @@ -31,214 +36,174 @@ namespace cel { namespace { -// Template functions providing arithmetic operations -template -Value Add(Type v0, Type v1, const Function::InvokeContext& context); - -template <> -Value Add(int64_t v0, int64_t v1, - const Function::InvokeContext& context) { - auto sum = cel::internal::CheckedAdd(v0, v1); - if (!sum.ok()) { - return ErrorValue::From(sum.status(), context.arena()); - } - return IntValue(*sum); -} - -template <> -Value Add(uint64_t v0, uint64_t v1, - const Function::InvokeContext& context) { - auto sum = cel::internal::CheckedAdd(v0, v1); - if (!sum.ok()) { - return ErrorValue::From(sum.status(), context.arena()); - } - return UintValue(*sum); -} - -template <> -Value Add(double v0, double v1, const Function::InvokeContext&) { - return DoubleValue(v0 + v1); -} - -template -Value Sub(Type v0, Type v1, const Function::InvokeContext& context); - -template <> -Value Sub(int64_t v0, int64_t v1, - const Function::InvokeContext& context) { - auto diff = cel::internal::CheckedSub(v0, v1); - if (!diff.ok()) { - return ErrorValue::From(diff.status(), context.arena()); - } - return IntValue(*diff); -} - -template <> -Value Sub(uint64_t v0, uint64_t v1, - const Function::InvokeContext& context) { - auto diff = cel::internal::CheckedSub(v0, v1); - if (!diff.ok()) { - return ErrorValue::From(diff.status(), context.arena()); - } - return UintValue(*diff); -} - -template <> -Value Sub(double v0, double v1, const Function::InvokeContext&) { - return DoubleValue(v0 - v1); -} - -template -Value Mul(Type v0, Type v1, const Function::InvokeContext& context); - -template <> -Value Mul(int64_t v0, int64_t v1, - const Function::InvokeContext& context) { - auto prod = cel::internal::CheckedMul(v0, v1); - if (!prod.ok()) { - return ErrorValue::From(prod.status(), context.arena()); - } - return IntValue(*prod); -} - -template <> -Value Mul(uint64_t v0, uint64_t v1, - const Function::InvokeContext& context) { - auto prod = cel::internal::CheckedMul(v0, v1); - if (!prod.ok()) { - return ErrorValue::From(prod.status(), context.arena()); - } - return UintValue(*prod); -} - -template <> -Value Mul(double v0, double v1, const Function::InvokeContext&) { - return DoubleValue(v0 * v1); -} - -template -Value Div(Type v0, Type v1, const Function::InvokeContext& context); - -// Division operations for integer types should check for -// division by 0 -template <> -Value Div(int64_t v0, int64_t v1, - const Function::InvokeContext& context) { - auto quot = cel::internal::CheckedDiv(v0, v1); - if (!quot.ok()) { - return ErrorValue::From(quot.status(), context.arena()); +class ArithmeticBinaryFunction final : public Function { + public: + enum class Op { kAdd, kSubtract, kMultiply, kDivide, kModulo }; + + explicit ArithmeticBinaryFunction(Op op) : op_(op) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if (args[0].kind() != args[1].kind()) { + return absl::InvalidArgumentError( + "unexpected argument kinds for arithmetic function"); + } + switch (args[0].kind()) { + case ValueKind::kInt: { + int64_t lhs = args[0].GetInt().NativeValue(); + int64_t rhs = args[1].GetInt().NativeValue(); + absl::StatusOr result; + switch (op_) { + case Op::kAdd: + result = cel::internal::CheckedAdd(lhs, rhs); + break; + case Op::kSubtract: + result = cel::internal::CheckedSub(lhs, rhs); + break; + case Op::kMultiply: + result = cel::internal::CheckedMul(lhs, rhs); + break; + case Op::kDivide: + result = cel::internal::CheckedDiv(lhs, rhs); + break; + case Op::kModulo: + result = cel::internal::CheckedMod(lhs, rhs); + break; + } + if (!result.ok()) { + return ErrorValue::From(result.status(), context.arena()); + } + return IntValue(*result); + } + case ValueKind::kUint: { + uint64_t lhs = args[0].GetUint().NativeValue(); + uint64_t rhs = args[1].GetUint().NativeValue(); + absl::StatusOr result; + switch (op_) { + case Op::kAdd: + result = cel::internal::CheckedAdd(lhs, rhs); + break; + case Op::kSubtract: + result = cel::internal::CheckedSub(lhs, rhs); + break; + case Op::kMultiply: + result = cel::internal::CheckedMul(lhs, rhs); + break; + case Op::kDivide: + result = cel::internal::CheckedDiv(lhs, rhs); + break; + case Op::kModulo: + result = cel::internal::CheckedMod(lhs, rhs); + break; + } + if (!result.ok()) { + return ErrorValue::From(result.status(), context.arena()); + } + return UintValue(*result); + } + case ValueKind::kDouble: { + static_assert(std::numeric_limits::is_iec559, + "Division by zero for doubles must be supported"); + double lhs = args[0].GetDouble().NativeValue(); + double rhs = args[1].GetDouble().NativeValue(); + switch (op_) { + case Op::kAdd: + return DoubleValue(lhs + rhs); + case Op::kSubtract: + return DoubleValue(lhs - rhs); + case Op::kMultiply: + return DoubleValue(lhs * rhs); + case Op::kDivide: + // For double, division will result in +/- inf + return DoubleValue(lhs / rhs); + case Op::kModulo: + return absl::InvalidArgumentError( + "modulo is not defined for double"); + } + } + default: + return absl::InvalidArgumentError("expected numeric value"); + } } - return IntValue(*quot); -} -// Division operations for integer types should check for -// division by 0 -template <> -Value Div(uint64_t v0, uint64_t v1, - const Function::InvokeContext& context) { - auto quot = cel::internal::CheckedDiv(v0, v1); - if (!quot.ok()) { - return ErrorValue::From(quot.status(), context.arena()); + private: + Op op_; +}; + +class ArithmeticNegateFunction final : public Function { + public: + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for unary function"); + } + switch (args[0].kind()) { + case ValueKind::kInt: { + auto inv = + cel::internal::CheckedNegation(args[0].GetInt().NativeValue()); + if (!inv.ok()) { + return ErrorValue::From(inv.status(), context.arena()); + } + return IntValue(*inv); + } + case ValueKind::kDouble: + return DoubleValue(-args[0].GetDouble().NativeValue()); + default: + return absl::InvalidArgumentError("expected int or double value"); + } } - return UintValue(*quot); -} - -template <> -Value Div(double v0, double v1, const Function::InvokeContext&) { - static_assert(std::numeric_limits::is_iec559, - "Division by zero for doubles must be supported"); - - // For double, division will result in +/- inf - return DoubleValue(v0 / v1); -} - -// Modulo operation -template -Value Modulo(Type v0, Type v1, const Function::InvokeContext& context); - -// Modulo operations for integer types should check for -// division by 0 -template <> -Value Modulo(int64_t v0, int64_t v1, - const Function::InvokeContext& context) { - auto mod = cel::internal::CheckedMod(v0, v1); - if (!mod.ok()) { - return ErrorValue::From(mod.status(), context.arena()); - } - return IntValue(*mod); -} - -template <> -Value Modulo(uint64_t v0, uint64_t v1, - const Function::InvokeContext& context) { - auto mod = cel::internal::CheckedMod(v0, v1); - if (!mod.ok()) { - return ErrorValue::From(mod.status(), context.arena()); +}; + +absl::Status RegisterBinaryOverloads(FunctionRegistry& registry, + absl::string_view name, + ArithmeticBinaryFunction::Op op, + absl::Span kinds) { + for (Kind kind : kinds) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(name, /*is_receiver_style=*/false, {kind, kind}), + std::make_unique(op))); } - return UintValue(*mod); -} - -// Helper method -// Registers all arithmetic functions for template parameter type. -template -absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) { - using FunctionAdapter = cel::BinaryFunctionAdapter; - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false), - FunctionAdapter::WrapFunction(&Add))); - - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false), - FunctionAdapter::WrapFunction(&Sub))); - - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false), - FunctionAdapter::WrapFunction(&Mul))); - - return registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false), - FunctionAdapter::WrapFunction(&Div)); + return absl::OkStatus(); } } // namespace absl::Status RegisterArithmeticFunctions(FunctionRegistry& registry, const RuntimeOptions& options) { - CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType(registry)); - CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType(registry)); - CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType(registry)); - - // Modulo - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor( - cel::builtin::kModulo, false), - BinaryFunctionAdapter::WrapFunction( - &Modulo))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor( - cel::builtin::kModulo, false), - BinaryFunctionAdapter::WrapFunction( - &Modulo))); + static constexpr Kind kNumericKinds[] = {Kind::kInt, Kind::kUint, + Kind::kDouble}; + static constexpr Kind kIntegralKinds[] = {Kind::kInt, Kind::kUint}; + + CEL_RETURN_IF_ERROR(RegisterBinaryOverloads( + registry, cel::builtin::kAdd, ArithmeticBinaryFunction::Op::kAdd, + kNumericKinds)); + CEL_RETURN_IF_ERROR(RegisterBinaryOverloads( + registry, cel::builtin::kSubtract, + ArithmeticBinaryFunction::Op::kSubtract, kNumericKinds)); + CEL_RETURN_IF_ERROR(RegisterBinaryOverloads( + registry, cel::builtin::kMultiply, + ArithmeticBinaryFunction::Op::kMultiply, kNumericKinds)); + CEL_RETURN_IF_ERROR(RegisterBinaryOverloads( + registry, cel::builtin::kDivide, ArithmeticBinaryFunction::Op::kDivide, + kNumericKinds)); + CEL_RETURN_IF_ERROR(RegisterBinaryOverloads( + registry, cel::builtin::kModulo, ArithmeticBinaryFunction::Op::kModulo, + kIntegralKinds)); // Negation group CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor(cel::builtin::kNeg, - false), - UnaryFunctionAdapter::WrapFunction( - [](int64_t value, const Function::InvokeContext& context) -> Value { - auto inv = cel::internal::CheckedNegation(value); - if (!inv.ok()) { - return ErrorValue::From(inv.status(), context.arena()); - } - return IntValue(*inv); - }))); - + FunctionDescriptor(cel::builtin::kNeg, /*is_receiver_style=*/false, + {Kind::kInt}), + std::make_unique())); return registry.Register( - UnaryFunctionAdapter::CreateDescriptor(cel::builtin::kNeg, - false), - UnaryFunctionAdapter::WrapFunction( - [](double value) -> double { return -value; })); + FunctionDescriptor(cel::builtin::kNeg, /*is_receiver_style=*/false, + {Kind::kDouble}), + std::make_unique()); } } // namespace cel diff --git a/runtime/standard/comparison_functions.cc b/runtime/standard/comparison_functions.cc index bddd1efe9..fd0c4abb6 100644 --- a/runtime/standard/comparison_functions.cc +++ b/runtime/standard/comparison_functions.cc @@ -14,15 +14,22 @@ #include "runtime/standard/comparison_functions.h" -#include +#include +#include #include "absl/status/status.h" -#include "absl/time/time.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "internal/number.h" #include "internal/status_macros.h" +#include "runtime/function.h" #include "runtime/function_registry.h" #include "runtime/runtime_options.h" @@ -32,231 +39,148 @@ namespace { using ::cel::internal::Number; -// Comparison template functions -template -bool LessThan(Type t1, Type t2) { - return (t1 < t2); -} - -template -bool LessThanOrEqual(Type t1, Type t2) { - return (t1 <= t2); -} - -template -bool GreaterThan(Type t1, Type t2) { - return LessThan(t2, t1); -} - -template -bool GreaterThanOrEqual(Type t1, Type t2) { - return LessThanOrEqual(t2, t1); -} - -// String value comparions specializations -template <> -bool LessThan(const StringValue& t1, const StringValue& t2) { - return t1.Compare(t2) < 0; -} - -template <> -bool LessThanOrEqual(const StringValue& t1, const StringValue& t2) { - return t1.Compare(t2) <= 0; -} - -template <> -bool GreaterThan(const StringValue& t1, const StringValue& t2) { - return t1.Compare(t2) > 0; -} - -template <> -bool GreaterThanOrEqual(const StringValue& t1, const StringValue& t2) { - return t1.Compare(t2) >= 0; -} - -// bytes value comparions specializations -template <> -bool LessThan(const BytesValue& t1, const BytesValue& t2) { - return t1.Compare(t2) < 0; -} - -template <> -bool LessThanOrEqual(const BytesValue& t1, const BytesValue& t2) { - return t1.Compare(t2) <= 0; -} - -template <> -bool GreaterThan(const BytesValue& t1, const BytesValue& t2) { - return t1.Compare(t2) > 0; -} - -template <> -bool GreaterThanOrEqual(const BytesValue& t1, const BytesValue& t2) { - return t1.Compare(t2) >= 0; -} - -// Duration comparison specializations -template <> -bool LessThan(absl::Duration t1, absl::Duration t2) { - return absl::operator<(t1, t2); -} - -template <> -bool LessThanOrEqual(absl::Duration t1, absl::Duration t2) { - return absl::operator<=(t1, t2); -} - -template <> -bool GreaterThan(absl::Duration t1, absl::Duration t2) { - return absl::operator>(t1, t2); -} - -template <> -bool GreaterThanOrEqual(absl::Duration t1, absl::Duration t2) { - return absl::operator>=(t1, t2); -} - -// Timestamp comparison specializations -template <> -bool LessThan(absl::Time t1, absl::Time t2) { - return absl::operator<(t1, t2); -} - -template <> -bool LessThanOrEqual(absl::Time t1, absl::Time t2) { - return absl::operator<=(t1, t2); -} - -template <> -bool GreaterThan(absl::Time t1, absl::Time t2) { - return absl::operator>(t1, t2); -} - -template <> -bool GreaterThanOrEqual(absl::Time t1, absl::Time t2) { - return absl::operator>=(t1, t2); -} - -template -bool CrossNumericLessThan(T t, U u) { - return Number(t) < Number(u); -} - -template -bool CrossNumericGreaterThan(T t, U u) { - return Number(t) > Number(u); -} - -template -bool CrossNumericLessOrEqualTo(T t, U u) { - return Number(t) <= Number(u); -} - -template -bool CrossNumericGreaterOrEqualTo(T t, U u) { - return Number(t) >= Number(u); +absl::optional NumberFromValue(const Value& value) { + switch (value.kind()) { + case ValueKind::kInt: + return Number::FromInt64(value.GetInt().NativeValue()); + case ValueKind::kUint: + return Number::FromUint64(value.GetUint().NativeValue()); + case ValueKind::kDouble: + return Number::FromDouble(value.GetDouble().NativeValue()); + default: + return absl::nullopt; + } } -template -absl::Status RegisterComparisonFunctionsForType( - cel::FunctionRegistry& registry) { - using FunctionAdapter = BinaryFunctionAdapter; - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kLess, false), - FunctionAdapter::WrapFunction(LessThan))); - - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kLessOrEqual, false), - FunctionAdapter::WrapFunction(LessThanOrEqual))); +class ComparisonFunction final : public Function { + public: + enum class Op { kLess, kLessOrEqual, kGreater, kGreaterOrEqual }; + + explicit ComparisonFunction(Op op) : op_(op) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext&) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if (args[0].kind() == args[1].kind()) { + switch (args[0].kind()) { + case ValueKind::kBool: + return BoolValue(CompareOrdered(args[0].GetBool().NativeValue(), + args[1].GetBool().NativeValue())); + case ValueKind::kInt: + return BoolValue(CompareOrdered(args[0].GetInt().NativeValue(), + args[1].GetInt().NativeValue())); + case ValueKind::kUint: + return BoolValue(CompareOrdered(args[0].GetUint().NativeValue(), + args[1].GetUint().NativeValue())); + case ValueKind::kDouble: + return BoolValue(CompareOrdered(args[0].GetDouble().NativeValue(), + args[1].GetDouble().NativeValue())); + case ValueKind::kString: { + int cmp = args[0].GetString().Compare(args[1].GetString()); + return BoolValue(CompareThreeWay(cmp)); + } + case ValueKind::kBytes: { + int cmp = args[0].GetBytes().Compare(args[1].GetBytes()); + return BoolValue(CompareThreeWay(cmp)); + } + case ValueKind::kDuration: + return BoolValue(CompareOrdered(args[0].GetDuration().ToDuration(), + args[1].GetDuration().ToDuration())); + case ValueKind::kTimestamp: + return BoolValue(CompareOrdered(args[0].GetTimestamp().ToTime(), + args[1].GetTimestamp().ToTime())); + default: + return absl::InvalidArgumentError("expected comparable value"); + } + } + + absl::optional lhs = NumberFromValue(args[0]); + absl::optional rhs = NumberFromValue(args[1]); + if (!lhs.has_value() || !rhs.has_value()) { + return absl::InvalidArgumentError("expected numeric value"); + } + return BoolValue(CompareOrdered(*lhs, *rhs)); + } - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kGreater, false), - FunctionAdapter::WrapFunction(GreaterThan))); + private: + template + bool CompareOrdered(const T& lhs, const T& rhs) const { + switch (op_) { + case Op::kLess: + return lhs < rhs; + case Op::kLessOrEqual: + return lhs <= rhs; + case Op::kGreater: + return lhs > rhs; + case Op::kGreaterOrEqual: + return lhs >= rhs; + } + } - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kGreaterOrEqual, false), - FunctionAdapter::WrapFunction(GreaterThanOrEqual))); + bool CompareThreeWay(int cmp) const { + switch (op_) { + case Op::kLess: + return cmp < 0; + case Op::kLessOrEqual: + return cmp <= 0; + case Op::kGreater: + return cmp > 0; + case Op::kGreaterOrEqual: + return cmp >= 0; + } + } + Op op_; +}; + +absl::Status RegisterComparisonOverloads(FunctionRegistry& registry, Kind lhs, + Kind rhs) { + static constexpr struct { + absl::string_view name; + ComparisonFunction::Op op; + } kOps[] = { + {cel::builtin::kLess, ComparisonFunction::Op::kLess}, + {cel::builtin::kLessOrEqual, ComparisonFunction::Op::kLessOrEqual}, + {cel::builtin::kGreater, ComparisonFunction::Op::kGreater}, + {cel::builtin::kGreaterOrEqual, ComparisonFunction::Op::kGreaterOrEqual}, + }; + for (const auto& entry : kOps) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(entry.name, /*is_receiver_style=*/false, {lhs, rhs}), + std::make_unique(entry.op))); + } return absl::OkStatus(); } absl::Status RegisterHomogenousComparisonFunctions( cel::FunctionRegistry& registry) { - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterComparisonFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - - return absl::OkStatus(); -} - -template -absl::Status RegisterCrossNumericComparisons(cel::FunctionRegistry& registry) { - using FunctionAdapter = BinaryFunctionAdapter; - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kLess, - /*receiver_style=*/false), - FunctionAdapter::WrapFunction(&CrossNumericLessThan))); - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kGreater, - /*receiver_style=*/false), - FunctionAdapter::WrapFunction(&CrossNumericGreaterThan))); - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kGreaterOrEqual, - /*receiver_style=*/false), - FunctionAdapter::WrapFunction(&CrossNumericGreaterOrEqualTo))); - CEL_RETURN_IF_ERROR(registry.Register( - FunctionAdapter::CreateDescriptor(cel::builtin::kLessOrEqual, - /*receiver_style=*/false), - FunctionAdapter::WrapFunction(&CrossNumericLessOrEqualTo))); + static constexpr Kind kOrderableKinds[] = { + Kind::kBool, Kind::kInt, Kind::kUint, Kind::kDouble, + Kind::kString, Kind::kBytes, Kind::kDuration, Kind::kTimestamp, + }; + for (Kind kind : kOrderableKinds) { + CEL_RETURN_IF_ERROR(RegisterComparisonOverloads(registry, kind, kind)); + } return absl::OkStatus(); } absl::Status RegisterHeterogeneousComparisonFunctions( cel::FunctionRegistry& registry) { - CEL_RETURN_IF_ERROR( - (RegisterCrossNumericComparisons(registry))); - CEL_RETURN_IF_ERROR( - (RegisterCrossNumericComparisons(registry))); - - CEL_RETURN_IF_ERROR( - (RegisterCrossNumericComparisons(registry))); - CEL_RETURN_IF_ERROR( - (RegisterCrossNumericComparisons(registry))); - - CEL_RETURN_IF_ERROR( - (RegisterCrossNumericComparisons(registry))); - CEL_RETURN_IF_ERROR( - (RegisterCrossNumericComparisons(registry))); - - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR( - RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR( - RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR( - RegisterComparisonFunctionsForType(registry)); - CEL_RETURN_IF_ERROR(RegisterComparisonFunctionsForType(registry)); - - return absl::OkStatus(); + static constexpr std::pair kCrossNumericKinds[] = { + {Kind::kDouble, Kind::kInt}, {Kind::kDouble, Kind::kUint}, + {Kind::kUint, Kind::kDouble}, {Kind::kUint, Kind::kInt}, + {Kind::kInt, Kind::kDouble}, {Kind::kInt, Kind::kUint}, + }; + for (const auto& pair : kCrossNumericKinds) { + CEL_RETURN_IF_ERROR( + RegisterComparisonOverloads(registry, pair.first, pair.second)); + } + return RegisterHomogenousComparisonFunctions(registry); } + } // namespace absl::Status RegisterComparisonFunctions(FunctionRegistry& registry, diff --git a/runtime/standard/container_functions.cc b/runtime/standard/container_functions.cc index c81dc7596..85a8073c2 100644 --- a/runtime/standard/container_functions.cc +++ b/runtime/standard/container_functions.cc @@ -16,16 +16,21 @@ #include #include +#include #include #include "absl/base/nullability.h" #include "absl/status/status.h" #include "absl/status/statusor.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "common/values/list_value_builder.h" #include "internal/status_macros.h" +#include "runtime/function.h" #include "runtime/function_registry.h" #include "runtime/runtime_options.h" #include "google/protobuf/arena.h" @@ -35,14 +40,6 @@ namespace cel { namespace { -absl::StatusOr MapSizeImpl(const MapValue& value) { - return value.Size(); -} - -absl::StatusOr ListSizeImpl(const ListValue& value) { - return value.Size(); -} - // Concatenation for CelList type. absl::StatusOr ConcatList( const ListValue& value1, const ListValue& value2, @@ -95,6 +92,64 @@ absl::StatusOr AppendList(ListValue value1, const Value& value2) { } return absl::InvalidArgumentError("Unexpected call to runtime list append."); } + +class ContainerFunction final : public Function { + public: + enum class Op { kSize, kConcatList, kAppendList }; + + explicit ContainerFunction(Op op) : op_(op) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + switch (op_) { + case Op::kSize: { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for unary function"); + } + absl::StatusOr size; + switch (args[0].kind()) { + case ValueKind::kList: + size = args[0].GetList().Size(); + break; + case ValueKind::kMap: + size = args[0].GetMap().Size(); + break; + default: + return absl::InvalidArgumentError("expected list or map value"); + } + if (!size.ok()) { + return std::move(size).status(); + } + return IntValue(*size); + } + case Op::kConcatList: + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if (!args[0].IsList() || !args[1].IsList()) { + return absl::InvalidArgumentError("expected list value"); + } + return ConcatList(args[0].GetList(), args[1].GetList(), + context.descriptor_pool(), context.message_factory(), + context.arena()); + case Op::kAppendList: + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if (!args[0].IsList()) { + return absl::InvalidArgumentError("expected list value"); + } + return AppendList(args[0].GetList(), args[1]); + } + } + + private: + Op op_; +}; + } // namespace absl::Status RegisterContainerFunctions(FunctionRegistry& registry, @@ -102,35 +157,26 @@ absl::Status RegisterContainerFunctions(FunctionRegistry& registry, // receiver style = true/false // Support both the global and receiver style size() for lists and maps. for (bool receiver_style : {true, false}) { - CEL_RETURN_IF_ERROR(registry.Register( - cel::UnaryFunctionAdapter, const ListValue&>:: - CreateDescriptor(cel::builtin::kSize, receiver_style), - UnaryFunctionAdapter, - const ListValue&>::WrapFunction(ListSizeImpl))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter, const MapValue&>:: - CreateDescriptor(cel::builtin::kSize, receiver_style), - UnaryFunctionAdapter, - const MapValue&>::WrapFunction(MapSizeImpl))); + for (Kind kind : {Kind::kList, Kind::kMap}) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(cel::builtin::kSize, receiver_style, {kind}), + std::make_unique(ContainerFunction::Op::kSize))); + } } if (options.enable_list_concat) { CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter< - absl::StatusOr, const ListValue&, - const ListValue&>::CreateDescriptor(cel::builtin::kAdd, false), - BinaryFunctionAdapter, const ListValue&, - const ListValue&>::WrapFunction(ConcatList))); + FunctionDescriptor(cel::builtin::kAdd, /*is_receiver_style=*/false, + {Kind::kList, Kind::kList}), + std::make_unique( + ContainerFunction::Op::kConcatList))); } return registry.Register( - BinaryFunctionAdapter< - absl::StatusOr, ListValue, - const Value&>::CreateDescriptor(cel::builtin::kRuntimeListAppend, - false), - BinaryFunctionAdapter, ListValue, - const Value&>::WrapFunction(AppendList)); + FunctionDescriptor(cel::builtin::kRuntimeListAppend, + /*is_receiver_style=*/false, + {Kind::kList, Kind::kAny}), + std::make_unique(ContainerFunction::Op::kAppendList)); } } // namespace cel diff --git a/runtime/standard/container_membership_functions.cc b/runtime/standard/container_membership_functions.cc index 26c115103..eda86f154 100644 --- a/runtime/standard/container_membership_functions.cc +++ b/runtime/standard/container_membership_functions.cc @@ -16,22 +16,22 @@ #include #include +#include -#include "absl/base/nullability.h" #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "internal/number.h" #include "internal/status_macros.h" +#include "runtime/function.h" #include "runtime/function_registry.h" -#include "runtime/register_function_helper.h" #include "runtime/runtime_options.h" -#include "google/protobuf/arena.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" namespace cel { namespace { @@ -44,274 +44,240 @@ static constexpr std::array in_operators = { cel::builtin::kInDeprecated, // deprecated _in_ -- for backwards compat }; -template -bool ValueEquals(const Value& value, T other); - -template <> -bool ValueEquals(const Value& value, bool other) { - if (auto bool_value = value.As(); bool_value) { - return bool_value->NativeValue() == other; - } - return false; -} - -template <> -bool ValueEquals(const Value& value, int64_t other) { - if (auto int_value = value.As(); int_value) { - return int_value->NativeValue() == other; +bool HomogenousValueEquals(const Value& lhs, const Value& rhs) { + if (lhs.kind() != rhs.kind()) { + return false; } - return false; -} - -template <> -bool ValueEquals(const Value& value, uint64_t other) { - if (auto uint_value = value.As(); uint_value) { - return uint_value->NativeValue() == other; + switch (lhs.kind()) { + case ValueKind::kBool: + return lhs.GetBool().NativeValue() == rhs.GetBool().NativeValue(); + case ValueKind::kInt: + return lhs.GetInt().NativeValue() == rhs.GetInt().NativeValue(); + case ValueKind::kUint: + return lhs.GetUint().NativeValue() == rhs.GetUint().NativeValue(); + case ValueKind::kDouble: + return lhs.GetDouble().NativeValue() == rhs.GetDouble().NativeValue(); + case ValueKind::kString: + return lhs.GetString().Equals(rhs.GetString()); + case ValueKind::kBytes: + return lhs.GetBytes().Equals(rhs.GetBytes()); + default: + return false; } - return false; } -template <> -bool ValueEquals(const Value& value, double other) { - if (auto double_value = value.As(); double_value) { - return double_value->NativeValue() == other; +absl::StatusOr EvaluateHomogenousListIn( + const Value& key, const ListValue& list, + const Function::InvokeContext& context) { + switch (key.kind()) { + case ValueKind::kBool: + case ValueKind::kInt: + case ValueKind::kUint: + case ValueKind::kDouble: + case ValueKind::kString: + case ValueKind::kBytes: + break; + default: + return absl::InvalidArgumentError("unsupported key kind for list in"); } - return false; -} - -template <> -bool ValueEquals(const Value& value, const StringValue& other) { - if (auto string_value = value.As(); string_value) { - return string_value->Equals(other); - } - return false; -} - -template <> -bool ValueEquals(const Value& value, const BytesValue& other) { - if (auto bytes_value = value.As(); bytes_value) { - return bytes_value->Equals(other); - } - return false; -} - -// Template function implementing CEL in() function -template -absl::StatusOr In( - T value, const ListValue& list, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) { CEL_ASSIGN_OR_RETURN(auto size, list.Size()); Value element; for (int i = 0; i < size; i++) { - CEL_RETURN_IF_ERROR( - list.Get(i, descriptor_pool, message_factory, arena, &element)); - if (ValueEquals(element, value)) { - return true; + CEL_RETURN_IF_ERROR(list.Get(i, context.descriptor_pool(), + context.message_factory(), context.arena(), + &element)); + if (HomogenousValueEquals(element, key)) { + return BoolValue(true); } } - - return false; + return BoolValue(false); } -// Implementation for @in operator using heterogeneous equality. -absl::StatusOr HeterogeneousEqualityIn( - const Value& value, const ListValue& list, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) { - return list.Contains(value, descriptor_pool, message_factory, arena); -} - -absl::Status RegisterListMembershipFunctions(FunctionRegistry& registry, - const RuntimeOptions& options) { - for (absl::string_view op : in_operators) { - if (options.enable_heterogeneous_equality) { - CEL_RETURN_IF_ERROR( - (RegisterHelper, const Value&, const ListValue&>>:: - RegisterGlobalOverload(op, &HeterogeneousEqualityIn, registry))); - } else { - CEL_RETURN_IF_ERROR( - (RegisterHelper, bool, - const ListValue&>>:: - RegisterGlobalOverload(op, In, registry))); - CEL_RETURN_IF_ERROR( - (RegisterHelper, int64_t, - const ListValue&>>:: - RegisterGlobalOverload(op, In, registry))); - CEL_RETURN_IF_ERROR( - (RegisterHelper, uint64_t, - const ListValue&>>:: - RegisterGlobalOverload(op, In, registry))); - CEL_RETURN_IF_ERROR( - (RegisterHelper, double, - const ListValue&>>:: - RegisterGlobalOverload(op, In, registry))); - CEL_RETURN_IF_ERROR( - (RegisterHelper, const StringValue&, const ListValue&>>:: - RegisterGlobalOverload(op, In, registry))); - CEL_RETURN_IF_ERROR( - (RegisterHelper, const BytesValue&, const ListValue&>>:: - RegisterGlobalOverload(op, In, registry))); +absl::StatusOr EvaluateMapIn(const Value& key, const MapValue& map_value, + bool enable_heterogeneous_equality, + const Function::InvokeContext& context) { + switch (key.kind()) { + case ValueKind::kString: { + Value result; + CEL_RETURN_IF_ERROR(map_value.Has(key, context.descriptor_pool(), + context.message_factory(), + context.arena(), &result)); + if (result.IsBool()) { + return result; + } + if (enable_heterogeneous_equality) { + return BoolValue(false); + } + return result; } - } - return absl::OkStatus(); -} - -absl::Status RegisterMapMembershipFunctions(FunctionRegistry& registry, - const RuntimeOptions& options) { - const bool enable_heterogeneous_equality = - options.enable_heterogeneous_equality; - - auto boolKeyInSet = - [enable_heterogeneous_equality]( - bool key, const MapValue& map_value, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - Value has; - CEL_RETURN_IF_ERROR(map_value.Has(BoolValue(key), descriptor_pool, - message_factory, arena, &has)); - if (has.IsTrue()) { + case ValueKind::kBool: { + Value has; + CEL_RETURN_IF_ERROR(map_value.Has(key, context.descriptor_pool(), + context.message_factory(), + context.arena(), &has)); + if (has.IsTrue()) { + return has; + } + if (enable_heterogeneous_equality) { + return BoolValue(false); + } return has; } - if (enable_heterogeneous_equality) { - return BoolValue(false); - } - return has; - }; - - auto intKeyInSet = - [enable_heterogeneous_equality]( - int64_t key, const MapValue& map_value, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - Value result; - CEL_RETURN_IF_ERROR(map_value.Has(IntValue(key), descriptor_pool, - message_factory, arena, &result)); - if (enable_heterogeneous_equality) { - if (result.IsTrue()) { - return result; - } - Number number = Number::FromInt64(key); - if (number.LosslessConvertibleToUint()) { - Value result_alt; - CEL_RETURN_IF_ERROR(map_value.Has(UintValue(number.AsUint()), - descriptor_pool, message_factory, - arena, &result_alt)); - if (result_alt.IsTrue()) { - return result_alt; + case ValueKind::kInt: { + Value result; + CEL_RETURN_IF_ERROR(map_value.Has(key, context.descriptor_pool(), + context.message_factory(), + context.arena(), &result)); + if (enable_heterogeneous_equality) { + if (result.IsTrue()) { + return result; } + Number number = Number::FromInt64(key.GetInt().NativeValue()); + if (number.LosslessConvertibleToUint()) { + Value result_alt; + CEL_RETURN_IF_ERROR(map_value.Has( + UintValue(number.AsUint()), context.descriptor_pool(), + context.message_factory(), context.arena(), &result_alt)); + if (result_alt.IsTrue()) { + return result_alt; + } + } + return BoolValue(false); } - return BoolValue(false); - } - return result; - }; - - auto stringKeyInSet = - [enable_heterogeneous_equality]( - const StringValue& key, const MapValue& map_value, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - Value result; - CEL_RETURN_IF_ERROR( - map_value.Has(key, descriptor_pool, message_factory, arena, &result)); - if (result.IsBool()) { return result; } - if (enable_heterogeneous_equality) { - return BoolValue(false); + case ValueKind::kUint: { + Value has; + CEL_RETURN_IF_ERROR(map_value.Has(key, context.descriptor_pool(), + context.message_factory(), + context.arena(), &has)); + if (enable_heterogeneous_equality) { + if (has.IsTrue()) { + return has; + } + Number number = Number::FromUint64(key.GetUint().NativeValue()); + if (number.LosslessConvertibleToInt()) { + Value has_alt; + CEL_RETURN_IF_ERROR(map_value.Has( + IntValue(number.AsInt()), context.descriptor_pool(), + context.message_factory(), context.arena(), &has_alt)); + if (has_alt.IsTrue()) { + return has_alt; + } + } + return BoolValue(false); + } + return has; } - return result; - }; - - auto uintKeyInSet = - [enable_heterogeneous_equality]( - uint64_t key, const MapValue& map_value, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - Value has; - CEL_RETURN_IF_ERROR(map_value.Has(UintValue(key), descriptor_pool, - message_factory, arena, &has)); - if (enable_heterogeneous_equality) { - if (has.IsTrue()) { - return has; + case ValueKind::kDouble: { + if (!enable_heterogeneous_equality) { + return absl::InvalidArgumentError( + "double key not supported for homogenous map in"); } - Value has_alt; - Number number = Number::FromUint64(key); + Number number = Number::FromDouble(key.GetDouble().NativeValue()); if (number.LosslessConvertibleToInt()) { - CEL_RETURN_IF_ERROR(map_value.Has(IntValue(number.AsInt()), - descriptor_pool, message_factory, - arena, &has_alt)); + Value has; + CEL_RETURN_IF_ERROR( + map_value.Has(IntValue(number.AsInt()), context.descriptor_pool(), + context.message_factory(), context.arena(), &has)); + if (has.IsTrue()) { + return has; + } + } + if (number.LosslessConvertibleToUint()) { + Value has; + CEL_RETURN_IF_ERROR( + map_value.Has(UintValue(number.AsUint()), context.descriptor_pool(), + context.message_factory(), context.arena(), &has)); if (has.IsTrue()) { return has; } } return BoolValue(false); } - return has; - }; + default: + return absl::InvalidArgumentError("unsupported key kind for map in"); + } +} - auto doubleKeyInSet = - [](double key, const MapValue& map_value, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - Number number = Number::FromDouble(key); - if (number.LosslessConvertibleToInt()) { - Value has; - CEL_RETURN_IF_ERROR(map_value.Has(IntValue(number.AsInt()), - descriptor_pool, message_factory, arena, - &has)); - if (has.IsTrue()) { - return has; - } +class ContainerMembershipFunction final : public Function { + public: + explicit ContainerMembershipFunction(bool enable_heterogeneous_equality) + : enable_heterogeneous_equality_(enable_heterogeneous_equality) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); } - if (number.LosslessConvertibleToUint()) { - Value has; - CEL_RETURN_IF_ERROR(map_value.Has(UintValue(number.AsUint()), - descriptor_pool, message_factory, arena, - &has)); - if (has.IsTrue()) { - return has; - } + switch (args[1].kind()) { + case ValueKind::kList: + if (enable_heterogeneous_equality_) { + return args[1].GetList().Contains(args[0], context.descriptor_pool(), + context.message_factory(), + context.arena()); + } + return EvaluateHomogenousListIn(args[0], args[1].GetList(), context); + case ValueKind::kMap: + return EvaluateMapIn(args[0], args[1].GetMap(), + enable_heterogeneous_equality_, context); + default: + return absl::InvalidArgumentError("expected list or map value"); } - return BoolValue(false); - }; - - for (auto op : in_operators) { - auto status = RegisterHelper, const StringValue&, - const MapValue&>>::RegisterGlobalOverload(op, stringKeyInSet, registry); - if (!status.ok()) return status; - - status = RegisterHelper< - BinaryFunctionAdapter, bool, const MapValue&>>:: - RegisterGlobalOverload(op, boolKeyInSet, registry); - if (!status.ok()) return status; + } - status = RegisterHelper, - int64_t, const MapValue&>>:: - RegisterGlobalOverload(op, intKeyInSet, registry); - if (!status.ok()) return status; + private: + bool enable_heterogeneous_equality_; +}; - status = RegisterHelper, - uint64_t, const MapValue&>>:: - RegisterGlobalOverload(op, uintKeyInSet, registry); - if (!status.ok()) return status; +absl::Status RegisterListMembershipFunctions(FunctionRegistry& registry, + const RuntimeOptions& options) { + static constexpr Kind kHomogenousListKeyKinds[] = { + Kind::kBool, Kind::kInt, Kind::kUint, + Kind::kDouble, Kind::kString, Kind::kBytes, + }; + for (absl::string_view op : in_operators) { + if (options.enable_heterogeneous_equality) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(op, /*is_receiver_style=*/false, + {Kind::kAny, Kind::kList}), + std::make_unique(true))); + } else { + for (Kind kind : kHomogenousListKeyKinds) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(op, /*is_receiver_style=*/false, + {kind, Kind::kList}), + std::make_unique(false))); + } + } + } + return absl::OkStatus(); +} +absl::Status RegisterMapMembershipFunctions(FunctionRegistry& registry, + const RuntimeOptions& options) { + const bool enable_heterogeneous_equality = + options.enable_heterogeneous_equality; + static constexpr Kind kMapKeyKinds[] = { + Kind::kString, + Kind::kBool, + Kind::kInt, + Kind::kUint, + }; + for (absl::string_view op : in_operators) { + for (Kind kind : kMapKeyKinds) { + CEL_RETURN_IF_ERROR( + registry.Register(FunctionDescriptor(op, /*is_receiver_style=*/false, + {kind, Kind::kMap}), + std::make_unique( + enable_heterogeneous_equality))); + } if (enable_heterogeneous_equality) { - status = RegisterHelper, - double, const MapValue&>>:: - RegisterGlobalOverload(op, doubleKeyInSet, registry); - if (!status.ok()) return status; + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(op, /*is_receiver_style=*/false, + {Kind::kDouble, Kind::kMap}), + std::make_unique(true))); } } return absl::OkStatus(); diff --git a/runtime/standard/equality_functions.cc b/runtime/standard/equality_functions.cc index 03e29223f..0a9dc72b9 100644 --- a/runtime/standard/equality_functions.cc +++ b/runtime/standard/equality_functions.cc @@ -15,27 +15,26 @@ #include "runtime/standard/equality_functions.h" #include -#include -#include -#include +#include #include #include "absl/base/nullability.h" -#include "absl/functional/function_ref.h" #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "absl/types/optional.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" #include "common/value_kind.h" #include "internal/number.h" #include "internal/status_macros.h" +#include "runtime/function.h" #include "runtime/function_registry.h" #include "runtime/internal/errors.h" -#include "runtime/register_function_helper.h" #include "runtime/runtime_options.h" #include "google/protobuf/arena.h" #include "google/protobuf/descriptor.h" @@ -72,57 +71,6 @@ struct HeterogeneousEqualProvider { google::protobuf::Arena* absl_nonnull arena) const; }; -// Comparison template functions -template -absl::optional Inequal(Type lhs, Type rhs) { - return lhs != rhs; -} - -template <> -absl::optional Inequal(const StringValue& lhs, const StringValue& rhs) { - return !lhs.Equals(rhs); -} - -template <> -absl::optional Inequal(const BytesValue& lhs, const BytesValue& rhs) { - return !lhs.Equals(rhs); -} - -template <> -absl::optional Inequal(const NullValue&, const NullValue&) { - return false; -} - -template <> -absl::optional Inequal(const TypeValue& lhs, const TypeValue& rhs) { - return lhs.name() != rhs.name(); -} - -template -absl::optional Equal(Type lhs, Type rhs) { - return lhs == rhs; -} - -template <> -absl::optional Equal(const StringValue& lhs, const StringValue& rhs) { - return lhs.Equals(rhs); -} - -template <> -absl::optional Equal(const BytesValue& lhs, const BytesValue& rhs) { - return lhs.Equals(rhs); -} - -template <> -absl::optional Equal(const NullValue&, const NullValue&) { - return true; -} - -template <> -absl::optional Equal(const TypeValue& lhs, const TypeValue& rhs) { - return lhs.name() == rhs.name(); -} - // Equality for lists. Template parameter provides either heterogeneous or // homogenous equality for comparing members. template @@ -181,7 +129,7 @@ absl::optional NumberFromValue(const Value& value) { return Number::FromDouble(value.GetDouble().NativeValue()); } - return std::nullopt; + return absl::nullopt; } absl::StatusOr> CheckAlternativeNumericType( @@ -192,7 +140,7 @@ absl::StatusOr> CheckAlternativeNumericType( absl::optional number = NumberFromValue(key); if (!number.has_value()) { - return std::nullopt; + return absl::nullopt; } if (!key.IsInt() && number->LosslessConvertibleToInt()) { @@ -215,7 +163,7 @@ absl::StatusOr> CheckAlternativeNumericType( } } - return std::nullopt; + return absl::nullopt; } // Equality for maps. Template parameter provides either heterogeneous or @@ -266,174 +214,6 @@ absl::StatusOr> MapEqual( return true; } -// Helper for wrapping ==/!= implementations. -// Name should point to a static constexpr string so the lambda capture is safe. -template -std::function -WrapComparison(Op op, absl::string_view name) { - return [op = std::move(op), name]( - Type lhs, Type rhs, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> Value { - absl::optional result = op(lhs, rhs); - - if (result.has_value()) { - return BoolValue(*result); - } - - return ErrorValue::From( - cel::runtime_internal::CreateNoMatchingOverloadError(name), arena); - }; -} - -// Helper method -// -// Registers all equality functions for template parameters type. -template -absl::Status RegisterEqualityFunctionsForType(cel::FunctionRegistry& registry) { - using FunctionAdapter = - cel::RegisterHelper>; - // Inequality - CEL_RETURN_IF_ERROR(FunctionAdapter::RegisterGlobalOverload( - kInequal, WrapComparison(&Inequal, kInequal), registry)); - - // Equality - CEL_RETURN_IF_ERROR(FunctionAdapter::RegisterGlobalOverload( - kEqual, WrapComparison(&Equal, kEqual), registry)); - - return absl::OkStatus(); -} - -template -auto ComplexEquality(Op&& op) { - return [op = std::forward(op)]( - const Type& t1, const Type& t2, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - CEL_ASSIGN_OR_RETURN(absl::optional result, - op(t1, t2, descriptor_pool, message_factory, arena)); - if (!result.has_value()) { - return ErrorValue::From( - cel::runtime_internal::CreateNoMatchingOverloadError(kEqual), arena); - } - return BoolValue(*result); - }; -} - -template -auto ComplexInequality(Op&& op) { - return [op = std::forward(op)]( - Type t1, Type t2, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) -> absl::StatusOr { - CEL_ASSIGN_OR_RETURN(absl::optional result, - op(t1, t2, descriptor_pool, message_factory, arena)); - if (!result.has_value()) { - return ErrorValue::From( - cel::runtime_internal::CreateNoMatchingOverloadError(kInequal), - arena); - } - return BoolValue(!*result); - }; -} - -template -absl::Status RegisterComplexEqualityFunctionsForType( - absl::FunctionRef>( - Type, Type, const google::protobuf::DescriptorPool* absl_nonnull, - google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull)> - op, - cel::FunctionRegistry& registry) { - using FunctionAdapter = cel::RegisterHelper< - BinaryFunctionAdapter, Type, Type>>; - // Inequality - CEL_RETURN_IF_ERROR(FunctionAdapter::RegisterGlobalOverload( - kInequal, ComplexInequality(op), registry)); - - // Equality - CEL_RETURN_IF_ERROR(FunctionAdapter::RegisterGlobalOverload( - kEqual, ComplexEquality(op), registry)); - - return absl::OkStatus(); -} - -absl::Status RegisterHomogenousEqualityFunctions( - cel::FunctionRegistry& registry) { - CEL_RETURN_IF_ERROR(RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR(RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterEqualityFunctionsForType(registry)); - - CEL_RETURN_IF_ERROR( - RegisterComplexEqualityFunctionsForType( - &ListEqual, registry)); - - CEL_RETURN_IF_ERROR( - RegisterComplexEqualityFunctionsForType( - &MapEqual, registry)); - - return absl::OkStatus(); -} - -absl::Status RegisterNullMessageEqualityFunctions(FunctionRegistry& registry) { - // equals - CEL_RETURN_IF_ERROR( - (cel::RegisterHelper< - BinaryFunctionAdapter>:: - RegisterGlobalOverload( - kEqual, - [](const StructValue&, const NullValue&) { return false; }, - registry))); - - CEL_RETURN_IF_ERROR( - (cel::RegisterHelper< - BinaryFunctionAdapter>:: - RegisterGlobalOverload( - kEqual, - [](const NullValue&, const StructValue&) { return false; }, - registry))); - - // inequals - CEL_RETURN_IF_ERROR( - (cel::RegisterHelper< - BinaryFunctionAdapter>:: - RegisterGlobalOverload( - kInequal, - [](const StructValue&, const NullValue&) { return true; }, - registry))); - - return cel::RegisterHelper< - BinaryFunctionAdapter>:: - RegisterGlobalOverload( - kInequal, [](const NullValue&, const StructValue&) { return true; }, - registry); -} - template absl::StatusOr> HomogenousValueEqual( const Value& v1, const Value& v2, @@ -441,39 +221,33 @@ absl::StatusOr> HomogenousValueEqual( google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* absl_nonnull arena) { if (v1.kind() != v2.kind()) { - return std::nullopt; + return absl::nullopt; } static_assert(std::is_lvalue_reference_v, "unexpected value copy"); - switch (v1->kind()) { + switch (v1.kind()) { case ValueKind::kBool: - return Equal(v1.GetBool().NativeValue(), - v2.GetBool().NativeValue()); + return v1.GetBool().NativeValue() == v2.GetBool().NativeValue(); case ValueKind::kNull: - return Equal(v1.GetNull(), v2.GetNull()); + return true; case ValueKind::kInt: - return Equal(v1.GetInt().NativeValue(), - v2.GetInt().NativeValue()); + return v1.GetInt().NativeValue() == v2.GetInt().NativeValue(); case ValueKind::kUint: - return Equal(v1.GetUint().NativeValue(), - v2.GetUint().NativeValue()); + return v1.GetUint().NativeValue() == v2.GetUint().NativeValue(); case ValueKind::kDouble: - return Equal(v1.GetDouble().NativeValue(), - v2.GetDouble().NativeValue()); + return v1.GetDouble().NativeValue() == v2.GetDouble().NativeValue(); case ValueKind::kDuration: - return Equal(v1.GetDuration().NativeValue(), - v2.GetDuration().NativeValue()); + return v1.GetDuration().ToDuration() == v2.GetDuration().ToDuration(); case ValueKind::kTimestamp: - return Equal(v1.GetTimestamp().NativeValue(), - v2.GetTimestamp().NativeValue()); + return v1.GetTimestamp().ToTime() == v2.GetTimestamp().ToTime(); case ValueKind::kCelType: - return Equal(v1.GetType(), v2.GetType()); + return v1.GetType().name() == v2.GetType().name(); case ValueKind::kString: - return Equal(v1.GetString(), v2.GetString()); + return v1.GetString().Equals(v2.GetString()); case ValueKind::kBytes: - return Equal(v1.GetBytes(), v2.GetBytes()); + return v1.GetBytes().Equals(v2.GetBytes()); case ValueKind::kList: return ListEqual(v1.GetList(), v2.GetList(), descriptor_pool, message_factory, arena); @@ -484,51 +258,108 @@ absl::StatusOr> HomogenousValueEqual( return OpaqueEqual(v1.GetOpaque(), v2.GetOpaque(), descriptor_pool, message_factory, arena); default: - return std::nullopt; + return absl::nullopt; } } -absl::StatusOr EqualOverloadImpl( - const Value& lhs, const Value& rhs, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) { - CEL_ASSIGN_OR_RETURN(absl::optional result, - runtime_internal::ValueEqualImpl( - lhs, rhs, descriptor_pool, message_factory, arena)); - if (result.has_value()) { - return BoolValue(*result); +class HomogenousEqualityFunction final : public Function { + public: + explicit HomogenousEqualityFunction(bool negation) : negation_(negation) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if ((args[0].IsStruct() && args[1].IsNull()) || + (args[0].IsNull() && args[1].IsStruct())) { + return BoolValue(negation_); + } + CEL_ASSIGN_OR_RETURN(absl::optional result, + HomogenousValueEqual( + args[0], args[1], context.descriptor_pool(), + context.message_factory(), context.arena())); + if (!result.has_value()) { + return ErrorValue::From( + cel::runtime_internal::CreateNoMatchingOverloadError( + negation_ ? kInequal : kEqual), + context.arena()); + } + return BoolValue(negation_ ? !*result : *result); } - return ErrorValue::From( - cel::runtime_internal::CreateNoMatchingOverloadError(kEqual), arena); -} -absl::StatusOr InequalOverloadImpl( - const Value& lhs, const Value& rhs, - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, - google::protobuf::MessageFactory* absl_nonnull message_factory, - google::protobuf::Arena* absl_nonnull arena) { - CEL_ASSIGN_OR_RETURN(absl::optional result, - runtime_internal::ValueEqualImpl( - lhs, rhs, descriptor_pool, message_factory, arena)); - if (result.has_value()) { - return BoolValue(!*result); + private: + bool negation_; +}; + +class HeterogeneousEqualityFunction final : public Function { + public: + explicit HeterogeneousEqualityFunction(bool negation) : negation_(negation) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + CEL_ASSIGN_OR_RETURN(absl::optional result, + runtime_internal::ValueEqualImpl( + args[0], args[1], context.descriptor_pool(), + context.message_factory(), context.arena())); + if (!result.has_value()) { + return ErrorValue::From( + cel::runtime_internal::CreateNoMatchingOverloadError( + negation_ ? kInequal : kEqual), + context.arena()); + } + return BoolValue(negation_ ? !*result : *result); } - return ErrorValue::From( - cel::runtime_internal::CreateNoMatchingOverloadError(kInequal), arena); + + private: + bool negation_; +}; + +absl::Status RegisterHomogenousPair(FunctionRegistry& registry, Kind lhs, + Kind rhs) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(kInequal, /*is_receiver_style=*/false, {lhs, rhs}), + std::make_unique(/*negation=*/true))); + return registry.Register( + FunctionDescriptor(kEqual, /*is_receiver_style=*/false, {lhs, rhs}), + std::make_unique(/*negation=*/false)); } -absl::Status RegisterHeterogeneousEqualityFunctions( +absl::Status RegisterHomogenousEqualityFunctions( cel::FunctionRegistry& registry) { - using Adapter = cel::RegisterHelper< - BinaryFunctionAdapter, const Value&, const Value&>>; - CEL_RETURN_IF_ERROR( - Adapter::RegisterGlobalOverload(kEqual, &EqualOverloadImpl, registry)); + static constexpr Kind kKinds[] = { + Kind::kBool, Kind::kInt, Kind::kUint, Kind::kDouble, + Kind::kString, Kind::kBytes, Kind::kDuration, Kind::kTimestamp, + Kind::kNull, Kind::kType, Kind::kList, Kind::kMap, + }; + for (Kind kind : kKinds) { + CEL_RETURN_IF_ERROR(RegisterHomogenousPair(registry, kind, kind)); + } + return absl::OkStatus(); +} - CEL_RETURN_IF_ERROR(Adapter::RegisterGlobalOverload( - kInequal, &InequalOverloadImpl, registry)); +absl::Status RegisterNullMessageEqualityFunctions(FunctionRegistry& registry) { + CEL_RETURN_IF_ERROR( + RegisterHomogenousPair(registry, Kind::kStruct, Kind::kNull)); + return RegisterHomogenousPair(registry, Kind::kNull, Kind::kStruct); +} - return absl::OkStatus(); +absl::Status RegisterHeterogeneousEqualityFunctions( + cel::FunctionRegistry& registry) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(kEqual, /*is_receiver_style=*/false, + {Kind::kAny, Kind::kAny}), + std::make_unique(/*negation=*/false))); + + return registry.Register( + FunctionDescriptor(kInequal, /*is_receiver_style=*/false, + {Kind::kAny, Kind::kAny}), + std::make_unique(/*negation=*/true)); } absl::StatusOr> HomogenousEqualProvider::operator()( @@ -583,7 +414,7 @@ absl::StatusOr> ValueEqualImpl( // map containing an Error. Return no matching overload to propagate an error // instead of a false result. if (v1.IsError() || v1.IsUnknown() || v2.IsError() || v2.IsUnknown()) { - return std::nullopt; + return absl::nullopt; } return false; diff --git a/runtime/standard/logical_functions.cc b/runtime/standard/logical_functions.cc index 6eed8b695..ff2e09f91 100644 --- a/runtime/standard/logical_functions.cc +++ b/runtime/standard/logical_functions.cc @@ -14,16 +14,19 @@ #include "runtime/standard/logical_functions.h" +#include + #include "absl/status/status.h" -#include "absl/strings/string_view.h" +#include "absl/status/statusor.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" #include "internal/status_macros.h" #include "runtime/function.h" #include "runtime/function_registry.h" #include "runtime/internal/errors.h" -#include "runtime/register_function_helper.h" #include "runtime/runtime_options.h" namespace cel { @@ -31,40 +34,67 @@ namespace { using ::cel::runtime_internal::CreateNoMatchingOverloadError; -Value NotStrictlyFalseImpl(const Value& value, - const Function::InvokeContext& context) { - if (value.IsBool()) { - return value; +class LogicalNotFunction final : public Function { + public: + absl::StatusOr Invoke(absl::Span args, + const InvokeContext&) const override { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for unary function"); + } + if (!args[0].IsBool()) { + return absl::InvalidArgumentError("expected bool value"); + } + return BoolValue(!args[0].GetBool().NativeValue()); } +}; - if (value.IsError() || value.IsUnknown()) { - return TrueValue(); - } +class NotStrictlyFalseFunction final : public Function { + public: + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for unary function"); + } + const Value& value = args[0]; + if (value.IsBool()) { + return value; + } - // Should only accept bool unknown or error. - return ErrorValue::From( - CreateNoMatchingOverloadError(builtin::kNotStrictlyFalse), - context.arena()); -} + if (value.IsError() || value.IsUnknown()) { + return TrueValue(); + } + + // Should only accept bool unknown or error. + return ErrorValue::From( + CreateNoMatchingOverloadError(builtin::kNotStrictlyFalse), + context.arena()); + } +}; } // namespace absl::Status RegisterLogicalFunctions(FunctionRegistry& registry, const RuntimeOptions& options) { // logical NOT - CEL_RETURN_IF_ERROR( - (RegisterHelper>::RegisterGlobalOverload( - builtin::kNot, [](bool value) -> bool { return !value; }, registry))); + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(builtin::kNot, /*is_receiver_style=*/false, + {Kind::kBool}), + std::make_unique())); // Strictness - using StrictnessHelper = RegisterHelper>; - CEL_RETURN_IF_ERROR(StrictnessHelper::RegisterNonStrictOverload( - builtin::kNotStrictlyFalse, &NotStrictlyFalseImpl, registry)); - - CEL_RETURN_IF_ERROR(StrictnessHelper::RegisterNonStrictOverload( - builtin::kNotStrictlyFalseDeprecated, &NotStrictlyFalseImpl, registry)); + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(builtin::kNotStrictlyFalse, + /*is_receiver_style=*/false, {Kind::kAny}, + /*is_strict=*/false), + std::make_unique())); - return absl::OkStatus(); + return registry.Register( + FunctionDescriptor(builtin::kNotStrictlyFalseDeprecated, + /*is_receiver_style=*/false, {Kind::kAny}, + /*is_strict=*/false), + std::make_unique()); } } // namespace cel diff --git a/runtime/standard/regex_functions.cc b/runtime/standard/regex_functions.cc index e51ef821f..4c4075b7b 100644 --- a/runtime/standard/regex_functions.cc +++ b/runtime/standard/regex_functions.cc @@ -13,10 +13,14 @@ // limitations under the License. #include "runtime/standard/regex_functions.h" +#include + #include "absl/status/status.h" -#include "absl/strings/string_view.h" +#include "absl/status/statusor.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" #include "internal/re2_options.h" #include "internal/status_macros.h" @@ -26,29 +30,43 @@ #include "re2/re2.h" namespace cel { -namespace {} // namespace +namespace { + +class RegexMatchFunction final : public Function { + public: + explicit RegexMatchFunction(int max_size) : max_size_(max_size) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if (!args[0].IsString() || !args[1].IsString()) { + return absl::InvalidArgumentError("expected string value"); + } + RE2 re2(args[1].GetString().ToString(), cel::internal::MakeRE2Options()); + CEL_RETURN_IF_ERROR(cel::internal::CheckRE2(re2, max_size_)) + .With(ErrorValueReturn(context.arena())); + return BoolValue(RE2::PartialMatch(args[0].GetString().ToString(), re2)); + } + + private: + int max_size_; +}; + +} // namespace absl::Status RegisterRegexFunctions(FunctionRegistry& registry, const RuntimeOptions& options) { if (options.enable_regex) { - auto regex_matches = [max_size = options.regex_max_program_size]( - const StringValue& target, - const StringValue& regex, - const Function::InvokeContext& context) -> Value { - RE2 re2(regex.ToString(), cel::internal::MakeRE2Options()); - CEL_RETURN_IF_ERROR(cel::internal::CheckRE2(re2, max_size)) - .With(ErrorValueReturn(context.arena())); - return BoolValue(RE2::PartialMatch(target.ToString(), re2)); - }; - // bind str.matches(re) and matches(str, re) for (bool receiver_style : {true, false}) { - using MatchFnAdapter = - BinaryFunctionAdapter; - CEL_RETURN_IF_ERROR( - registry.Register(MatchFnAdapter::CreateDescriptor( - cel::builtin::kRegexMatch, receiver_style), - MatchFnAdapter::WrapFunction(regex_matches))); + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(cel::builtin::kRegexMatch, receiver_style, + {Kind::kString, Kind::kString}), + std::make_unique( + options.regex_max_program_size))); } } // if options.enable_regex diff --git a/runtime/standard/string_functions.cc b/runtime/standard/string_functions.cc index 2bcfe185c..f383ee75b 100644 --- a/runtime/standard/string_functions.cc +++ b/runtime/standard/string_functions.cc @@ -14,94 +14,109 @@ #include "runtime/standard/string_functions.h" -#include +#include -#include "absl/base/nullability.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/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "internal/status_macros.h" +#include "runtime/function.h" #include "runtime/function_registry.h" #include "runtime/runtime_options.h" -#include "google/protobuf/arena.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" namespace cel { namespace { -// Concatenation for string type. -absl::StatusOr ConcatString( - const StringValue& value1, const StringValue& value2, - const google::protobuf::DescriptorPool* absl_nonnull, - google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull arena) { - return StringValue::Concat(value1, value2, arena); -} - -// Concatenation for bytes type. -absl::StatusOr ConcatBytes( - const BytesValue& value1, const BytesValue& value2, - const google::protobuf::DescriptorPool* absl_nonnull, - google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull arena) { - return BytesValue::Concat(value1, value2, arena); -} - -bool StringContains(const StringValue& value, const StringValue& substr) { - return value.Contains(substr); -} +class StringFunction final : public Function { + public: + enum class Op { kContains, kEndsWith, kStartsWith, kConcat, kSize }; + + explicit StringFunction(Op op) : op_(op) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (op_ == Op::kSize) { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for unary function"); + } + switch (args[0].kind()) { + case ValueKind::kString: + return IntValue(args[0].GetString().Size()); + case ValueKind::kBytes: + return IntValue(args[0].GetBytes().Size()); + default: + return absl::InvalidArgumentError("expected string or bytes value"); + } + } + + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + if (op_ == Op::kConcat) { + if (args[0].kind() != args[1].kind()) { + return absl::InvalidArgumentError("expected matching types for concat"); + } + switch (args[0].kind()) { + case ValueKind::kString: + return StringValue::Concat(args[0].GetString(), args[1].GetString(), + context.arena()); + case ValueKind::kBytes: + return BytesValue::Concat(args[0].GetBytes(), args[1].GetBytes(), + context.arena()); + default: + return absl::InvalidArgumentError("expected string or bytes value"); + } + } + + if (!args[0].IsString() || !args[1].IsString()) { + return absl::InvalidArgumentError("expected string value"); + } + const StringValue& value = args[0].GetString(); + const StringValue& substr = args[1].GetString(); + switch (op_) { + case Op::kContains: + return BoolValue(value.Contains(substr)); + case Op::kEndsWith: + return BoolValue(value.EndsWith(substr)); + case Op::kStartsWith: + return BoolValue(value.StartsWith(substr)); + case Op::kConcat: + case Op::kSize: + return absl::InternalError("unreachable"); + } + } -bool StringEndsWith(const StringValue& value, const StringValue& suffix) { - return value.EndsWith(suffix); -} - -bool StringStartsWith(const StringValue& value, const StringValue& prefix) { - return value.StartsWith(prefix); -} + private: + Op op_; +}; absl::Status RegisterSizeFunctions(FunctionRegistry& registry) { - // String size - auto size_func = [](const StringValue& value) -> int64_t { - return value.Size(); - }; - - // Support global and receiver style size() operations on strings. - using StrSizeFnAdapter = UnaryFunctionAdapter; - CEL_RETURN_IF_ERROR(StrSizeFnAdapter::RegisterGlobalOverload( - cel::builtin::kSize, size_func, registry)); - - CEL_RETURN_IF_ERROR(StrSizeFnAdapter::RegisterMemberOverload( - cel::builtin::kSize, size_func, registry)); - - // Bytes size - auto bytes_size_func = [](const BytesValue& value) -> int64_t { - return value.Size(); - }; - - // Support global and receiver style size() operations on bytes. - using BytesSizeFnAdapter = UnaryFunctionAdapter; - CEL_RETURN_IF_ERROR(BytesSizeFnAdapter::RegisterGlobalOverload( - cel::builtin::kSize, bytes_size_func, registry)); - - return BytesSizeFnAdapter::RegisterMemberOverload(cel::builtin::kSize, - bytes_size_func, registry); + for (bool receiver_style : {true, false}) { + for (Kind kind : {Kind::kString, Kind::kBytes}) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(cel::builtin::kSize, receiver_style, {kind}), + std::make_unique(StringFunction::Op::kSize))); + } + } + return absl::OkStatus(); } absl::Status RegisterConcatFunctions(FunctionRegistry& registry) { - using StrCatFnAdapter = - BinaryFunctionAdapter, const StringValue&, - const StringValue&>; - CEL_RETURN_IF_ERROR(StrCatFnAdapter::RegisterGlobalOverload( - cel::builtin::kAdd, &ConcatString, registry)); - - using BytesCatFnAdapter = - BinaryFunctionAdapter, const BytesValue&, - const BytesValue&>; - return BytesCatFnAdapter::RegisterGlobalOverload(cel::builtin::kAdd, - &ConcatBytes, registry); + for (Kind kind : {Kind::kString, Kind::kBytes}) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(cel::builtin::kAdd, /*is_receiver_style=*/false, + {kind, kind}), + std::make_unique(StringFunction::Op::kConcat))); + } + return absl::OkStatus(); } } // namespace @@ -109,24 +124,21 @@ absl::Status RegisterConcatFunctions(FunctionRegistry& registry) { absl::Status RegisterStringFunctions(FunctionRegistry& registry, const RuntimeOptions& options) { // Basic substring tests (contains, startsWith, endsWith) + static constexpr struct { + absl::string_view name; + StringFunction::Op op; + } kSubstringOps[] = { + {cel::builtin::kStringContains, StringFunction::Op::kContains}, + {cel::builtin::kStringEndsWith, StringFunction::Op::kEndsWith}, + {cel::builtin::kStringStartsWith, StringFunction::Op::kStartsWith}, + }; for (bool receiver_style : {true, false}) { - auto status = - BinaryFunctionAdapter:: - Register(cel::builtin::kStringContains, receiver_style, - StringContains, registry); - CEL_RETURN_IF_ERROR(status); - - status = - BinaryFunctionAdapter:: - Register(cel::builtin::kStringEndsWith, receiver_style, - StringEndsWith, registry); - CEL_RETURN_IF_ERROR(status); - - status = - BinaryFunctionAdapter:: - Register(cel::builtin::kStringStartsWith, receiver_style, - StringStartsWith, registry); - CEL_RETURN_IF_ERROR(status); + for (const auto& entry : kSubstringOps) { + CEL_RETURN_IF_ERROR( + registry.Register(FunctionDescriptor(entry.name, receiver_style, + {Kind::kString, Kind::kString}), + std::make_unique(entry.op))); + } } // string concatenation if enabled diff --git a/runtime/standard/time_functions.cc b/runtime/standard/time_functions.cc index d517e6f40..e9feae711 100644 --- a/runtime/standard/time_functions.cc +++ b/runtime/standard/time_functions.cc @@ -15,25 +15,28 @@ #include "runtime/standard/time_functions.h" #include -#include +#include #include #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/match.h" +#include "absl/strings/str_cat.h" #include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" #include "absl/time/civil_time.h" #include "absl/time/time.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "internal/overflow.h" #include "internal/status_macros.h" #include "runtime/function.h" #include "runtime/function_registry.h" #include "runtime/runtime_options.h" -#include "google/protobuf/arena.h" namespace cel { namespace { @@ -72,480 +75,286 @@ absl::Status FindTimeBreakdown(absl::Time timestamp, absl::string_view tz, return absl::InvalidArgumentError("Invalid timezone"); } -Value GetTimeBreakdownPart( - absl::Time timestamp, absl::string_view tz, - const std::function& - extractor_func, - google::protobuf::Arena* arena) { - absl::TimeZone::CivilInfo breakdown; - auto status = FindTimeBreakdown(timestamp, tz, &breakdown); - - if (!status.ok()) { - return ErrorValue::From(status, arena); - } - - return IntValue(extractor_func(breakdown)); -} - -Value GetFullYear(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.year(); - }, - context.arena()); -} - -Value GetMonth(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.month() - 1; - }, - context.arena()); -} - -Value GetDayOfYear(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return absl::GetYearDay(absl::CivilDay(breakdown.cs)) - 1; - }, - context.arena()); -} +class TimeAccessorFunction final : public Function { + public: + enum class Field { + kFullYear, + kMonth, + kDayOfYear, + kDayOfMonth, + kDate, + kDayOfWeek, + kHours, + kMinutes, + kSeconds, + kMilliseconds, + }; + + explicit TimeAccessorFunction(Field field) : field_(field) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.empty() || args.size() > 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for time accessor"); + } + if (args[0].IsDuration()) { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for duration accessor"); + } + absl::Duration d = args[0].GetDuration().ToDuration(); + switch (field_) { + case Field::kHours: + return IntValue(absl::ToInt64Hours(d)); + case Field::kMinutes: + return IntValue(absl::ToInt64Minutes(d)); + case Field::kSeconds: + return IntValue(absl::ToInt64Seconds(d)); + case Field::kMilliseconds: { + constexpr int64_t millis_per_second = 1000L; + return IntValue(absl::ToInt64Milliseconds(d) % millis_per_second); + } + default: + return absl::InvalidArgumentError( + "unsupported accessor field for duration"); + } + } -Value GetDayOfMonth(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.day() - 1; - }, - context.arena()); -} + if (!args[0].IsTimestamp()) { + return absl::InvalidArgumentError("expected timestamp or duration value"); + } + std::string tz; + if (args.size() == 2) { + if (!args[1].IsString()) { + return absl::InvalidArgumentError("expected string timezone"); + } + tz = args[1].GetString().ToString(); + } -Value GetDate(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.day(); - }, - context.arena()); -} + absl::TimeZone::CivilInfo breakdown; + absl::Status status = + FindTimeBreakdown(args[0].GetTimestamp().ToTime(), tz, &breakdown); + if (!status.ok()) { + return ErrorValue::From(status, context.arena()); + } -Value GetDayOfWeek(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { + switch (field_) { + case Field::kFullYear: + return IntValue(breakdown.cs.year()); + case Field::kMonth: + return IntValue(breakdown.cs.month() - 1); + case Field::kDayOfYear: + return IntValue(absl::GetYearDay(absl::CivilDay(breakdown.cs)) - 1); + case Field::kDayOfMonth: + return IntValue(breakdown.cs.day() - 1); + case Field::kDate: + return IntValue(breakdown.cs.day()); + case Field::kDayOfWeek: { absl::Weekday weekday = absl::GetWeekday(breakdown.cs); - // get day of week from the date in UTC, zero-based, zero for Sunday, // based on GetDayOfWeek CEL function definition. int weekday_num = static_cast(weekday); weekday_num = (weekday_num == 6) ? 0 : weekday_num + 1; - return weekday_num; - }, - context.arena()); -} - -Value GetHours(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.hour(); - }, - context.arena()); -} - -Value GetMinutes(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.minute(); - }, - context.arena()); -} - -Value GetSeconds(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return breakdown.cs.second(); - }, - context.arena()); -} - -Value GetMilliseconds(absl::Time timestamp, absl::string_view tz, - const Function::InvokeContext& context) { - return GetTimeBreakdownPart( - timestamp, tz, - [](const absl::TimeZone::CivilInfo& breakdown) { - return absl::ToInt64Milliseconds(breakdown.subsecond); - }, - context.arena()); -} - -absl::Status RegisterTimestampFunctions(FunctionRegistry& registry, - const RuntimeOptions& options) { - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kFullYear, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetFullYear(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kFullYear, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetFullYear(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kMonth, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetMonth(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor(builtin::kMonth, - true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetMonth(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kDayOfYear, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetDayOfYear(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kDayOfYear, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetDayOfYear(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kDayOfMonth, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetDayOfMonth(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kDayOfMonth, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetDayOfMonth(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kDate, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetDate(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor(builtin::kDate, - true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetDate(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kDayOfWeek, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetDayOfWeek(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kDayOfWeek, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetDayOfWeek(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kHours, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetHours(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor(builtin::kHours, - true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetHours(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kMinutes, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetMinutes(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kMinutes, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetMinutes(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kSeconds, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetSeconds(ts, tz.ToString(), context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kSeconds, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetSeconds(ts, "", context); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kMilliseconds, true), - BinaryFunctionAdapter:: - WrapFunction([](absl::Time ts, const StringValue& tz, - const Function::InvokeContext& context) -> Value { - return GetMilliseconds(ts, tz.ToString(), context); - }))); - - return registry.Register( - UnaryFunctionAdapter::CreateDescriptor( - builtin::kMilliseconds, true), - UnaryFunctionAdapter::WrapFunction( - [](absl::Time ts, const Function::InvokeContext& context) -> Value { - return GetMilliseconds(ts, "", context); - })); -} + return IntValue(weekday_num); + } + case Field::kHours: + return IntValue(breakdown.cs.hour()); + case Field::kMinutes: + return IntValue(breakdown.cs.minute()); + case Field::kSeconds: + return IntValue(breakdown.cs.second()); + case Field::kMilliseconds: + return IntValue(absl::ToInt64Milliseconds(breakdown.subsecond)); + } + } -absl::Status RegisterCheckedTimeArithmeticFunctions( - FunctionRegistry& registry) { - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor(builtin::kAdd, - false), - BinaryFunctionAdapter, absl::Time, absl::Duration>:: - WrapFunction([](absl::Time t1, absl::Duration d2, - const Function::InvokeContext& context) - -> absl::StatusOr { - auto sum = cel::internal::CheckedAdd(t1, d2); - if (!sum.ok()) { - return ErrorValue::From(sum.status(), context.arena()); - } - return TimestampValue(*sum); - }))); + private: + Field field_; +}; - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter, absl::Duration, - absl::Time>::CreateDescriptor(builtin::kAdd, false), - BinaryFunctionAdapter, absl::Duration, absl::Time>:: - WrapFunction([](absl::Duration d2, absl::Time t1, - const Function::InvokeContext& context) - -> absl::StatusOr { - auto sum = cel::internal::CheckedAdd(t1, d2); - if (!sum.ok()) { - return ErrorValue::From(sum.status(), context.arena()); - } - return TimestampValue(*sum); - }))); +class TimeArithmeticFunction final : public Function { + public: + enum class Op { kAdd, kSubtract }; - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter, absl::Duration, - absl::Duration>::CreateDescriptor(builtin::kAdd, - false), - BinaryFunctionAdapter, absl::Duration, - absl::Duration>:: - WrapFunction([](absl::Duration d1, absl::Duration d2, - const Function::InvokeContext& context) - -> absl::StatusOr { - auto sum = cel::internal::CheckedAdd(d1, d2); - if (!sum.ok()) { - return ErrorValue::From(sum.status(), context.arena()); - } - return DurationValue(*sum); - }))); + TimeArithmeticFunction(Op op, bool checked) : op_(op), checked_(checked) {} - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter, absl::Time, absl::Duration>:: - CreateDescriptor(builtin::kSubtract, false), - BinaryFunctionAdapter, absl::Time, absl::Duration>:: - WrapFunction([](absl::Time t1, absl::Duration d2, - const Function::InvokeContext& context) - -> absl::StatusOr { - auto diff = cel::internal::CheckedSub(t1, d2); - if (!diff.ok()) { - return ErrorValue::From(diff.status(), context.arena()); - } - return TimestampValue(*diff); - }))); + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 2) { + return absl::InvalidArgumentError( + "unexpected number of arguments for binary function"); + } + ValueKind lhs_kind = args[0].kind(); + ValueKind rhs_kind = args[1].kind(); + if (op_ == Op::kAdd) { + if (lhs_kind == ValueKind::kTimestamp && + rhs_kind == ValueKind::kDuration) { + absl::Time t1 = args[0].GetTimestamp().ToTime(); + absl::Duration d2 = args[1].GetDuration().ToDuration(); + if (!checked_) { + return UnsafeTimestampValue(t1 + d2); + } + auto sum = cel::internal::CheckedAdd(t1, d2); + if (!sum.ok()) { + return ErrorValue::From(sum.status(), context.arena()); + } + return TimestampValue(*sum); + } + if (lhs_kind == ValueKind::kDuration && + rhs_kind == ValueKind::kTimestamp) { + absl::Duration d2 = args[0].GetDuration().ToDuration(); + absl::Time t1 = args[1].GetTimestamp().ToTime(); + if (!checked_) { + return UnsafeTimestampValue(t1 + d2); + } + auto sum = cel::internal::CheckedAdd(t1, d2); + if (!sum.ok()) { + return ErrorValue::From(sum.status(), context.arena()); + } + return TimestampValue(*sum); + } + if (lhs_kind == ValueKind::kDuration && + rhs_kind == ValueKind::kDuration) { + absl::Duration d1 = args[0].GetDuration().ToDuration(); + absl::Duration d2 = args[1].GetDuration().ToDuration(); + if (!checked_) { + return UnsafeDurationValue(d1 + d2); + } + auto sum = cel::internal::CheckedAdd(d1, d2); + if (!sum.ok()) { + return ErrorValue::From(sum.status(), context.arena()); + } + return DurationValue(*sum); + } + return absl::InvalidArgumentError( + "expected timestamp/duration arguments for add"); + } - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter, absl::Time, - absl::Time>::CreateDescriptor(builtin::kSubtract, - false), - BinaryFunctionAdapter, absl::Time, absl::Time>:: - WrapFunction([](absl::Time t1, absl::Time t2, - const Function::InvokeContext& context) - -> absl::StatusOr { - auto diff = cel::internal::CheckedSub(t1, t2); - if (!diff.ok()) { - return ErrorValue::From(diff.status(), context.arena()); - } - return DurationValue(*diff); - }))); + // Op::kSubtract + if (lhs_kind == ValueKind::kTimestamp && rhs_kind == ValueKind::kDuration) { + absl::Time t1 = args[0].GetTimestamp().ToTime(); + absl::Duration d2 = args[1].GetDuration().ToDuration(); + if (!checked_) { + return UnsafeTimestampValue(t1 - d2); + } + auto diff = cel::internal::CheckedSub(t1, d2); + if (!diff.ok()) { + return ErrorValue::From(diff.status(), context.arena()); + } + return TimestampValue(*diff); + } + if (lhs_kind == ValueKind::kTimestamp && + rhs_kind == ValueKind::kTimestamp) { + absl::Time t1 = args[0].GetTimestamp().ToTime(); + absl::Time t2 = args[1].GetTimestamp().ToTime(); + if (!checked_) { + return UnsafeDurationValue(t1 - t2); + } + auto diff = cel::internal::CheckedSub(t1, t2); + if (!diff.ok()) { + return ErrorValue::From(diff.status(), context.arena()); + } + return DurationValue(*diff); + } + if (lhs_kind == ValueKind::kDuration && rhs_kind == ValueKind::kDuration) { + absl::Duration d1 = args[0].GetDuration().ToDuration(); + absl::Duration d2 = args[1].GetDuration().ToDuration(); + if (!checked_) { + return UnsafeDurationValue(d1 - d2); + } + auto diff = cel::internal::CheckedSub(d1, d2); + if (!diff.ok()) { + return ErrorValue::From(diff.status(), context.arena()); + } + return DurationValue(*diff); + } + return absl::InvalidArgumentError( + "expected timestamp/duration arguments for subtract"); + } - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter< - absl::StatusOr, absl::Duration, - absl::Duration>::CreateDescriptor(builtin::kSubtract, false), - BinaryFunctionAdapter, absl::Duration, - absl::Duration>:: - WrapFunction([](absl::Duration d1, absl::Duration d2, - const Function::InvokeContext& context) - -> absl::StatusOr { - auto diff = cel::internal::CheckedSub(d1, d2); - if (!diff.ok()) { - return ErrorValue::From(diff.status(), context.arena()); - } - return DurationValue(*diff); - }))); + private: + Op op_; + bool checked_; +}; +absl::Status RegisterTimestampFunctions(FunctionRegistry& registry, + const RuntimeOptions& options) { + static constexpr struct { + absl::string_view name; + TimeAccessorFunction::Field field; + } kTimestampAccessors[] = { + {builtin::kFullYear, TimeAccessorFunction::Field::kFullYear}, + {builtin::kMonth, TimeAccessorFunction::Field::kMonth}, + {builtin::kDayOfYear, TimeAccessorFunction::Field::kDayOfYear}, + {builtin::kDayOfMonth, TimeAccessorFunction::Field::kDayOfMonth}, + {builtin::kDate, TimeAccessorFunction::Field::kDate}, + {builtin::kDayOfWeek, TimeAccessorFunction::Field::kDayOfWeek}, + {builtin::kHours, TimeAccessorFunction::Field::kHours}, + {builtin::kMinutes, TimeAccessorFunction::Field::kMinutes}, + {builtin::kSeconds, TimeAccessorFunction::Field::kSeconds}, + {builtin::kMilliseconds, TimeAccessorFunction::Field::kMilliseconds}, + }; + for (const auto& entry : kTimestampAccessors) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(entry.name, /*is_receiver_style=*/true, + {Kind::kTimestamp, Kind::kString}), + std::make_unique(entry.field))); + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(entry.name, /*is_receiver_style=*/true, + {Kind::kTimestamp}), + std::make_unique(entry.field))); + } return absl::OkStatus(); } -absl::Status RegisterUncheckedTimeArithmeticFunctions( - FunctionRegistry& registry) { - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor(builtin::kAdd, - false), - BinaryFunctionAdapter::WrapFunction( - [](absl::Time t1, absl::Duration d2) -> Value { - return UnsafeTimestampValue(t1 + d2); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor(builtin::kAdd, false), - BinaryFunctionAdapter::WrapFunction( - [](absl::Duration d2, absl::Time t1) -> Value { - return UnsafeTimestampValue(t1 + d2); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor(builtin::kAdd, - false), - BinaryFunctionAdapter:: - WrapFunction([](absl::Duration d1, absl::Duration d2) -> Value { - return UnsafeDurationValue(d1 + d2); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kSubtract, false), - - BinaryFunctionAdapter::WrapFunction( - - [](absl::Time t1, absl::Duration d2) -> Value { - return UnsafeTimestampValue(t1 - d2); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter::CreateDescriptor( - builtin::kSubtract, false), - BinaryFunctionAdapter::WrapFunction( - - [](absl::Time t1, absl::Time t2) -> Value { - return UnsafeDurationValue(t1 - t2); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - BinaryFunctionAdapter:: - CreateDescriptor(builtin::kSubtract, false), - BinaryFunctionAdapter:: - WrapFunction([](absl::Duration d1, absl::Duration d2) -> Value { - return UnsafeDurationValue(d1 - d2); - }))); - +absl::Status RegisterTimeArithmeticFunctions(FunctionRegistry& registry, + bool checked) { + static constexpr struct { + absl::string_view name; + TimeArithmeticFunction::Op op; + Kind lhs; + Kind rhs; + } kOverloads[] = { + {builtin::kAdd, TimeArithmeticFunction::Op::kAdd, Kind::kTimestamp, + Kind::kDuration}, + {builtin::kAdd, TimeArithmeticFunction::Op::kAdd, Kind::kDuration, + Kind::kTimestamp}, + {builtin::kAdd, TimeArithmeticFunction::Op::kAdd, Kind::kDuration, + Kind::kDuration}, + {builtin::kSubtract, TimeArithmeticFunction::Op::kSubtract, + Kind::kTimestamp, Kind::kDuration}, + {builtin::kSubtract, TimeArithmeticFunction::Op::kSubtract, + Kind::kTimestamp, Kind::kTimestamp}, + {builtin::kSubtract, TimeArithmeticFunction::Op::kSubtract, + Kind::kDuration, Kind::kDuration}, + }; + for (const auto& entry : kOverloads) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(entry.name, /*is_receiver_style=*/false, + {entry.lhs, entry.rhs}), + std::make_unique(entry.op, checked))); + } return absl::OkStatus(); } absl::Status RegisterDurationFunctions(FunctionRegistry& registry) { - // duration breakdown accessor functions - using DurationAccessorFunction = - UnaryFunctionAdapter; - CEL_RETURN_IF_ERROR(registry.Register( - DurationAccessorFunction::CreateDescriptor(builtin::kHours, true), - DurationAccessorFunction::WrapFunction( - [](absl::Duration d) -> int64_t { return absl::ToInt64Hours(d); }))); - - CEL_RETURN_IF_ERROR(registry.Register( - DurationAccessorFunction::CreateDescriptor(builtin::kMinutes, true), - DurationAccessorFunction::WrapFunction([](absl::Duration d) -> int64_t { - return absl::ToInt64Minutes(d); - }))); - - CEL_RETURN_IF_ERROR(registry.Register( - DurationAccessorFunction::CreateDescriptor(builtin::kSeconds, true), - DurationAccessorFunction::WrapFunction([](absl::Duration d) -> int64_t { - return absl::ToInt64Seconds(d); - }))); - - return registry.Register( - DurationAccessorFunction::CreateDescriptor(builtin::kMilliseconds, true), - DurationAccessorFunction::WrapFunction([](absl::Duration d) -> int64_t { - constexpr int64_t millis_per_second = 1000L; - return absl::ToInt64Milliseconds(d) % millis_per_second; - })); + static constexpr struct { + absl::string_view name; + TimeAccessorFunction::Field field; + } kDurationAccessors[] = { + {builtin::kHours, TimeAccessorFunction::Field::kHours}, + {builtin::kMinutes, TimeAccessorFunction::Field::kMinutes}, + {builtin::kSeconds, TimeAccessorFunction::Field::kSeconds}, + {builtin::kMilliseconds, TimeAccessorFunction::Field::kMilliseconds}, + }; + for (const auto& entry : kDurationAccessors) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(entry.name, /*is_receiver_style=*/true, + {Kind::kDuration}), + std::make_unique(entry.field))); + } + return absl::OkStatus(); } } // namespace @@ -558,11 +367,8 @@ absl::Status RegisterTimeFunctions(FunctionRegistry& registry, // Special arithmetic operators for Timestamp and Duration // TODO(uncreated-issue/37): deprecate unchecked time math functions when clients no // longer depend on them. - if (options.enable_timestamp_duration_overflow_errors) { - return RegisterCheckedTimeArithmeticFunctions(registry); - } - - return RegisterUncheckedTimeArithmeticFunctions(registry); + return RegisterTimeArithmeticFunctions( + registry, options.enable_timestamp_duration_overflow_errors); } } // namespace cel diff --git a/runtime/standard/type_conversion_functions.cc b/runtime/standard/type_conversion_functions.cc index 2c5130aa3..9302e6e0c 100644 --- a/runtime/standard/type_conversion_functions.cc +++ b/runtime/standard/type_conversion_functions.cc @@ -16,6 +16,7 @@ #include #include +#include #include // NOLINT (required for std::to_chars_result) #include @@ -25,9 +26,12 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" +#include "absl/types/span.h" #include "base/builtins.h" -#include "base/function_adapter.h" +#include "common/function_descriptor.h" +#include "common/kind.h" #include "common/value.h" +#include "common/value_kind.h" #include "internal/overflow.h" #include "internal/status_macros.h" #include "internal/time.h" @@ -80,417 +84,401 @@ Value LegacyFormatDouble(double v, const Function::InvokeContext& context) { return StringValue::From(absl::StrCat(v), context.arena()); } -absl::Status RegisterBoolConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions&) { - // bool -> bool - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kBool, [](bool v) { return v; }, registry); - CEL_RETURN_IF_ERROR(status); - - // string -> bool - return UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kBool, - [](const StringValue& v, - const Function::InvokeContext& context) -> Value { - if ((v == "true") || (v == "True") || (v == "TRUE") || (v == "t") || - (v == "1")) { - return TrueValue(); - } else if ((v == "false") || (v == "FALSE") || (v == "False") || - (v == "f") || (v == "0")) { - return FalseValue(); - } else { - return ErrorValue::From( - absl::InvalidArgumentError( - "Type conversion error from 'string' to 'bool'"), - context.arena()); - } - }, - registry); +Value ConvertToBool(const Value& arg, const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kBool: + return arg; + case ValueKind::kString: { + const StringValue& v = arg.GetString(); + if ((v == "true") || (v == "True") || (v == "TRUE") || (v == "t") || + (v == "1")) { + return TrueValue(); + } + if ((v == "false") || (v == "FALSE") || (v == "False") || (v == "f") || + (v == "0")) { + return FalseValue(); + } + return ErrorValue::From( + absl::InvalidArgumentError( + "Type conversion error from 'string' to 'bool'"), + context.arena()); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to bool"), + context.arena()); + } } -absl::Status RegisterIntConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions&) { - // bool -> int - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kInt, [](bool v) { return static_cast(v); }, - registry); - CEL_RETURN_IF_ERROR(status); - - // double -> int - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kInt, - [](double v, const Function::InvokeContext& context) -> Value { - auto conv = cel::internal::CheckedDoubleToInt64(v); - if (!conv.ok()) { - return ErrorValue::From(conv.status(), context.arena()); - } - return IntValue(*conv); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // int -> int - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kInt, [](int64_t v) { return v; }, registry); - CEL_RETURN_IF_ERROR(status); - - // string -> int - status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kInt, - [](const StringValue& s, - const Function::InvokeContext& context) -> Value { - int64_t result; - if (!absl::SimpleAtoi(s.ToString(), &result)) { - return ErrorValue::From( - absl::InvalidArgumentError("cannot convert string to int"), - context.arena()); - } - return IntValue(result); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // time -> int - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kInt, [](absl::Time t) { return absl::ToUnixSeconds(t); }, - registry); - CEL_RETURN_IF_ERROR(status); - - // uint -> int - return UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kInt, - [](uint64_t v, const Function::InvokeContext& context) -> Value { - auto conv = cel::internal::CheckedUint64ToInt64(v); - if (!conv.ok()) { - return ErrorValue::From(conv.status(), context.arena()); - } - return IntValue(*conv); - }, - registry); +Value ConvertToInt(const Value& arg, const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kInt: + return arg; + case ValueKind::kBool: + return IntValue(static_cast(arg.GetBool().NativeValue())); + case ValueKind::kDouble: { + auto conv = + cel::internal::CheckedDoubleToInt64(arg.GetDouble().NativeValue()); + if (!conv.ok()) { + return ErrorValue::From(conv.status(), context.arena()); + } + return IntValue(*conv); + } + case ValueKind::kString: { + int64_t result; + if (!absl::SimpleAtoi(arg.GetString().ToString(), &result)) { + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert string to int"), + context.arena()); + } + return IntValue(result); + } + case ValueKind::kTimestamp: + return IntValue(absl::ToUnixSeconds(arg.GetTimestamp().ToTime())); + case ValueKind::kUint: { + auto conv = + cel::internal::CheckedUint64ToInt64(arg.GetUint().NativeValue()); + if (!conv.ok()) { + return ErrorValue::From(conv.status(), context.arena()); + } + return IntValue(*conv); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to int"), + context.arena()); + } } -absl::Status RegisterStringConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions& options) { - // May be optionally disabled to reduce potential allocs. - if (!options.enable_string_conversion) { - return absl::OkStatus(); +Value ConvertToUint(const Value& arg, const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kUint: + return arg; + case ValueKind::kDouble: { + auto conv = + cel::internal::CheckedDoubleToUint64(arg.GetDouble().NativeValue()); + if (!conv.ok()) { + return ErrorValue::From(conv.status(), context.arena()); + } + return UintValue(*conv); + } + case ValueKind::kInt: { + auto conv = + cel::internal::CheckedInt64ToUint64(arg.GetInt().NativeValue()); + if (!conv.ok()) { + return ErrorValue::From(conv.status(), context.arena()); + } + return UintValue(*conv); + } + case ValueKind::kString: { + uint64_t result; + if (!absl::SimpleAtoi(arg.GetString().ToString(), &result)) { + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert string to uint"), + context.arena()); + } + return UintValue(result); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to uint"), + context.arena()); } - - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - - [](const BytesValue& value, - const Function::InvokeContext& context) -> Value { - auto valid = value.NativeValue([](const auto& value) -> bool { - return internal::Utf8IsValid(value); - }); - if (!valid) { - return ErrorValue::From( - absl::InvalidArgumentError("malformed UTF-8 bytes"), - context.arena()); - } - return StringValue(value); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // bool -> string - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - [](bool value) -> StringValue { - return StringValue::WrapUnsafe(value ? "true" : "false"); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // double -> string - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - (options.enable_precision_preserving_double_format ? &FormatDouble - : &LegacyFormatDouble), - registry); - CEL_RETURN_IF_ERROR(status); - - // int -> string - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - [](int64_t value, const Function::InvokeContext& context) -> StringValue { - return StringValue::From(absl::StrCat(value), context.arena()); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // string -> string - status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - [](StringValue value) -> StringValue { return value; }, registry); - CEL_RETURN_IF_ERROR(status); - - // uint -> string - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - [](uint64_t value, - const Function::InvokeContext& context) -> StringValue { - return StringValue::From(absl::StrCat(value), context.arena()); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // duration -> string - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - [](absl::Duration value, - const Function::InvokeContext& context) -> Value { - auto encode = EncodeDurationToJson(value); - if (!encode.ok()) { - return ErrorValue::From(encode.status(), context.arena()); - } - return StringValue::From(*encode, context.arena()); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // timestamp -> string - return UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kString, - [](absl::Time value, const Function::InvokeContext& context) -> Value { - auto encode = EncodeTimestampToJson(value); - if (!encode.ok()) { - return ErrorValue::From(encode.status(), context.arena()); - } - return StringValue::From(*encode, context.arena()); - }, - registry); } -absl::Status RegisterUintConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions&) { - // double -> uint - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kUint, - [](double v, const Function::InvokeContext& context) -> Value { - auto conv = cel::internal::CheckedDoubleToUint64(v); - if (!conv.ok()) { - return ErrorValue::From(conv.status(), context.arena()); - } - return UintValue(*conv); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // int -> uint - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kUint, - [](int64_t v, const Function::InvokeContext& context) -> Value { - auto conv = cel::internal::CheckedInt64ToUint64(v); - if (!conv.ok()) { - return ErrorValue::From(conv.status(), context.arena()); - } - return UintValue(*conv); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // string -> uint - status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kUint, - [](const StringValue& s, - const Function::InvokeContext& context) -> Value { - uint64_t result; - if (!absl::SimpleAtoi(s.ToString(), &result)) { - return ErrorValue::From( - absl::InvalidArgumentError("cannot convert string to uint"), - context.arena()); - } - return UintValue(result); - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // uint -> uint - return UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kUint, [](uint64_t v) { return v; }, registry); +Value ConvertToDouble(const Value& arg, + const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kDouble: + return arg; + case ValueKind::kInt: + return DoubleValue(static_cast(arg.GetInt().NativeValue())); + case ValueKind::kUint: + return DoubleValue(static_cast(arg.GetUint().NativeValue())); + case ValueKind::kString: { + double result; + if (absl::SimpleAtod(arg.GetString().ToString(), &result)) { + return DoubleValue(result); + } + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert string to double"), + context.arena()); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to double"), + context.arena()); + } } -absl::Status RegisterBytesConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions&) { - // bytes -> bytes - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kBytes, - - [](BytesValue value) -> BytesValue { return value; }, registry); - CEL_RETURN_IF_ERROR(status); - - // string -> bytes - return UnaryFunctionAdapter, const StringValue&>:: - RegisterGlobalOverload( - cel::builtin::kBytes, - [](const StringValue& value) { return BytesValue(value); }, registry); +Value ConvertToString(const Value& arg, bool precision_preserving_double_format, + const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kString: + return arg; + case ValueKind::kBytes: { + const BytesValue& value = arg.GetBytes(); + auto valid = value.NativeValue([](const auto& bytes) -> bool { + return internal::Utf8IsValid(bytes); + }); + if (!valid) { + return ErrorValue::From( + absl::InvalidArgumentError("malformed UTF-8 bytes"), + context.arena()); + } + return StringValue(value); + } + case ValueKind::kBool: + return StringValue::WrapUnsafe(arg.GetBool().NativeValue() ? "true" + : "false"); + case ValueKind::kDouble: + return precision_preserving_double_format + ? FormatDouble(arg.GetDouble().NativeValue(), context) + : LegacyFormatDouble(arg.GetDouble().NativeValue(), context); + case ValueKind::kInt: + return StringValue::From(absl::StrCat(arg.GetInt().NativeValue()), + context.arena()); + case ValueKind::kUint: + return StringValue::From(absl::StrCat(arg.GetUint().NativeValue()), + context.arena()); + case ValueKind::kDuration: { + auto encode = EncodeDurationToJson(arg.GetDuration().ToDuration()); + if (!encode.ok()) { + return ErrorValue::From(encode.status(), context.arena()); + } + return StringValue::From(*encode, context.arena()); + } + case ValueKind::kTimestamp: { + auto encode = EncodeTimestampToJson(arg.GetTimestamp().ToTime()); + if (!encode.ok()) { + return ErrorValue::From(encode.status(), context.arena()); + } + return StringValue::From(*encode, context.arena()); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to string"), + context.arena()); + } } -absl::Status RegisterDoubleConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions&) { - // double -> double - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDouble, [](double v) { return v; }, registry); - CEL_RETURN_IF_ERROR(status); - - // int -> double - status = UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDouble, [](int64_t v) { return static_cast(v); }, - registry); - CEL_RETURN_IF_ERROR(status); - - // string -> double - status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDouble, - [](const StringValue& s, - const Function::InvokeContext& context) -> Value { - double result; - if (absl::SimpleAtod(s.ToString(), &result)) { - return DoubleValue(result); - } else { - return ErrorValue::From( - absl::InvalidArgumentError("cannot convert string to double"), - context.arena()); - } - }, - registry); - CEL_RETURN_IF_ERROR(status); - - // uint -> double - return UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDouble, [](uint64_t v) { return static_cast(v); }, - registry); +Value ConvertToDuration(const Value& arg, + const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kDuration: + return UnsafeDurationValue(arg.GetDuration().ToDuration()); + case ValueKind::kString: { + absl::Duration d; + if (!absl::ParseDuration(arg.GetString().ToString(), &d)) { + return ErrorValue::From( + absl::InvalidArgumentError("String to Duration conversion failed"), + context.arena()); + } + auto status = internal::ValidateDuration(d); + if (!status.ok()) { + return ErrorValue::From(std::move(status), context.arena()); + } + return DurationValue(d); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to duration"), + context.arena()); + } } -Value CreateDurationFromString(const StringValue& dur_str, - const Function::InvokeContext& context) { - absl::Duration d; - if (!absl::ParseDuration(dur_str.ToString(), &d)) { - return ErrorValue::From( - absl::InvalidArgumentError("String to Duration conversion failed"), - context.arena()); +Value ConvertToTimestamp(const Value& arg, + bool enable_timestamp_duration_overflow_errors, + const Function::InvokeContext& context) { + switch (arg.kind()) { + case ValueKind::kTimestamp: + return UnsafeTimestampValue(arg.GetTimestamp().ToTime()); + case ValueKind::kInt: { + absl::Time ts = absl::FromUnixSeconds(arg.GetInt().NativeValue()); + if (enable_timestamp_duration_overflow_errors) { + if (ts < MinTimestamp() || ts > MaxTimestamp()) { + return ErrorValue::From(absl::OutOfRangeError("timestamp overflow"), + context.arena()); + } + } + return UnsafeTimestampValue(ts); + } + case ValueKind::kString: { + absl::Time ts; + if (!absl::ParseTime(absl::RFC3339_full, arg.GetString().ToString(), &ts, + nullptr)) { + return ErrorValue::From( + absl::InvalidArgumentError("String to Timestamp conversion failed"), + context.arena()); + } + if (enable_timestamp_duration_overflow_errors) { + if (ts < MinTimestamp() || ts > MaxTimestamp()) { + return ErrorValue::From(absl::OutOfRangeError("timestamp overflow"), + context.arena()); + } + } + return UnsafeTimestampValue(ts); + } + default: + return ErrorValue::From( + absl::InvalidArgumentError("cannot convert value to timestamp"), + context.arena()); } +} - auto status = internal::ValidateDuration(d); - if (!status.ok()) { - return ErrorValue::From(std::move(status), context.arena()); +class TypeConversionFunction final : public Function { + public: + enum class Target { + kBool, + kInt, + kUint, + kDouble, + kString, + kBytes, + kDuration, + kTimestamp, + kDyn, + kType, + }; + + TypeConversionFunction(Target target, + bool precision_preserving_double_format = true, + bool enable_timestamp_duration_overflow_errors = false) + : target_(target), + precision_preserving_double_format_(precision_preserving_double_format), + enable_timestamp_duration_overflow_errors_( + enable_timestamp_duration_overflow_errors) {} + + absl::StatusOr Invoke(absl::Span args, + const InvokeContext& context) const override { + if (args.size() != 1) { + return absl::InvalidArgumentError( + "unexpected number of arguments for unary function"); + } + const Value& arg = args[0]; + switch (target_) { + case Target::kBool: + return ConvertToBool(arg, context); + case Target::kInt: + return ConvertToInt(arg, context); + case Target::kUint: + return ConvertToUint(arg, context); + case Target::kDouble: + return ConvertToDouble(arg, context); + case Target::kString: + return ConvertToString(arg, precision_preserving_double_format_, + context); + case Target::kBytes: + if (arg.IsBytes()) { + return arg; + } + if (arg.IsString()) { + return BytesValue(arg.GetString()); + } + return absl::InvalidArgumentError("cannot convert value to bytes"); + case Target::kDuration: + return ConvertToDuration(arg, context); + case Target::kTimestamp: + return ConvertToTimestamp( + arg, enable_timestamp_duration_overflow_errors_, context); + case Target::kDyn: + return arg; + case Target::kType: + return TypeValue(arg.GetRuntimeType()); + } } - return DurationValue(d); -} -absl::Status RegisterTimeConversionFunctions(FunctionRegistry& registry, - const RuntimeOptions& options) { - // duration() conversion from string. - CEL_RETURN_IF_ERROR( - (UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDuration, CreateDurationFromString, registry))); - - bool enable_timestamp_duration_overflow_errors = - options.enable_timestamp_duration_overflow_errors; - - // timestamp conversion from int. - CEL_RETURN_IF_ERROR( - (UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kTimestamp, - [=](int64_t epoch_seconds, - const Function::InvokeContext& context) -> Value { - absl::Time ts = absl::FromUnixSeconds(epoch_seconds); - if (enable_timestamp_duration_overflow_errors) { - if (ts < MinTimestamp() || ts > MaxTimestamp()) { - return ErrorValue::From( - absl::OutOfRangeError("timestamp overflow"), - context.arena()); - } - } - return UnsafeTimestampValue(ts); - }, - registry))); - - // timestamp -> timestamp - CEL_RETURN_IF_ERROR( - (UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kTimestamp, - [](absl::Time value) -> Value { return UnsafeTimestampValue(value); }, - registry))); - - // duration -> duration - CEL_RETURN_IF_ERROR( - (UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDuration, - [](absl::Duration value) -> Value { - return UnsafeDurationValue(value); - }, - registry))); - - // timestamp() conversion from string. - return UnaryFunctionAdapter:: - RegisterGlobalOverload( - cel::builtin::kTimestamp, - [=](const StringValue& time_str, - const Function::InvokeContext& context) -> Value { - absl::Time ts; - if (!absl::ParseTime(absl::RFC3339_full, time_str.ToString(), &ts, - nullptr)) { - return ErrorValue::From( - absl::InvalidArgumentError( - "String to Timestamp conversion failed"), - context.arena()); - } - if (enable_timestamp_duration_overflow_errors) { - if (ts < MinTimestamp() || ts > MaxTimestamp()) { - return ErrorValue::From( - absl::OutOfRangeError("timestamp overflow"), - context.arena()); - } - } - return UnsafeTimestampValue(ts); - }, - registry); + private: + Target target_; + bool precision_preserving_double_format_; + bool enable_timestamp_duration_overflow_errors_; +}; + +absl::Status RegisterConversionOverloads( + FunctionRegistry& registry, absl::string_view name, + TypeConversionFunction::Target target, absl::Span arg_kinds, + bool precision_preserving_double_format = true, + bool enable_timestamp_duration_overflow_errors = false) { + for (Kind kind : arg_kinds) { + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(name, /*is_receiver_style=*/false, {kind}), + std::make_unique( + target, precision_preserving_double_format, + enable_timestamp_duration_overflow_errors))); + } + return absl::OkStatus(); } } // namespace absl::Status RegisterTypeConversionFunctions(FunctionRegistry& registry, const RuntimeOptions& options) { - CEL_RETURN_IF_ERROR(RegisterBoolConversionFunctions(registry, options)); - - CEL_RETURN_IF_ERROR(RegisterBytesConversionFunctions(registry, options)); - - CEL_RETURN_IF_ERROR(RegisterDoubleConversionFunctions(registry, options)); - - CEL_RETURN_IF_ERROR(RegisterIntConversionFunctions(registry, options)); - - CEL_RETURN_IF_ERROR(RegisterStringConversionFunctions(registry, options)); - - CEL_RETURN_IF_ERROR(RegisterUintConversionFunctions(registry, options)); + static constexpr Kind kBoolKinds[] = {Kind::kBool, Kind::kString}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kBool, TypeConversionFunction::Target::kBool, + kBoolKinds)); + + static constexpr Kind kBytesKinds[] = {Kind::kBytes, Kind::kString}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kBytes, TypeConversionFunction::Target::kBytes, + kBytesKinds)); + + static constexpr Kind kDoubleKinds[] = {Kind::kDouble, Kind::kInt, + Kind::kString, Kind::kUint}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kDouble, TypeConversionFunction::Target::kDouble, + kDoubleKinds)); + + static constexpr Kind kIntKinds[] = {Kind::kBool, Kind::kDouble, + Kind::kInt, Kind::kString, + Kind::kTimestamp, Kind::kUint}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kInt, TypeConversionFunction::Target::kInt, + kIntKinds)); + + if (options.enable_string_conversion) { + static constexpr Kind kStringKinds[] = { + Kind::kBytes, Kind::kBool, Kind::kDouble, Kind::kInt, + Kind::kString, Kind::kUint, Kind::kDuration, Kind::kTimestamp}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kString, + TypeConversionFunction::Target::kString, kStringKinds, + options.enable_precision_preserving_double_format)); + } - CEL_RETURN_IF_ERROR(RegisterTimeConversionFunctions(registry, options)); + static constexpr Kind kUintKinds[] = {Kind::kDouble, Kind::kInt, + Kind::kString, Kind::kUint}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kUint, TypeConversionFunction::Target::kUint, + kUintKinds)); + + static constexpr Kind kDurationKinds[] = {Kind::kString, Kind::kDuration}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kDuration, + TypeConversionFunction::Target::kDuration, kDurationKinds)); + + static constexpr Kind kTimestampKinds[] = {Kind::kInt, Kind::kTimestamp, + Kind::kString}; + CEL_RETURN_IF_ERROR(RegisterConversionOverloads( + registry, cel::builtin::kTimestamp, + TypeConversionFunction::Target::kTimestamp, kTimestampKinds, + /*precision_preserving_double_format=*/true, + options.enable_timestamp_duration_overflow_errors)); // dyn() identity function. // TODO(issues/102): strip dyn() function references at type-check time. - absl::Status status = - UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kDyn, [](const Value& value) -> Value { return value; }, - registry); - CEL_RETURN_IF_ERROR(status); + CEL_RETURN_IF_ERROR(registry.Register( + FunctionDescriptor(cel::builtin::kDyn, /*is_receiver_style=*/false, + {Kind::kAny}), + std::make_unique( + TypeConversionFunction::Target::kDyn))); // type(dyn) -> type - return UnaryFunctionAdapter::RegisterGlobalOverload( - cel::builtin::kType, - [](const Value& value) { return TypeValue(value.GetRuntimeType()); }, - registry); + return registry.Register( + FunctionDescriptor(cel::builtin::kType, /*is_receiver_style=*/false, + {Kind::kAny}), + std::make_unique( + TypeConversionFunction::Target::kType)); } } // namespace cel