/proc/self/cwd/common/any.h
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_ANY_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_COMMON_ANY_H_ |
17 | | |
18 | | #include <string> |
19 | | |
20 | | #include "google/protobuf/any.pb.h" |
21 | | #include "absl/base/attributes.h" |
22 | | #include "absl/base/nullability.h" |
23 | | #include "absl/strings/cord.h" |
24 | | #include "absl/strings/str_cat.h" |
25 | | #include "absl/strings/string_view.h" |
26 | | #include "absl/strings/strip.h" |
27 | | |
28 | | namespace cel { |
29 | | |
30 | | inline google::protobuf::Any MakeAny(absl::string_view type_url, |
31 | 0 | const absl::Cord& value) { |
32 | 0 | google::protobuf::Any any; |
33 | 0 | any.set_type_url(type_url); |
34 | 0 | any.set_value(static_cast<std::string>(value)); |
35 | 0 | return any; |
36 | 0 | } |
37 | | |
38 | | inline google::protobuf::Any MakeAny(absl::string_view type_url, |
39 | 0 | absl::string_view value) { |
40 | 0 | google::protobuf::Any any; |
41 | 0 | any.set_type_url(type_url); |
42 | 0 | any.set_value(value); |
43 | 0 | return any; |
44 | 0 | } |
45 | | |
46 | 0 | inline absl::Cord GetAnyValueAsCord(const google::protobuf::Any& any) { |
47 | 0 | return absl::Cord(any.value()); |
48 | 0 | } |
49 | | |
50 | 0 | inline std::string GetAnyValueAsString(const google::protobuf::Any& any) { |
51 | 0 | return std::string(any.value()); |
52 | 0 | } |
53 | | |
54 | | inline void SetAnyValueFromCord(google::protobuf::Any* absl_nonnull any, |
55 | 0 | const absl::Cord& value) { |
56 | 0 | any->set_value(static_cast<std::string>(value)); |
57 | 0 | } |
58 | | |
59 | | inline absl::string_view GetAnyValueAsStringView( |
60 | | const google::protobuf::Any& any ABSL_ATTRIBUTE_LIFETIME_BOUND, |
61 | 0 | std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
62 | 0 | return absl::string_view(any.value()); |
63 | 0 | } |
64 | | |
65 | | inline constexpr absl::string_view kTypeGoogleApisComPrefix = |
66 | | "type.googleapis.com/"; |
67 | | |
68 | | inline std::string MakeTypeUrlWithPrefix(absl::string_view prefix, |
69 | 0 | absl::string_view type_name) { |
70 | 0 | return absl::StrCat(absl::StripSuffix(prefix, "/"), "/", type_name); |
71 | 0 | } |
72 | | |
73 | 0 | inline std::string MakeTypeUrl(absl::string_view type_name) { |
74 | 0 | return MakeTypeUrlWithPrefix(kTypeGoogleApisComPrefix, type_name); |
75 | 0 | } |
76 | | |
77 | | bool ParseTypeUrl(absl::string_view type_url, |
78 | | absl::string_view* absl_nullable prefix, |
79 | | absl::string_view* absl_nullable type_name); |
80 | | inline bool ParseTypeUrl(absl::string_view type_url, |
81 | 0 | absl::string_view* absl_nullable type_name) { |
82 | 0 | return ParseTypeUrl(type_url, nullptr, type_name); |
83 | 0 | } |
84 | 0 | inline bool ParseTypeUrl(absl::string_view type_url) { |
85 | 0 | return ParseTypeUrl(type_url, nullptr); |
86 | 0 | } |
87 | | |
88 | | } // namespace cel |
89 | | |
90 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_ANY_H_ |