I am trying to implement a way to check if all nontrivial fields in proto3 message are set (since presence is not encoded for trivial types like int).
I have code like this
namespace pb = google::protobuf;
bool HasAll(const pb::Message& msg)
{
std::vector<const pb::FieldDescriptor*> fieldDescriptors;
const auto* reflection = msg.GetReflection();
reflection->ListFields(msg, &fieldDescriptors);
const auto* descriptor = msg.GetDescriptor();
const auto numFields = descriptor->field_count();
const bool same = numFields == fieldDescriptors.size();
return same;
}
This seems to work at "top level", but I wonder if there is a way to tell the reflection
and descriptor
to recourse into submessages, so my fieldDescriptors
contains the data about nested fields, and numFields
also counts the fields in nested fields.
I know I can manually implement recursion, but I am hoping somebody already solved this problem.
note: I know in general why fields are optional in pb3, long story, but I really really do need all fields to be set.