<p>I've spoken to a few people about this issue, but haven't found any concrete answers. I'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'm calling data from MongoDB, binding it to a struct, such as...</p>
<pre><code>// Item -
type Item struct {
ID bson.ObjectId `json:"id"`
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Prices []Price `json:"prices" bson:"prices"`
}
</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 := &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'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 "<strong>payload object</strong>" instead of a "<strong>model struct</strong>" (<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 "convertor function" (like: <code>toItem(in *pb.Item) out &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't even add the custom struct tag in <code>.proto</code> file (<em>which is not recommended by the official</em>), it's a pain for me when there're so many packages that require you to specify the struct tags.</p></pre>ewanvalentine: <pre><p>That's exactly what I'm doing at the moment, but because it's a NoSQL database, there'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'm not really getting any benefit of gRPC'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'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'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'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's converting the Id field to and from <code>Id string</code> and <code>ID bson.ObjectId</code> </p></pre>TheMerovius: <pre><p>It'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>
What would be the best way to re-use the generated protobuf structs for other purposes?
blov · · 664 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传