Ken
Ken

Reputation: 457

protobuf embedded message best practice

I have a protobuf message that contains another repeating message. It looks like this:

message AddGroupsRequest
{
    message GroupProperties
    {
        string name = 1;
        int32 some_property_1 = 2;
        int32 some_property_2 = 3;
    }

    repeated GroupProperties group_properties = 1;
}

My question is whether that's the best way to encode the messages, or should they be separated like this:

message GroupProperties
{
    string name = 1;
    int32 some_property_1 = 2;
    int32 some_property_2 = 3;
}

message AddGroupsRequest
{
    repeated GroupProperties group_properties = 1;
}

Are there implications for defining the message one way or another?

Thanks

Upvotes: 3

Views: 6978

Answers (1)

Travis Gockel
Travis Gockel

Reputation: 27633

The underlying encoding of the two specifications will be identical when using the binary output.

$> echo 'group_properties { name: "Bob", some_property_1: 6 }' \
      | protoc nested.proto --encode='AddGroupsRequest'        \
      | xxd

00000000: 0a07 0a03 426f 6210 06                   ....Bob..
$> echo 'group_properties { name: "Bob", some_property_1: 6 }' \
      | protoc sibling.proto --encode='AddGroupsRequest'       \
      | xxd

00000000: 0a07 0a03 426f 6210 06                   ....Bob..

The only decision point is if you want the generated C++ class name to be AddGroupsRequest_GroupProperties (in nested form) or if you want an independent GroupProperties type, which is largely a preference thing. Note that you can change this later without breaking the already-encoded messages.

Upvotes: 4

Related Questions