Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions 3rdparty/libprocess/include/process/protobuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ inline void post(const process::UPID& to,
{
std::string data;
if (message.SerializeToString(&data)) {
post(to, message.GetTypeName(), data.data(), data.size());
post(to, std::string(message.GetTypeName()), data.data(), data.size());
} else {
LOG(ERROR) << "Failed to post '" << message.GetTypeName() << "' to "
<< to << ": Failed to serialize";
Expand All @@ -54,7 +54,7 @@ inline void post(const process::UPID& from,
{
std::string data;
if (message.SerializeToString(&data)) {
post(from, to, message.GetTypeName(), data.data(), data.size());
post(from, to, std::string(message.GetTypeName()), data.data(), data.size());
} else {
LOG(ERROR) << "Failed to post '" << message.GetTypeName() << "' to "
<< to << ": Failed to serialize";
Expand Down Expand Up @@ -128,7 +128,8 @@ class ProtobufProcess : public process::Process<T>
{
std::string data;
if (message.SerializeToString(&data)) {
process::Process<T>::send(to, message.GetTypeName(), std::move(data));
process::Process<T>::send(
to, std::string(message.GetTypeName()), std::move(data));
} else {
LOG(ERROR) << "Failed to send '" << message.GetTypeName() << "' to "
<< to << ": Failed to serialize";
Expand All @@ -149,7 +150,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(&handlerM<M>,
t, method,
lambda::_1, lambda::_2);
Expand All @@ -161,7 +162,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(&handlerMutM<M>,
t, method,
lambda::_1, lambda::_2);
Expand All @@ -176,7 +177,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(&handler0,
t, method,
lambda::_1, lambda::_2);
Expand All @@ -191,7 +192,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(static_cast<void(&)(
T*,
void (T::*)(const process::UPID&, PC...),
Expand All @@ -209,7 +210,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(&_handlerM<M>,
t, method,
lambda::_1, lambda::_2);
Expand All @@ -221,7 +222,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(&_handlerMutM<M>,
t, method,
lambda::_1, lambda::_2);
Expand All @@ -233,7 +234,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(&_handler0,
t, method,
lambda::_1, lambda::_2);
Expand All @@ -248,7 +249,7 @@ class ProtobufProcess : public process::Process<T>
{
google::protobuf::Message* m = new M();
T* t = static_cast<T*>(this);
protobufHandlers[m->GetTypeName()] =
protobufHandlers[std::string(m->GetTypeName())] =
lambda::bind(static_cast<void(&)(
T*,
void (T::*)(PC...),
Expand All @@ -272,7 +273,7 @@ class ProtobufProcess : public process::Process<T>
const std::string& data)
{
google::protobuf::Arena arena;
M* m = CHECK_NOTNULL(google::protobuf::Arena::CreateMessage<M>(&arena));
M* m = CHECK_NOTNULL(google::protobuf::Arena::Create<M>(&arena));

if (m->ParseFromString(data)) {
(t->*method)(sender, *m);
Expand Down Expand Up @@ -318,7 +319,7 @@ class ProtobufProcess : public process::Process<T>
MessageProperty<M, P>... p)
{
google::protobuf::Arena arena;
M* m = CHECK_NOTNULL(google::protobuf::Arena::CreateMessage<M>(&arena));
M* m = CHECK_NOTNULL(google::protobuf::Arena::Create<M>(&arena));

if (m->ParseFromString(data)) {
(t->*method)(sender, google::protobuf::convert((m->*p)())...);
Expand All @@ -337,7 +338,7 @@ class ProtobufProcess : public process::Process<T>
const std::string& data)
{
google::protobuf::Arena arena;
M* m = CHECK_NOTNULL(google::protobuf::Arena::CreateMessage<M>(&arena));
M* m = CHECK_NOTNULL(google::protobuf::Arena::Create<M>(&arena));

if (m->ParseFromString(data)) {
(t->*method)(*m);
Expand Down Expand Up @@ -383,7 +384,7 @@ class ProtobufProcess : public process::Process<T>
MessageProperty<M, P>... p)
{
google::protobuf::Arena arena;
M* m = CHECK_NOTNULL(google::protobuf::Arena::CreateMessage<M>(&arena));
M* m = CHECK_NOTNULL(google::protobuf::Arena::Create<M>(&arena));

if (m->ParseFromString(data)) {
(t->*method)(google::protobuf::convert((m->*p)())...);
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/stout/include/stout/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <limits>
#include <map>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

Expand Down Expand Up @@ -81,6 +82,7 @@ struct String
String() {}
String(const char* _value) : value(_value) {}
String(const std::string& _value) : value(_value) {}
String(std::string_view _value) : value(_value) {}
std::string value;
};

Expand Down
44 changes: 40 additions & 4 deletions 3rdparty/stout/include/stout/jsonify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <functional>
#include <ostream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -285,6 +286,15 @@ class StringWriter
CHECK(writer_->String(value));
}

void set(std::string_view value)
{
empty_ = false;

// This check will fail if we enable write validation in rapidjson;
// we'll need to figure out a way to surface the error.
CHECK(writer_->String(value.data(), value.size()));
}

private:
rapidjson::Writer<rapidjson::StringBuffer>* writer_;
bool empty_;
Expand Down Expand Up @@ -344,15 +354,15 @@ class ObjectWriter
ObjectWriter& operator=(ObjectWriter&&) = delete;

template <typename T>
void field(const std::string& key, const T& value)
void field(std::string_view key, const T& value)
{
// This check will fail we enable write validation in rapidjson;
// we'll need to figure out a way to surface the error.
//
// TODO(bmahler): The 1.1.0 release of rapidjson did not
// yet have the std::string overload for `Key`, avoid calling
// `c_str()` and `size()` when we upgrade beyond 1.1.0.
CHECK(writer_->Key(key.c_str(), key.size()));
CHECK(writer_->Key(key.data(), key.size()));
jsonify(value).write(writer_);
}

Expand Down Expand Up @@ -438,6 +448,26 @@ inline void json(StringWriter* writer, const std::string& value)
writer->set(value);
}


// For the `absl::string_view` the protobuf descriptor accessors return since
// protobuf 22. Without this, such a value is written as an array of
// one-character strings: `std::string`'s converting constructor from
// `string_view` is explicit, so the overload above is not viable, and a
// `string_view` is iterable, so the one below is.
//
// Constrained to an exact `std::string_view` on purpose. A plain
// `json(StringWriter*, std::string_view)` overload would instead make
// `const char*` ambiguous against the overload above, both being
// user-defined conversions.
template <
typename T,
typename std::enable_if<
std::is_same<T, std::string_view>::value, int>::type = 0>
void json(StringWriter* writer, T value)
{
writer->set(value);
}

namespace internal {

// TODO(mpark): Pull this out to something like <stout/meta.hpp>.
Expand Down Expand Up @@ -499,12 +529,18 @@ struct HasMappedType
// `json` function for iterables (e.g., std::vector).
// This function is only enabled if `Iterable` is iterable, is not a
// `const char (&)[N]` (in order to avoid ambiguity with the string literal
// overload), and does not have a member typedef `mapped_type` (we take the
// existence of `mapped_type` as the indication of an associative container).
// overload), is not convertible to `std::string_view` (strings are iterable
// too, and would otherwise be written as an array of one-character strings
// -- the string overload above only wins for `std::string` because a
// non-template beats a template, which does not help a type that reaches it
// by conversion), and does not have a member typedef `mapped_type` (we take
// the existence of `mapped_type` as the indication of an associative
// container).
template <
typename Iterable,
typename std::enable_if<
internal::IsSequence<Iterable>::value &&
!std::is_convertible<const Iterable&, std::string_view>::value &&
!(std::is_array<Iterable>::value &&
std::rank<Iterable>::value == 1 &&
std::is_same<
Expand Down
34 changes: 17 additions & 17 deletions 3rdparty/stout/include/stout/protobuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Try<T> deserialize(const std::string& value)
value.data(),
static_cast<int>(value.size()));
if (!t.ParseFromZeroCopyStream(&stream)) {
return Error("Failed to deserialize " + t.GetDescriptor()->full_name());
return Error("Failed to deserialize " + std::string(t.GetDescriptor()->full_name()));
}
return t;
}
Expand All @@ -233,7 +233,7 @@ Try<std::string> serialize(const T& t)

std::string value;
if (!t.SerializeToString(&value)) {
return Error("Failed to serialize " + t.GetDescriptor()->full_name());
return Error("Failed to serialize " + std::string(t.GetDescriptor()->full_name()));
}
return value;
}
Expand Down Expand Up @@ -483,7 +483,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
break;
default:
return Error("Not expecting a JSON object for field '" +
field->name() + "'");
std::string(field->name()) + "'");
}
return Nothing();
}
Expand All @@ -502,7 +502,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
Try<std::string> decode = base64::decode(string.value);
if (decode.isError()) {
return Error("Failed to base64 decode bytes field"
" '" + field->name() + "': " + decode.error());
" '" + std::string(field->name()) + "': " + decode.error());
}

if (field->is_repeated()) {
Expand Down Expand Up @@ -552,7 +552,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
if (number.isError()) {
return Error(
"Failed to parse '" + string.value + "' as a JSON number "
"for field '" + field->name() + "': " + number.error());
"for field '" + std::string(field->name()) + "': " + number.error());
}

return operator()(number.get());
Expand All @@ -572,7 +572,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
if (number.isError()) {
return Error(
"Failed to parse '" + string.value + "' as a JSON number "
"for field '" + field->name() + "': " + number.error());
"for field '" + std::string(field->name()) + "': " + number.error());
}

return operator()(number.get());
Expand All @@ -582,14 +582,14 @@ struct Parser : boost::static_visitor<Try<Nothing>>
if (boolean.isError()) {
return Error(
"Failed to parse '" + string.value + "' as a JSON boolean "
"for field '" + field->name() + "': " + boolean.error());
"for field '" + std::string(field->name()) + "': " + boolean.error());
}

return operator()(boolean.get());
}
default:
return Error("Not expecting a JSON string for field '" +
field->name() + "'");
std::string(field->name()) + "'");
}
return Nothing();
}
Expand Down Expand Up @@ -647,7 +647,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
break;
default:
return Error("Not expecting a JSON number for field '" +
field->name() + "'");
std::string(field->name()) + "'");
}
return Nothing();
}
Expand All @@ -656,7 +656,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
{
if (!field->is_repeated()) {
return Error("Not expecting a JSON array for field '" +
field->name() + "'");
std::string(field->name()) + "'");
}

foreach (const JSON::Value& value, array.values) {
Expand All @@ -683,7 +683,7 @@ struct Parser : boost::static_visitor<Try<Nothing>>
break;
default:
return Error("Not expecting a JSON boolean for field '" +
field->name() + "'");
std::string(field->name()) + "'");
}
return Nothing();
}
Expand Down Expand Up @@ -864,7 +864,7 @@ inline void json(ObjectWriter* writer, const Protobuf& protobuf)
foreach (const FieldDescriptor* field, fields) {
if (field->is_repeated() && !field->is_map()) {
writer->field(
field->name(),
std::string(field->name()),
[&field, &reflection, &message](JSON::ArrayWriter* writer) {
int fieldSize = reflection->FieldSize(message, field);
for (int i = 0; i < fieldSize; ++i) {
Expand Down Expand Up @@ -966,11 +966,11 @@ inline void json(ObjectWriter* writer, const Protobuf& protobuf)
};

if (!field->is_repeated()) { // Singular field.
writeField(field->name(), reflection, message, field);
writeField(std::string(field->name()), reflection, message, field);
} else { // Map field.
CHECK(field->is_map());
writer->field(
field->name(),
std::string(field->name()),
[&field, &reflection, &message, &writeField](
JSON::ObjectWriter* writer) {
foreach (
Expand Down Expand Up @@ -1176,7 +1176,7 @@ inline Object protobuf(const google::protobuf::Message& message)

map.values[name] = value_for_field(entry, value_field);
}
object.values[field->name()] = map;
object.values[std::string(field->name())] = map;
} else if (field->is_repeated()) {
JSON::Array array;
int fieldSize = reflection->FieldSize(message, field);
Expand Down Expand Up @@ -1243,9 +1243,9 @@ inline Object protobuf(const google::protobuf::Message& message)
stringify(field->type()));
}
}
object.values[field->name()] = array;
object.values[std::string(field->name())] = array;
} else {
object.values[field->name()] = value_for_field(message, field);
object.values[std::string(field->name())] = value_for_field(message, field);
}
}

Expand Down
Loading