-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5584ddb
commit a45e9ae
Showing
7 changed files
with
94 additions
and
17 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
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
11 changes: 11 additions & 0 deletions
11
generic/src/main/scala/io/finch/generic/GenericDerivation.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,11 @@ | ||
package io.finch.generic | ||
|
||
import io.finch._ | ||
import shapeless._ | ||
|
||
final class GenericDerivation[A] { | ||
def fromParams[Repr <: HList](implicit | ||
gen: LabelledGeneric.Aux[A, Repr], | ||
fp: FromParams[Repr] | ||
): Endpoint[A] = fp.endpoint.map(gen.from) | ||
} |
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,8 @@ | ||
package io.finch | ||
|
||
package object generic { | ||
/** | ||
* Generically derive a very basic instance of [[Endpoint]] for a given type `A`. | ||
*/ | ||
def deriveEndpointFor[A]: GenericDerivation[A] = new GenericDerivation[A] | ||
} |
36 changes: 36 additions & 0 deletions
36
generic/src/test/scala/io/finch/generic/DerivedEndpointLaws.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,36 @@ | ||
package io.finch | ||
|
||
import cats.Eq | ||
import cats.instances.AllInstances | ||
import cats.laws._ | ||
import cats.laws.discipline._ | ||
import org.scalacheck.{Arbitrary, Prop} | ||
import org.typelevel.discipline.Laws | ||
|
||
trait DerivedEndpointLaws[A] extends Laws with MissingInstances with AllInstances { | ||
|
||
def endpoint: Endpoint[A] | ||
def toParams: A => Seq[(String, String)] | ||
|
||
def roundTrip(a: A): IsEq[A] = { | ||
val i = Input.get("/", toParams(a): _*) | ||
endpoint(i).awaitValueUnsafe().get <-> a | ||
} | ||
|
||
def evaluating(implicit A: Arbitrary[A], eq: Eq[A]): RuleSet = | ||
new DefaultRuleSet( | ||
name = "evaluating", | ||
parent = None, | ||
"roundTrip" -> Prop.forAll { (a: A) => roundTrip(a) } | ||
) | ||
} | ||
|
||
object DerivedEndpointLaws { | ||
def apply[A]( | ||
e: Endpoint[A], | ||
tp: A => Seq[(String, String)] | ||
): DerivedEndpointLaws[A] = new DerivedEndpointLaws[A] { | ||
val endpoint: Endpoint[A] = e | ||
val toParams = tp | ||
} | ||
} |
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,28 @@ | ||
package io.finch.generic | ||
|
||
import cats.kernel.Eq | ||
import io.finch._ | ||
import org.scalacheck.Arbitrary | ||
|
||
class GenericSpec extends FinchSpec { | ||
|
||
behavior of "generic" | ||
|
||
case class Foo(a: String, b: Int) | ||
|
||
val e: Endpoint[Foo] = deriveEndpointFor[Foo].fromParams | ||
|
||
implicit val eq: Eq[Foo] = Eq.fromUniversalEquals | ||
|
||
implicit val arbitraryFoo: Arbitrary[Foo] = Arbitrary(for { | ||
s <- Arbitrary.arbitrary[String] | ||
i <- Arbitrary.arbitrary[Int] | ||
} yield Foo(s, i)) | ||
|
||
val f: Foo => Seq[(String, String)] = foo => Seq( | ||
("a" -> foo.a), | ||
("b" -> foo.b.toString) | ||
) | ||
|
||
checkAll("DerivedEndpoint[Foo]", DerivedEndpointLaws[Foo](e, f).evaluating) | ||
} |