如何在 Thrift 服务方法中 return 多个值?
How to return more than one value in a Thrift service method?
我在 IDL 中定义了一个 thrift 服务方法 ImportantData GetImportantData()
。我想 return 此方法的状态代码。我想做一些类似 Status GetImportantData(ImportantData&)
的事情,有办法吗?
我已经学习了多个节俭教程,例如
以身作则
最简单的方法 - 在 ImportantData
.
中包含 Status
Thrift 必须是可移植的——并非每种语言都支持输出参数(引用等),因此 Thrift 也不支持它。所以只能有一个 returned 对象。
通常最好的解决方案,提供良好的未来兼容性等是创建 FunctionReturn
结构(在您的示例中包含 ImportantData
和 Status
)。这样,如果需要,可以添加更多 return 值。
顺便说一句:如果 Status
表示可能的错误并且大多数时候会是 StatusOk
,请考虑 returning ImportantData
并抛出异常( Thrift 支持)如果发生错误。这样的解决方案在 C++ 中看起来会更好(一如既往 - 异常情况 - 错误 - 应该被异常覆盖)。
我在 IDL 中定义了一个 thrift 服务方法 ImportantData GetImportantData()
。我想 return 此方法的状态代码。我想做一些类似 Status GetImportantData(ImportantData&)
的事情,有办法吗?
我已经学习了多个节俭教程,例如 以身作则
最简单的方法 - 在 ImportantData
.
Status
Thrift 必须是可移植的——并非每种语言都支持输出参数(引用等),因此 Thrift 也不支持它。所以只能有一个 returned 对象。
通常最好的解决方案,提供良好的未来兼容性等是创建 FunctionReturn
结构(在您的示例中包含 ImportantData
和 Status
)。这样,如果需要,可以添加更多 return 值。
顺便说一句:如果 Status
表示可能的错误并且大多数时候会是 StatusOk
,请考虑 returning ImportantData
并抛出异常( Thrift 支持)如果发生错误。这样的解决方案在 C++ 中看起来会更好(一如既往 - 异常情况 - 错误 - 应该被异常覆盖)。