Skip to main content

Syntax

Render Syntax

There is Render syntax provided for convenience.

A.render

(a: A).render // when `Render[A]` is available
import extras.render.Render

final case class Foo(id: Int, name: String)
object Foo {
implicit val fooRender: Render[Foo] =
foo => s"ID=${foo.id.toString} / Name=${foo.name}"
}

import extras.render.syntax._

def bar[A: Render](a: A): Unit =
println(s">> a: ${a.render}")

Foo(1, "Something").render
// res1: String = "ID=1 / Name=Something"

bar(Foo(1, "Something"))
// >> a: ID=1 / Name=Something

List[A].renderString

List[A](a..).renderString // when Render[A] is available
List[A](a..).renderString(delimiter) // when Render[A] is available
List[A](a..).renderString(start, delimiter, end) // when Render[A] is available
import extras.render.Render

final case class Foo(id: Int, name: String)
object Foo {
implicit val fooRender: Render[Foo] =
foo => s"{ID=${foo.id.toString},Name=${foo.name}}"
}

import extras.render.syntax._

List(Foo(1, "A"), Foo(2, "B"), Foo(3, "C")).renderString
// res4: String = "{ID=1,Name=A}{ID=2,Name=B}{ID=3,Name=C}"
List(Foo(1, "A"), Foo(2, "B"), Foo(3, "C")).renderString(", ")
// res5: String = "{ID=1,Name=A}, {ID=2,Name=B}, {ID=3,Name=C}"
List(Foo(1, "A"), Foo(2, "B"), Foo(3, "C")).renderString("[", ", ", "]")
// res6: String = "[{ID=1,Name=A}, {ID=2,Name=B}, {ID=3,Name=C}]"

render String interpolation

render"TEXT $someRenderable"
import extras.render.Render
import extras.render.syntax._

final case class Foo(id: Int, name: String)

object Foo {
implicit val fooRender: Render[Foo] =
foo => s"{ID=${foo.id.toString},Name=${foo.name}}"
}

val foo1 = Foo(1, "A")
// foo1: Foo = Foo(id = 1, name = "A")
val foo2 = Foo(2, "B")
// foo2: Foo = Foo(id = 2, name = "B")
val foo3 = Foo(3, "C")
// foo3: Foo = Foo(id = 3, name = "C")

render">>> $foo1 > $foo2 >> $foo3 <<<"
// res8: String = ">>> {ID=1,Name=A} > {ID=2,Name=B} >> {ID=3,Name=C} <<<"

val foos = List(foo1, foo2, foo3)
// foos: List[Foo] = List(
// Foo(id = 1, name = "A"),
// Foo(id = 2, name = "B"),
// Foo(id = 3, name = "C")
// )

render">>> $foo1 > $foo2 >> $foo3 - ${foos.renderString("[", ", ", "]")} <<<"
// res9: String = ">>> {ID=1,Name=A} > {ID=2,Name=B} >> {ID=3,Name=C} - [{ID=1,Name=A}, {ID=2,Name=B}, {ID=3,Name=C}] <<<"