What would be the best way to re-use the generated protobuf structs for other purposes?

blov · · 664 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve spoken to a few people about this issue, but haven&#39;t found any concrete answers. I&#39;ve figured surely someone else has come across this problem before?</p> <p>I have structs which are used to bind data to mgo/mongodb calls. Currently, I&#39;m calling data from MongoDB, binding it to a struct, such as...</p> <pre><code>// Item - type Item struct { ID bson.ObjectId `json:&#34;id&#34;` Name string `json:&#34;name&#34; bson:&#34;name&#34;` Description string `json:&#34;description&#34; bson:&#34;description&#34;` Prices []Price `json:&#34;prices&#34; bson:&#34;prices&#34;` } </code></pre> <p>I then have a protobuf definition that matches these structs, trouble is to use the generated code, I have to manually do things like...</p> <pre><code>item := &amp;pb.Item{ Id: itemModel.ID.String(), Name: itemModel.Name, Description: itemModel.Description, Prices: convertPrices(itemModel.Prices), } </code></pre> <p>And have functions to convert all embedded structs. Which is incredibly tedious and not great performance wise.</p> <p>So are there any short cuts or plugins to be able to either use generated code for other purposes, such as database entities, or to somehow marshal between the two types?</p> <p>Here&#39;s a Stack Overflow link with more details: <a href="https://stackoverflow.com/questions/45154825/what-would-be-the-best-approach-to-converting-protoc-generated-structs-from-bson?noredirect=1#comment77278817_45154825">https://stackoverflow.com/questions/45154825/what-would-be-the-best-approach-to-converting-protoc-generated-structs-from-bson?noredirect=1#comment77278817_45154825</a></p> <hr/>**评论:**<br/><br/>yami_odymel: <pre><p>Not sure if I get your point or not, but when I was using gRPC in Golang, I treat the gRPC-generated struct like a &#34;<strong>payload object</strong>&#34; instead of a &#34;<strong>model struct</strong>&#34; (<em>which means I only use it for transferring the data between client and the server.</em>). </p> <p>So normally I would make a &#34;convertor function&#34; (like: <code>toItem(in *pb.Item) out &amp;Item</code>) for it, when I received the gRPC struct, I convert them into a local struct type.</p> <p>It sounds might be stupid but, hell. You are going to use <code>Id</code> instead of <code>ID</code> with the gRPC-generated struct in Golang when everything other is named as <code>ID</code>. And you can&#39;t even add the custom struct tag in <code>.proto</code> file (<em>which is not recommended by the official</em>), it&#39;s a pain for me when there&#39;re so many packages that require you to specify the struct tags.</p></pre>ewanvalentine: <pre><p>That&#39;s exactly what I&#39;m doing at the moment, but because it&#39;s a NoSQL database, there&#39;s 3 levels of nesting, so I have to write an iterator for each of those maps to convert each entity into the pb format. It just feels totally wrong and like I&#39;m not really getting any benefit of gRPC&#39;s performance, if I have to do a bunch of serialisation of my own before it gets serialised into binary!</p> <p>I wish there was a plugin to just include mongodb friendly tags somehow, or some kind of quick serialise method. Ah well, at least it&#39;s not just me, I guess! </p></pre>jsoniter: <pre><p>if protobuf support field tags, will this pain go away?</p></pre>ewanvalentine: <pre><p>I believe so, yes. Though there&#39;s still the issue of converting the <code>Id</code> field name into <code>ID</code> and the type from string to <code>bson.ObjectId</code>. </p></pre>shovelpost: <pre><p>I think the way <a href="/u/yami_odymel" rel="nofollow">/u/yami_odymel</a> recommends is our best bet. This is also <a href="https://github.com/upspin/upspin/blob/62ed57d0abd779585114757c9ab9b123bd6a34cb/upspin/proto/proto.go#L38" rel="nofollow">how the upspin team does it</a>.</p> <p>It is very unfortunate that the protobuf team <a href="https://github.com/golang/protobuf/issues/156" rel="nofollow">have decided not to fix</a> the non idiomatic field names like Id.</p></pre>recurrency: <pre><p>if it&#39;s about the struct tags I think gogo/protobuf has an extension for that? <a href="https://github.com/gogo/protobuf/" rel="nofollow">https://github.com/gogo/protobuf/</a></p></pre>recurrency: <pre><p>exemple: <a href="https://github.com/gogo/protobuf/blob/master/test/tags/tags.proto" rel="nofollow">https://github.com/gogo/protobuf/blob/master/test/tags/tags.proto</a></p></pre>ewanvalentine: <pre><p>Not just that unfortunately, there&#39;s converting the Id field to and from <code>Id string</code> and <code>ID bson.ObjectId</code> </p></pre>TheMerovius: <pre><p>It&#39;s pretty trivial, to write your own <a href="https://developers.google.com/protocol-buffers/docs/reference/other" rel="nofollow">protoc-plugin</a>, FWIW. If your conversion is sufficiently automatic, it might be worth giving that a try.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

664 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传