Is there a readable pattern for creating nested Protobuf messages in C++?

0

I'm new to Protobuf and would like to know if there is a good pattern for creating protobuf messages that results in readable code.

You can do it like this:

message.mutable_foo()->mutable_bar()->mutable_gazoo->set_gronk(4711);
message.mutable_foo()->mutable_bar()->mutable_gazoo->set_grunk(0xdeadbeef);
message.mutable_foo()->mutable_bar()->mutable_spunk->set_snafu("Boink!");

Or like this:

auto foo = message.mutable_foo();
auto bar = foo->mutable_bar();
auto gazoo = bar->mutable_gazoo();
gazoo->set_gronk(4711);
gazoo->set_grunk(0xdeadbeef);
auto spunk = bar->mutable_spunk();
spunk->set_snafu("Boink!");

But it is not very readable.

I would like to do something along the lines of:

message.set_foo(set_bar(set_gazoo(set_gronk(4711),
                                  set_grunk(0xdeadbeef)
                                 ),
                        set_spunk(set_snafu("Boink!"))
                );

Perhaps comparable to the Java builder syntax. Is anything like this possible? Is there any more readable code patterns for this?

c++
protocol-buffers
asked on Stack Overflow Aug 29, 2019 by Pibben • edited Aug 29, 2019 by Pibben

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0