如何将结果从一个源流传递到另一个

时间:2018-12-16 03:10:49

标签: scala akka akka-stream

我有一个处理Source并返回的方法。我正在尝试修改它,但似乎无法返回相同的内容:

原始

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

1 个答案:

答案 0 :(得分:0)

  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(在您调用calculate(然后不需要for)时或之后。通常,这不是使用期货的方法

相关问题