-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathstruct_value.cc
More file actions
237 lines (194 loc) · 8.27 KB
/
Copy pathstruct_value.cc
File metadata and controls
237 lines (194 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2022 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 "base/values/struct_value.h"
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/variant.h"
#include "base/handle.h"
#include "base/internal/data.h"
#include "base/internal/message_wrapper.h"
#include "base/types/struct_type.h"
#include "base/value.h"
#include "internal/rtti.h"
#include "internal/status_macros.h"
namespace cel {
CEL_INTERNAL_VALUE_IMPL(StructValue);
#define CEL_INTERNAL_STRUCT_VALUE_DISPATCH(method, ...) \
base_internal::Metadata::IsStoredInline(*this) \
? static_cast<const base_internal::LegacyStructValue&>(*this).method( \
__VA_ARGS__) \
: static_cast<const base_internal::AbstractStructValue&>(*this).method( \
__VA_ARGS__)
Handle<StructType> StructValue::type() const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(type);
}
size_t StructValue::field_count() const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(field_count);
}
std::string StructValue::DebugString() const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(DebugString);
}
absl::StatusOr<Handle<Value>> StructValue::GetFieldByName(
const GetFieldContext& context, absl::string_view name) const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(GetFieldByName, context, name);
}
absl::StatusOr<Handle<Value>> StructValue::GetFieldByNumber(
const GetFieldContext& context, int64_t number) const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(GetFieldByNumber, context, number);
}
absl::StatusOr<bool> StructValue::HasFieldByName(const HasFieldContext& context,
absl::string_view name) const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(HasFieldByName, context, name);
}
absl::StatusOr<bool> StructValue::HasFieldByNumber(
const HasFieldContext& context, int64_t number) const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(HasFieldByNumber, context, number);
}
absl::StatusOr<UniqueRef<StructValue::FieldIterator>>
StructValue::NewFieldIterator(MemoryManager& memory_manager) const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(NewFieldIterator, memory_manager);
}
internal::TypeInfo StructValue::TypeId() const {
return CEL_INTERNAL_STRUCT_VALUE_DISPATCH(TypeId);
}
#undef CEL_INTERNAL_STRUCT_VALUE_DISPATCH
struct StructValue::GetFieldVisitor final {
const StructValue& struct_value;
const GetFieldContext& context;
absl::StatusOr<Handle<Value>> operator()(absl::string_view name) const {
return struct_value.GetFieldByName(context, name);
}
absl::StatusOr<Handle<Value>> operator()(int64_t number) const {
return struct_value.GetFieldByNumber(context, number);
}
};
struct StructValue::HasFieldVisitor final {
const StructValue& struct_value;
const HasFieldContext& context;
absl::StatusOr<bool> operator()(absl::string_view name) const {
return struct_value.HasFieldByName(context, name);
}
absl::StatusOr<bool> operator()(int64_t number) const {
return struct_value.HasFieldByNumber(context, number);
}
};
absl::StatusOr<Handle<Value>> StructValue::GetField(
const GetFieldContext& context, FieldId field) const {
return absl::visit(GetFieldVisitor{*this, context}, field.data_);
}
absl::StatusOr<bool> StructValue::HasField(const HasFieldContext& context,
FieldId field) const {
return absl::visit(HasFieldVisitor{*this, context}, field.data_);
}
absl::StatusOr<StructValue::FieldId> StructValue::FieldIterator::NextId(
const StructValue::GetFieldContext& context) {
CEL_ASSIGN_OR_RETURN(auto entry, Next(context));
return entry.id;
}
absl::StatusOr<Handle<Value>> StructValue::FieldIterator::NextValue(
const StructValue::GetFieldContext& context) {
CEL_ASSIGN_OR_RETURN(auto entry, Next(context));
return std::move(entry.value);
}
namespace base_internal {
class LegacyStructValueFieldIterator final : public StructValue::FieldIterator {
public:
LegacyStructValueFieldIterator(uintptr_t msg, uintptr_t type_info)
: msg_(msg),
type_info_(type_info),
field_names_(MessageValueListFields(msg_, type_info_)) {}
bool HasNext() override { return index_ < field_names_.size(); }
absl::StatusOr<Field> Next(
const StructValue::GetFieldContext& context) override {
if (ABSL_PREDICT_FALSE(index_ >= field_names_.size())) {
return absl::FailedPreconditionError(
"StructValue::FieldIterator::Next() called when "
"StructValue::FieldIterator::HasNext() returns false");
}
const auto& field_name = field_names_[index_];
CEL_ASSIGN_OR_RETURN(
auto value, MessageValueGetFieldByName(
msg_, type_info_, context.value_factory(), field_name,
context.unbox_null_wrapper_types()));
++index_;
return Field(LegacyStructType::MakeFieldId(field_name), std::move(value));
}
absl::StatusOr<StructValue::FieldId> NextId(
const StructValue::GetFieldContext& context) override {
if (ABSL_PREDICT_FALSE(index_ >= field_names_.size())) {
return absl::FailedPreconditionError(
"StructValue::FieldIterator::Next() called when "
"StructValue::FieldIterator::HasNext() returns false");
}
return LegacyStructType::MakeFieldId(field_names_[index_++]);
}
private:
const uintptr_t msg_;
const uintptr_t type_info_;
const std::vector<absl::string_view> field_names_;
size_t index_ = 0;
};
Handle<StructType> LegacyStructValue::type() const {
if ((msg_ & kMessageWrapperTagMask) == kMessageWrapperTagMask) {
// google::protobuf::Message
return HandleFactory<StructType>::Make<LegacyStructType>(msg_);
}
// LegacyTypeInfoApis
return HandleFactory<StructType>::Make<LegacyStructType>(type_info_);
}
size_t LegacyStructValue::field_count() const {
return MessageValueFieldCount(msg_, type_info_);
}
std::string LegacyStructValue::DebugString() const {
return type()->DebugString();
}
absl::StatusOr<Handle<Value>> LegacyStructValue::GetFieldByName(
const GetFieldContext& context, absl::string_view name) const {
return MessageValueGetFieldByName(msg_, type_info_, context.value_factory(),
name, context.unbox_null_wrapper_types());
}
absl::StatusOr<Handle<Value>> LegacyStructValue::GetFieldByNumber(
const GetFieldContext& context, int64_t number) const {
return MessageValueGetFieldByNumber(msg_, type_info_, context.value_factory(),
number,
context.unbox_null_wrapper_types());
}
absl::StatusOr<bool> LegacyStructValue::HasFieldByName(
const HasFieldContext& context, absl::string_view name) const {
return MessageValueHasFieldByName(msg_, type_info_, name);
}
absl::StatusOr<bool> LegacyStructValue::HasFieldByNumber(
const HasFieldContext& context, int64_t number) const {
return MessageValueHasFieldByNumber(msg_, type_info_, number);
}
absl::StatusOr<UniqueRef<StructValue::FieldIterator>>
LegacyStructValue::NewFieldIterator(MemoryManager& memory_manager) const {
return MakeUnique<LegacyStructValueFieldIterator>(memory_manager, msg_,
type_info_);
}
AbstractStructValue::AbstractStructValue(Handle<StructType> type)
: StructValue(), base_internal::HeapData(kKind), type_(std::move(type)) {
// Ensure `Value*` and `base_internal::HeapData*` are not thunked.
ABSL_ASSERT(
reinterpret_cast<uintptr_t>(static_cast<Value*>(this)) ==
reinterpret_cast<uintptr_t>(static_cast<base_internal::HeapData*>(this)));
}
} // namespace base_internal
} // namespace cel