使用Protobuf生成带有显示名称类型属性的枚举

发布于 2025-01-25 11:46:43 字数 863 浏览 2 评论 0原文

一直在试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2025-02-01 11:46:43

经过此帖子如何访问proto.enumvalueoptions扩展值< /a>

不知道GetExtension方法,但基本上是在查看该答案之后,这就是我想到的:

func TestThis(t *testing.T) {
 vals := enums.TestEnum_ThisIsATest.Descriptor().Values()
 valDesc := vals.ByName("ThisIsATest")
 ext := proto.GetExtension(valDesc.Options(), enums.E_TestValueOption)
 fmt.Println(ext)
}

现在,我只需要弄清楚如何将其抽象(如果可能的话),这样我就可以在此期间重复使用它序列化/次要化

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:

func TestThis(t *testing.T) {
 vals := enums.TestEnum_ThisIsATest.Descriptor().Values()
 valDesc := vals.ByName("ThisIsATest")
 ext := proto.GetExtension(valDesc.Options(), enums.E_TestValueOption)
 fmt.Println(ext)
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文