从 protobuf 生成的文件中获取可更改的对象
我有一个相当简单的 Android 应用程序,它从 Web 服务器获取 protobuf 数据,并有一个由 protoc 生成的 java 类。
现在我的应用程序中除了此数据之外不需要任何其他数据。所以我想我会简单地使用这些数据而不是包装它,因为这只是很多额外的工作。
然而,有时我用新数据(原始文件中的嵌套消息)扩展树,然后我需要能够将这些数据插入到当前树中,这是不允许的,因为 java 类总是返回不可修改的列表。
我现在认为我可以通过反射获取列表,从而可以修改。这看起来不是一个很好的解决方案,但它胜过做额外的工作来包装类和其中的所有内容。所以我真正的问题是,这是一个大问题还是可以。
我在某处读到,即使在谷歌内部,他们也争论将 protobuf 对象作为数据结构或仅作为纯粹的发送/接收消息是否明智。
I have a fairly simple android app, it gets protobuf data from a web server and has a java class generated by protoc.
Now I have no need for any other data than this data in my app. So i thought I would simply use this data rather than wrapping it since this would just be a lot of extra work.
However sometimes i expand the tree with new data (nested messages in proto file) and then i need to be able to insert these in the current tree which is not allowed since the java class always returns unmodifiableList.
I am now thinking that i could get the list with reflection, thus modifiable. It does not seem like a great solution but it beats doing extra work for wrapping the class and all the stuff therein. So my actual question is, is this a big no no or would it be okay.
I read somewhere that even within google they argue if it's smart having the protobuf objects as data structure or only as pure send/recieve messages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Protocol Buffer 消息实例是不可变的设计。
如果您想操作最初来自 protobuf 的数据,请创建一个新的 Builder,使用 mergeFrom 预填充原始字段:
Protocol Buffer messages instances are immutable by design.
If you want to manipulate data originally from a protobuf, create a new Builder, using mergeFrom to prepopulate the original fields: