具有数据和行为的 Thrift 枚举定义
关于 thrift 的新手问题,如果其他人处于尝试从 thriftIDL 生成以下 java 类的情况。我看到它只支持分配的 32 位非负整数值。这是因为其他语言缺乏对以下枚举类型的支持吗?我没有看到这种情况是否可以在 Thrift IDL 中完成。
public enum ExceptionTypes {
ERROR_THIS("APP_EXP_001","Some message"), ERROR_THAT(...etc
private String errorCode;
private String defaultMessage;
private ExceptionTypes(String errorCode, String defaultMessage) {
this.errorCode = errorCode;
this.defaultMessage = defaultMessage;
}
public String getErrorCode() {
return this.errorCode;
}
public String getDefaultMessage() {
return this.defaultMessage;
}
}
A newbie question on thrift if anyone else is in a situation trying to generate the below java class from thriftIDL. I see it only supports 32-bit non negative integer values assigned. Is this because of lack of support for below enum types in other languages? I did not see if this kind can be done in thrift IDL.
public enum ExceptionTypes {
ERROR_THIS("APP_EXP_001","Some message"), ERROR_THAT(...etc
private String errorCode;
private String defaultMessage;
private ExceptionTypes(String errorCode, String defaultMessage) {
this.errorCode = errorCode;
this.defaultMessage = defaultMessage;
}
public String getErrorCode() {
return this.errorCode;
}
public String getDefaultMessage() {
return this.defaultMessage;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法向 Thirft 枚举添加行为描述,主要是因为 Thirft 本来就很小并且适用于多种语言。
但是,您可以自己编写一个构造函数,在给定 thirft 枚举时创建一个像上面这样的 java 枚举。在您从 thrift 层接收到数据后,这将在代码中添加一层处理,但在实际用例中,线路上使用的数据结构很少与应用程序内部使用的数据结构完全相同。
Thirft(以及任何 rpc、CORBA 或类似层)用于简化网络代码并增加稳健性,而不是隐藏其存在以便人们可以忘记它。
There is no way to add description of behavior to the Thirft enums, mainly because Thirft was intended to be small and applicable to many languages.
However, you could write yourself a constructor that creates one java enum like above when given thirft one. This will add one layer of processing in your code after you have received data from the thrift layer, but in real use cases, data structures used on the wire are rarely exactly the same as ones used inside the app anyway.
Thirft (and any rpc, CORBA or similar layer) serves to simplify network code and add robustness, not to hide its existence so that one could forget about it.