map support in protobuf version 2

2,254 views
Skip to first unread message

Nagarjuna Reddy

unread,
Jun 13, 2017, 6:04:54 AM6/13/17
to Protocol Buffers
Hi,

As per the below link https://developers.google.com/protocol-buffers/docs/proto#maps, I see that maps are supported in protobuf2.
But when I use them I get the following error

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

Is the feature really supported in protobuf2? As per some of the queries on this forum others are also facing problem in protobuf2.

Regards,
Nagarjuna

Marc Gravell

unread,
Jun 13, 2017, 7:06:29 AM6/13/17
to Nagarjuna Reddy, Protocol Buffers
Oops; I meant to reply to group; email is hard...

(repeats response)

What protoc compiler version are you using? what does protoc --version say? It should be 3.3.0 or similar.

The following works fine using protoc (note it defaults to protogen - you'll need to change the drop-down to protoc 3.3.0, for example "(protoc) Java"): https://protogen.marcgravell.com/#ga1fce91670098b10f326bf20f99ae7d6

(which is just https://protogen.marcgravell.com/ which hosts "protoc" and "protogen", pre-loading with gist https://gist.github.com/mgravell/a1fce91670098b10f326bf20f99ae7d6 via client-side javascript)

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

Nagarjuna Reddy

unread,
Jun 13, 2017, 7:27:05 AM6/13/17
to Protocol Buffers, nagarju...@gmail.com
Hi Marc,

I am using libprotoc 2.6.1 and have to be on this version for some more time before moving protoc version 3 due to some constraints.
I was wondering if the documentation is incorrect saying map is supported in version 2.

Regards,
Nagarjuna


On Tuesday, June 13, 2017 at 4:36:29 PM UTC+5:30, Marc Gravell wrote:
Oops; I meant to reply to group; email is hard...

(repeats response)

What protoc compiler version are you using? what does protoc --version say? It should be 3.3.0 or similar.

The following works fine using protoc (note it defaults to protogen - you'll need to change the drop-down to protoc 3.3.0, for example "(protoc) Java"): https://protogen.marcgravell.com/#ga1fce91670098b10f326bf20f99ae7d6

(which is just https://protogen.marcgravell.com/ which hosts "protoc" and "protogen", pre-loading with gist https://gist.github.com/mgravell/a1fce91670098b10f326bf20f99ae7d6 via client-side javascript)
On 13 June 2017 at 11:04, Nagarjuna Reddy <nagarju...@gmail.com> wrote:
Hi,

As per the below link https://developers.google.com/protocol-buffers/docs/proto#maps, I see that maps are supported in protobuf2.
But when I use them I get the following error

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

Is the feature really supported in protobuf2? As per some of the queries on this forum others are also facing problem in protobuf2.

Regards,
Nagarjuna

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

Marc Gravell

unread,
Jun 13, 2017, 7:39:03 AM6/13/17
to Nagarjuna Reddy, Protocol Buffers
It is supported in the proto2 *language*, but you'll need to use a *proto compiler version* that understands it. Depending on the implementation (i.e. which language and library you are generating for), you *may* also need to use a *library* version that has additional features to support maps.

Note that the binary format does not change between version 2 and version 3, but the proto compiler has changes (to understand the back-ported syntax changes), and the library may or may not have changes.

Basically, that guidance should be read as:

"You can use map in your proto2 schemas without needing to change to proto3 DSL syntax, but you will need to use an up to data version of protoc and related tools"


If you can't update your tooling and library versions, then you can still *read and write* map data. You can just use:

    message MyMapEntry {
        optional int32 key = 1 [default = 0]; // change per your TKey
        optional string value = 2 [default = ""]; // change per your TValue
    }
    repeated MyMapEntry data = 1;

with the slight caveat that it will materialize as a list/array rather than a native dictionary.
Reply all
Reply to author
Forward
0 new messages