valuetype

Build Status Download

A scala micro-library introducing Value Type (tiny type) trait and some subtraits for simple types like String, Int, Boolean and BigDecimal. It also contains tooling for creating Play JSON format and PathBinders for derived types.

Play JSON format

import play.api.libs.json.Json
import play.api.libs.json.Json.{toJson, fromJson}
import uk.gov.voa.valuetype.IntValue
import uk.gov.voa.valuetype.play.formats.ValueTypeFormat

case class MyIntValue(value: Int) extends IntValue

implicit val myIntValueFormat = ValueTypeFormat.format(MyIntValue.apply)

toJson(MyIntValue(2))

fromJson(Json.parse("2"))

Play PathBinders

import play.api.mvc.PathBindable
import uk.gov.voa.valuetype.IntValue
import uk.gov.voa.valuetype.play.binders.ValueTypePathBinder._

import scala.util.Try

case class MyIntValue(value: Int) extends IntValue

implicit val intValueParser: (String) => Try[MyIntValue] = value => Try {
  MyIntValue(value.toInt)
}

val intValueBinder = implicitly[PathBindable[MyIntValue]]

intValueBinder.bind("key", "1")

intValueBinder.unbind("key", MyIntValue(1))

Installing

Include the following dependency in your SBT build

resolvers += Resolver.bintrayRepo("hmrc", "releases")

libraryDependencies += "uk.gov.hmrc" %% "valuetype" % "<INSERT VERSION>"

License

This code is open source software licensed under the Apache 2.0 License.