英文:
How to merge values of two protobuff of the same type?
问题
我有一个用于我的服务配置的原型,并通过不同的方式(s3、env、db等)进行读取。
我希望将它们所有的值(按顺序)合并到一个单独的配置中,这样如果有任何缺失的属性,将会被添加进去,如果有重复的值,将会用优先级较高的值进行替换。
英文:
I have a proto for my service configs and read it through different ways ( s3, env, db, etc. ).
I want two merge values of all of them ( with order ) into a single config, so if any attribute that is missing will be added or if there are duplicate values, the higher priority one's value gets replaced.
答案1
得分: 1
根据评论,proto
包中有一个Merge函数:
Merge函数将src合并到dst中,其中dst必须是具有相同描述符的消息。
src中的填充标量字段将被复制到dst中,而src中的填充的单个消息将通过递归调用Merge函数合并到dst中。src中每个列表字段的元素都将附加到dst中相应的列表字段中。src中每个映射字段的条目将被复制到dst中相应的映射字段中,可能会替换现有条目。src的未知字段将附加到dst的未知字段中。
英文:
As per the comments there is a Merge function in the proto
package which:
>Merge merges src into dst, which must be a message with the same descriptor.
>
>Populated scalar fields in src are copied to dst, while populated singular messages in src are merged into dst by recursively calling Merge. The elements of every list field in src is appended to the corresponded list fields in dst. The entries of every map field in src is copied into the corresponding map field in dst, possibly replacing existing entries. The unknown fields of src are appended to the unknown fields of dst.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论