jrwren
jrwren

Reputation: 17928

How to ask protoc to use a value instead of a pointer as the value side of a map with Go?

I am using protocol buffers defined like this:

message Index {
     message albums {
         repeated string name = 1;
     }
     map<string, albums> artists_albums= 1;
     map<int32, albums> year_albums = 2;
}

It generates go code like this:

type Index struct {
    ArtistsAlbums map[string]*IndexAlbums
    YearAlbums     map[int32]*IndexAlbums
 }

How can I make it generate map values of type IndexAlbums instead of *IndexAlbums?

Upvotes: 2

Views: 379

Answers (1)

sberry
sberry

Reputation: 132098

If you use gogoprotobuf then there is an extension that will allow that

map<string, albums> artists_albums = 1 [(gogoproto.nullable) = false];

With regular goprotobuf I don't believe there is a way.

nullable, if false, a field is generated without a pointer (see warning below).

Warning about nullable: According to the Protocol Buffer specification, you should be able to tell whether a field is set or unset. With the option nullable=false this feature is lost, since your non-nullable fields will always be set. It can be seen as a layer on top of Protocol Buffers, where before and after marshalling all non-nullable fields are set and they cannot be unset.

Upvotes: 2

Related Questions