Properties for APIs
PropertySupport pairs a descriptor with its accessor. That is the handle an API needs when users name properties in code: schemas, forms, filters, query builders.
One property
makeProperty
The result knows its name, whether it is writable, and reads and writes an instance.
- property.name
- name
- property.isWriteable
- false
- property.get(Person("Ada", 37))
- Ada
- makeProperty[Settings, String](_.theme).isWriteable
- true
scala
val property = PropertySupport.makeProperty[Person, String](_.name)
property.get(Person("Ada", 37))
This is how json-mapper schemas work
EntitySchema.property(_.title) is an inline call to makeProperty. The schema gets the property's name and accessor from the selector, checked by the compiler.
All properties
extractPropertiesWithAccessors
Every property of a type with its accessor, inherited ones included.
- id
- 1
- createdBy
- system
- title
- Reflection
scala
trait Audited {
val createdBy: String = "system"
}
class Document(val id: String, val title: String) extends Audited
val document = Document("1", "Reflection")
PropertySupport.extractPropertiesWithAccessors[Document]
.filterNot(_.name == "##") // 1.1.4 lists the hash code method as well
.foreach(property => println(s"${property.name} = ${property.get(document)}"))