/proc/self/cwd/base/attribute.h
Line | Count | Source |
1 | | // Copyright 2022 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_BASE_ATTRIBUTE_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <cstring> |
21 | | #include <memory> |
22 | | #include <optional> |
23 | | #include <string> |
24 | | #include <utility> |
25 | | #include <variant> |
26 | | #include <vector> |
27 | | |
28 | | #include "absl/base/attributes.h" |
29 | | #include "absl/base/macros.h" |
30 | | #include "absl/base/nullability.h" |
31 | | #include "absl/functional/overload.h" |
32 | | #include "absl/log/absl_check.h" |
33 | | #include "absl/status/statusor.h" |
34 | | #include "absl/strings/string_view.h" |
35 | | #include "absl/types/compare.h" |
36 | | #include "absl/types/optional_ref.h" |
37 | | #include "absl/types/span.h" |
38 | | #include "base/kind.h" |
39 | | |
40 | | namespace cel { |
41 | | |
42 | | namespace common_internal { |
43 | | struct WildcardType {}; |
44 | | // Important. The types in the variants must align with each other and the order |
45 | | // matters. It must be int < uint < string < bool. If you change it here in the |
46 | | // header you must change the logic in the source. |
47 | | using AttributeQualifierVariant = |
48 | | std::variant<std::monostate, int64_t, uint64_t, std::string, bool>; |
49 | | using AttributeQualifierPatternVariant = |
50 | | std::variant<std::monostate, int64_t, uint64_t, std::string, bool, |
51 | | WildcardType>; |
52 | | using AttributeQualifierViewVariant = |
53 | | std::variant<std::monostate, int64_t, uint64_t, absl::string_view, bool>; |
54 | | } // namespace common_internal |
55 | | |
56 | | class AttributeQualifier; |
57 | | class AttributeQualifierPattern; |
58 | | class Attribute; |
59 | | class AttributePattern; |
60 | | class AttributeQualifierView; |
61 | | |
62 | | namespace common_internal { |
63 | | [[nodiscard]] |
64 | | const AttributeQualifierVariant& AsVariant( |
65 | | const AttributeQualifier& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND); |
66 | | [[nodiscard]] |
67 | | AttributeQualifierVariant&& AsVariant( |
68 | | AttributeQualifier&& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND); |
69 | | [[nodiscard]] |
70 | | const AttributeQualifierPatternVariant& AsVariant( |
71 | | const AttributeQualifierPattern& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND); |
72 | | [[nodiscard]] |
73 | | AttributeQualifierPatternVariant&& AsVariant( |
74 | | AttributeQualifierPattern&& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND); |
75 | | [[nodiscard]] |
76 | | const AttributeQualifierViewVariant& AsVariant( |
77 | | const AttributeQualifierView& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND); |
78 | | [[nodiscard]] |
79 | | AttributeQualifierViewVariant&& AsVariant( |
80 | | AttributeQualifierView&& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND); |
81 | | } // namespace common_internal |
82 | | |
83 | | // AttributeQualifier represents a segment in the |
84 | | // attribute resolution path. A segment can be qualified by values of |
85 | | // following types: string/int64_t/uint64_t/bool. |
86 | | class AttributeQualifier { |
87 | | public: |
88 | 0 | static AttributeQualifier OfInt(int64_t value) { |
89 | 0 | return AttributeQualifier(std::in_place_type<int64_t>, std::move(value)); |
90 | 0 | } |
91 | | |
92 | 0 | static AttributeQualifier OfUint(uint64_t value) { |
93 | 0 | return AttributeQualifier(std::in_place_type<uint64_t>, std::move(value)); |
94 | 0 | } |
95 | | |
96 | 0 | static AttributeQualifier OfString(const char* value) { |
97 | 0 | return OfString(absl::string_view(value)); |
98 | 0 | } |
99 | | |
100 | 0 | static AttributeQualifier OfString(std::string value) { |
101 | 0 | return AttributeQualifier(std::in_place_type<std::string>, |
102 | 0 | std::move(value)); |
103 | 0 | } |
104 | | |
105 | 0 | static AttributeQualifier OfString(absl::string_view value) { |
106 | 0 | return AttributeQualifier(std::in_place_type<std::string>, |
107 | 0 | std::string(value)); |
108 | 0 | } |
109 | | |
110 | 0 | static AttributeQualifier OfBool(bool value) { |
111 | 0 | return AttributeQualifier(std::in_place_type<bool>, std::move(value)); |
112 | 0 | } |
113 | | |
114 | 0 | AttributeQualifier() = default; |
115 | 0 | AttributeQualifier(const AttributeQualifier&) = default; |
116 | 0 | AttributeQualifier(AttributeQualifier&&) = default; |
117 | | AttributeQualifier& operator=(const AttributeQualifier&) = default; |
118 | 0 | AttributeQualifier& operator=(AttributeQualifier&&) = default; |
119 | | |
120 | | explicit AttributeQualifier(const AttributeQualifierView& other); |
121 | | |
122 | | Kind kind() const; |
123 | | |
124 | | ABSL_DEPRECATE_AND_INLINE() |
125 | 0 | std::optional<int64_t> GetInt64Key() const { return AsInt(); } |
126 | | |
127 | | ABSL_DEPRECATE_AND_INLINE() |
128 | 0 | std::optional<uint64_t> GetUint64Key() const { return AsUint(); } |
129 | | |
130 | | ABSL_DEPRECATED("Use AsString") |
131 | 0 | std::optional<absl::string_view> GetStringKey() const { |
132 | 0 | if (auto string = AsString(); string.has_value()) { |
133 | 0 | return *string; |
134 | 0 | } |
135 | 0 | return std::nullopt; |
136 | 0 | } |
137 | | |
138 | | ABSL_DEPRECATE_AND_INLINE() |
139 | 0 | std::optional<bool> GetBoolKey() const { return AsBool(); } |
140 | | |
141 | 0 | explicit operator bool() const { |
142 | 0 | return !std::holds_alternative<std::monostate>(value_); |
143 | 0 | } |
144 | | |
145 | | [[nodiscard]] |
146 | 0 | bool IsBool() const { |
147 | 0 | return std::holds_alternative<bool>(value_); |
148 | 0 | } |
149 | | |
150 | | [[nodiscard]] |
151 | 0 | bool IsInt() const { |
152 | 0 | return std::holds_alternative<int64_t>(value_); |
153 | 0 | } |
154 | | |
155 | | [[nodiscard]] |
156 | 0 | bool IsUint() const { |
157 | 0 | return std::holds_alternative<uint64_t>(value_); |
158 | 0 | } |
159 | | |
160 | | [[nodiscard]] |
161 | 0 | bool IsString() const { |
162 | 0 | return std::holds_alternative<std::string>(value_); |
163 | 0 | } |
164 | | |
165 | | [[nodiscard]] |
166 | 0 | bool GetBool() const { |
167 | 0 | ABSL_DCHECK(IsBool()); |
168 | 0 | return std::get<bool>(value_); |
169 | 0 | } |
170 | | |
171 | | [[nodiscard]] |
172 | 0 | int64_t GetInt() const { |
173 | 0 | ABSL_DCHECK(IsInt()); |
174 | 0 | return std::get<int64_t>(value_); |
175 | 0 | } |
176 | | |
177 | | [[nodiscard]] |
178 | 0 | uint64_t GetUint() const { |
179 | 0 | ABSL_DCHECK(IsUint()); |
180 | 0 | return std::get<uint64_t>(value_); |
181 | 0 | } |
182 | | |
183 | | [[nodiscard]] |
184 | 0 | const std::string& GetString() const { |
185 | 0 | ABSL_DCHECK(IsString()); |
186 | 0 | return std::get<std::string>(value_); |
187 | 0 | } |
188 | | |
189 | | [[nodiscard]] |
190 | 0 | std::optional<bool> AsBool() const { |
191 | 0 | if (const auto* value = std::get_if<bool>(&value_); value != nullptr) { |
192 | 0 | return *value; |
193 | 0 | } |
194 | 0 | return std::nullopt; |
195 | 0 | } |
196 | | |
197 | | [[nodiscard]] |
198 | 0 | std::optional<int64_t> AsInt() const { |
199 | 0 | if (const auto* value = std::get_if<int64_t>(&value_); value != nullptr) { |
200 | 0 | return *value; |
201 | 0 | } |
202 | 0 | return std::nullopt; |
203 | 0 | } |
204 | | |
205 | | [[nodiscard]] |
206 | 0 | std::optional<uint64_t> AsUint() const { |
207 | 0 | if (const auto* value = std::get_if<uint64_t>(&value_); value != nullptr) { |
208 | 0 | return *value; |
209 | 0 | } |
210 | 0 | return std::nullopt; |
211 | 0 | } |
212 | | |
213 | | [[nodiscard]] |
214 | 0 | absl::optional_ref<const std::string> AsString() const { |
215 | 0 | if (const auto* value = std::get_if<std::string>(&value_); |
216 | 0 | value != nullptr) { |
217 | 0 | return *value; |
218 | 0 | } |
219 | 0 | return std::nullopt; |
220 | 0 | } |
221 | | |
222 | | void AppendToString(std::string* absl_nonnull out) const; |
223 | | |
224 | | [[nodiscard]] |
225 | 0 | std::string ToString() const { |
226 | 0 | std::string out; |
227 | 0 | AppendToString(&out); |
228 | 0 | return out; |
229 | 0 | } |
230 | | |
231 | | void AppendJoinString(std::string* absl_nonnull out) const; |
232 | | |
233 | | [[nodiscard]] |
234 | 0 | std::string JoinString() const { |
235 | 0 | std::string out; |
236 | 0 | AppendJoinString(&out); |
237 | 0 | return out; |
238 | 0 | } |
239 | | |
240 | | static void AppendJoinString(std::string* absl_nonnull out, |
241 | | absl::Span<const AttributeQualifier> qualifiers); |
242 | | |
243 | | [[nodiscard]] |
244 | | bool IsMatch(const AttributeQualifier& other) const; |
245 | | |
246 | | [[nodiscard]] |
247 | | bool IsMatch(absl::string_view other_key) const; |
248 | | |
249 | | [[nodiscard]] |
250 | | absl::strong_ordering Compare(const AttributeQualifier& other) const; |
251 | | |
252 | | [[nodiscard]] |
253 | | absl::strong_ordering Compare(const AttributeQualifierView& other) const; |
254 | | |
255 | | [[nodiscard]] |
256 | | absl::strong_ordering Compare(const AttributeQualifierPattern& other) const; |
257 | | |
258 | | private: |
259 | | friend const common_internal::AttributeQualifierVariant& |
260 | | common_internal::AsVariant(const AttributeQualifier& qualifier); |
261 | | friend common_internal::AttributeQualifierVariant&& |
262 | | common_internal::AsVariant(AttributeQualifier&& qualifier); |
263 | | |
264 | | template <typename T, typename... Args> |
265 | | explicit AttributeQualifier(std::in_place_type_t<T> in_place_type, |
266 | | Args&&... args) |
267 | 0 | : value_(in_place_type, std::forward<Args>(args)...) {}Unexecuted instantiation: cel::AttributeQualifier::AttributeQualifier<long, long>(std::__1::in_place_type_t<long>, long&&) Unexecuted instantiation: cel::AttributeQualifier::AttributeQualifier<unsigned long, unsigned long>(std::__1::in_place_type_t<unsigned long>, unsigned long&&) Unexecuted instantiation: cel::AttributeQualifier::AttributeQualifier<bool, bool>(std::__1::in_place_type_t<bool>, bool&&) Unexecuted instantiation: cel::AttributeQualifier::AttributeQualifier<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::in_place_type_t<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) |
268 | | |
269 | | using Variant = common_internal::AttributeQualifierVariant; |
270 | | |
271 | | // The previous implementation of Attribute preserved all value |
272 | | // instances, regardless of whether they are supported in this context or not. |
273 | | // We represented unsupported types by using the first alternative and thus |
274 | | // preserve backwards compatibility with the result of `type()` above. |
275 | | Variant value_; |
276 | | }; |
277 | | |
278 | | class AttributeQualifierView { |
279 | | public: |
280 | 0 | static AttributeQualifierView OfInt(int64_t value) { |
281 | 0 | return AttributeQualifierView(std::in_place_type<int64_t>, |
282 | 0 | std::move(value)); |
283 | 0 | } |
284 | | |
285 | 0 | static AttributeQualifierView OfUint(uint64_t value) { |
286 | 0 | return AttributeQualifierView(std::in_place_type<uint64_t>, |
287 | 0 | std::move(value)); |
288 | 0 | } |
289 | | |
290 | 0 | static AttributeQualifierView OfString(const char* value) { |
291 | 0 | return OfString(absl::string_view(value)); |
292 | 0 | } |
293 | | |
294 | 0 | static AttributeQualifierView OfString(absl::string_view value) { |
295 | 0 | return AttributeQualifierView(std::in_place_type<absl::string_view>, |
296 | 0 | std::move(value)); |
297 | 0 | } |
298 | | |
299 | | static AttributeQualifierView OfString(std::string&&) = delete; |
300 | | |
301 | 0 | static AttributeQualifierView OfBool(bool value) { |
302 | 0 | return AttributeQualifierView(std::in_place_type<bool>, std::move(value)); |
303 | 0 | } |
304 | | |
305 | 0 | AttributeQualifierView() = default; |
306 | | AttributeQualifierView(const AttributeQualifierView&) = default; |
307 | | AttributeQualifierView& operator=(const AttributeQualifierView&) = default; |
308 | | |
309 | | // NOLINTNEXTLINE(google-explicit-constructor) |
310 | | AttributeQualifierView( |
311 | | const AttributeQualifier& other ABSL_ATTRIBUTE_LIFETIME_BOUND) |
312 | 0 | : value_(std::visit( |
313 | 0 | absl::Overload( |
314 | 0 | [](std::monostate value) -> Variant { |
315 | 0 | return Variant(std::in_place_type<std::monostate>, value); |
316 | 0 | }, |
317 | 0 | [](bool value) -> Variant { |
318 | 0 | return Variant(std::in_place_type<bool>, value); |
319 | 0 | }, |
320 | 0 | [](int64_t value) -> Variant { |
321 | 0 | return Variant(std::in_place_type<int64_t>, value); |
322 | 0 | }, |
323 | 0 | [](uint64_t value) -> Variant { |
324 | 0 | return Variant(std::in_place_type<uint64_t>, value); |
325 | 0 | }, |
326 | 0 | [](const std::string& value) -> Variant { |
327 | 0 | return Variant(std::in_place_type<absl::string_view>, value); |
328 | 0 | }), |
329 | 0 | common_internal::AsVariant(other))) {} |
330 | | |
331 | | // NOLINTNEXTLINE(google-explicit-constructor) |
332 | | AttributeQualifierView& operator=( |
333 | 0 | const AttributeQualifier& other ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
334 | 0 | return *this = AttributeQualifierView(other); |
335 | 0 | } |
336 | | |
337 | | AttributeQualifierView& operator=(AttributeQualifier&&) = delete; |
338 | | |
339 | 0 | explicit operator bool() const { |
340 | 0 | return !std::holds_alternative<std::monostate>(value_); |
341 | 0 | } |
342 | | |
343 | | ABSL_DEPRECATE_AND_INLINE() |
344 | 0 | std::optional<int64_t> GetInt64Key() const { return AsInt(); } |
345 | | |
346 | | ABSL_DEPRECATE_AND_INLINE() |
347 | 0 | std::optional<uint64_t> GetUint64Key() const { return AsUint(); } |
348 | | |
349 | | ABSL_DEPRECATE_AND_INLINE() |
350 | 0 | std::optional<absl::string_view> GetStringKey() const { return AsString(); } |
351 | | |
352 | | ABSL_DEPRECATE_AND_INLINE() |
353 | 0 | std::optional<bool> GetBoolKey() const { return AsBool(); } |
354 | | |
355 | | [[nodiscard]] |
356 | 0 | bool IsBool() const { |
357 | 0 | return std::holds_alternative<bool>(value_); |
358 | 0 | } |
359 | | |
360 | | [[nodiscard]] |
361 | 0 | bool IsInt() const { |
362 | 0 | return std::holds_alternative<int64_t>(value_); |
363 | 0 | } |
364 | | |
365 | | [[nodiscard]] |
366 | 0 | bool IsUint() const { |
367 | 0 | return std::holds_alternative<uint64_t>(value_); |
368 | 0 | } |
369 | | |
370 | | [[nodiscard]] |
371 | 0 | bool IsString() const { |
372 | 0 | return std::holds_alternative<absl::string_view>(value_); |
373 | 0 | } |
374 | | |
375 | | [[nodiscard]] |
376 | 0 | bool GetBool() const { |
377 | 0 | ABSL_DCHECK(IsBool()); |
378 | 0 | return std::get<bool>(value_); |
379 | 0 | } |
380 | | |
381 | | [[nodiscard]] |
382 | 0 | int64_t GetInt() const { |
383 | 0 | ABSL_DCHECK(IsInt()); |
384 | 0 | return std::get<int64_t>(value_); |
385 | 0 | } |
386 | | |
387 | | [[nodiscard]] |
388 | 0 | uint64_t GetUint() const { |
389 | 0 | ABSL_DCHECK(IsUint()); |
390 | 0 | return std::get<uint64_t>(value_); |
391 | 0 | } |
392 | | |
393 | | [[nodiscard]] |
394 | 0 | absl::string_view GetString() const { |
395 | 0 | ABSL_DCHECK(IsString()); |
396 | 0 | return std::get<absl::string_view>(value_); |
397 | 0 | } |
398 | | |
399 | | [[nodiscard]] |
400 | 0 | std::optional<bool> AsBool() const { |
401 | 0 | if (const auto* value = std::get_if<bool>(&value_); value != nullptr) { |
402 | 0 | return *value; |
403 | 0 | } |
404 | 0 | return std::nullopt; |
405 | 0 | } |
406 | | |
407 | | [[nodiscard]] |
408 | 0 | std::optional<int64_t> AsInt() const { |
409 | 0 | if (const auto* value = std::get_if<int64_t>(&value_); value != nullptr) { |
410 | 0 | return *value; |
411 | 0 | } |
412 | 0 | return std::nullopt; |
413 | 0 | } |
414 | | |
415 | | [[nodiscard]] |
416 | 0 | std::optional<uint64_t> AsUint() const { |
417 | 0 | if (const auto* value = std::get_if<uint64_t>(&value_); value != nullptr) { |
418 | 0 | return *value; |
419 | 0 | } |
420 | 0 | return std::nullopt; |
421 | 0 | } |
422 | | |
423 | | [[nodiscard]] |
424 | 0 | std::optional<absl::string_view> AsString() const { |
425 | 0 | if (const auto* value = std::get_if<absl::string_view>(&value_); |
426 | 0 | value != nullptr) { |
427 | 0 | return *value; |
428 | 0 | } |
429 | 0 | return std::nullopt; |
430 | 0 | } |
431 | | |
432 | | void AppendToString(std::string* absl_nonnull out) const; |
433 | | |
434 | | [[nodiscard]] |
435 | 0 | std::string ToString() const { |
436 | 0 | std::string out; |
437 | 0 | AppendToString(&out); |
438 | 0 | return out; |
439 | 0 | } |
440 | | |
441 | | void AppendJoinString(std::string* absl_nonnull out) const; |
442 | | |
443 | | [[nodiscard]] |
444 | 0 | std::string JoinString() const { |
445 | 0 | std::string out; |
446 | 0 | AppendJoinString(&out); |
447 | 0 | return out; |
448 | 0 | } |
449 | | |
450 | | static void AppendJoinString( |
451 | | std::string* absl_nonnull out, |
452 | | absl::Span<const AttributeQualifierView> qualifiers); |
453 | | |
454 | | [[nodiscard]] |
455 | | absl::strong_ordering Compare(const AttributeQualifier& other) const; |
456 | | |
457 | | [[nodiscard]] |
458 | | absl::strong_ordering Compare(const AttributeQualifierView& other) const; |
459 | | |
460 | | [[nodiscard]] |
461 | | absl::strong_ordering Compare(const AttributeQualifierPattern& other) const; |
462 | | |
463 | | private: |
464 | | friend const common_internal::AttributeQualifierViewVariant& |
465 | | common_internal::AsVariant(const AttributeQualifierView& qualifier); |
466 | | friend common_internal::AttributeQualifierViewVariant&& |
467 | | common_internal::AsVariant(AttributeQualifierView&& qualifier); |
468 | | |
469 | | using Variant = common_internal::AttributeQualifierViewVariant; |
470 | | |
471 | | template <typename T> |
472 | | AttributeQualifierView(std::in_place_type_t<T> in_place_type, T&& value) |
473 | 0 | : value_(in_place_type, std::forward<T>(value)) {}Unexecuted instantiation: cel::AttributeQualifierView::AttributeQualifierView<long>(std::__1::in_place_type_t<long>, long&&) Unexecuted instantiation: cel::AttributeQualifierView::AttributeQualifierView<unsigned long>(std::__1::in_place_type_t<unsigned long>, unsigned long&&) Unexecuted instantiation: cel::AttributeQualifierView::AttributeQualifierView<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::in_place_type_t<std::__1::basic_string_view<char, std::__1::char_traits<char> > >, std::__1::basic_string_view<char, std::__1::char_traits<char> >&&) Unexecuted instantiation: cel::AttributeQualifierView::AttributeQualifierView<bool>(std::__1::in_place_type_t<bool>, bool&&) |
474 | | |
475 | | Variant value_; |
476 | | }; |
477 | | |
478 | | inline AttributeQualifier::AttributeQualifier( |
479 | | const AttributeQualifierView& other) |
480 | 0 | : value_(std::visit( |
481 | 0 | absl::Overload([](std::monostate) -> Variant { return Variant(); }, |
482 | 0 | [](int64_t value) -> Variant { |
483 | 0 | return Variant(std::in_place_type<int64_t>, value); |
484 | 0 | }, |
485 | 0 | [](uint64_t value) -> Variant { |
486 | 0 | return Variant(std::in_place_type<uint64_t>, value); |
487 | 0 | }, |
488 | 0 | [](absl::string_view value) -> Variant { |
489 | 0 | return Variant(std::in_place_type<std::string>, |
490 | 0 | value); |
491 | 0 | }, |
492 | 0 | [](bool value) -> Variant { |
493 | 0 | return Variant(std::in_place_type<bool>, value); |
494 | 0 | }), |
495 | 0 | common_internal::AsVariant(other))) {} |
496 | | |
497 | | // AttributeQualifierPattern matches a segment in |
498 | | // attribute resolution path. AttributeQualifierPattern is capable of |
499 | | // matching path elements of types string/int64/uint64/bool. |
500 | | class AttributeQualifierPattern { |
501 | | public: |
502 | 0 | static AttributeQualifierPattern OfInt(int64_t value) { |
503 | 0 | return AttributeQualifierPattern(std::in_place_type<int64_t>, value); |
504 | 0 | } |
505 | | |
506 | 0 | static AttributeQualifierPattern OfUint(uint64_t value) { |
507 | 0 | return AttributeQualifierPattern(std::in_place_type<uint64_t>, value); |
508 | 0 | } |
509 | | |
510 | 0 | static AttributeQualifierPattern OfString(const char* value) { |
511 | 0 | return OfString(absl::string_view(value)); |
512 | 0 | } |
513 | | |
514 | 0 | static AttributeQualifierPattern OfString(std::string value) { |
515 | 0 | return AttributeQualifierPattern(std::in_place_type<std::string>, |
516 | 0 | std::move(value)); |
517 | 0 | } |
518 | | |
519 | 0 | static AttributeQualifierPattern OfString(absl::string_view value) { |
520 | 0 | return AttributeQualifierPattern(std::in_place_type<std::string>, |
521 | 0 | std::string(value)); |
522 | 0 | } |
523 | | |
524 | 0 | static AttributeQualifierPattern OfBool(bool value) { |
525 | 0 | return AttributeQualifierPattern(std::in_place_type<bool>, value); |
526 | 0 | } |
527 | | |
528 | | ABSL_DEPRECATE_AND_INLINE() |
529 | 0 | static AttributeQualifierPattern CreateWildcard() { return Wildcard(); } |
530 | | |
531 | 0 | static AttributeQualifierPattern Wildcard() { |
532 | 0 | return AttributeQualifierPattern(std::in_place_type<WildcardType>); |
533 | 0 | } |
534 | | |
535 | | AttributeQualifierPattern() = default; |
536 | 0 | AttributeQualifierPattern(const AttributeQualifierPattern&) = default; |
537 | 0 | AttributeQualifierPattern(AttributeQualifierPattern&&) = default; |
538 | | AttributeQualifierPattern& operator=(const AttributeQualifierPattern&) = |
539 | | default; |
540 | | AttributeQualifierPattern& operator=(AttributeQualifierPattern&&) = default; |
541 | | |
542 | | // NOLINTNEXTLINE(google-explicit-constructor) |
543 | | AttributeQualifierPattern(const AttributeQualifier& value) |
544 | 0 | : value_(std::visit( |
545 | 0 | absl::Overload( |
546 | 0 | [](std::monostate value) -> Variant { |
547 | 0 | return Variant(std::in_place_type<std::monostate>, value); |
548 | 0 | }, |
549 | 0 | [](bool value) -> Variant { |
550 | 0 | return Variant(std::in_place_type<bool>, value); |
551 | 0 | }, |
552 | 0 | [](int64_t value) -> Variant { |
553 | 0 | return Variant(std::in_place_type<int64_t>, value); |
554 | 0 | }, |
555 | 0 | [](uint64_t value) -> Variant { |
556 | 0 | return Variant(std::in_place_type<uint64_t>, value); |
557 | 0 | }, |
558 | 0 | [](const std::string& value) -> Variant { |
559 | 0 | return Variant(std::in_place_type<std::string>, value); |
560 | 0 | }), |
561 | 0 | common_internal::AsVariant(value))) {} |
562 | | |
563 | | // NOLINTNEXTLINE(google-explicit-constructor) |
564 | | AttributeQualifierPattern(AttributeQualifier&& value) |
565 | | : value_(std::visit( |
566 | | absl::Overload( |
567 | | [](std::monostate value) -> Variant { |
568 | | return Variant(std::in_place_type<std::monostate>, value); |
569 | | }, |
570 | | [](bool value) -> Variant { |
571 | | return Variant(std::in_place_type<bool>, value); |
572 | | }, |
573 | | [](int64_t value) -> Variant { |
574 | | return Variant(std::in_place_type<int64_t>, value); |
575 | | }, |
576 | | [](uint64_t value) -> Variant { |
577 | | return Variant(std::in_place_type<uint64_t>, value); |
578 | | }, |
579 | | [](std::string&& value) -> Variant { |
580 | | return Variant(std::in_place_type<std::string>, |
581 | | std::move(value)); |
582 | | }), |
583 | 0 | common_internal::AsVariant(std::move(value)))) {} |
584 | | |
585 | 0 | explicit operator bool() const { |
586 | 0 | return !std::holds_alternative<std::monostate>(value_); |
587 | 0 | } |
588 | | |
589 | | [[nodiscard]] |
590 | 0 | bool IsBool() const { |
591 | 0 | return std::holds_alternative<bool>(value_); |
592 | 0 | } |
593 | | |
594 | | [[nodiscard]] |
595 | 0 | bool IsInt() const { |
596 | 0 | return std::holds_alternative<int64_t>(value_); |
597 | 0 | } |
598 | | |
599 | | [[nodiscard]] |
600 | 0 | bool IsUint() const { |
601 | 0 | return std::holds_alternative<uint64_t>(value_); |
602 | 0 | } |
603 | | |
604 | | [[nodiscard]] |
605 | 0 | bool IsString() const { |
606 | 0 | return std::holds_alternative<std::string>(value_); |
607 | 0 | } |
608 | | |
609 | | [[nodiscard]] |
610 | 0 | bool IsWildcard() const { |
611 | 0 | return std::holds_alternative<WildcardType>(value_); |
612 | 0 | } |
613 | | |
614 | | [[nodiscard]] |
615 | 0 | bool GetBool() const { |
616 | 0 | ABSL_DCHECK(IsBool()); |
617 | 0 | return std::get<bool>(value_); |
618 | 0 | } |
619 | | |
620 | | [[nodiscard]] |
621 | 0 | int64_t GetInt() const { |
622 | 0 | ABSL_DCHECK(IsInt()); |
623 | 0 | return std::get<int64_t>(value_); |
624 | 0 | } |
625 | | |
626 | | [[nodiscard]] |
627 | 0 | uint64_t GetUint() const { |
628 | 0 | ABSL_DCHECK(IsUint()); |
629 | 0 | return std::get<uint64_t>(value_); |
630 | 0 | } |
631 | | |
632 | | [[nodiscard]] |
633 | 0 | const std::string& GetString() const { |
634 | 0 | ABSL_DCHECK(IsString()); |
635 | 0 | return std::get<std::string>(value_); |
636 | 0 | } |
637 | | |
638 | | [[nodiscard]] |
639 | 0 | std::optional<bool> AsBool() const { |
640 | 0 | if (const auto* value = std::get_if<bool>(&value_); value != nullptr) { |
641 | 0 | return *value; |
642 | 0 | } |
643 | 0 | return std::nullopt; |
644 | 0 | } |
645 | | |
646 | | [[nodiscard]] |
647 | 0 | std::optional<int64_t> AsInt() const { |
648 | 0 | if (const auto* value = std::get_if<int64_t>(&value_); value != nullptr) { |
649 | 0 | return *value; |
650 | 0 | } |
651 | 0 | return std::nullopt; |
652 | 0 | } |
653 | | |
654 | | [[nodiscard]] |
655 | 0 | std::optional<uint64_t> AsUint() const { |
656 | 0 | if (const auto* value = std::get_if<uint64_t>(&value_); value != nullptr) { |
657 | 0 | return *value; |
658 | 0 | } |
659 | 0 | return std::nullopt; |
660 | 0 | } |
661 | | |
662 | | [[nodiscard]] |
663 | 0 | absl::optional_ref<const std::string> AsString() const { |
664 | 0 | if (const auto* value = std::get_if<std::string>(&value_); |
665 | 0 | value != nullptr) { |
666 | 0 | return *value; |
667 | 0 | } |
668 | 0 | return std::nullopt; |
669 | 0 | } |
670 | | |
671 | | [[nodiscard]] |
672 | 0 | std::optional<AttributeQualifier> ToQualifier() const { |
673 | 0 | return std::visit( |
674 | 0 | absl::Overload( |
675 | 0 | [](std::monostate) -> std::optional<AttributeQualifier> { |
676 | 0 | return AttributeQualifier(); |
677 | 0 | }, |
678 | 0 | [](bool value) -> std::optional<AttributeQualifier> { |
679 | 0 | return AttributeQualifier::OfBool(value); |
680 | 0 | }, |
681 | 0 | [](int64_t value) -> std::optional<AttributeQualifier> { |
682 | 0 | return AttributeQualifier::OfInt(value); |
683 | 0 | }, |
684 | 0 | [](uint64_t value) -> std::optional<AttributeQualifier> { |
685 | 0 | return AttributeQualifier::OfUint(value); |
686 | 0 | }, |
687 | 0 | [](const std::string& value) -> std::optional<AttributeQualifier> { |
688 | 0 | return AttributeQualifier::OfString(value); |
689 | 0 | }, |
690 | 0 | [](common_internal::WildcardType value) |
691 | 0 | -> std::optional<AttributeQualifier> { return std::nullopt; }), |
692 | 0 | value_); |
693 | 0 | } |
694 | | |
695 | | [[nodiscard]] |
696 | | std::optional<AttributeQualifierView> ToQualifierView() const |
697 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
698 | 0 | return std::visit( |
699 | 0 | absl::Overload( |
700 | 0 | [](std::monostate) -> std::optional<AttributeQualifierView> { |
701 | 0 | return AttributeQualifierView(); |
702 | 0 | }, |
703 | 0 | [](bool value) -> std::optional<AttributeQualifierView> { |
704 | 0 | return AttributeQualifierView::OfBool(value); |
705 | 0 | }, |
706 | 0 | [](int64_t value) -> std::optional<AttributeQualifierView> { |
707 | 0 | return AttributeQualifierView::OfInt(value); |
708 | 0 | }, |
709 | 0 | [](uint64_t value) -> std::optional<AttributeQualifierView> { |
710 | 0 | return AttributeQualifierView::OfUint(value); |
711 | 0 | }, |
712 | 0 | [](const std::string& value) |
713 | 0 | -> std::optional<AttributeQualifierView> { |
714 | 0 | return AttributeQualifierView::OfString(value); |
715 | 0 | }, |
716 | 0 | [](common_internal::WildcardType value) |
717 | 0 | -> std::optional<AttributeQualifierView> { |
718 | 0 | return std::nullopt; |
719 | 0 | }), |
720 | 0 | value_); |
721 | 0 | } |
722 | | |
723 | | void AppendToString(std::string* absl_nonnull out) const; |
724 | | |
725 | | [[nodiscard]] |
726 | 0 | std::string ToString() const { |
727 | 0 | std::string out; |
728 | 0 | AppendToString(&out); |
729 | 0 | return out; |
730 | 0 | } |
731 | | |
732 | | void AppendJoinString(std::string* absl_nonnull out) const; |
733 | | |
734 | | [[nodiscard]] |
735 | 0 | std::string JoinString() const { |
736 | 0 | std::string out; |
737 | 0 | AppendJoinString(&out); |
738 | 0 | return out; |
739 | 0 | } |
740 | | |
741 | | static void AppendJoinString( |
742 | | std::string* absl_nonnull out, |
743 | | absl::Span<const AttributeQualifierPattern> qualifiers); |
744 | | |
745 | | [[nodiscard]] |
746 | | bool IsMatch(const AttributeQualifier& qualifier) const; |
747 | | |
748 | | [[nodiscard]] |
749 | | bool IsMatch(absl::string_view other_key) const; |
750 | | |
751 | | [[nodiscard]] |
752 | | absl::strong_ordering Compare(const AttributeQualifier& other) const; |
753 | | |
754 | | [[nodiscard]] |
755 | | absl::strong_ordering Compare(const AttributeQualifierView& other) const; |
756 | | |
757 | | [[nodiscard]] |
758 | | absl::strong_ordering Compare(const AttributeQualifierPattern& other) const; |
759 | | |
760 | | private: |
761 | | friend const common_internal::AttributeQualifierPatternVariant& |
762 | | common_internal::AsVariant(const AttributeQualifierPattern& qualifier); |
763 | | friend common_internal::AttributeQualifierPatternVariant&& |
764 | | common_internal::AsVariant(AttributeQualifierPattern&& qualifier); |
765 | | |
766 | | using Variant = common_internal::AttributeQualifierPatternVariant; |
767 | | using WildcardType = common_internal::WildcardType; |
768 | | |
769 | | template <typename T, typename... Args> |
770 | | explicit AttributeQualifierPattern(std::in_place_type_t<T> in_place_type, |
771 | | Args&&... args) |
772 | 0 | : value_(in_place_type, std::forward<Args>(args)...) {}Unexecuted instantiation: cel::AttributeQualifierPattern::AttributeQualifierPattern<cel::common_internal::WildcardType>(std::__1::in_place_type_t<cel::common_internal::WildcardType>) Unexecuted instantiation: cel::AttributeQualifierPattern::AttributeQualifierPattern<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::in_place_type_t<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) |
773 | | |
774 | | // Qualifier value. If not set, treated as wildcard. |
775 | | common_internal::AttributeQualifierPatternVariant value_; |
776 | | }; |
777 | | |
778 | | [[nodiscard]] |
779 | | bool operator==(const AttributeQualifier& lhs, const AttributeQualifier& rhs); |
780 | | |
781 | | [[nodiscard]] |
782 | | bool operator==(const AttributeQualifierView& lhs, |
783 | | const AttributeQualifierView& rhs); |
784 | | |
785 | | [[nodiscard]] |
786 | | bool operator==(const AttributeQualifierPattern& lhs, |
787 | | const AttributeQualifierPattern& rhs); |
788 | | |
789 | | [[nodiscard]] |
790 | | bool operator==(const AttributeQualifier& lhs, |
791 | | const AttributeQualifierView& rhs); |
792 | | |
793 | | [[nodiscard]] |
794 | | bool operator==(const AttributeQualifier& lhs, |
795 | | const AttributeQualifierPattern& rhs); |
796 | | |
797 | | [[nodiscard]] |
798 | | bool operator==(const AttributeQualifierView& lhs, |
799 | | const AttributeQualifier& rhs); |
800 | | |
801 | | [[nodiscard]] |
802 | | bool operator==(const AttributeQualifierView& lhs, |
803 | | const AttributeQualifierPattern& rhs); |
804 | | |
805 | | [[nodiscard]] |
806 | | bool operator==(const AttributeQualifierPattern& lhs, |
807 | | const AttributeQualifier& rhs); |
808 | | |
809 | | [[nodiscard]] |
810 | | bool operator==(const AttributeQualifierPattern& lhs, |
811 | | const AttributeQualifierView& rhs); |
812 | | |
813 | | #if defined(__cpp_impl_three_way_comparison) && \ |
814 | | defined(__cpp_lib_three_way_comparison) |
815 | | [[nodiscard]] |
816 | | inline absl::strong_ordering operator<=>(const AttributeQualifier& lhs, |
817 | | const AttributeQualifier& rhs) { |
818 | | return lhs.Compare(rhs); |
819 | | } |
820 | | |
821 | | [[nodiscard]] |
822 | | inline absl::strong_ordering operator<=>(const AttributeQualifier& lhs, |
823 | | const AttributeQualifierView& rhs) { |
824 | | return lhs.Compare(rhs); |
825 | | } |
826 | | |
827 | | [[nodiscard]] |
828 | | inline absl::strong_ordering operator<=>(const AttributeQualifier& lhs, |
829 | | const AttributeQualifierPattern& rhs) { |
830 | | return lhs.Compare(rhs); |
831 | | } |
832 | | |
833 | | [[nodiscard]] |
834 | | inline absl::strong_ordering operator<=>(const AttributeQualifierView& lhs, |
835 | | const AttributeQualifier& rhs) { |
836 | | return lhs.Compare(rhs); |
837 | | } |
838 | | |
839 | | [[nodiscard]] |
840 | | inline absl::strong_ordering operator<=>(const AttributeQualifierView& lhs, |
841 | | const AttributeQualifierView& rhs) { |
842 | | return lhs.Compare(rhs); |
843 | | } |
844 | | |
845 | | [[nodiscard]] |
846 | | inline absl::strong_ordering operator<=>(const AttributeQualifierView& lhs, |
847 | | const AttributeQualifierPattern& rhs) { |
848 | | return lhs.Compare(rhs); |
849 | | } |
850 | | |
851 | | [[nodiscard]] |
852 | | inline absl::strong_ordering operator<=>(const AttributeQualifierPattern& lhs, |
853 | | const AttributeQualifier& rhs) { |
854 | | return lhs.Compare(rhs); |
855 | | } |
856 | | |
857 | | [[nodiscard]] |
858 | | inline absl::strong_ordering operator<=>(const AttributeQualifierPattern& lhs, |
859 | | const AttributeQualifierView& rhs) { |
860 | | return lhs.Compare(rhs); |
861 | | } |
862 | | |
863 | | [[nodiscard]] |
864 | | inline absl::strong_ordering operator<=>(const AttributeQualifierPattern& lhs, |
865 | | const AttributeQualifierPattern& rhs) { |
866 | | return lhs.Compare(rhs); |
867 | | } |
868 | | #else |
869 | | [[nodiscard]] |
870 | | inline bool operator!=(const AttributeQualifier& lhs, |
871 | 0 | const AttributeQualifier& rhs) { |
872 | 0 | return !operator==(lhs, rhs); |
873 | 0 | } |
874 | | |
875 | | [[nodiscard]] |
876 | | inline bool operator!=(const AttributeQualifierView& lhs, |
877 | 0 | const AttributeQualifierView& rhs) { |
878 | 0 | return !operator==(lhs, rhs); |
879 | 0 | } |
880 | | |
881 | | [[nodiscard]] |
882 | | inline bool operator!=(const AttributeQualifierPattern& lhs, |
883 | 0 | const AttributeQualifierPattern& rhs) { |
884 | 0 | return !operator==(lhs, rhs); |
885 | 0 | } |
886 | | |
887 | | [[nodiscard]] |
888 | | inline bool operator!=(const AttributeQualifier& lhs, |
889 | 0 | const AttributeQualifierView& rhs) { |
890 | 0 | return !operator==(lhs, rhs); |
891 | 0 | } |
892 | | |
893 | | [[nodiscard]] |
894 | | inline bool operator!=(const AttributeQualifier& lhs, |
895 | 0 | const AttributeQualifierPattern& rhs) { |
896 | 0 | return !operator==(lhs, rhs); |
897 | 0 | } |
898 | | |
899 | | [[nodiscard]] |
900 | | inline bool operator!=(const AttributeQualifierView& lhs, |
901 | 0 | const AttributeQualifier& rhs) { |
902 | 0 | return !operator==(lhs, rhs); |
903 | 0 | } |
904 | | |
905 | | [[nodiscard]] |
906 | | inline bool operator!=(const AttributeQualifierView& lhs, |
907 | 0 | const AttributeQualifierPattern& rhs) { |
908 | 0 | return !operator==(lhs, rhs); |
909 | 0 | } |
910 | | |
911 | | [[nodiscard]] |
912 | | inline bool operator!=(const AttributeQualifierPattern& lhs, |
913 | 0 | const AttributeQualifier& rhs) { |
914 | 0 | return !operator==(lhs, rhs); |
915 | 0 | } |
916 | | |
917 | | [[nodiscard]] |
918 | | inline bool operator!=(const AttributeQualifierPattern& lhs, |
919 | 0 | const AttributeQualifierView& rhs) { |
920 | 0 | return !operator==(lhs, rhs); |
921 | 0 | } |
922 | | |
923 | | [[nodiscard]] |
924 | | inline bool operator<(const AttributeQualifier& lhs, |
925 | 0 | const AttributeQualifier& rhs) { |
926 | 0 | return lhs.Compare(rhs) < 0; |
927 | 0 | } |
928 | | |
929 | | [[nodiscard]] |
930 | | inline bool operator<(const AttributeQualifierView& lhs, |
931 | 0 | const AttributeQualifierView& rhs) { |
932 | 0 | return lhs.Compare(rhs) < 0; |
933 | 0 | } |
934 | | |
935 | | [[nodiscard]] |
936 | | inline bool operator<(const AttributeQualifierPattern& lhs, |
937 | 0 | const AttributeQualifierPattern& rhs) { |
938 | 0 | return lhs.Compare(rhs) < 0; |
939 | 0 | } |
940 | | |
941 | | [[nodiscard]] |
942 | | inline bool operator<(const AttributeQualifier& lhs, |
943 | 0 | const AttributeQualifierView& rhs) { |
944 | 0 | return lhs.Compare(rhs) < 0; |
945 | 0 | } |
946 | | |
947 | | [[nodiscard]] |
948 | | inline bool operator<(const AttributeQualifier& lhs, |
949 | 0 | const AttributeQualifierPattern& rhs) { |
950 | 0 | return lhs.Compare(rhs) < 0; |
951 | 0 | } |
952 | | |
953 | | [[nodiscard]] |
954 | | inline bool operator<(const AttributeQualifierView& lhs, |
955 | 0 | const AttributeQualifier& rhs) { |
956 | 0 | return lhs.Compare(rhs) < 0; |
957 | 0 | } |
958 | | |
959 | | [[nodiscard]] |
960 | | inline bool operator<(const AttributeQualifierView& lhs, |
961 | 0 | const AttributeQualifierPattern& rhs) { |
962 | 0 | return lhs.Compare(rhs) < 0; |
963 | 0 | } |
964 | | |
965 | | [[nodiscard]] |
966 | | inline bool operator<(const AttributeQualifierPattern& lhs, |
967 | 0 | const AttributeQualifier& rhs) { |
968 | 0 | return lhs.Compare(rhs) < 0; |
969 | 0 | } |
970 | | |
971 | | [[nodiscard]] |
972 | | inline bool operator<(const AttributeQualifierPattern& lhs, |
973 | 0 | const AttributeQualifierView& rhs) { |
974 | 0 | return lhs.Compare(rhs) < 0; |
975 | 0 | } |
976 | | #endif |
977 | | |
978 | | namespace common_internal { |
979 | | |
980 | | [[nodiscard]] |
981 | | inline const AttributeQualifierVariant& AsVariant( |
982 | 0 | const AttributeQualifier& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
983 | 0 | return qualifier.value_; |
984 | 0 | } |
985 | | |
986 | | [[nodiscard]] |
987 | | inline AttributeQualifierVariant&& AsVariant( |
988 | 0 | AttributeQualifier&& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
989 | 0 | return std::move(qualifier.value_); |
990 | 0 | } |
991 | | |
992 | | [[nodiscard]] |
993 | | inline const AttributeQualifierPatternVariant& AsVariant( |
994 | 0 | const AttributeQualifierPattern& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
995 | 0 | return qualifier.value_; |
996 | 0 | } |
997 | | |
998 | | [[nodiscard]] |
999 | | inline AttributeQualifierPatternVariant&& AsVariant( |
1000 | 0 | AttributeQualifierPattern&& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
1001 | 0 | return std::move(qualifier.value_); |
1002 | 0 | } |
1003 | | |
1004 | | [[nodiscard]] |
1005 | | inline const AttributeQualifierViewVariant& AsVariant( |
1006 | 0 | const AttributeQualifierView& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
1007 | 0 | return qualifier.value_; |
1008 | 0 | } |
1009 | | |
1010 | | [[nodiscard]] |
1011 | | inline AttributeQualifierViewVariant&& AsVariant( |
1012 | 0 | AttributeQualifierView&& qualifier ABSL_ATTRIBUTE_LIFETIME_BOUND) { |
1013 | 0 | return std::move(qualifier.value_); |
1014 | 0 | } |
1015 | | |
1016 | | } // namespace common_internal |
1017 | | |
1018 | | // Attribute represents resolved attribute path. |
1019 | | class Attribute { |
1020 | | public: |
1021 | | explicit Attribute(std::string variable_name) |
1022 | 0 | : Attribute(std::move(variable_name), {}) {} |
1023 | | |
1024 | | Attribute(std::string variable_name, |
1025 | | std::vector<AttributeQualifier> qualifier_path) |
1026 | 0 | : impl_(std::make_shared<Impl>(std::move(variable_name), |
1027 | 0 | std::move(qualifier_path))) {} |
1028 | | |
1029 | 0 | absl::string_view variable_name() const { return impl_->variable_name; } |
1030 | | |
1031 | 0 | bool has_variable_name() const { return !impl_->variable_name.empty(); } |
1032 | | |
1033 | 0 | absl::Span<const AttributeQualifier> qualifier_path() const { |
1034 | 0 | return impl_->qualifier_path; |
1035 | 0 | } |
1036 | | |
1037 | | absl::StatusOr<std::string> AsString() const; |
1038 | | |
1039 | | [[nodiscard]] |
1040 | | std::string ToString() const; |
1041 | | |
1042 | | [[nodiscard]] |
1043 | | absl::strong_ordering Compare(const Attribute& other) const; |
1044 | | |
1045 | | private: |
1046 | | struct Impl final { |
1047 | | Impl(std::string variable_name, |
1048 | | std::vector<AttributeQualifier> qualifier_path) |
1049 | 0 | : variable_name(std::move(variable_name)), |
1050 | 0 | qualifier_path(std::move(qualifier_path)) {} |
1051 | | |
1052 | | std::string variable_name; |
1053 | | std::vector<AttributeQualifier> qualifier_path; |
1054 | | }; |
1055 | | |
1056 | | std::shared_ptr<const Impl> impl_; |
1057 | | }; |
1058 | | |
1059 | | [[nodiscard]] |
1060 | | bool operator==(const Attribute& lhs, const Attribute& rhs); |
1061 | | |
1062 | | #if defined(__cpp_impl_three_way_comparison) && \ |
1063 | | defined(__cpp_lib_three_way_comparison) |
1064 | | [[nodiscard]] |
1065 | | inline absl::strong_ordering operator<=>(const Attribute& lhs, |
1066 | | const Attribute& rhs) { |
1067 | | return lhs.Compare(rhs); |
1068 | | } |
1069 | | #else |
1070 | | [[nodiscard]] |
1071 | 0 | inline bool operator!=(const Attribute& lhs, const Attribute& rhs) { |
1072 | 0 | return !operator==(lhs, rhs); |
1073 | 0 | } |
1074 | | |
1075 | | [[nodiscard]] |
1076 | 0 | inline bool operator<(const Attribute& lhs, const Attribute& rhs) { |
1077 | 0 | return lhs.Compare(rhs) < 0; |
1078 | 0 | } |
1079 | | #endif |
1080 | | |
1081 | | template <typename S> |
1082 | | void AbslStringify(S& sink, const Attribute& attribute) { |
1083 | | sink.Append(attribute.ToString()); |
1084 | | } |
1085 | | |
1086 | | // AttributePattern is a fully-qualified absolute attribute path pattern. |
1087 | | // Supported segments steps in the path are: |
1088 | | // - field selection; |
1089 | | // - map lookup by key; |
1090 | | // - list access by index. |
1091 | | class AttributePattern { |
1092 | | public: |
1093 | | // MatchType enum specifies how closely pattern is matching the attribute: |
1094 | | enum class MatchType { |
1095 | | NONE, // Pattern does not match attribute itself nor its children |
1096 | | PARTIAL, // Pattern matches an entity nested within attribute; |
1097 | | FULL // Pattern matches an attribute itself. |
1098 | | }; |
1099 | | |
1100 | | explicit AttributePattern( |
1101 | | std::string variable, |
1102 | | std::vector<AttributeQualifierPattern> qualifier_path = {}) |
1103 | 0 | : variable_(std::move(variable)), |
1104 | 0 | qualifier_path_(std::move(qualifier_path)) {} |
1105 | | |
1106 | 0 | absl::string_view variable() const { return variable_; } |
1107 | | |
1108 | 0 | absl::Span<const AttributeQualifierPattern> qualifier_path() const { |
1109 | 0 | return qualifier_path_; |
1110 | 0 | } |
1111 | | |
1112 | | // Matches the pattern to an attribute. |
1113 | | // Distinguishes between no-match, partial match and full match cases. |
1114 | 0 | MatchType IsMatch(const Attribute& attribute) const { |
1115 | 0 | MatchType result = MatchType::NONE; |
1116 | 0 | if (attribute.variable_name() != variable_) { |
1117 | 0 | return result; |
1118 | 0 | } |
1119 | 0 |
|
1120 | 0 | auto max_index = qualifier_path().size(); |
1121 | 0 | result = MatchType::FULL; |
1122 | 0 | if (qualifier_path().size() > attribute.qualifier_path().size()) { |
1123 | 0 | max_index = attribute.qualifier_path().size(); |
1124 | 0 | result = MatchType::PARTIAL; |
1125 | 0 | } |
1126 | 0 |
|
1127 | 0 | for (size_t i = 0; i < max_index; i++) { |
1128 | 0 | if (!(qualifier_path()[i].IsMatch(attribute.qualifier_path()[i]))) { |
1129 | 0 | return MatchType::NONE; |
1130 | 0 | } |
1131 | 0 | } |
1132 | 0 | return result; |
1133 | 0 | } |
1134 | | |
1135 | | [[nodiscard]] |
1136 | | std::string ToString() const; |
1137 | | |
1138 | | private: |
1139 | | std::string variable_; |
1140 | | std::vector<AttributeQualifierPattern> qualifier_path_; |
1141 | | }; |
1142 | | |
1143 | | template <typename S> |
1144 | | void AbslStringify(S& sink, const AttributePattern& pattern) { |
1145 | | sink.Append(pattern.ToString()); |
1146 | | } |
1147 | | |
1148 | | [[nodiscard]] |
1149 | | bool operator==(const AttributePattern& lhs, const AttributePattern& rhs); |
1150 | | |
1151 | | [[nodiscard]] |
1152 | | inline bool operator!=(const AttributePattern& lhs, |
1153 | 0 | const AttributePattern& rhs) { |
1154 | 0 | return !operator==(lhs, rhs); |
1155 | 0 | } |
1156 | | |
1157 | | struct FieldSpecifier { |
1158 | | int64_t number; |
1159 | | std::string name; |
1160 | | }; |
1161 | | |
1162 | | using SelectQualifier = std::variant<FieldSpecifier, AttributeQualifier>; |
1163 | | |
1164 | | } // namespace cel |
1165 | | |
1166 | | #endif // THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_H_ |