在protobuf中添加地图列表?

时间:2019-07-09 13:27:00

标签: protocol-buffers protobuf-c

背景

我正在尝试为我们的一个应用程序使用probuff,但是我在理解协议时遇到了麻烦,我需要帮助来创建.proto文件。

数据

我需要编码的数据是地图列表,其结构如下:

[
  {
    "AwayTeam": "Osasuna",
    "Date": "2017-05-07",
    "Div": "SP1",
    "FTAG": 1,
    "FTHG": 4,
    "FTR": "H",
    "HTAG": 0,
    "HTHG": 2,
    "HTR": "H",
    "HomeTeam": "Valencia",
    "Season": 201617
  },
  {
    "AwayTeam": "Osasuna",
    "Date": "2016-02-27",
    "Div": "SP2",
    "FTAG": 1,
    "FTHG": 0,
    "FTR": "A",
    "HTAG": 0,
    "HTHG": 0,
    "HTR": "D",
    "HomeTeam": "Cordoba",
    "Season": 201516
  }
]

每个地图都具有以下结构:

{
  "AwayTeam": string,  required: true
  "Date":     string,  required: true
  "Div":      string,  required: true
  "FTAG":     integer, required: true
  "FTHG":     integer, required: true
  "FTR":      string,  required: true
  "HTAG":     integer, required: true
  "HTHG":     integer, required: true
  "HTR":      string,  required: true
  "HomeTeam": string,  required: true
  "Season":   integer, required: true
}

研究

我的目标是使用proto3创建.proto文件。因此,我决定阅读.proto3文件的文档:

https://developers.google.com/protocol-buffers/docs/proto3#maps

但是我更加困惑。根据文档,我不能拥有包含不同类型值的地图:

为此,我需要使用JSON object类型的等效项并检查.struct.proto的文档,但是该页面没有提及任何内容。

问题

所以我在这里迷路了。如何在.proto中表示上述数据结构?

1 个答案:

答案 0 :(得分:0)

答案

事实证明,我实际上不需要地图,只需列出对象(消息)即可:

    syntax = "proto3";

    message Result {
      string AwayTeam = 1;
      string Date = 2;
      string Div = 3;
      int32 FTAG = 4;
      int32 FTHG = 5;
      string FTR = 6;
      int32 HTAG = 7;
      int32 HTHG = 8;
      string HTR = 9;
      string HomeTeam = 10;
      int32 Season = 11;
  }

  message Response {
    repeated Result results = 1;
  }