Skip to content

v0.22.0 Enums

Choose a tag to compare

@marcgrue marcgrue released this 15 Jun 14:17
· 111 commits to main since this release

You can now define enumerations (or "enums") in your Domain Structure definition file that Molecule uses to generate boilerplate code:

object BoxCompany extends DomainStructure(5) {
  
  trait Box {
    val name = oneString("name of box")
    
    enum Color:
      case Red, Green, Blue

    val color    = oneEnum[Color]
    val colorSet = setEnum[Color]
    val colorSeq = seqEnum[Color]
  }
}

As you can see, we can define attributes with a single enum value by using oneEnum[Color], a Set of enum values with setEnum[Color] or a sequence of enum values with seqEnum[Color].

Enum values can be applied and filtered with compile-time safety:

Box.color(Color.Red).save.transact
Box.color(Color.Red).query.get ==> List("Red")
Box.color.query.get ==> List("Red")

Underlying String type

Internally, the enum values are saved as Strings in the databases and returned as strings too.

Since we serialize our DataModel with Boopickle to enable marshalling between the jvm and js platforms, the enum type is not encoded in the DataModel. That would require using a generic type parameter for arbitrary Enum types, and we can't serialise generic type parameters with Boopickle.

Furthermore, it seems reasonable to return the string values for further processing in our code. And if we want we can simply continue with enums by saying Color.valueOf("Blue") to get a Color.Blue.

Operations on enum values

// Mandatory value
Box.color(Color.Red).query.get ==> List("red box") // match 
Box.color.not(Color.Red).query.get ==> List("blue box") // negate

// Tacit filter
Box.name.color_(Color.Red).query.get ==> List("red box") // match, but don't return color 
Box.name.color_.not(Color.Red).query.get ==> List("blue box") // negate, but don't return color 

// Optional
Box.name.color_?.query.get ==> List(
  ("red box", Some("Red")),
  ("blue box", Some("Blue")),
  ("new box", None),
)

val maybeColor = Some(Color.Red)
Box.name.color_?(maybeColor).query.get ==> List(
  ("red box", Some("Red")),
)

Set of enum values

We can poke a Set of enum values to see if it has one or more enum values

import Color.*
Box.colorSet(Set(Red, Blue)).save.transact
Box.colorSet.has(Red).query.get.map(_ ==> List(Set("Red", "Blue")))

// OR semantics with multiple values
Box.colorSet.has(Red, Blue).query.get.map(_ ==> List(Set("Red", "Blue")))
Box.colorSet.has(Red, Black).query.get.map(_ ==> List(Set("Red", "Blue")))
Box.colorSet.has(White, Black).query.get.map(_ ==> List())

Box.colorSet.hasNo(Red).query.get.map(_ ==> List())
Box.colorSet.hasNo(Yellow).query.get.map(_ ==> List(Set("Red", "Blue")))

// Tacit
Box.name.colorSet_(Set(Red, Blue)).save.transact
Box.name.colorSet_.has(Red).query.get.map(_ ==> List(Set("Red", "Blue")))
Box.name.colorSet_.hasNo(Yellow).query.get.map(_ ==> List())

Seq of enum values

And likewise with Seqs that allow duplicate values and guaranteed order

Box.colorSeq(Seq(Red, Blue, Blue)).save.transact
Box.colorSeq.has(Red).query.get ==> List(Seq("Red", "Blue", "Blue"))

// OR semantics with multiple values
Box.colorSeq.has(Red, Blue).query.get.map(_ ==> List(Seq("Red", "Blue", "Blue")))
Box.colorSeq.has(Red, Black).query.get.map(_ ==> List(Seq("Red", "Blue", "Blue")))
Box.colorSeq.has(White, Black).query.get.map(_ ==> List())

Box.colorSeq.hasNo(Red).query.get.map(_ ==> List())
Box.colorSeq.hasNo(Yellow).query.get.map(_ ==> List(Seq("Red", "Blue", "Blue")))

// Tacit
Box.name.colorSeq_(Seq(Red, Blue)).save.transact
Box.name.colorSeq_.has(Red).query.get ==> List(Seq("Red", "Blue", "Blue"))
Box.name.colorSeq_.hasNo(Yellow).query.get ==> List()