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;