Search code examples
typescriptserializationprotocol-buffers

Use protobuf to serialize a list of list of string


I am working on a typescript project in which I want to serialize RequestBody using protobuff.

My body look like this:

{
  "streams": [
    {
      "stream": {
        "a": "1",
        "b": "2",
        "c": "3",
        "d": "4",
        "e": "5",
        "f": "6",
        "g": "7"
      },
      "values": [
        [
          "1675947803118000000",
          "logfmt"
        ]
      ]
    }
  ]
}

Where I can have more than one stream in streams.

I managed to serialize all excepted the values key.

I do not know how I can define a list of list of string with no field name.

I tried many things with no success:

 Stream stream = 1;
 //repeated string values = 2;// Concat timestamp and log line: "1675947803118000000,logfmt"
 //repeated google.protobuf.Any values = 2; //values: [ Any {} ]
 //repeated Values values = 2; // values: [ Values { a: [] } ],
 // repeated MyMessage values = 2; // values: [ MyMessage { some_identifier: [] } ],
 repeated MyFields values = 2; // values: [ MyFields { MyMap: {} } ],

I do find resources in protobuff doc or anywhere else about this issue.

Could someone provide guidance ?

Thx for any advice.

Regards

Quentin


Solution

  • You cannot.

    Protobuf does not permit values to be defined as:

    message Foo {
      repeated repeated string values = 1;
    }
    

    repeated is only permitted to take a the scalar type or a Message.

    The "equivalent" in Protobuf is:

    message Bar {
      repeated string values = 1;
    }
    message Foo {
      repeated Bar bars = 1;
    }
    

    This is a case where your on-the-wire format must (and it generally should always) differ from the application format. You will need to map your RequestBody to the equivalent Protobuf message before marshaling.