使用Protobuf生成带有显示名称类型属性的枚举
一直在试图在C#中巩固我们的枚举,并通过Protobuf列入库,目前陷入困境,试图弄清楚如何保留枚举的“显示名称”属性。例如:Enum Spousex的显示名称为“配偶前”。我们在C#中这样做的正常方式是在属性中定义它,在GO中,我们将覆盖String()方法。但不确定如何使用Protobuf进行操作。
我在网上看,看来我可能需要使用选项/自定义 https://developers.google.com/protocol.com/protocol-buffers/docs/docs/proto3,定制这是我最终使用的方法
syntax = "proto3";
import "google/protobuf/descriptor.proto";
package enums.test;
option go_package = "github.com/tester/common/proto/enums;enums";
extend google.protobuf.EnumValueOptions {
string enum_value_option = 5000;
}
enum TestEnum {
NA = 0;
ThisIsATest = 1 [(enum_value_option) = "this is a test"];
}
,但是生成的输出并没有表明“这是测试”已编译。确实,在测试枚举的字符串()函数时,我仍然只是获得“ Thisisatest”。请注意,原始3中选项的语法与proto2的语法略有不同。
Been trying to consolidate our enums in our C# and Go libraries via protobuf and am currently stuck trying to figure out how to retain the 'Display Name' attribute of the enum. eg: Enum SpouseEx would have a display name of 'spouse ex'. The normal way we do this in C# is to define it in an attribute and in Go, we'd override the String() method. But not sure how to go about it using Protobuf.
I looked online and it seems I might need to use options/ custom
https://developers.google.com/protocol-buffers/docs/proto3#customoptions and this is what I ended up using
syntax = "proto3";
import "google/protobuf/descriptor.proto";
package enums.test;
option go_package = "github.com/tester/common/proto/enums;enums";
extend google.protobuf.EnumValueOptions {
string enum_value_option = 5000;
}
enum TestEnum {
NA = 0;
ThisIsATest = 1 [(enum_value_option) = "this is a test"];
}
But the generated output doesn't give any indication that "this is a test" got compiled. Indeed, while testing the enum's string() function, I'm still just getting "ThisIsATest". Please note that the syntax for options is slightly different in proto3 than that of proto2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过此帖子如何访问proto.enumvalueoptions扩展值< /a>
不知道GetExtension方法,但基本上是在查看该答案之后,这就是我想到的:
现在,我只需要弄清楚如何将其抽象(如果可能的话),这样我就可以在此期间重复使用它序列化/次要化
Figured it out after going through this SO post How to access proto.EnumValueOptions extension values
Did not know about the GetExtension method but basically after looking at that answer, this is what I came up with:
Now I just need to figure out how to abstract this out (if it's even possible) so I can reuse it during serialization/deserialization