是否可以在Google protobuf中为类型(枚举或消息)定义别名? [英] Is it possible to define an alias for type (enum or message) in google protobuf?

查看:1004
本文介绍了是否可以在Google protobuf中为类型(枚举或消息)定义别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的原型文件中有两个枚举,它们定义了几乎相同的值。

I have two enums in my proto file which define almost the same values.

是否可以剪掉其中一个并保留别名以保留所有值

Is it possible to cut out one of them and to leave an alias to keep all the code working?

示例:

enum A {
   a = 0;
   b = 1;
}
enum B {
   a = 0;
   b = 1;
}

我想在c ++中使用类似typedef的东西:

I want to have something like typedef in c++:

enum A {
   a = 0;
   b = 1;
}

typedef A B;

我在文档中找不到此内容。有任何解决方法吗?

I haven't found this in documentation. Are there any workarounds ?

推荐答案

这是一个老问题,但是如果有人仍然有兴趣,
是可以立即使用protobuf在枚举上创建别名

This is an old question, but if some people are still interested, It is possible to create alias on enum with protobuf now

enum EnumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
}
enum EnumNotAllowingAlias {
  UNKNOWN = 0;
  STARTED = 1;
  // RUNNING = 1;  // Uncommenting this line will cause a compile error inside Google and a warning message outside.
}

如官方文档中所述此处。您只需要通过添加 option allow_alias = true;

As explained in the official documentation here. You just need to enable the alias option by adding option allow_alias = true;

这篇关于是否可以在Google protobuf中为类型(枚举或消息)定义别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆