Skip to content

Commit

Permalink
Trying to get the auto codec stuff up and running.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanparsons committed Dec 4, 2014
1 parent a42af67 commit 29a3eeb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
2 changes: 1 addition & 1 deletion project/build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object build extends Build {
val monocle = "com.github.julien-truffaut" %% "monocle-core" % "0.5.0"
def shapeless(v: String) =
if (v.contains("2.10")) "com.chuusai" % s"shapeless_${v}" % "2.0.0"
else "com.chuusai" %% s"shapeless" % "2.1.0-SNAPSHOT"
else "com.chuusai" %% s"shapeless" % "2.1.0-SNAPSHOT" from "file:///home/sean/.ivy2/local/com.chuusai/shapeless_2.11/2.1.0-SNAPSHOT/bundles/shapeless_2.11.jar"

def reflect(v: String) =
Seq("org.scala-lang" % "scala-compiler" % v,
Expand Down
52 changes: 24 additions & 28 deletions src/main/scala/argonaut/DecodeJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,41 +130,37 @@ object DecodeJson extends DecodeJsons {
def derive[A]: DecodeJson[A] =
macro GenericMacros.materialize[DecodeJson[A], A]

object auto {
implicit def AutoDecodeJson[A]: DecodeJson[A] =
macro GenericMacros.materialize[DecodeJson[A], A]
}

implicit def DecodeJsonTypeClass: LabelledTypeClass = new LabelledTypeClass {
type C[T] = DecodeJson[T]
type Elem[T] = DecodeJson[T]
object auto extends SimpleTypeClassCompanion[DecodeJson] {
implicit def AutoDecodeJson[A]: DecodeJson[A] = macro GenericMacros.materialize[DecodeJson[A], A]

override def emptyCoproduct: DecodeJson[CNil] = DecodeJson(c => DecodeResult.fail("CNil", c.history))
object typeClass extends SimpleTypeClass with LabelledTypeClass {
override def emptyCoproduct: DecodeJson[CNil] = DecodeJson(c => DecodeResult.fail("CNil", c.history))

override def coproduct[L, R <: Coproduct](name: String, djl: => DecodeJson[L], djr: => DecodeJson[R]): DecodeJson[L :+: R] = {
DecodeJson { c =>
(c --\ name).focus.fold[DecodeResult[L :+: R]](
djr.decode(c).map(Inr(_))
)(aJson => aJson.as(djl).map(Inl(_)))
override def coproduct[L, R <: Coproduct](name: String, djl: => DecodeJson[L], djr: => DecodeJson[R]): DecodeJson[L :+: R] = {
DecodeJson { c =>
(c --\ name).focus.fold[DecodeResult[L :+: R]](
djr.decode(c).map(Inr(_))
)(aJson => aJson.as(djl).map(Inl(_)))
}
}
}

override def emptyProduct: DecodeJson[HNil] = {
DecodeJson(c =>
c.focus.obj.filter(_.isEmpty).fold[DecodeResult[HNil]](
DecodeResult.fail("HNil", c.history)
)(_ => (HNil: HNil).point[DecodeResult])
)
}
override def emptyProduct: DecodeJson[HNil] = {
DecodeJson(c =>
c.focus.obj.filter(_.isEmpty).fold[DecodeResult[HNil]](
DecodeResult.fail("HNil", c.history)
)(_ => (HNil: HNil).point[DecodeResult])
)
}

override def product[A, T <: HList](name: String, A: DecodeJson[A], T: DecodeJson[T]): DecodeJson[A :: T] = {
DecodeJson { c =>
val aJson = c --\ name
(aJson.as(A) |@| aJson.delete.as(T))(_ :: _)
override def product[A, T <: HList](name: String, A: DecodeJson[A], T: DecodeJson[T]): DecodeJson[A :: T] = {
DecodeJson { c =>
val aJson = c --\ name
(aJson.as(A) |@| aJson.delete.as(T))(_ :: _)
}
}
}

override def project[F, G](instance: => DecodeJson[G], to: F => G, from: G => F): DecodeJson[F] = instance.map(from)
override def project[F, G](instance: => DecodeJson[G], to: F => G, from: G => F): DecodeJson[F] = instance.map(from)
}
}

def of[A: DecodeJson] =
Expand Down
34 changes: 15 additions & 19 deletions src/main/scala/argonaut/EncodeJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,27 @@ object EncodeJson extends EncodeJsons {
def derive[A]: EncodeJson[A] =
macro GenericMacros.materialize[EncodeJson[A], A]

object auto {
implicit def AutoEncodeJson[A]: EncodeJson[A] =
macro GenericMacros.materialize[EncodeJson[A], A]
}
object auto extends SimpleTypeClassCompanion[EncodeJson] {
implicit def AutoEncodeJson[A]: EncodeJson[A] = macro GenericMacros.materialize[EncodeJson[A], A]

implicit val EncodeJsonTypeClass: LabelledTypeClass = new LabelledTypeClass {
type C[T] = EncodeJson[T]
type Elem[T] = EncodeJson[T]
object typeClass extends SimpleTypeClass with LabelledTypeClass {
override def emptyCoproduct: EncodeJson[CNil] = EncodeJson(_ => jEmptyObject)

override def emptyCoproduct: EncodeJson[CNil] = EncodeJson(_ => jEmptyObject)
override def coproduct[L, R <: Coproduct](name: String, ejl: => EncodeJson[L], ejr: => EncodeJson[R]): EncodeJson[L :+: R] = {
EncodeJson(a => a match {
case Inl(x) => Json((name -> ejl.encode(x)))
case Inr(t) => ejr.encode(t)
})
}

override def coproduct[L, R <: Coproduct](name: String, ejl: => EncodeJson[L], ejr: => EncodeJson[R]): EncodeJson[L :+: R] = {
EncodeJson(a => a match {
case Inl(x) => Json((name -> ejl.encode(x)))
case Inr(t) => ejr.encode(t)
})
}
override def emptyProduct: EncodeJson[HNil] = EncodeJson(_ => jEmptyObject)

override def emptyProduct: EncodeJson[HNil] = EncodeJson(_ => jEmptyObject)
override def product[A, T <: HList](name: String, A: EncodeJson[A], T: EncodeJson[T]): EncodeJson[A :: T] = {
EncodeJson(a => (name -> A.encode(a.head)) ->: T.encode(a.tail))
}

override def product[A, T <: HList](name: String, A: EncodeJson[A], T: EncodeJson[T]): EncodeJson[A :: T] = {
EncodeJson(a => (name -> A.encode(a.head)) ->: T.encode(a.tail))
override def project[F, G](instance: => EncodeJson[G], to: F => G, from: G => F): EncodeJson[F] = instance.contramap(to)
}

override def project[F, G](instance: => EncodeJson[G], to: F => G, from: G => F): EncodeJson[F] = instance.contramap(to)
}

def of[A: EncodeJson] = implicitly[EncodeJson[A]]
Expand Down

0 comments on commit 29a3eeb

Please sign in to comment.