CSV record representation with type-safe access to its values. Typed records are created as result of schema validation.
Typed records values are accessed similarly to regular records, as values referenced by string keys. In contract to regular record, however, the keys are verified at compile time. See apply method for more. The returned value has already required type, as declared by schema definition:
val id: Int = record("id")
Thanks to this, in contrast to a regular Record, field access operation through apply and conversion to case class through to return required type directly, without wrapping it in Either.
The key is verified at compile time and has to be present in the record to use it. To achieve this, the provided key is required to have a singleton type. This works out of the box for literals, but has to be declared explicitly for variables:
val v1 = record("key") // OK
val key: "key" = "key"
val v2 = record(key) // OK
val wrong = "key"
val v3 = record(wrong) // does not compile
The returned value has the type which has been declared for given field by schema definition and validation (the proper type is inferred, you do not have to provide it explicitly): val value: Double = record("value") The formal return type, Select[K, KS, VS], is reduced to proper type through match type.
@param key the key of retrieved field @tparam K the key type - this is a singleton type representing the key @return field value
Because typed record has already got all the values properly typed, it may be safely converted to a case class. Assuming, that the record has a field name of type String and a field birthdate of type LocalDate, the conversion is as simple as that:
case class Person(name: String, birthdate: LocalDate)
val person = record.to[Person]
Please note that the conversion is name-based (case class field names have to match record fields), is case sensitive and only shallow conversion is supported. Case class may be narrower and effectively retrieve only a subset of record's fields.
It is possible to use a tuple instead of case class. In such a case the field names must match the "classic" tuple field naming convention: _1, _2 etc.