slagathor
slagathor

Reputation: 322

Using maps in protobuf v2

Currently I am using protobuf version 2 in my project. So far all of the messages are working great; however I have hit a road block trying to use the 'map' keyword.

The TLDR usage behind needing the map, is that I want to pass some JSON key/value pairs to my server to do a lookup, and potential log of data to a server (which uses a JSON message interface).

I am currently using the backwards compatibility method that is recommend on the docs page: https://developers.google.com/protocol-buffers/docs/proto#maps

What I would like to understand is why is the following declaration of my message (using maps) failing to compile? I am using the following veriosn of the protoc compiler: '# protoc --version => libprotoc 2.6.1'

message MapFieldEntry {
  optional string key = 1;
  optional string value = 2;
}

message Lookup {
  repeated MapFieldEntry map_field = 1;
  map<string, string> test_map = 2;
}

The error I receive is as follows (the errors don't make sense to me considering the documentation of the map feature):

Expected "required", "optional", or "repeated".
Expected field name.

I have tried adding syntax="proto2"; at the top, but I still get the error.

Edit: Just as a note; the issue I am having is regarding the second argument of the Lookup message. The first argument is what I am currently using as a work around.

Upvotes: 4

Views: 8497

Answers (1)

Luc
Luc

Reputation: 1433

I found someone else with a similar issue on git: https://github.com/google/protobuf/issues/799

The response is:

The maps syntax is only supported starting from v3.0.0. The "proto2" in the doc is referring to the syntax version, not protobuf release version. v3.0.0 supports both proto2 syntax and proto3 syntax while v2.6.1 only supports proto2 syntax. For all users, it's recommended to use v3.0.0-beta-1 instead of v2.6.1.

So it looks like to fix your problem, you should use protoc 3, instead of 2.6.1.

And keep your syntax=proto2 at the top of your file to precise this is the proto2 syntax that you use.

Could you try and let me know if this work? this is an interesting question as the official doc does not mention it.

Upvotes: 5

Related Questions