Skip to content

Commit

Permalink
Fix and refactor after rebasing on prototyping
Browse files Browse the repository at this point in the history
  • Loading branch information
vil1 committed Feb 10, 2019
1 parent 8c24080 commit de00e7f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
10 changes: 6 additions & 4 deletions modules/core/src/main/scala/SchemaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,23 @@ trait Interpreter[Prim[_], SumTermId, ProductTermId, F[_]] {
* A natural transformation that will transform a schema for any type `A`
* into an `F[A]`.
*/
def interpret: Schema.FSchema[Prim, SumTermId, ProductTermId, ?] ~> F
def interpret: SchemaF.FSchema[Prim, SumTermId, ProductTermId, ?] ~> F
}

object SchemaF {

implicit def schemaHFunctor[Prim[_], SumTermId, ProductTermId] =
new HFunctor[Schema[Prim, SumTermId, ProductTermId, ?[_], ?]] {
new HFunctor[SchemaF[Prim, SumTermId, ProductTermId, ?[_], ?]] {

def hmap[F[_], G[_]](nt: F ~> G) =
new (Schema[Prim, SumTermId, ProductTermId, F, ?] ~> Schema[
new (SchemaF[Prim, SumTermId, ProductTermId, F, ?] ~> SchemaF[
Prim,
SumTermId,
ProductTermId,
G,
?
]) {
def apply[A](fa: Schema[Prim, SumTermId, ProductTermId, F, A]) = fa.hmap(nt)
def apply[A](fa: SchemaF[Prim, SumTermId, ProductTermId, F, A]) = fa.hmap(nt)
}
}

Expand Down Expand Up @@ -264,6 +264,8 @@ trait SchemaModule[R <: Realisation] {

val R: R

import SchemaF._

type RInterpreter[F[_]] = Interpreter[R.Prim, R.SumTermId, R.ProductTermId, F]

type RSchema[F[_], A] = SchemaF[R.Prim, R.SumTermId, R.ProductTermId, F, A]
Expand Down
2 changes: 1 addition & 1 deletion modules/play-json/src/main/scala/PlayJsonModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import _root_.play.api.libs.functional.syntax._

trait PlayJsonModule[R <: Realisation] extends SchemaModule[R] {

import Schema._
import SchemaF._

type LabelledSchema[A] = (Boolean, FSchema[R.Prim, R.SumTermId, R.ProductTermId, A])

Expand Down
2 changes: 1 addition & 1 deletion modules/tests/src/main/scala/GenModuleExamples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait PrimToGen {
case JsonSchema.JsonString => arbitrary[String]
case JsonSchema.JsonNumber => arbitrary[BigDecimal]
case JsonSchema.JsonBool => arbitrary[Boolean]
case JsonSchema.JsonNull => arbitrary[Null]
case JsonSchema.JsonNull => arbitrary[Unit]
}
}

Expand Down
6 changes: 4 additions & 2 deletions modules/tests/src/main/scala/GenericGenModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ trait GenericGenModule[R <: Realisation] extends GenericSchemaModule[R] {

implicit final def algebra(
implicit primNT: R.Prim ~> Gen
): HAlgebra[RSchema, Gen] =
covariantTargetFunctor[Gen](
): RInterpreter[Gen] = new Interpreter[R.Prim, R.SumTermId, R.ProductTermId, Gen] {
private val alg = covariantTargetFunctor[Gen](
primNT,
λ[Gen ~> λ[X => Gen[List[X]]]](x => Gen.listOf(x)),
λ[RProductTerm[Gen, ?] ~> Gen](gen => gen.schema),
λ[RSumTerm[Gen, ?] ~> Gen](gen => gen.schema),
Gen.const(())
)

def interpret: Schema ~> Gen = cataNT(alg)
}
}
1 change: 0 additions & 1 deletion modules/tests/src/main/scala/PlayJsonExamples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ object PlayJsonExamples {

def tests[T](harness: Harness[T]): T = {
import harness._
import Schema._
import module._
import JsonSchema._

Expand Down
14 changes: 9 additions & 5 deletions modules/tests/src/main/scala/ShowExamples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ import generic._

object ShowExamples {

import JsonSchema._

val showModule = new TestModule with ShowModule[JsonSchema.type] {

import SchemaF._
import JsonSchema._

val primToShowNT = new (JsonSchema.Prim ~> Show) {

def apply[A](fa: JsonSchema.Prim[A]): Show[A] =
fa match {
case JsonNumber => Show.showFromToString[BigDecimal]
case JsonBool => Show.showFromToString[Boolean]
case JsonString => Show.shows[String](s => s""""$s"""")
case JsonNull => Show.shows[Null](_ => "null")
case JsonNull => Show.shows[Unit](_ => "null")
}
}

implicit val interpreter = new Interpreter[R.Prim, R.SumTermId, R.ProductTermId, Show] {
private val alg = showAlgebra(primToShowNT, identity[String], identity[String])
def interpret: Schema ~> Show = cataNT(alg)
}
}

def tests[T](harness: Harness[T]): T = {
Expand All @@ -33,8 +39,6 @@ object ShowExamples {
test("commons Show Instance") { () =>
{

implicit val alg = showAlgebra(primToShowNT, identity[String], identity[String])

val testCases: List[(Person, String)] = List(
Person(null, None) -> """(name = ("null"), role = (()))""",
Person("Alfred", None) -> """(name = ("Alfred"), role = (()))""",
Expand Down

0 comments on commit de00e7f

Please sign in to comment.