ToValue
ToValue
is a type class to convert from
Scala types to a variable value for use in URI Template expansion. There are 3 different types of as defined by RFC 6570:
StringValue
- Used for simple string expansion.ListValue
- List of string values, e.g. expressing URI path segments.AssociativeArray
- Name value pairs, e.g. defining query params.
Creating a custom ToValue
A custom ToValue
can be defined for Scala types without the explicit support provided by uritemplate4s.
An example would be creating a ToValue
instance for java.time.Instant
.
import uritemplate4s._
import java.time.Instant
implicit val instantToStringValue: ToStringValue[Instant] = (instant: Instant) => instant.toString
Test the type class is wired in correctly
ToValue[Instant].apply(Instant.now())
// res0: Value = StringValue(value = "2021-04-26T14:31:59.839339Z")
uritemplate"http://clock-service.com/clock{?time}".expand("time" -> Instant.now()).value
// res1: String = "http://clock-service.com/clock?time=2021-04-26T14%3A31%3A59.840823Z"