/proc/self/cwd/common/values/unknown_value.cc
Line | Count | Source |
1 | | // Copyright 2024 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 | | #include <algorithm> |
16 | | #include <cstddef> |
17 | | #include <cstdint> |
18 | | #include <string> |
19 | | #include <utility> |
20 | | #include <vector> |
21 | | |
22 | | #include "absl/base/nullability.h" |
23 | | #include "absl/log/absl_check.h" |
24 | | #include "absl/status/status.h" |
25 | | #include "absl/strings/str_cat.h" |
26 | | #include "base/attribute.h" |
27 | | #include "base/attribute_set.h" |
28 | | #include "base/function_result.h" |
29 | | #include "base/function_result_set.h" |
30 | | #include "common/internal/unknowns.h" |
31 | | #include "common/unknown.h" |
32 | | #include "common/value.h" |
33 | | #include "google/protobuf/arena.h" |
34 | | #include "google/protobuf/descriptor.h" |
35 | | #include "google/protobuf/io/zero_copy_stream.h" |
36 | | #include "google/protobuf/message.h" |
37 | | |
38 | | namespace cel { |
39 | | |
40 | | absl::Status UnknownValue::SerializeTo( |
41 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
42 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
43 | 0 | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const { |
44 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
45 | 0 | ABSL_DCHECK(message_factory != nullptr); |
46 | 0 | ABSL_DCHECK(output != nullptr); |
47 | |
|
48 | 0 | return absl::FailedPreconditionError( |
49 | 0 | absl::StrCat(GetTypeName(), " is unserializable")); |
50 | 0 | } |
51 | | |
52 | | absl::Status UnknownValue::ConvertToJson( |
53 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
54 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
55 | 0 | google::protobuf::Message* absl_nonnull json) const { |
56 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
57 | 0 | ABSL_DCHECK(message_factory != nullptr); |
58 | 0 | ABSL_DCHECK(json != nullptr); |
59 | 0 | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
60 | 0 | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
61 | |
|
62 | 0 | return absl::FailedPreconditionError( |
63 | 0 | absl::StrCat(GetTypeName(), " is not convertable to JSON")); |
64 | 0 | } |
65 | | |
66 | | absl::Status UnknownValue::Equal( |
67 | | const Value&, const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
68 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
69 | 0 | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const { |
70 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
71 | 0 | ABSL_DCHECK(message_factory != nullptr); |
72 | 0 | ABSL_DCHECK(arena != nullptr); |
73 | 0 | ABSL_DCHECK(result != nullptr); |
74 | |
|
75 | 0 | *result = FalseValue(); |
76 | 0 | return absl::OkStatus(); |
77 | 0 | } |
78 | | |
79 | | [[nodiscard]] |
80 | 0 | AttributeSet UnknownValue::ToAttributeSet() const { |
81 | 0 | AttributeSet attribute_set; |
82 | 0 | if (rep_.attributes == nullptr || rep_.attributes->size == 0) { |
83 | 0 | return attribute_set; |
84 | 0 | } |
85 | 0 | std::vector<const common_internal::UnknownAttributeNode*> path; |
86 | 0 | path.resize(rep_.attributes->max_depth - 1); |
87 | 0 | for (uint32_t i = 0; i < rep_.attributes->size; ++i) { |
88 | 0 | const common_internal::UnknownAttributeNode* leaf = |
89 | 0 | rep_.attributes->nodes[i]; |
90 | 0 | const size_t num_qualifiers = leaf->depth() - 1; |
91 | 0 | size_t j = num_qualifiers; |
92 | 0 | while (leaf->parent() != rep_.root->Attributes()) { |
93 | 0 | path[--j] = leaf; |
94 | 0 | leaf = leaf->parent(); |
95 | 0 | } |
96 | | // leaf is now the ancestor which is a child of root_, also known as our |
97 | | // variable name. |
98 | 0 | std::vector<AttributeQualifier> qualifiers; |
99 | 0 | qualifiers.reserve(num_qualifiers); |
100 | 0 | for (j = 0; j < num_qualifiers; ++j) { |
101 | 0 | qualifiers.push_back(AttributeQualifier(path[j]->key())); |
102 | 0 | } |
103 | 0 | attribute_set.Add( |
104 | 0 | Attribute(std::string(leaf->key().GetString()), std::move(qualifiers))); |
105 | 0 | } |
106 | 0 | return attribute_set; |
107 | 0 | } |
108 | | |
109 | 0 | FunctionResultSet UnknownValue::ToFunctionResultSet() const { |
110 | 0 | FunctionResultSet function_set; |
111 | 0 | if (rep_.functions == nullptr || rep_.functions->size == 0) { |
112 | 0 | return function_set; |
113 | 0 | } |
114 | 0 | for (size_t i = 0; i < rep_.functions->size; ++i) { |
115 | 0 | function_set.Add(FunctionResult(rep_.functions->nodes[i]->key())); |
116 | 0 | } |
117 | 0 | return function_set; |
118 | 0 | } |
119 | | |
120 | | namespace common_internal { |
121 | | |
122 | | UnknownValue MakeUnknownValue(const Unknown& value, |
123 | 0 | google::protobuf::Arena* absl_nonnull arena) { |
124 | 0 | if (value.unknown_attributes().empty() && |
125 | 0 | value.unknown_function_results().empty()) { |
126 | 0 | return UnknownValue(); |
127 | 0 | } |
128 | 0 | common_internal::UnknownTree tree(arena); |
129 | 0 | common_internal::UnknownRoot* root = tree.Root(); |
130 | 0 | common_internal::UnknownAttributeSet* attributes = nullptr; |
131 | 0 | common_internal::UnknownFunctionSet* functions = nullptr; |
132 | 0 | if (!value.unknown_attributes().empty()) { |
133 | 0 | attributes = common_internal::UnknownAttributeSet::Allocate( |
134 | 0 | value.unknown_attributes().size(), arena); |
135 | 0 | attributes->max_depth = 0; |
136 | 0 | size_t i = 0; |
137 | 0 | for (const auto& attribute : value.unknown_attributes()) { |
138 | 0 | common_internal::UnknownAttributeNode* node = root->Attributes()->Step( |
139 | 0 | &tree, AttributeQualifierView::OfString(attribute.variable_name())); |
140 | 0 | for (const auto& attribute_qualifier : attribute.qualifier_path()) { |
141 | 0 | node = node->Step(&tree, attribute_qualifier); |
142 | 0 | } |
143 | 0 | attributes->max_depth = |
144 | 0 | std::max(attributes->max_depth, static_cast<uint32_t>(node->depth())); |
145 | 0 | attributes->nodes[i++] = node; |
146 | 0 | } |
147 | 0 | std::sort(attributes->nodes, attributes->nodes + attributes->size); |
148 | 0 | } |
149 | 0 | if (!value.unknown_function_results().empty()) { |
150 | 0 | functions = common_internal::UnknownFunctionSet::Allocate( |
151 | 0 | value.unknown_function_results().size(), arena); |
152 | 0 | size_t i = 0; |
153 | 0 | for (const auto& function_result : value.unknown_function_results()) { |
154 | 0 | functions->nodes[i++] = root->SetFunction(&tree, function_result.name()); |
155 | 0 | } |
156 | 0 | std::sort(functions->nodes, functions->nodes + functions->size); |
157 | 0 | } |
158 | 0 | return MakeUnknownValue(root, attributes, functions); |
159 | 0 | } |
160 | | |
161 | | } // namespace common_internal |
162 | | |
163 | | } // namespace cel |