ValueBinding
com.tersesystems.blindsight.jsonld.ValueBinding
final case class ValueBinding[T](key: ValueBindingKey)(implicit evidence$3: ValueMapper[T]) extends Binding
A binding between a key that is a "JSON-LD value" and a Scala type that can be represented as that value. A ValueMapper is responsible for doing the conversion.
You can define your own ValueMapper for custom types, and primitives (String, Int, etc) are already defined:
val givenName = schemaOrg("givenName").bindValue[String]
val willPerson = NodeObject(
givenName -> "Person"
)
Using a "null" is done by binding to an Option[T] of the type you want:
val givenName = schemaOrg("givenName").bindValue[Option[String]]
val willPerson = NodeObject(
givenName -> None
)
Some JSON-LD values are represented as "value objects", notably strings with languages and typed values.
To represent a string value:
val givenName = schemaOrg("givenName").bindValue[String]
val willPerson = NodeObject(
givenName -> Value("Will", StringDirection.LeftToRight, "en"),
)
You can require string values over literals by binding explicitly:
val givenName = schemaOrg("givenName").bindValue[StringValue]
Likewise typed values are usually converted but can be defined explicitly:
val name = schemaOrg("name").bindValue[String]
val dateCreated = schemaOrg("dateCreated").bindValue[TypedValue]
val localDate = LocalDate.of(2020, 1, 1)
val abridgedMobyDick = NodeObject(
`@type` -> "Book",
name -> "Moby Dick",
dateCreated -> Value(DateTimeFormatter.ISO_DATE.format(localDate), xsdDate)
)
You can map any type to a value using a custom ValueMapper:
val `@type` = Keyword.`@type`.bindIRI
val schemaOrg: Vocab = IRI("https://schema.org/").vocab
val name: ValueBinding[String] = schemaOrg("name").bindValue[String]
val value: ValueBinding[Int] = schemaOrg("value").bindValue[Int]
implicit val currencyMapper: ValueMapper[Currency] = ValueMapper { currency =>
Value(currency.getCurrencyCode)
}
val currency: ValueBinding[Currency] = schemaOrg("currency").bindValue[Currency]
Attributes
- T
the type which has a ValueMapper type class instance
- key
the value binding key
- Source:
- Binding.scala
- Graph
- Supertypes