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: uritemplate4s.Value = StringValue(2020-03-31T18:32:01.691Z)

uritemplate"http://clock-service.com/clock{?time}".expand("time" -> Instant.now()).value
// res1: String = http://clock-service.com/clock?time=2020-03-31T18%3A32%3A02.154Z