protobuf嵌入式消息最佳实践

时间:2020-02-18 17:09:32

标签: c++ protocol-buffers

我有一个protobuf消息,其中包含另一个重复消息。看起来像这样:

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

    repeated GroupProperties group_properties = 1;
}

我的问题是,这是编码消息的最佳方法,还是应该像这样将它们分开:

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

message AddGroupsRequest
{
    repeated GroupProperties group_properties = 1;
}

以一种或另一种方式定义消息是否有含义?

谢谢

1 个答案:

答案 0 :(得分:4)

使用二进制输出时,两个规范的基础编码将相同。

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

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

唯一的决定点是希望生成的C ++类名是GroupProperties(以嵌套形式)还是要使用独立的<?php require 'databaseConnection.php'; $date = $_POST['dueDate']; $importance = $_POST['importance']; $description = $_POST['description']; $sql="INSERT INTO tasklist VALUES (Null,'$date','$importance','$description')"; if ($result = mysqli_query($conn, $sql)) { $data = "<tr> <td>".$date."</td> <td>".$importance."</td> <td>".$description."</td> <td><i class='fas fa-trash'<></i></td></tr>"; echo json_encode($data); } ?> 类型,这在很大程度上是一个优先选择。请注意,您以后可以更改此设置,而不会破坏已编码的消息。

相关问题