.proto file for exprotobuf

Background

I am trying to use protobuff for one of our apps by using exprotobuf, 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?

relying on my memory here

it should be something like this

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
}

# Your final response type

message Response {
  repeated Result results = 1
}

# empty request 
message Request {}

# example service definition

service ServiceName  {
  rpc method_name (Request) returns (Reponse) {}
}


2 Likes

Your code is 99% accurate! You were only missing the semi-colons at the end:


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

  message Request {}
  """

Now, while I don’t know if this will work as expected, I am happy that at least now it compiles !

1 Like