This trait defines implicits for the common Play Framework transformations on values.
Defines an interface that can take a value of type V and wrap it in an instance of type W, and take a W and unwrap it to a V.
Defines an interface that can take a value of type V and wrap it in an instance of type W, and take a W and unwrap it to a V.
For example, if W is case class Foo(value:String) extends AnyVal
then
we can create an instance of ValueWrapper as:
implicit val FooValueWrapper extends ValueWrapper[String, Foo] { def wrap(v:String) : Either[String, Foo] = Right(Foo(v)) def unwrap(f:Foo) = foo.value }
The wrap
method returns Either[String, W]
to allow for situations where
the V being passed in cannot be converted to a W.
By making this a type class we can re-use instances of it in a number of places, for example to remove a great deal of boilerplace when creating instances of Play Framework json formatters and url path binders for case classes that extend AnyVal
the type of the value that is being wrapped
the type of the wrapper instance
Provide functions to create various Play type class instances for types that implement a ValueWrapper
This trait defines implicits for the common Play Framework transformations on values. Namely: - A
Reads[W]
to read from json - AWrites[W]
to write to json - APathBindable
to convert to and from strings in the url pattern - AQueryStringBindable
to convert to and from url query parametersTo use this with, for instance, a case class that extends AnyVal, create a companion object that extends this trait with V being the primitive type (e.g. String, Int) and W being the wrapped type. Then make an implicit instance of
ValueWrapper[V,W]
within the companion object, defining the appropriate wrap and unwrap methods.