Skip to content

Commit

Permalink
Add utilities for creating google.protobuf.Any.type_url
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 549468310
  • Loading branch information
jcking authored and copybara-github committed Jul 20, 2023
1 parent 4582906 commit 9c4b0a5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ cc_library(
],
)

cc_test(
name = "any_test",
srcs = ["any_test.cc"],
deps = [
":any",
"//internal:testing",
],
)

cc_library(
name = "json",
srcs = ["json.cc"],
Expand Down
14 changes: 14 additions & 0 deletions common/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include <utility>

#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"

namespace cel {

Expand Down Expand Up @@ -51,6 +53,18 @@ inline Any MakeAny(std::string type_url, const std::string& value) {
return MakeAny(std::move(type_url), absl::Cord(value));
}

inline constexpr absl::string_view kTypeGoogleApisComPrefix =
"type.googleapis.com/";

inline std::string MakeTypeUrlWithPrefix(absl::string_view prefix,
absl::string_view type_name) {
return absl::StrCat(absl::StripSuffix(prefix, "/"), "/", type_name);
}

inline std::string MakeTypeUrl(absl::string_view type_name) {
return MakeTypeUrlWithPrefix(kTypeGoogleApisComPrefix, type_name);
}

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_ANY_H_
32 changes: 32 additions & 0 deletions common/any_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 "common/any.h"

#include "internal/testing.h"

namespace cel {
namespace {

TEST(MakeTypeUrlWithPrefix, Basic) {
EXPECT_EQ(MakeTypeUrlWithPrefix("foo", "bar.Baz"), "foo/bar.Baz");
EXPECT_EQ(MakeTypeUrlWithPrefix("foo/", "bar.Baz"), "foo/bar.Baz");
}

TEST(MakeTypeUrl, Basic) {
EXPECT_EQ(MakeTypeUrl("bar.Baz"), "type.googleapis.com/bar.Baz");
}

} // namespace
} // namespace cel

0 comments on commit 9c4b0a5

Please # to comment.