-
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.
Protocol buffer
AnyToJsonConverter
implementation
PiperOrigin-RevId: 612491506
- Loading branch information
1 parent
c49f5ce
commit 04fa70d
Showing
5 changed files
with
140 additions
and
29 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
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,56 @@ | ||
// Copyright 2024 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/json.h" | ||
|
||
#include "absl/memory/memory.h" | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/cord.h" | ||
#include "absl/strings/str_cat.h" | ||
#include "absl/strings/string_view.h" | ||
#include "common/any.h" | ||
#include "common/json.h" | ||
#include "extensions/protobuf/internal/json.h" | ||
#include "google/protobuf/descriptor.h" | ||
#include "google/protobuf/message.h" | ||
|
||
namespace cel::extensions { | ||
|
||
absl::StatusOr<Json> ProtoAnyToJsonConverter::ConvertToJson( | ||
absl::string_view type_url, const absl::Cord& value) { | ||
absl::string_view type_name; | ||
if (!ParseTypeUrl(type_url, &type_name)) { | ||
return absl::InvalidArgumentError( | ||
absl::StrCat("invalid type URL: ", type_url)); | ||
} | ||
const auto* descriptor = pool_->FindMessageTypeByName(type_name); | ||
if (descriptor == nullptr) { | ||
return absl::NotFoundError( | ||
absl::StrCat("unable to locate descriptor for `", type_name, "`")); | ||
} | ||
const auto* prototype = factory_->GetPrototype(descriptor); | ||
if (prototype == nullptr) { | ||
return absl::NotFoundError( | ||
absl::StrCat("unable to create prototype for `", type_name, "`")); | ||
} | ||
auto message = absl::WrapUnique(prototype->New()); | ||
if (!message->ParsePartialFromCord(value)) { | ||
return absl::InvalidArgumentError( | ||
absl::StrCat("failed to parse `", type_name, "`")); | ||
} | ||
return protobuf_internal::ProtoMessageToJson(*this, *message); | ||
} | ||
|
||
} // namespace cel::extensions |
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,44 @@ | ||
// Copyright 2024 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_JSON_H_ | ||
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_JSON_H_ | ||
|
||
#include "absl/base/nullability.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/cord.h" | ||
#include "absl/strings/string_view.h" | ||
#include "common/json.h" | ||
#include "google/protobuf/descriptor.h" | ||
#include "google/protobuf/message.h" | ||
|
||
namespace cel::extensions { | ||
|
||
class ProtoAnyToJsonConverter final : public AnyToJsonConverter { | ||
public: | ||
ProtoAnyToJsonConverter(absl::Nonnull<const google::protobuf::DescriptorPool*> pool, | ||
absl::Nonnull<google::protobuf::MessageFactory*> factory) | ||
: pool_(pool), factory_(factory) {} | ||
|
||
absl::StatusOr<Json> ConvertToJson(absl::string_view type_url, | ||
const absl::Cord& value) override; | ||
|
||
private: | ||
const google::protobuf::DescriptorPool* const pool_; | ||
google::protobuf::MessageFactory* const factory_; | ||
}; | ||
|
||
} // namespace cel::extensions | ||
|
||
#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_JSON_H_ |