简体   繁体   English

如何在grpc-web中将枚举值转换为字符串

[英]How to turn enum values to strings in grpc-web

I have been working on a web application using React-Redux and gRPC. 我一直在使用React-Redux和gRPC开发Web应用程序。 The server sends the frontend gRPC messages which the frontend receives with the enums in the form of integers. 服务器以整数形式发送前端接收的前端gRPC消息和枚举。 How can I turn these integer values to their corresponding string values? 如何将这些整数值转换为其相应的字符串值?

I have tried the methods in the protoc generated _pb.js files (.toObject(), get<Enum>() , etc.) 我已尝试在协议生成的_pb.js文件(.toObject(), get<Enum>()等)中使用这些方法。

message Example {
    ExampleType type = 2;
}

enum ExampleType {
    UNKNOWN_TYPE = 0;
    TEST_TYPE = 1;
    OTHER_TYPE = 2;
}

I expect the following shape of the message.toObject() JSON object: 我期望message.toObject() JSON对象具有以下形状:

{ type: string } 

However, I receive the following: 但是,我收到以下信息:

{ type: integer }

I am afraid you cannot get a string value for that. 恐怕您无法为此获得字符串值。 web protobuf does that for a reason. 网络protobuf这样做是有原因的。 If it allows string enum, there will be more code needed to specify those enum names, which is not good for code size. 如果允许使用字符串枚举,则将需要更多代码来指定这些枚举名称,这对代码大小不利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM