Skip to content

Commit 6139548

Browse files
jckingcopybara-github
authored andcommitted
Value::SerializeTo(..., absl::Cord*) -> Value::SerializeTo(..., ZeroCopyOutputStream*)
PiperOrigin-RevId: 740430374
1 parent 8d8391b commit 6139548

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+289
-274
lines changed

common/BUILD

-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,6 @@ cc_test(
648648
"value_test.cc",
649649
],
650650
deps = [
651-
":allocator",
652651
":casting",
653652
":memory",
654653
":native_type",

common/legacy_value.cc

+11-9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "runtime/runtime_options.h"
6262
#include "google/protobuf/arena.h"
6363
#include "google/protobuf/descriptor.h"
64+
#include "google/protobuf/io/zero_copy_stream.h"
6465
#include "google/protobuf/message.h"
6566
#include "google/protobuf/message_lite.h"
6667

@@ -312,10 +313,10 @@ std::string LegacyListValue::DebugString() const {
312313
absl::Status LegacyListValue::SerializeTo(
313314
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
314315
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
315-
absl::Nonnull<absl::Cord*> value) const {
316+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
316317
ABSL_DCHECK(descriptor_pool != nullptr);
317318
ABSL_DCHECK(message_factory != nullptr);
318-
ABSL_DCHECK(value != nullptr);
319+
ABSL_DCHECK(output != nullptr);
319320

320321
const google::protobuf::Descriptor* descriptor =
321322
descriptor_pool->FindMessageTypeByName("google.protobuf.ListValue");
@@ -331,7 +332,7 @@ absl::Status LegacyListValue::SerializeTo(
331332
if (wrapped == nullptr) {
332333
return absl::UnknownError("failed to convert legacy map to JSON");
333334
}
334-
if (!wrapped->SerializePartialToCord(value)) {
335+
if (!wrapped->SerializePartialToZeroCopyStream(output)) {
335336
return absl::UnknownError(
336337
absl::StrCat("failed to serialize message: ", wrapped->GetTypeName()));
337338
}
@@ -487,10 +488,10 @@ std::string LegacyMapValue::DebugString() const {
487488
absl::Status LegacyMapValue::SerializeTo(
488489
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
489490
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
490-
absl::Nonnull<absl::Cord*> value) const {
491+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
491492
ABSL_DCHECK(descriptor_pool != nullptr);
492493
ABSL_DCHECK(message_factory != nullptr);
493-
ABSL_DCHECK(value != nullptr);
494+
ABSL_DCHECK(output != nullptr);
494495

495496
const google::protobuf::Descriptor* descriptor =
496497
descriptor_pool->FindMessageTypeByName("google.protobuf.Struct");
@@ -505,7 +506,7 @@ absl::Status LegacyMapValue::SerializeTo(
505506
if (wrapped == nullptr) {
506507
return absl::UnknownError("failed to convert legacy map to JSON");
507508
}
508-
if (!wrapped->SerializePartialToCord(value)) {
509+
if (!wrapped->SerializePartialToZeroCopyStream(output)) {
509510
return absl::UnknownError(
510511
absl::StrCat("failed to serialize message: ", wrapped->GetTypeName()));
511512
}
@@ -732,14 +733,15 @@ std::string LegacyStructValue::DebugString() const {
732733
absl::Status LegacyStructValue::SerializeTo(
733734
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
734735
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
735-
absl::Nonnull<absl::Cord*> value) const {
736+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
736737
ABSL_DCHECK(descriptor_pool != nullptr);
737738
ABSL_DCHECK(message_factory != nullptr);
738-
ABSL_DCHECK(value != nullptr);
739+
ABSL_DCHECK(output != nullptr);
739740

740741
auto message_wrapper = AsMessageWrapper(message_ptr_, legacy_type_info_);
741742
if (ABSL_PREDICT_TRUE(
742-
message_wrapper.message_ptr()->SerializePartialToCord(value))) {
743+
message_wrapper.message_ptr()->SerializePartialToZeroCopyStream(
744+
output))) {
743745
return absl::OkStatus();
744746
}
745747
return absl::UnknownError("failed to serialize protocol buffer message");

common/value.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,13 @@ std::string Value::DebugString() const {
131131
absl::Status Value::SerializeTo(
132132
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
133133
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
134-
absl::Nonnull<absl::Cord*> value) const {
134+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
135135
ABSL_DCHECK(descriptor_pool != nullptr);
136136
ABSL_DCHECK(message_factory != nullptr);
137-
ABSL_DCHECK(value != nullptr);
137+
ABSL_DCHECK(output != nullptr);
138138

139-
return variant_.Visit([descriptor_pool, message_factory,
140-
value](const auto& alternative) -> absl::Status {
141-
return alternative.SerializeTo(descriptor_pool, message_factory, value);
139+
return variant_.Visit([&](const auto& alternative) -> absl::Status {
140+
return alternative.SerializeTo(descriptor_pool, message_factory, output);
142141
});
143142
}
144143

common/value.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "google/protobuf/arena.h"
7878
#include "google/protobuf/descriptor.h"
7979
#include "google/protobuf/generated_enum_reflection.h"
80+
#include "google/protobuf/io/zero_copy_stream.h"
8081
#include "google/protobuf/map_field.h"
8182
#include "google/protobuf/message.h"
8283

@@ -346,14 +347,13 @@ class Value final : private common_internal::ValueMixin<Value> {
346347

347348
std::string DebugString() const;
348349

349-
// `SerializeTo` serializes this value to `value`, which is assigned to and
350-
// not appended to. If an error is returned, `value` is in a valid but
351-
// unspecified state. If this value does not support serialization,
352-
// `FAILED_PRECONDITION` is returned.
350+
// `SerializeTo` serializes this value to `output`. If an error is returned,
351+
// `output` is in a valid but unspecified state. If this value does not
352+
// support serialization, `FAILED_PRECONDITION` is returned.
353353
absl::Status SerializeTo(
354354
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
355355
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
356-
absl::Nonnull<absl::Cord*> value) const;
356+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const;
357357

358358
// `ConvertToJson` converts this value to its JSON representation. The
359359
// argument `json` **MUST** be an instance of `google.protobuf.Value` which is

common/values/bool_value.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
#include "absl/base/nullability.h"
1919
#include "absl/log/absl_check.h"
2020
#include "absl/status/status.h"
21-
#include "absl/strings/cord.h"
2221
#include "absl/strings/str_cat.h"
2322
#include "common/value.h"
2423
#include "internal/status_macros.h"
2524
#include "internal/well_known_types.h"
2625
#include "google/protobuf/arena.h"
2726
#include "google/protobuf/descriptor.h"
27+
#include "google/protobuf/io/zero_copy_stream.h"
2828
#include "google/protobuf/message.h"
2929

3030
namespace cel {
@@ -44,14 +44,14 @@ std::string BoolValue::DebugString() const {
4444
absl::Status BoolValue::SerializeTo(
4545
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
4646
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
47-
absl::Nonnull<absl::Cord*> value) const {
47+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
4848
ABSL_DCHECK(descriptor_pool != nullptr);
4949
ABSL_DCHECK(message_factory != nullptr);
50-
ABSL_DCHECK(value != nullptr);
50+
ABSL_DCHECK(output != nullptr);
5151

5252
google::protobuf::BoolValue message;
5353
message.set_value(NativeValue());
54-
if (!message.SerializePartialToCord(value)) {
54+
if (!message.SerializePartialToZeroCopyStream(output)) {
5555
return absl::UnknownError(
5656
absl::StrCat("failed to serialize message: ", message.GetTypeName()));
5757
}

common/values/bool_value.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323

2424
#include "absl/base/nullability.h"
2525
#include "absl/status/status.h"
26-
#include "absl/strings/cord.h"
2726
#include "absl/strings/string_view.h"
2827
#include "common/type.h"
2928
#include "common/value_kind.h"
3029
#include "common/values/values.h"
3130
#include "google/protobuf/arena.h"
3231
#include "google/protobuf/descriptor.h"
32+
#include "google/protobuf/io/zero_copy_stream.h"
3333
#include "google/protobuf/message.h"
3434

3535
namespace cel {
@@ -64,7 +64,7 @@ class BoolValue final : private common_internal::ValueMixin<BoolValue> {
6464
absl::Status SerializeTo(
6565
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
6666
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
67-
absl::Nonnull<absl::Cord*> value) const;
67+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const;
6868

6969
// See Value::ConvertToJson().
7070
absl::Status ConvertToJson(

common/values/bytes_value.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "internal/well_known_types.h"
3131
#include "google/protobuf/arena.h"
3232
#include "google/protobuf/descriptor.h"
33+
#include "google/protobuf/io/zero_copy_stream.h"
3334
#include "google/protobuf/message.h"
3435

3536
namespace cel {
@@ -65,14 +66,14 @@ std::string BytesValue::DebugString() const { return BytesDebugString(*this); }
6566
absl::Status BytesValue::SerializeTo(
6667
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
6768
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
68-
absl::Nonnull<absl::Cord*> value) const {
69+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
6970
ABSL_DCHECK(descriptor_pool != nullptr);
7071
ABSL_DCHECK(message_factory != nullptr);
71-
ABSL_DCHECK(value != nullptr);
72+
ABSL_DCHECK(output != nullptr);
7273

7374
google::protobuf::BytesValue message;
7475
message.set_value(NativeString());
75-
if (!message.SerializePartialToCord(value)) {
76+
if (!message.SerializePartialToZeroCopyStream(output)) {
7677
return absl::UnknownError(
7778
absl::StrCat("failed to serialize message: ", message.GetTypeName()));
7879
}

common/values/bytes_value.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "common/values/values.h"
4141
#include "google/protobuf/arena.h"
4242
#include "google/protobuf/descriptor.h"
43+
#include "google/protobuf/io/zero_copy_stream.h"
4344
#include "google/protobuf/message.h"
4445

4546
namespace cel {
@@ -137,7 +138,7 @@ class BytesValue final : private common_internal::ValueMixin<BytesValue> {
137138
absl::Status SerializeTo(
138139
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
139140
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
140-
absl::Nonnull<absl::Cord*> value) const;
141+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const;
141142

142143
// See Value::ConvertToJson().
143144
absl::Status ConvertToJson(

common/values/custom_list_value.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "absl/log/absl_check.h"
2323
#include "absl/status/status.h"
2424
#include "absl/status/statusor.h"
25-
#include "absl/strings/cord.h"
2625
#include "absl/strings/str_cat.h"
2726
#include "absl/strings/string_view.h"
2827
#include "common/casting.h"
@@ -35,6 +34,7 @@
3534
#include "internal/well_known_types.h"
3635
#include "google/protobuf/arena.h"
3736
#include "google/protobuf/descriptor.h"
37+
#include "google/protobuf/io/zero_copy_stream.h"
3838
#include "google/protobuf/message.h"
3939

4040
namespace cel {
@@ -196,10 +196,10 @@ class CustomListValueDispatcherIterator final : public ValueIterator {
196196
absl::Status CustomListValueInterface::SerializeTo(
197197
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
198198
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
199-
absl::Nonnull<absl::Cord*> value) const {
199+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
200200
ABSL_DCHECK(descriptor_pool != nullptr);
201201
ABSL_DCHECK(message_factory != nullptr);
202-
ABSL_DCHECK(value != nullptr);
202+
ABSL_DCHECK(output != nullptr);
203203

204204
ListValueReflection reflection;
205205
CEL_RETURN_IF_ERROR(reflection.Initialize(descriptor_pool));
@@ -214,7 +214,7 @@ absl::Status CustomListValueInterface::SerializeTo(
214214
google::protobuf::Message* message = prototype->New(&arena);
215215
CEL_RETURN_IF_ERROR(
216216
ConvertToJsonArray(descriptor_pool, message_factory, message));
217-
if (!message->SerializePartialToCord(value)) {
217+
if (!message->SerializePartialToZeroCopyStream(output)) {
218218
return absl::UnknownError(
219219
"failed to serialize message: google.protobuf.ListValue");
220220
}
@@ -324,17 +324,17 @@ std::string CustomListValue::DebugString() const {
324324
absl::Status CustomListValue::SerializeTo(
325325
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
326326
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
327-
absl::Nonnull<absl::Cord*> value) const {
327+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const {
328328
if (dispatcher_ == nullptr) {
329329
CustomListValueInterface::Content content =
330330
content_.To<CustomListValueInterface::Content>();
331331
ABSL_DCHECK(content.interface != nullptr);
332332
return content.interface->SerializeTo(descriptor_pool, message_factory,
333-
value);
333+
output);
334334
}
335335
if (dispatcher_->serialize_to != nullptr) {
336336
return dispatcher_->serialize_to(dispatcher_, content_, descriptor_pool,
337-
message_factory, value);
337+
message_factory, output);
338338
}
339339
return absl::UnimplementedError(
340340
absl::StrCat(GetTypeName(), " is unserializable"));

common/values/custom_list_value.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
#include "absl/log/absl_check.h"
3636
#include "absl/status/status.h"
3737
#include "absl/status/statusor.h"
38-
#include "absl/strings/cord.h"
3938
#include "absl/strings/string_view.h"
4039
#include "common/native_type.h"
4140
#include "common/value_kind.h"
4241
#include "common/values/custom_value.h"
4342
#include "common/values/values.h"
4443
#include "google/protobuf/arena.h"
4544
#include "google/protobuf/descriptor.h"
45+
#include "google/protobuf/io/zero_copy_stream.h"
4646
#include "google/protobuf/message.h"
4747

4848
namespace cel {
@@ -72,7 +72,7 @@ struct CustomListValueDispatcher {
7272
CustomListValueContent content,
7373
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
7474
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
75-
absl::Nonnull<absl::Cord*> value);
75+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output);
7676

7777
using ConvertToJsonArray = absl::Status (*)(
7878
absl::Nonnull<const CustomListValueDispatcher*> dispatcher,
@@ -183,7 +183,7 @@ class CustomListValueInterface : public CustomValueInterface {
183183
absl::Status SerializeTo(
184184
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
185185
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
186-
absl::Nonnull<absl::Cord*> value) const override;
186+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const override;
187187

188188
absl::Status Equal(
189189
const Value& other,
@@ -290,7 +290,7 @@ class CustomListValue final
290290
absl::Status SerializeTo(
291291
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
292292
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
293-
absl::Nonnull<absl::Cord*> value) const;
293+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const;
294294

295295
// See Value::ConvertToJson().
296296
absl::Status ConvertToJson(

common/values/custom_list_value_test.cc

+13-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "internal/testing.h"
3333
#include "google/protobuf/arena.h"
3434
#include "google/protobuf/descriptor.h"
35+
#include "google/protobuf/io/zero_copy_stream.h"
36+
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
3537
#include "google/protobuf/message.h"
3638

3739
namespace cel {
@@ -63,12 +65,12 @@ class CustomListValueInterfaceTest final : public CustomListValueInterface {
6365
absl::Status SerializeTo(
6466
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
6567
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
66-
absl::Nonnull<absl::Cord*> value) const override {
68+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output) const override {
6769
google::protobuf::Value json;
6870
google::protobuf::ListValue* json_array = json.mutable_list_value();
6971
json_array->add_values()->set_bool_value(true);
7072
json_array->add_values()->set_number_value(1.0);
71-
if (!json.SerializePartialToCord(value)) {
73+
if (!json.SerializePartialToZeroCopyStream(output)) {
7274
return absl::UnknownError(
7375
"failed to serialize message: google.protobuf.Value");
7476
}
@@ -164,12 +166,13 @@ class CustomListValueTest : public common_internal::ValueTest<> {
164166
CustomListValueContent content,
165167
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
166168
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
167-
absl::Nonnull<absl::Cord*> value) -> absl::Status {
169+
absl::Nonnull<google::protobuf::io::ZeroCopyOutputStream*> output)
170+
-> absl::Status {
168171
google::protobuf::Value json;
169172
google::protobuf::Struct* json_object = json.mutable_struct_value();
170173
(*json_object->mutable_fields())["foo"].set_bool_value(true);
171174
(*json_object->mutable_fields())["bar"].set_number_value(1.0);
172-
if (!json.SerializePartialToCord(value)) {
175+
if (!json.SerializePartialToZeroCopyStream(output)) {
173176
return absl::UnknownError(
174177
"failed to serialize message: google.protobuf.Value");
175178
}
@@ -268,19 +271,19 @@ TEST_F(CustomListValueTest, Interface_IsZeroValue) {
268271
}
269272

270273
TEST_F(CustomListValueTest, Dispatcher_SerializeTo) {
271-
absl::Cord serialized;
274+
google::protobuf::io::CordOutputStream output;
272275
EXPECT_THAT(MakeDispatcher().SerializeTo(descriptor_pool(), message_factory(),
273-
&serialized),
276+
&output),
274277
IsOk());
275-
EXPECT_THAT(serialized, Not(IsEmpty()));
278+
EXPECT_THAT(std::move(output).Consume(), Not(IsEmpty()));
276279
}
277280

278281
TEST_F(CustomListValueTest, Interface_SerializeTo) {
279-
absl::Cord serialized;
282+
google::protobuf::io::CordOutputStream output;
280283
EXPECT_THAT(MakeInterface().SerializeTo(descriptor_pool(), message_factory(),
281-
&serialized),
284+
&output),
282285
IsOk());
283-
EXPECT_THAT(serialized, Not(IsEmpty()));
286+
EXPECT_THAT(std::move(output).Consume(), Not(IsEmpty()));
284287
}
285288

286289
TEST_F(CustomListValueTest, Dispatcher_ConvertToJson) {

0 commit comments

Comments
 (0)