-
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
d27f085
commit 8c23bb4
Showing
8 changed files
with
136 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 deriveFor[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 | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
generic/src/test/scala/io/finch/generic/EntityEndpointLaws.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,42 @@ | ||
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 | ||
import scala.reflect.ClassTag | ||
|
||
trait EntityEndpointLaws[A] extends Laws with MissingInstances with AllInstances { | ||
|
||
def decoder: DecodeEntity[A] | ||
def classTag: ClassTag[A] | ||
def endpoint: Endpoint[Option[String]] | ||
def serialize: String => Input | ||
|
||
def roundTrip(a: A): IsEq[Option[A]] = { | ||
val s = a.toString | ||
val i = serialize(s) | ||
val e = endpoint.as(decoder, classTag) | ||
e(i).awaitValueUnsafe().flatten <-> Some(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 EntityEndpointLaws { | ||
def apply[A: DecodeEntity: ClassTag]( | ||
e: Endpoint[Option[String]] | ||
)(f: String => Input): EntityEndpointLaws[A] = new EntityEndpointLaws[A] { | ||
val decoder: DecodeEntity[A] = DecodeEntity[A] | ||
val classTag: ClassTag[A] = implicitly[ClassTag[A]] | ||
val endpoint: Endpoint[Option[String]] = e | ||
val serialize: String => Input = f | ||
} | ||
} |
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] = deriveFor[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) | ||
} |