Quill

Derivation of MappedEncoding instances.

Make sure to familiarize yourself with creating structible instances before looking at the examples below.

Dependency

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

Example

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

object MappedEncodingExample {

  final class UserId private (val value: Long) extends AnyVal

  object UserId {

    private val structible: Structible[Long, UserId] = Structible.structible(new UserId(_), _.value, c >= 0)

    implicit val decodeForUserId: MappedEncoding[Long, UserId] = structible.toDecode

    implicit val encodeForUserId: MappedEncoding[UserId, Long] = structible.toEncode

    def apply(value: Long): UserId = structible.construct(value)
  }

  final case class User(userId: UserId)

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

  import TestContext._

  def findUserById(userId: UserId): List[User] = {
    run(quote {
      query[User]
        .filter(_.userId == lift(userId))
    })
  }

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