Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ cc_test(
"@com_google_absl//absl/types:optional",
"@com_google_cel_spec//proto/cel/expr/conformance/proto2:test_all_types_cc_proto",
"@com_google_cel_spec//proto/cel/expr/conformance/proto3:test_all_types_cc_proto",
"@com_google_protobuf//:field_mask_cc_proto",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:struct_cc_proto",
"@com_google_protobuf//:type_cc_proto",
Expand Down
9 changes: 2 additions & 7 deletions common/values/parsed_message_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "base/attribute.h"
#include "common/memory.h"
#include "common/value.h"
#include "common/values/values.h"
#include "extensions/protobuf/internal/qualify.h"
#include "internal/empty_descriptors.h"
#include "internal/json.h"
Expand All @@ -51,8 +52,6 @@ namespace cel {

namespace {

using ::cel::well_known_types::ValueReflection;

template <typename T>
std::enable_if_t<std::is_base_of_v<google::protobuf::Message, T>,
const google::protobuf::Message* absl_nonnull>
Expand Down Expand Up @@ -114,12 +113,8 @@ absl::Status ParsedMessageValue::ConvertToJson(
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);

ValueReflection value_reflection;
CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
google::protobuf::Message* json_object = value_reflection.MutableStructValue(json);

return internal::MessageToJson(*value_, descriptor_pool, message_factory,
json_object);
json);
}

absl::Status ParsedMessageValue::ConvertToJsonObject(
Expand Down
14 changes: 14 additions & 0 deletions common/values/parsed_message_value_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <utility>

#include "google/protobuf/field_mask.pb.h"
#include "google/protobuf/struct.pb.h"
#include "absl/status/status_matchers.h"
#include "absl/strings/cord.h"
Expand Down Expand Up @@ -84,6 +85,19 @@ TEST_F(ParsedMessageValueTest, ConvertToJson) {
R"pb(struct_value: {})pb"));
}

TEST_F(ParsedMessageValueTest, ConvertToJsonFieldMask) {
ParsedMessageValue value =
MakeParsedMessage<google::protobuf::FieldMask>(R"pb(paths: "foo.bar"
paths: "baz")pb");
google::protobuf::Message* json =
DynamicParseTextProto<google::protobuf::Value>(R"pb()pb");
ASSERT_THAT(value.ConvertToJson(descriptor_pool(), message_factory(),
cel::to_address(json)),
IsOk());
EXPECT_THAT(*json, EqualsTextProto<google::protobuf::Value>(
R"pb(string_value: "foo.bar,baz")pb"));
}

TEST_F(ParsedMessageValueTest, Equal) {
MessageValue value = MakeParsedMessage<TestAllTypesProto3>();
EXPECT_THAT(
Expand Down
2 changes: 0 additions & 2 deletions conformance/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ _TESTS_TO_SKIP = [
"namespace/namespace/self_eval_container_lookup,self_eval_container_lookup_unchecked",

# Skip until fixed.
"wrappers/field_mask/to_json",
"wrappers/empty/to_json",
"fields/qualified_identifier_resolution/map_value_repeat_key_heterogeneous",

# Future features for CEL 1.0
Expand Down
4 changes: 4 additions & 0 deletions eval/public/structs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ cc_library(
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -111,9 +112,11 @@ cc_test(
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:any_cc_proto",
"@com_google_protobuf//:duration_cc_proto",
"@com_google_protobuf//:empty_cc_proto",
"@com_google_protobuf//:field_mask_cc_proto",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:struct_cc_proto",
"@com_google_protobuf//:wrappers_cc_proto",
Expand Down Expand Up @@ -218,6 +221,7 @@ cc_test(
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:any_cc_proto",
"@com_google_protobuf//:duration_cc_proto",
"@com_google_protobuf//:empty_cc_proto",
Expand Down
66 changes: 65 additions & 1 deletion eval/public/structs/cel_proto_wrap_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
Expand All @@ -31,6 +32,7 @@
#include "absl/base/optimization.h"
#include "absl/functional/overload.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
Expand All @@ -50,6 +52,7 @@
#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"
#include "google/protobuf/message_lite.h"

Expand Down Expand Up @@ -79,6 +82,8 @@ using google::protobuf::Descriptor;
using google::protobuf::DescriptorPool;
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;
Expand All @@ -98,6 +103,27 @@ static bool IsJSONSafe(uint64_t i) {
return i <= static_cast<uint64_t>(kMaxIntJSON);
}

static std::optional<std::string> 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);
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;
}

// Map implementation wrapping google.protobuf.ListValue
class DynamicList : public CelList {
public:
Expand Down Expand Up @@ -1079,6 +1105,25 @@ google::protobuf::Message* ValueFromValue(google::protobuf::Message* message, co
return message;
}
} break;
case CelValue::Type::kMessage: {
const google::protobuf::Message* message_ptr = value.MessageOrDie();
if (message_ptr->GetDescriptor()->full_name() ==
"google.protobuf.Empty") {
reflection.MutableStructValue(message);
return message;
}
if (message_ptr->GetDescriptor()->full_name() ==
"google.protobuf.FieldMask") {
std::optional<std::string> fm_str =
GetFieldMaskJsonString(*message_ptr);
if (fm_str.has_value()) {
reflection.SetStringValue(message, *fm_str);
return message;
}
return nullptr;
}
return nullptr;
} break;
case CelValue::Type::kNullType:
reflection.SetNullValue(message);
return message;
Expand Down Expand Up @@ -1229,6 +1274,25 @@ bool ValueFromValue(Value* json, const CelValue& value, google::protobuf::Arena*
return ListFromValue(json->mutable_list_value(), value, arena);
case CelValue::Type::kMap:
return StructFromValue(json->mutable_struct_value(), value, arena);
case CelValue::Type::kMessage: {
const google::protobuf::Message* message_ptr = value.MessageOrDie();
if (message_ptr->GetDescriptor()->full_name() ==
"google.protobuf.Empty") {
json->mutable_struct_value();
return true;
}
if (message_ptr->GetDescriptor()->full_name() ==
"google.protobuf.FieldMask") {
std::optional<std::string> fm_str =
GetFieldMaskJsonString(*message_ptr);
if (fm_str.has_value()) {
json->set_string_value(*fm_str);
return true;
}
return false;
}
return false;
}
case CelValue::Type::kNullType:
json->set_null_value(protobuf::NULL_VALUE);
return true;
Expand All @@ -1254,7 +1318,7 @@ google::protobuf::Message* AnyFromValue(const google::protobuf::Message* prototy
case CelValue::Type::kBytes: {
BytesValue v;
type_name = v.GetTypeName();
v.set_value(std::string(value.BytesOrDie().value()));
v.set_value(value.BytesOrDie().value());
payload = v.SerializeAsCord();
} break;
case CelValue::Type::kDouble: {
Expand Down
Loading
Loading