Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

hmrc/valuetype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.