2

I am trying to compose multiple Endpoint together when starting the http server. Multiple Endpoints are defined like this:

  val foo = get("foo") { Ok("bar") }
  val test = get("test") { Ok("test") }

This code is working

  foo :+: test

However, this code doesnt work.

  List(foo, test).reduceLeft(_ :+: _)

The error is

 type mismatch;
 found   : io.finch.Endpoint[shapeless.:+:[String,shapeless.:+:[String,shapeless.CNil]]]
 required: io.finch.Endpoint[String]
 val controllers = List(foo, test).reduce(_ :+: _)
                                         ^

I dont quite understand why reduce wont work here and what is the best practice to combine Endpoint in Finch

Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53
  • May I suggest you to look at my project: https://github.com/akozhemiakin/finchrich It may help you to combine endpoints in a less verbose way. – Artyom Kozhemiakin Jul 18 '16 at 20:52

1 Answers1

3

why reduce wont work here

If you have x: Endpoint[String], y: Endpoint[String] then x :+: y returns a Endpoint[Coproduct[String, String]]1 - importantly it is not the same type as x or y

Take a look at the reduce2 signature:

List[A].reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1

You have a List[Endpoint[String]] - the op must take arguments x: Endpoint[String], y: Endpoint[String] and return a Endpoint[String] - but :+: will return Endpoint[Coproduct[String, String]]

best practice to combine Endpoint in Finch

I haven't used Finch but all the examples in the repo simply combine the endpoints with :+:

https://github.com/finagle/finch/blob/e3a62bf9a1cb26af40af428dd9be8b2dc3339c44/examples/src/main/scala/io/finch/todo/Main.scala#L83

getTodos :+: postTodo :+: deleteTodo :+: deleteTodos :+: patchTodo

Collections (like List) are super useful if you are going to manipulate the elements at runtime - is that a use case you need to support? I've generally found that I know all my routes at compile time


1. Some liberties were taken with the Coproduct type signature - Coproduct[String, String] rather than shapeless.:+:[String, shapeless.:+:[String, shapeless.CNil]]

2. A similar argument holds for reduceLeft

wmmeyer
  • 466
  • 3
  • 4