This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
forked from yanana/agni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from tkrs/overhaul-generic-codec
Overhaul generic codec
- Loading branch information
Showing
11 changed files
with
130 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package agni.generic | ||
|
||
import agni.{ Binder, RowSerializer } | ||
import shapeless.labelled.FieldType | ||
import shapeless.{ ::, HList, HNil, LabelledGeneric, Lazy, Witness } | ||
|
||
trait DerivedBinder[A] extends Binder[A] | ||
|
||
object DerivedBinder extends DerivedBinder1 | ||
|
||
trait DerivedBinder1 { | ||
|
||
implicit val bindHnil: DerivedBinder[HNil] = (bound, _, _) => Right(bound) | ||
|
||
implicit def bindLabelledHList[K <: Symbol, H, T <: HList]( | ||
implicit | ||
K: Witness.Aux[K], | ||
H: RowSerializer[H], | ||
T: DerivedBinder[T] | ||
): DerivedBinder[FieldType[K, H] :: T] = | ||
(bound, version, xs) => H(bound, K.value.name, xs.head, version).flatMap(T(_, version, xs.tail)) | ||
|
||
implicit def bindCaseClass[A, R <: HList]( | ||
implicit | ||
gen: LabelledGeneric.Aux[A, R], | ||
bind: Lazy[DerivedBinder[R]] | ||
): DerivedBinder[A] = | ||
(bound, version, a) => | ||
bind.value(bound, version, gen.to(a)) | ||
} |
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,33 @@ | ||
package agni.generic | ||
|
||
import agni.{ RowDecoder, RowDeserializer } | ||
import shapeless.labelled._ | ||
import shapeless.{ ::, HList, HNil, LabelledGeneric, Lazy, Witness } | ||
|
||
trait DerivedRowDecoder[A] extends RowDecoder[A] | ||
|
||
object DerivedRowDecoder extends DerivedRowDecoder1 | ||
|
||
trait DerivedRowDecoder1 { | ||
|
||
implicit val decodeHNil: DerivedRowDecoder[HNil] = | ||
(_, version) => Right(HNil) | ||
|
||
implicit def decodeLabelledHList[K <: Symbol, H, T <: HList]( | ||
implicit | ||
K: Witness.Aux[K], | ||
H: RowDeserializer[H], | ||
T: DerivedRowDecoder[T] | ||
): DerivedRowDecoder[FieldType[K, H] :: T] = | ||
(row, version) => for { | ||
h <- H(row, K.value.name, version) | ||
t <- T(row, version) | ||
} yield field[K](h) :: t | ||
|
||
implicit def decodeCaseClass[A, R <: HList]( | ||
implicit | ||
gen: LabelledGeneric.Aux[A, R], | ||
decode: Lazy[DerivedRowDecoder[R]] | ||
): DerivedRowDecoder[A] = | ||
(row, version) => decode.value(row, version) 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,9 @@ | ||
package agni.generic | ||
|
||
import agni.{ Binder, RowDecoder } | ||
import shapeless.Lazy | ||
|
||
object auto { | ||
implicit def autoDerivedBinder[A](implicit A: Lazy[DerivedBinder[A]]): Binder[A] = A.value | ||
implicit def autoDerivedRowDecoder[A](implicit A: Lazy[DerivedRowDecoder[A]]): RowDecoder[A] = A.value | ||
} |
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,9 @@ | ||
package agni.generic | ||
|
||
import agni.{ Binder, RowDecoder } | ||
import shapeless.Lazy | ||
|
||
object semiauto { | ||
def derivedBinder[A](implicit A: Lazy[DerivedBinder[A]]): Binder[A] = A.value | ||
def derivedRowDecoder[A](implicit A: Lazy[DerivedRowDecoder[A]]): RowDecoder[A] = A.value | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package agni.generic | ||
|
||
import agni.{ Binder, TypedSuite } | ||
import org.scalatest.Assertion | ||
import org.scalatestplus.mockito.MockitoSugar | ||
|
||
class DerivedBinderSpec extends TypedSuite with MockitoSugar { | ||
import auto._ | ||
import TypedSuite._ | ||
|
||
def checkType[A: Binder]: Assertion = { | ||
assertCompiles("Binder.apply[A]") | ||
} | ||
|
||
test("Binder[Named]")(checkType[Named]) | ||
test("Binder[IDV]")(checkType[IDV]) | ||
} |
16 changes: 16 additions & 0 deletions
16
core/src/test/scala/agni/generic/DerivedRowDecoderSpec.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,16 @@ | ||
package agni.generic | ||
|
||
import agni.{ RowDecoder, TypedSuite } | ||
import org.scalatest.Assertion | ||
|
||
class RowDecoderSpec extends TypedSuite { | ||
import TypedSuite._ | ||
import auto._ | ||
|
||
def checkType[A: RowDecoder]: Assertion = { | ||
assertCompiles("RowDecoder.apply[A]") | ||
} | ||
|
||
test("RowDecoder[Named]")(checkType[Named]) | ||
test("RowDecoder[IDV]")(checkType[IDV]) | ||
} |
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