dox19458 2018-01-03 02:41
浏览 62

如何通过Go要求protoc使用值而不是指针作为映射的值侧?

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?

  • 写回答

1条回答 默认 最新

  • dongxiao3694 2018-01-03 02:50
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能