PathInterpolator

zio.blocks.schema.PathInterpolator

Adds the p"..." interpolator, which turns a path literal into a DynamicOptic at compile time. The package object zio.blocks.schema mixes this trait in, so import zio.blocks.schema._ brings p into scope.

Attributes

Source
PathInterpolator.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Extensions

Extensions

extension (inline sc: StringContext)
transparent inline def p(inline args: Any*): DynamicOptic

Parses a path literal into a DynamicOptic at compile time. The syntax is the one DynamicOptic#toString prints:

Parses a path literal into a DynamicOptic at compile time. The syntax is the one DynamicOptic#toString prints:

  • .name or name: a record field (a name starts with a letter or _ and goes on with letters, digits or _);
  • ``: a variant case;
  • [0], [0,2,5], [1:4] (indices 1, 2 and 3; empty when the end is not above the start), and [*], [:*] or [*:]: one index, several indices, a range of indices, or every element;
  • {"key"}, {'c'}, {42}, {-1}, {true} and {"a", "b"}: one map key, which is a string, char, Int or boolean literal, or several keys;
  • {*} or {:*}: every map value, and {*:}: every map key;
  • #Book, #string, #list(Book), #record { title: string }: a search for every value that matches a schema pattern (a SchemaRepr).

The empty literal p"" is the root path. Whitespace is allowed inside [], {} and <, and inside a search pattern, but not between steps. String keys accept the escapes \", \\, \n, \t and \r, and char keys \', \\, \n, \t and \r. The macro reads the literal as written, without processing escapes first, so \" reaches the parser and is rejected: write string keys in a triple-quoted literal, p"""{"api.key"}""", or with the $" escape for a quote.

A malformed path is a compile error, and so is a literal with interpolated arguments. There is no syntax for a wrapper step, so p"..." rejects the .~ that DynamicOptic#toString prints for one (see docs/dev/issues.md).

val path: DynamicOptic = p".books[*].title"

path.nodes.toList                                                // List(Field(books), Elements, Field(title))
path == DynamicOptic.root.field("books").elements.field("title") // true

Attributes

See also
Source
PathInterpolator.scala