-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement partial support for setting singular
google.protobuf.Any
…
…fields in `ProtoStructValueBuilder` PiperOrigin-RevId: 540957263
- Loading branch information
1 parent
0d23085
commit 98fbc3b
Showing
9 changed files
with
436 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "extensions/protobuf/internal/any.h" | ||
|
||
#include <string> | ||
|
||
#include "google/protobuf/any.pb.h" | ||
#include "absl/base/optimization.h" | ||
#include "absl/log/absl_check.h" | ||
#include "absl/status/status.h" | ||
#include "absl/strings/str_cat.h" | ||
#include "internal/casts.h" | ||
|
||
namespace cel::extensions::protobuf_internal { | ||
|
||
absl::Status SetAny(google::protobuf::Message& message, absl::string_view type_url, | ||
const absl::Cord& value) { | ||
ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Any"); | ||
const auto* desc = message.GetDescriptor(); | ||
if (ABSL_PREDICT_FALSE(desc == nullptr)) { | ||
return absl::InternalError( | ||
absl::StrCat(message.GetTypeName(), " missing descriptor")); | ||
} | ||
if (ABSL_PREDICT_TRUE(desc == google::protobuf::Any::descriptor())) { | ||
auto& any = cel::internal::down_cast<google::protobuf::Any&>(message); | ||
any.set_type_url(type_url); | ||
any.set_value(std::string(value)); | ||
return absl::OkStatus(); | ||
} | ||
const auto* reflect = message.GetReflection(); | ||
if (ABSL_PREDICT_FALSE(reflect == nullptr)) { | ||
return absl::InternalError( | ||
absl::StrCat(message.GetTypeName(), " missing reflection")); | ||
} | ||
const auto* type_url_field = | ||
desc->FindFieldByNumber(google::protobuf::Any::kTypeUrlFieldNumber); | ||
if (ABSL_PREDICT_FALSE(type_url_field == nullptr)) { | ||
return absl::InternalError(absl::StrCat( | ||
message.GetTypeName(), " missing type_url field descriptor")); | ||
} | ||
if (ABSL_PREDICT_FALSE(type_url_field->cpp_type() != | ||
google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { | ||
return absl::InternalError(absl::StrCat( | ||
message.GetTypeName(), " has unexpected type_url field type: ", | ||
type_url_field->cpp_type_name())); | ||
} | ||
const auto* value_field = | ||
desc->FindFieldByNumber(google::protobuf::Any::kValueFieldNumber); | ||
if (ABSL_PREDICT_FALSE(value_field == nullptr)) { | ||
return absl::InternalError( | ||
absl::StrCat(message.GetTypeName(), " missing value field descriptor")); | ||
} | ||
if (ABSL_PREDICT_FALSE(value_field->cpp_type() != | ||
google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { | ||
return absl::InternalError(absl::StrCat( | ||
message.GetTypeName(), | ||
" has unexpected value field type: ", value_field->cpp_type_name())); | ||
} | ||
reflect->SetString(&message, type_url_field, std::string(type_url)); | ||
reflect->SetString(&message, value_field, value); | ||
return absl::OkStatus(); | ||
} | ||
|
||
} // namespace cel::extensions::protobuf_internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_H_ | ||
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_H_ | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/cord.h" | ||
#include "absl/strings/string_view.h" | ||
#include "google/protobuf/message.h" | ||
|
||
namespace cel::extensions::protobuf_internal { | ||
|
||
absl::Status SetAny(google::protobuf::Message& message, absl::string_view type_url, | ||
const absl::Cord& value); | ||
|
||
} // namespace cel::extensions::protobuf_internal | ||
|
||
#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "extensions/protobuf/internal/any.h" | ||
|
||
#include <memory> | ||
|
||
#include "google/protobuf/any.pb.h" | ||
#include "google/protobuf/descriptor.pb.h" | ||
#include "absl/memory/memory.h" | ||
#include "absl/strings/cord.h" | ||
#include "internal/testing.h" | ||
#include "google/protobuf/descriptor.h" | ||
#include "google/protobuf/descriptor_database.h" | ||
#include "google/protobuf/dynamic_message.h" | ||
|
||
namespace cel::extensions::protobuf_internal { | ||
namespace { | ||
|
||
TEST(Any, GeneratedToProto) { | ||
google::protobuf::Any proto; | ||
ASSERT_OK(SetAny(proto, "type.googleapis.com/foo.Bar", absl::Cord("baz"))); | ||
EXPECT_EQ(proto.type_url(), "type.googleapis.com/foo.Bar"); | ||
EXPECT_EQ(proto.value(), "baz"); | ||
} | ||
|
||
TEST(Any, CustomToProto) { | ||
google::protobuf::SimpleDescriptorDatabase database; | ||
{ | ||
google::protobuf::FileDescriptorProto fd; | ||
google::protobuf::Any::descriptor()->file()->CopyTo(&fd); | ||
ASSERT_TRUE(database.Add(fd)); | ||
} | ||
google::protobuf::DescriptorPool pool(&database); | ||
pool.AllowUnknownDependencies(); | ||
google::protobuf::DynamicMessageFactory factory(&pool); | ||
factory.SetDelegateToGeneratedFactory(false); | ||
std::unique_ptr<google::protobuf::Message> proto = absl::WrapUnique( | ||
factory.GetPrototype(pool.FindMessageTypeByName("google.protobuf.Any")) | ||
->New()); | ||
const auto* descriptor = proto->GetDescriptor(); | ||
const auto* reflection = proto->GetReflection(); | ||
const auto* type_url_field = descriptor->FindFieldByName("type_url"); | ||
ASSERT_NE(type_url_field, nullptr); | ||
const auto* value_field = descriptor->FindFieldByName("value"); | ||
ASSERT_NE(value_field, nullptr); | ||
|
||
ASSERT_OK(SetAny(*proto, "type.googleapis.com/foo.Bar", absl::Cord("baz"))); | ||
|
||
EXPECT_EQ(reflection->GetString(*proto, type_url_field), | ||
"type.googleapis.com/foo.Bar"); | ||
EXPECT_EQ(reflection->GetString(*proto, value_field), "baz"); | ||
} | ||
|
||
} // namespace | ||
} // namespace cel::extensions::protobuf_internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.