基于字段值的Protobuf oneof

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

Protobuf oneof based on a field value

问题

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

  1. type UpdateName = {
  2. field: 'name',
  3. value: string
  4. }
  5. type UpdateAge = {
  6. field: 'age',
  7. value: number
  8. }
  9. type UpdateRequest = {
  10. fields: Array<UpdateName | UpdateAge>;
  11. };
英文:

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?

  1. type UpdateName = {
  2. field: &#39;name&#39;,
  3. value: string
  4. }
  5. type UpdateAge = {
  6. field: &#39;age&#39;,
  7. value: number
  8. }
  9. type UpdateRequest = {
  10. fields: Array&lt;UpdateName | UpdateAge&gt;;
  11. };

答案1

得分: 1

是的,我认为是这样的:

  1. 消息 更新
  2. {
  3. oneof 类型 {
  4. 字符串 名称 = 1;
  5. 整数 年龄 = 2;
  6. }
  7. }
  8. 消息 更新请求
  9. {
  10. 重复 更新 更新 = 3;
  11. }

看起来差不多正确。

英文:

Yes, I should think so:

  1. message Update
  2. {
  3. oneof type {
  4. string name = 1;
  5. integer age = 2;
  6. }
  7. }
  8. message UpdateRequest
  9. {
  10. repeated Update updates = 3;
  11. }

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:

确定