Skip to content

Commit

Permalink
Typeclass instances on companion objects rather than package (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrktkt authored Sep 26, 2020
1 parent 0d10875 commit e284cc1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
16 changes: 16 additions & 0 deletions ninny/src/io/github/kag0/ninny/Auto.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package io.github.kag0.ninny

import shapeless.{LabelledGeneric, Lazy}

trait AutoToJson {
implicit def lgToJson[A, Head](implicit
generic: LabelledGeneric.Aux[A, Head],
headToJson: Lazy[ToSomeJsonObject[Head]]
) = ToJsonAuto.labelledGenericToJson[A, Head].toJson
}

trait AutoFromJson {
implicit def lgFromJson[A, Head](implicit
generic: LabelledGeneric.Aux[A, Head],
headFromJson: Lazy[FromJson[Head]]
) = FromJsonAuto.labelledGenericFromJson[A, Head].fromJson
}

object Auto extends AutoToJson with AutoFromJson
2 changes: 1 addition & 1 deletion ninny/src/io/github/kag0/ninny/FromJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait FromJson[A] {
def map[B](f: A => B): FromJson[B] = from(_).map(f)
}

object FromJson {
object FromJson extends FromJsonInstances {
def apply[A: FromJson]: FromJson[A] = implicitly[FromJson[A]]
def fromSome[A](read: JsonValue => Try[A]): FromJson[A] = {
case Some(json) => read(json)
Expand Down
15 changes: 5 additions & 10 deletions ninny/src/io/github/kag0/ninny/FromJsonInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ trait FromJsonInstances {
FromJson.fromSome(
_.to[String].flatMap(s => Try(ZonedDateTime.parse(s)))
)
implicit val hNilFromJson: FromJson[HNil] = _ => Success(HNil)

import shapeless.::

implicit val hNilFromJson: FromJson[HNil] = _ => Success(HNil)

implicit def recordFromJson[Key <: Symbol, Head, Tail <: HList](implicit
witness: Witness.Aux[Key],
headFromJson: Lazy[FromJson[Head]],
Expand All @@ -131,18 +130,14 @@ trait FromJsonInstances {
} yield field[Key](head) :: tail
}
}

implicit def labelledGenericFromJson[A, Head](implicit
generic: LabelledGeneric.Aux[A, Head],
headFromJson: Lazy[FromJson[Head]]
) = new FromJsonAuto[A](headFromJson.value.from(_).map(generic.from))
}
object FromJsonInstances extends FromJsonInstances

class FromJsonAuto[A](val fromJson: FromJson[A]) extends AnyVal
object FromJsonAuto {

trait AutoFromJson {
implicit def lgFromJson[A, Head](implicit
implicit def labelledGenericFromJson[A, Head](implicit
generic: LabelledGeneric.Aux[A, Head],
headFromJson: Lazy[FromJson[Head]]
) = labelledGenericFromJson[A, Head].fromJson
) = new FromJsonAuto[A](headFromJson.value.from(_).map(generic.from))
}
2 changes: 2 additions & 0 deletions ninny/src/io/github/kag0/ninny/ToJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ trait ToJsonValue[A, +Json <: JsonValue] {
def contramap[B](f: B => A): ToJsonValue[B, Json] = b => to(f(b))
}

object ToJsonValue extends ToJsonInstances

object ToJson {
def apply[A: ToJson]: ToJson[A] = implicitly[ToJson[A]]

Expand Down
12 changes: 4 additions & 8 deletions ninny/src/io/github/kag0/ninny/ToJsonInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,14 @@ trait ToJsonInstances {
}
}
}

implicit def labelledGenericToJson[A, Head](implicit
generic: LabelledGeneric.Aux[A, Head],
headToJson: Lazy[ToSomeJsonObject[Head]]
) = new ToJsonAuto[A](a => headToJson.value.toSome(generic.to(a)))
}
object ToJsonInstances extends ToJsonInstances

class ToJsonAuto[A](val toJson: ToSomeJsonObject[A]) extends AnyVal
object ToJsonAuto {

trait AutoToJson {
implicit def lgToJson[A, Head](implicit
implicit def labelledGenericToJson[A, Head](implicit
generic: LabelledGeneric.Aux[A, Head],
headToJson: Lazy[ToSomeJsonObject[Head]]
) = labelledGenericToJson[A, Head].toJson
) = new ToJsonAuto[A](a => headToJson.value.toSome(generic.to(a)))
}
2 changes: 1 addition & 1 deletion ninny/src/io/github/kag0/ninny/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.github.kag0.ninny.ast.{JsonArray, JsonObject, JsonValue}
import scala.language.dynamics
import scala.util.Try

package object ninny extends ToJsonInstances with FromJsonInstances {
package object ninny {

type ToJson[A] = ToJsonValue[A, JsonValue]
type ToJsonObject[A] = ToJsonValue[A, JsonObject]
Expand Down

0 comments on commit e284cc1

Please sign in to comment.