How does the parameters directive convert it's own parameter list to a TupleN return value?

I’m wanting to implement a similar functionality to the parameters directive for my own API. I understand the Magnet pattern it uses, but what I haven’t figured out is how it takes a list of parameters and turns it into a tuple such that one can write:

parameters('color, 'backgroundColor.?) { (color, backgroundColor) => ...

What might the implicit conversion for something like that look like?

I think the relevant code here is https://github.com/akka/akka-http/blob/master/akka-http/src/main/scala/akka/http/scaladsl/server/directives/ParameterDirectives.scala#L186

Indeed it is not so straightforward :smile:.

At the implementation side, it takes the ‘left parameters directive’ which is a Directive that produces a value of ‘Type A’ (Directive[TA]). It then takes the ‘right parameter definition’ that, given a value of type P produces a Directive that produces values of ‘type B’ (Directive[TB]). Then it uses ‘normal’ directive composition & to combine these 2 directives together.

How this works at the type level is all a bit harder to explain (and I’m not sure I fully understand it myself), but this perhaps might be a starting point?

1 Like