New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FieldLister to use an implicit Printer #104

Closed
fedefernandez opened this Issue Oct 23, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@fedefernandez
Contributor

fedefernandez commented Oct 23, 2017

A Printer is just a trait that transforms a String into a String, as shown here:

trait Printer {
def print(name: String): String
}

We currently have a FieldLister that provides a way to list the column names for a case class:

trait FieldLister[A] {
val list: List[String]
}

We need to refactor it to make use of an implicit Printer instead of using the actual case class field name here:

FieldLister[FieldType[K, H] :: T](witness.value.name :: tLister.list)

We also need to adapt Unit Tests to this new implementation. The tests are located here:

"insert" should {
"generate a right statement for a regular case class" in {
import FieldLister._
StatementGenerator[A].insert("A") shouldBe "INSERT INTO A (a1,a2,a3) VALUES (?,?,?)"
}
"generate a right statement for a case class with another embedded case class" in {
import FieldLister._
StatementGenerator[C].insert("C") shouldBe "INSERT INTO C (c1,c2) VALUES (?,?)"
}
"generate a right expanded statement for a case class with another embedded case class" in {
import StatementGenerator._
import FieldListerExpanded._
StatementGenerator[C].insert("C") shouldBe "INSERT INTO C (c1,b1,b2) VALUES (?,?,?)"
}
}
"select" should {
"generate a right statement for a regular case class" in {
import FieldLister._
StatementGenerator[A].select("A") shouldBe "SELECT a1,a2,a3 FROM A"
}
"generate a right statement for a case class with another embedded case class" in {
import FieldLister._
StatementGenerator[C].select("C") shouldBe "SELECT c1,c2 FROM C"
}
"generate a right expanded statement for a case class with another embedded case class" in {
import FieldListerExpanded._
StatementGenerator[C].select("C") shouldBe "SELECT c1,b1,b2 FROM C"
}
}

An example of the behavior we are looking for can be found here:

implicit def hconsFromReader[K <: Symbol, V, L <: HList](
implicit
witness: Witness.Aux[K],
codec: ByteBufferCodec[V],
grT: FromReader[L],
printer: Printer): FromReader[FieldType[K, V] :: L] =
new FromReader[FieldType[K, V] :: L] {
override def apply[M[_]](reader: ByteBufferReader)(
implicit ME: MonadError[M, Throwable]): M[FieldType[K, V] :: L] = {
val newName = printer.print(witness.value.name)
ME.flatMap(reader.read(newName)) { byteBuffer =>
ME.map2(codec.deserialize(byteBuffer), grT(reader)) {
case (result, l) => new FieldBuilder[K].apply(result) :: l
}
}
}
}

@fedefernandez fedefernandez changed the title from Update FieldLister to use a printer implicit to Update FieldLister to use an implicit Printer Oct 26, 2017

@fedefernandez fedefernandez added the spree label Oct 26, 2017

@fedefernandez fedefernandez self-assigned this Nov 6, 2017

@fedefernandez fedefernandez closed this in #110 Nov 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment