Skip to content

Commit fe09c12

Browse files
committed
Modernise protobuf usage.
1 parent b5ae5a1 commit fe09c12

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

lib/config/proto.cc

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -117,39 +117,31 @@ static ProtoField resolveProtoPath(
117117
"config field '{}' in '{}' is not a message", item, path));
118118

119119
const auto* reflection = message->GetReflection();
120-
if ((field->label() !=
121-
google::protobuf::FieldDescriptor::LABEL_REPEATED) &&
122-
(index != -1))
120+
if (!field->is_repeated() && (index != -1))
123121
throw ProtoPathNotFoundException(fmt::format(
124122
"config field '{}[{}]' is indexed, but not repeated",
125123
item,
126124
index));
127125

128-
switch (field->label())
126+
if (field->is_optional() || field->is_required())
129127
{
130-
case google::protobuf::FieldDescriptor::LABEL_OPTIONAL:
131-
case google::protobuf::FieldDescriptor::LABEL_REQUIRED:
132-
if (!create && !reflection->HasField(*message, field))
133-
throw ProtoPathNotFoundException(fmt::format(
134-
"could not find config field '{}'", field->name()));
135-
message = reflection->MutableMessage(message, field);
136-
break;
137-
138-
case google::protobuf::FieldDescriptor::LABEL_REPEATED:
139-
if (index == -1)
140-
throw ProtoPathNotFoundException(fmt::format(
141-
"config field '{}' is repeated and must be indexed",
142-
item));
143-
while (reflection->FieldSize(*message, field) <= index)
144-
reflection->AddMessage(message, field);
145-
146-
message =
147-
reflection->MutableRepeatedMessage(message, field, index);
148-
break;
128+
if (!create && !reflection->HasField(*message, field))
129+
throw ProtoPathNotFoundException(fmt::format(
130+
"could not find config field '{}'", field->name()));
131+
message = reflection->MutableMessage(message, field);
132+
}
133+
else if (field->is_repeated())
134+
{
135+
if (index == -1)
136+
throw ProtoPathNotFoundException(fmt::format(
137+
"config field '{}' is repeated and must be indexed", item));
138+
while (reflection->FieldSize(*message, field) <= index)
139+
reflection->AddMessage(message, field);
149140

150-
default:
151-
error("bad proto label for field '{}' in '{}'", item, path);
141+
message = reflection->MutableRepeatedMessage(message, field, index);
152142
}
143+
else
144+
error("bad proto label for field '{}' in '{}'", item, path);
153145

154146
descriptor = message->GetDescriptor();
155147
}
@@ -215,7 +207,7 @@ static void updateRepeatedField(
215207
void ProtoField::set(const std::string& value)
216208
{
217209
const auto* reflection = _message->GetReflection();
218-
if (_field->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
210+
if (_field->is_repeated())
219211
{
220212
if (_index == -1)
221213
error("field '{}' is repeated but no index is provided");
@@ -359,7 +351,7 @@ void ProtoField::set(const std::string& value)
359351
std::string ProtoField::get() const
360352
{
361353
const auto* reflection = _message->GetReflection();
362-
if (_field->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
354+
if (_field->is_repeated())
363355
{
364356
if (_index == -1)
365357
error("field '{}' is repeated but no index is provided",
@@ -456,7 +448,7 @@ std::string ProtoField::get() const
456448
google::protobuf::Message* ProtoField::getMessage() const
457449
{
458450
const auto* reflection = _message->GetReflection();
459-
if (_field->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
451+
if (_field->is_repeated())
460452
{
461453
if (_index == -1)
462454
error("field '{}' is repeated but no index is provided",
@@ -477,7 +469,7 @@ google::protobuf::Message* ProtoField::getMessage() const
477469
std::string ProtoField::getBytes() const
478470
{
479471
const auto* reflection = _message->GetReflection();
480-
if (_field->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
472+
if (_field->is_repeated())
481473
{
482474
if (_index == -1)
483475
error("field '{}' is repeated but no index is provided",
@@ -536,7 +528,7 @@ findAllPossibleProtoFields(const google::protobuf::Descriptor* descriptor)
536528
const google::protobuf::FieldDescriptor* f = d->field(i);
537529
std::string n = s + (std::string)f->name();
538530

539-
if (f->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
531+
if (f->is_repeated())
540532
n += "[]";
541533

542534
if (shouldRecurse(f))
@@ -568,7 +560,7 @@ std::vector<ProtoField> findAllProtoFields(google::protobuf::Message* message)
568560
basename += '.';
569561
basename += f->name();
570562

571-
if (f->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
563+
if (f->is_repeated())
572564
{
573565
for (int i = 0; i < reflection->FieldSize(*message, f); i++)
574566
{

0 commit comments

Comments
 (0)