【问题标题】:Can you represent an object that can have arbitrary fields in proto3?你能代表一个在 proto3 中可以有任意字段的对象吗?
【发布时间】:2020-06-04 06:29:10
【问题描述】:

考虑以下对象的 json 表示

{
   "format":  "0.0.1",
   "has_impl": true,

   "mtv_1b": 1,
   "mtv_1c": "h",
   "ktc_12": true,
   "ktc_zz": true,
}

前两个 formathas_impl 字段是已知的。此外,对象可能具有任意数量的mtv_XXktc_XX 类似字段。

这样的对象是否可以在 proto3 中表示,您将如何处理?

以下可能是一个明显的起点。是否有oneOfWellKnownTypes 的组合可以在这里使用?

message MyObject {

   string format  = 0;
   bool has_impl = 1;


   // Is there anything that can go in here ? 
   ....

}

【问题讨论】:

    标签: protocol-buffers proto


    【解决方案1】:

    不直接。您可以做的最接近的方法是使用struct.proto 使用Struct(这是map<string, Value>,其中Valueoneof 而不是常见类型)。不完全相同,但允许相同的想法。

    【讨论】:

    • Struct 方法中,我将失去明确定义formathas_impl 字段的能力,对吗?
    • @HakanBaba 你将失去定义在同一级别的能力;你可以有一个类型有formathas_impl 一个Struct,虽然
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2022-11-23
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    相关资源
    最近更新 更多