Generic resource routes issues

18 views
Skip to first unread message

Ryan Proud

unread,
Aug 28, 2015, 11:18:28 AM8/28/15
to spray.io User List
I'm attempting to create a trait which will define a default set of routes given a path, but I'm running into an issue illegal inheritance issue.  I'm just wondering if there's a way around this.  It's basically my inexperience with Scala that is preventing me from seeing a solution, or lack of one.  Any help would be appreciated.

trait DefaultResourceRoutes[M <: Serializable] {
 
this: HttpService with Actor =>

 
def resourceRoutes(resourceName: String)(implicit um: FromRequestUnmarshaller[M]) = path(resourceName) {
   
get {
      complete
(s"$resourceName-getAll")
   
} ~
    post
{
      entity
(as[M]) { entity =>
        complete
(s"$resourceName-create")
     
}
   
} ~
      patch
{
        entity
(as[M]) { entity =>
          complete
(s"$resourceName-update")
       
}
     
}
 
} ~
    path
(resourceName / LongNumber) { entityId =>
     
get {
        complete
(s"$resourceName-get")
     
}
   
} ~
    path
(resourceName / "name" / Segment) { namePattern =>
     
get {
        complete
(s"$resourceName-getByName")
     
}
   
}
}


object Boot extends App {

 
implicit val system = ActorSystem("API")

 
/* Use Akka to create our Spray Service */
  val router
= system.actorOf(Props[Router], "api-router")

 
/* and bind to Akka's I/O interface */
  IO
(Http) ! Http.Bind(router, "0.0.0.0", 8888)
}

class Router extends HttpServiceActor with ApiService with ActorLogging {
 
def receive = runRoute(fooRoutes ~ barRoutes)
}

trait
ApiService extends HttpService with FooService with BarService

trait
FooService extends HttpService with Actor with DefaultResourceRoutes[Foo] {
 
import Json4sProtocol._
 
def fooRoutes = resourceRoutes("foo")
}

trait
BarService extends HttpService with Actor with DefaultResourceRoutes[Bar] {
 
import Json4sProtocol._
 
def barRoutes = resourceRoutes("bar")
}

object Json4sProtocol extends Json4sSupport {
 
override implicit def json4sFormats: Formats = DefaultFormats
}
[error] src/main/scala/Boot.scala:19: illegal inheritance;
[error]  class Router inherits different type instances of trait DefaultResourceRoutes:
[error] DefaultResourceRoutes[Bar] and DefaultResourceRoutes[Foo]
[error] class Router extends HttpServiceActor with ApiService with ActorLogging {
[error]       ^
[error] src/main/scala/Boot.scala:23: illegal inheritance;
[error]  trait ApiService inherits different type instances of trait DefaultResourceRoutes:
[error] DefaultResourceRoutes[Bar] and DefaultResourceRoutes[Foo]
[error] trait ApiService extends FooService with BarService
[error]       ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed 28-Aug-2015 12:15:04 PM

Dependencies if anyone cares: https://gist.github.com/anonymous/708b6cdd1c1a66b73c53
Reply all
Reply to author
Forward
0 new messages