Quill

Derivation of MappedEncoding instances.

Dependency

sbt
libraryDependencies += "com.github.cerst" % "structible-quill" % "0.4.0"
Maven
<dependency>
  <groupId>com.github.cerst</groupId>
  <artifactId>structible-quill</artifactId>
  <version>0.4.0</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.github.cerst', name: 'structible-quill', version: '0.4.0'
}

Example

import com.github.cerst.structible.core.Structible
import com.github.cerst.structible.quill.ops._
import io.getquill.{MappedEncoding, PostgresJdbcContext, SnakeCase}

object MappedEncodingExample {

  final case class PersonId(value: Long) {
    require(value >= 0, s"PersonId must be non-negative (got: '$value')")
  }

  object PersonId {

    // you can also pass-in 'construct' functions returning Either[String, A], Option[A] or Try[A]
    private val structible: Structible[Long, PersonId] = Structible.structible(PersonId.apply, _.value)

    implicit val decodeForPersonId: MappedEncoding[Long, PersonId] = structible.toDecode

    implicit val encodeForPersonId: MappedEncoding[PersonId, Long] = structible.toEncode

  }

  final case class Person(personId: PersonId)

  // this specific context is solely for demonstration - any works
  object TestContext extends PostgresJdbcContext(SnakeCase, "configPrefix")

  import TestContext._

  def findPersonById(personId: PersonId): List[Person] = {
    run(quote {
      query[Person]
        .filter(_.personId == lift(personId))
    })
  }

}
The source code for this page can be found here.