Seq[Directive] to Directive[Seq]

Hi to all!
I’m facing this problem with directives and I do not know how to proceed.

I have a sequence of directive and i would like to transform them into a directive that provides to the inner the sequence of the result. Something like Future.sequence() does on a sequence of futures…

So, briefly, i have a Seq[Directive[Tuple(A,B)] and i want a Directive[Seq[Tuple(A,B)]].

Is it something possible? or i am looking at this problem from the wrong point of view?

Here’s a non-tail-recursive, strict version (untested):

  def sequence[L: Tuple](directives: Seq[Directive[L]]): Directive1[Seq[L]] = {
    Directive[Tuple1[Seq[L]]] { routeCons ⇒
      def inner(collected: Seq[L], remaining: Seq[Directive[L]]): Route = remaining match {
        case first +: rest ⇒
          first.tapply { next ⇒
            inner(collected :+ next, rest)
        case Nil ⇒

      inner(Seq.empty, directives)

I would be interested to know what you are using this for?


Thanks for the answer, i will test it.

By the way, i need something like this because i need to upload multiple files (the number of the files and the name of the fields of the form is known only at runtime), so i need to iterate the fields of the form to upload the files, and this generate a sequence of directive, incompatible with directive composition.

Can you suggest to me a better way to achieve this?

This will probably not work. Because if the files are big enough, the stream of parts of a multipart upload needs to be read in order and can only be read once.

Have you already seen the fileUploadAll directive? It buffers all uploaded files to disk and gives you random access to the uploaded files afterwards.

If the field name is not known, that’s currently difficult to achieve. I would probably copy the source code of the FileUploadDirectives and remove the fieldName filter in the storeUploadedFiles directive.