[英]Can you represent an object that can have arbitrary fields in proto3?
Consider the following json representation of an object考虑以下 object 的 json 表示
{
"format": "0.0.1",
"has_impl": true,
"mtv_1b": 1,
"mtv_1c": "h",
"ktc_12": true,
"ktc_zz": true,
}
The first two format
and has_impl
fields are known.前两个
format
和has_impl
字段是已知的。 In addition, the object may have arbitrary number of mtv_XX
and ktc_XX
like fields.此外,object 可能具有任意数量的
mtv_XX
和ktc_XX
类字段。
Is such an object representable in proto3 and how would you go at it?这样的 object 是否可以在 proto3 中表示,您将如何处理 go?
The following could be an obvious starting point.以下可能是一个明显的起点。 Are there a combination of oneOf , WellKnownTypes that could be used here?
是否可以在这里使用oneOf 、 WellKnownTypes的组合?
message MyObject {
string format = 0;
bool has_impl = 1;
// Is there anything that can go in here ?
....
}
Not directly.不是直接的。 The closest you can do would be to have a
Struct
(which is a map<string, Value>
, where Value
is a oneof
over common types), using struct.proto .您可以做的最接近的方法是使用
oneof
拥有一个Struct
(这是一个map<string, Value>
,其中Value
是常见类型的一个)。 Not quite the same, but allows the same ideas.不完全相同,但允许相同的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.