基于字段值的Protobuf oneof

huangapple go评论93阅读模式
英文:

Protobuf oneof based on a field value

问题

我想使用proto3语法编写一个与下面的TypeScript类型匹配的Protobuf文件。基本上,我想让value类型取决于field字符串是什么。如果必要的话,我可以处理额外的对象嵌套技巧。这是否可行?

type UpdateName = {
  field: 'name',
  value: string
}

type UpdateAge = {
  field: 'age',
  value: number
}

type UpdateRequest = {
  fields: Array<UpdateName | UpdateAge>;
};
英文:

I want to write a Protobuf file using proto3 syntax that matches the Typescript types below. Basically I would like the value type to be dependent on what the field string is. I can deal with extra object nesting hacks if necessary. Is this possible?

type UpdateName = {
  field: &#39;name&#39;,
  value: string
}

type UpdateAge = {
  field: &#39;age&#39;,
  value: number
}

type UpdateRequest = {
  fields: Array&lt;UpdateName | UpdateAge&gt;;
};

答案1

得分: 1

是的,我认为是这样的:

消息 更新
{
    oneof 类型 {
        字符串 名称 = 1;
        整数 年龄 = 2;
    }
}

消息 更新请求
{
    重复 更新 更新 = 3;
}

看起来差不多正确。

英文:

Yes, I should think so:

message Update
{
    oneof type {
        string name = 1;
        integer age = 2;
    }
}

message UpdateRequest
{
    repeated Update updates = 3;
}

looks about right.

huangapple
  • 本文由 发表于 2023年8月11日 04:46:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定