Search code examples
protocol-buffersprotobuf-c

Enconde a list of maps in protobuf?


Background

I am trying to use protobuff for one of our apps, but I am having trouble understanding the protocol and I need help creating a .proto file.

Data

The data I need to encode is a list of maps, with the following structure:

[
  {
    "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
  }
]

Each map has the following structure:

{
  "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
}

Research

My goal is to create .proto file using proto3. So I decided to read the documentation for .proto3 files:

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

But I was even more confused. According to the docs, I cannot have a map holding values of different types:

For that I would need the equivalent of the JSON object type and check the docs for .struct.proto but that page doesn't mention anything about it.

Question

So I am rather lost here. How do I represent the mentioned data structure in a .proto?


Solution

  • Answer

    Turns out that I don't actually need a map, a list of objects (messages) would suffice:

        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;
      }