Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put a python dictionary in a protobuf message?

Suppose we have this Json blob:

{
  "thing": {
    "x": 1,
    "str": "hello,
    "params": {
      "opaque": "yes",
      "unknown": 1,
      "more": ...
    }
  }
}

The contents of params is unknown. All we know is that it's a dictionary. How do we define a protobuf message that can parse it?

// file: thing.proto
message Thing {
    uint32 x = 1;
    string str = 2;
    WhatGoesHere? params = 3;
}

[Edit] Moved solution to answer as per comment.

like image 583
timmer Avatar asked Oct 12 '17 00:10

timmer


1 Answers

Solutions: Use google provided messages.

// file: solution.proto
import "google/protobuf/struct.proto";

message Solution1 {
    uint32 x = 1;
    string str = 2;
    google.protobuf.Struct params = 3;
}

message Solution2 {
    uint32 x = 1;
    string str = 2;
    map<string, google.protobuf.Value> params = 3;
}
like image 94
timmer Avatar answered Sep 28 '22 18:09

timmer