scala - 如何将结果从一个源流传递到另一个源流
问题描述
我有一个处理 aSource
并返回的方法。我正在尝试修改它,但似乎无法返回相同的内容:
原来的
def originalMethod[as: AS, mat: MAT, ec: EC](checkType: String)
: Flow[ByteString, MyValidation[MyClass], NotUsed]{
collectStuff
.map { ts =>
val errors = MyEngine.checkAll(ts.code)
(ts, errors)
}
.map { x =>
x._2
.leftMap(xs => {
addInformation(x._1, xs.toList)
})
.toEither
}
}
我正在通过使用另一个源进行修改并将结果传递给原始源,但返回相同的内容:
def calculate[T: AS: MAT](source: Source[T, NotUsed]): Future[Seq[T]] =
{
source.runWith(Sink.seq)
}
def modifiedMethod[as: AS, mat: MAT, ec: EC](checkType: String, mySource: Source[LoanApplicationRegister, NotUsed])
: Flow[ByteString, MyValidation[MyClass], NotUsed]{
for {
calc <- calculate(mySource)
orig <- collectStuff
.map { ts =>
val errors = MyEngine.checkAll(ts.code, calc)
(ts, errors)
}
.map { x =>
x._2
.leftMap(xs => {
addInformation(x._1, xs.toList)
})
.toEither
}
}
yield {
orig
}
}
但我收到编译错误Expression of type Future[Nothing] doesn't conform to existing type Flow[ByteString, MyValidation[MyClass]
Flow[ByteString, MyValidation[MyClass]
我怎样才能modifiedMethod
像originalMethod
以前一样返回
解决方案
for { calc <- calculate(mySource)}
yield {
collectStuff
.map { ts =>
val errors = MyEngine.checkAll(ts.code, calc)
(ts, errors)
}
.map { x =>
x._2
.leftMap(xs => {
addInformation(x._1, xs.toList)
})
.toEither
}
}
会给你一个Future[Flow[ByteString, MyValidation[MyClass], NotUsed]]
而不是,Future[Nothing]
但如果你想删除Future
你需要在Await
某个地方为它(当你调用计算时(然后你不需要for
)或在它之后。通常,这不是使用期货的方式
推荐阅读
- sql - 如何构建丢弃重复数据的 SQL 查询?
- python - 如何将字符串年-日-年-毫秒转换为日期时间对象
- javascript - 如何优化列表所有猫鼬查询?
- javascript - 在 Node.js 和浏览器中排序数组结果不同
- django - django 模型保存方法的奇怪行为。“(1048,“列'created_at'不能为空”)“在具有auto_now_add = True的字段上
- symfony - OVH 服务器上的 symfony 4.3,缓存清除返回错误 255(同时 composer update / heroku deploy from github)
- azure - 使用 Get-AzTable Powershell 脚本从 Azure 表访问数据时出错
- c - 编写一个在 C 中创建锯齿状 2D 零数组的函数
- android - Android 深层链接总是先在浏览器中打开,然后才是正确的活动
- flutter - 如何在 Flutter 中删除 TextFormField 中的错误消息