Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move implementation into the type EndpointExtractor class
- Loading branch information
1 parent
d02bc6b
commit ed507ff
Showing
10 changed files
with
143 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ language: scala | |
sudo: false | ||
scala: | ||
- 2.11.8 | ||
- 2.10.6 | ||
jdk: | ||
- openjdk7 | ||
- oraclejdk7 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
controller/src/main/scala/ru/arkoit/finchrich/controller/EndpointExtractor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ru.arkoit.finchrich.controller | ||
|
||
import ru.arkoit.finchrich.controller.internal.FinchRichMacro | ||
import scala.language.experimental.macros | ||
import io.finch.Endpoint | ||
|
||
trait EndpointExtractor[A <: Controller] { | ||
type R | ||
|
||
def apply(c: A): Endpoint[R] | ||
} | ||
|
||
object EndpointExtractor { | ||
type Aux[C <: Controller, RR] = EndpointExtractor[C] { type R = RR } | ||
|
||
def apply[A <: Controller](implicit t: EndpointExtractor[A]): Aux[A, t.R] = t | ||
|
||
implicit def materialize[A <: Controller, R]: Aux[A, R] = | ||
macro FinchRichMacro.materialize[A, R] | ||
|
||
trait Ops[A <: Controller, R] { | ||
def toEndpoint: Endpoint[R] | ||
} | ||
|
||
trait ToOps { | ||
implicit def toControllerToEndpointOps[A <: Controller] | ||
(c: A)(implicit cte: EndpointExtractor[A]): Ops[A, cte.R] = | ||
new Ops[A, cte.R] { | ||
override def toEndpoint: Endpoint[cte.R] = cte(c) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
controller/src/main/scala/ru/arkoit/finchrich/controller/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package ru.arkoit.finchrich | ||
|
||
import ru.arkoit.finchrich.controller.internal.FinchRichMacro | ||
import scala.language.experimental.macros | ||
import io.finch.Endpoint | ||
|
||
package object controller { | ||
def controllerToEndpoint[T <: Controller](cnt: T): Any = macro FinchRichMacro.controllerToEndpoint[T] | ||
package object controller extends EndpointExtractor.ToOps { | ||
@deprecated("Use toEndpoint on the controller instance instead", "0.2.0") | ||
def controllerToEndpoint[T <: Controller](cnt: T) | ||
(implicit cte: EndpointExtractor[T]): Endpoint[cte.R] = | ||
cte(cnt) | ||
} |
15 changes: 15 additions & 0 deletions
15
controller/src/test/scala/ru/arkoit/finchrich/controller/DeprecatedFinchRichSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.arkoit.finchrich.controller | ||
|
||
import io.finch._ | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class DeprecatedFinchRichSpec extends FlatSpec with Matchers with Fixtures { | ||
behavior of "controllerToEndpoint (deprecated)" | ||
|
||
it should "transform controller to an endpoint" in { | ||
val cnt = new ComplexController() | ||
val ep = controllerToEndpoint(cnt) | ||
checkEndpointType(ep) | ||
ep.toServiceAs[Text.Plain] | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
controller/src/test/scala/ru/arkoit/finchrich/controller/EndpointExtractorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.arkoit.finchrich.controller | ||
|
||
import io.finch._ | ||
import org.scalatest.{FlatSpec, Inside, Matchers} | ||
|
||
class EndpointExtractorSpec extends FlatSpec with Matchers with Fixtures { | ||
behavior of "EndpointExtractor" | ||
|
||
it should "transform controller to an endpoint" in { | ||
val cnt = new ComplexController() | ||
val ep = cnt.toEndpoint | ||
checkEndpointType(ep) | ||
ep.toServiceAs[Text.Plain] | ||
} | ||
} |
60 changes: 0 additions & 60 deletions
60
controller/src/test/scala/ru/arkoit/finchrich/controller/FinchRichSpec.scala
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
controller/src/test/scala/ru/arkoit/finchrich/controller/Fixtures.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ru.arkoit.finchrich.controller | ||
|
||
import shapeless._ | ||
import io.finch._ | ||
import scala.reflect.runtime.universe._ | ||
|
||
trait Fixtures { | ||
implicit def universalEncoder[A](implicit tenc: Encode.Aux[String, Text.Plain]): Encode.Aux[A, Text.Plain] = | ||
Encode.text[A]((a, c) => tenc(a.toString, c)) | ||
|
||
type ComplexControllerEPType = Boolean :+: String :+: Int :+: Int :+: String :+: Unit :+: String :+: CNil | ||
|
||
class ControllerA extends Controller { | ||
val ep = get("foo") { Ok(10) } | ||
def ep2 = get("bar" :: string) { s: String => Ok(s)} | ||
} | ||
|
||
class ControllerB extends Controller { | ||
val ep = get("hehe") { Ok(true) } | ||
val ep2 = get("jjj") { Ok("hhh") } | ||
} | ||
|
||
case class CaseClassController ( | ||
ep: Endpoint[Int], | ||
cnt: ControllerA | ||
) extends Controller | ||
|
||
class ComplexController extends Controller { | ||
val cnt = new ControllerB | ||
val cnt2 = CaseClassController( | ||
ep = get("baz") { Ok(10) }, | ||
cnt = new ControllerA | ||
) | ||
val ep = get("hojjk") { Ok() } | ||
val ep2 = get("mmmmm") { Ok("koko") } | ||
} | ||
|
||
def checkEndpointType[T: WeakTypeTag](b: Endpoint[T]): Boolean = { | ||
weakTypeOf[T] match { | ||
case x if x =:= weakTypeOf[ComplexControllerEPType] => true | ||
case _ => false | ||
} | ||
} | ||
} |