Repository navigation
v0.12.1 Map attr ops aligned
Map attributes can now be filtered by key or value in a consistent way that corresponds to operations on a Scala Map. This should make working with Map attributes in Molecule feel natural for a Scala programmer.
Examples
We'll use the following example of a Map attribute capturing Shostakovich's name in different languages and see how we can use the operations available.
val namesMap = Map(
"en" -> "Shostakovich",
"de" -> "Schostakowitsch",
"fr" -> "Chostakovitch",
)
Person.name("Shostakovich").langNames(namesMap).save.transactFilter by key
attr(key)
Retrieve a value by applying a key to a mandatory Map attribute:
// Get German spelling of Shostakovich
Person.langNames("de").query.get ==> List("Schostakowitsch")Equivalent to calling apply on a Scala Map
namesMap("de") ==> "Schostakowitsch"Looking up a non-existing key simply returns an empty result
Person.langNames("xx").query.get ==> Nilattr_(key)
Ensure a certain key is present by applying the key to a tacit Map attribute:
// Get (English) name having a German spelling
Person.name.langNames_("de").query.get ==> List("Shostakovich")
Person.name.langNames_("xx").query.get ==> Nilattr_?(key)
Retrieve an optional value by applying a key to an optional Map attribute:
Person.langNames_?("de").query.get.head ==> Some("Schostakowitsch")
Person.langNames_?("xx").query.get.head ==> NoneEquivalent to calling get on a Scala Map
namesMap.get("de") ==> Some("Schostakowitsch")
namesMap.get("xx") ==> Noneattr.not(keys)
Get Maps not having a certain key by applying the key to not of a mandatory Map attribute:
// Get langNames maps without a Spanish spelling
Person.langNames.not("es").query.get ==> List(namesMap)
// Get langNames maps without an English spelling
Person.langNames.not("en").query.get ==> NilMultiple keys kan be applied as varargs or a Seq
// Get langNames maps without Spanish or Chinese spelling
Person.langNames.not("es", "cn").query.get ==> List(namesMap)
Person.langNames.not(Seq("es", "cn")).query.get ==> List(namesMap)
// One of the keys exists, so no match
Person.langNames.not(List("es", "en")).query.get ==> Nilattr_.not(keys)
Match Maps not having a certain key by applying the key to not of a tacit Map attribute:
// Match langNames maps without a Spanish spelling
Person.name.langNames_.not("es").query.get ==> List("Shostakovich")
// Match langNames maps without an English spelling
Person.name.langNames_.not("en").query.get ==> NilMultiple keys kan be applied as varargs or a Seq
// Match langNames maps without Spanish or Chinese spelling
Person.name.langNames_.not("es", "cn").query.get ==> List("Shostakovich")
Person.name.langNames_.not(Seq("es", "cn")).query.get ==> List("Shostakovich")
// One of the keys exists, so no match
Person.name.langNames_.not(List("es", "en")).query.get ==> NilFilter by value
attr.has(values)
Return Maps that have certain values with has on a mandatory Map attribute:
// Get map if it has a spelling value of "Chostakovitch"
Person.langNames.has("Chostakovitch").query.get ==> List(namesMap)
// Get map if it has a spelling
// value of "Chostakovitch" or "Sjostakovitj"
Person.langNames.has("Chostakovitch", "Sjostakovitj").query.get ==> List(namesMap)attr_.has(values)
Match Maps that have certain values with has on a tacit Map attribute:
// Match map if it has a spelling value of "Chostakovitch"
Person.name.langNames_.has("Chostakovitch").query.get ==> List("Shostakovich")
Person.langNames_.has("Chostakovitch", "Sjostakovitj").query.get ==> List("Shostakovich")Likewise we can ask for Map attributes without certain values:
attr.hasNo(values)
Return Maps without certain values using hasNo on a mandatory Map attribute:
// Get map if it doesn't have a spelling of "Sjostakovitj"
Person.langNames.hasNo("Sjostakovitj").query.get ==> List(namesMap)
// Get map if it doesn't have a spelling
// value of "Chostakovitch" or "Sjostakovitj"
Person.langNames.hasNo("Chostakovitch", "Sjostakovitj").query.get ==> Nilattr_.hasNo(values)
Match Maps without certain values using hasNo on a tacit Map attribute:
// Match map if it doesn't have a spelling value of "Sjostakovitj"
Person.name.langNames_.hasNo("Sjostakovitj").query.get ==> List("Shostakovich")
// Match map if it doesn't have a spelling
// value of "Chostakovitch" or "Sjostakovitj"
Person.langNames_.hasNo("Chostakovitch", "Sjostakovitj").query.get ==> Nil