scala-cass
scala-cass copied to clipboard
provide ability to write a NULL
None will always remove the write. provide a way to write down a NULL
what about providing alternative to Option (with easy conversion)
sealed trait Nullable[+A] {
def toOption: Option[A]
}
final case class NotNull[+A](x: A) extends Nullable[A] {
def toOption: Option[A] = Some(x)
}
case object IsNull extends Nullable[Nothing] {
def toOption: Option[Nothing] = None
}
object Nullable {
implicit class NullableOption[+A](val opt: Option[A]) extends AnyVal {
def toNullable: Nullable[A] = opt.fold[Nullable[A]](IsNull)(NotNull.apply)
}
}
usage could be
case class SomeTable(couldBeNull: Nullable[String])
val str: Option[String] = Some("a string")
SomeTable(str.toNullable)
or
case class SomeTable(couldBeNull: Nullable[String])
object SomeTable {
def apply(couldBeNull: Option[String]) = new SomeTable(couldBeNull.toNullable)
}
SomeTable(Some("a string")) // just works
in the latter case, maybe it makes more sense to have a
implicit def option2nullable[A](opt: Option[A]): Nullable[A]) = opt.toNullable
case class SomeTable(couldBeNull: Nullable[String])
SomeTable(Some("a string")) // just works
this is implemented. no docs yet, so leaving this here until then
i feel like this going to loose type constraints on fields of data record If i get a clean way to say "write down it an null" using None (why else am i trying TO UPDATE record in cassandra) - i'll need this possibility only in queues
there were some cases in my project where "absence of data" and "data set to null" were semantically different, so I wanted to enable this. if you don't care about that difference, then just don't use Nullable
also, thanks for reminding me about this. it shouldn't take much to add this documentation, so i should just do it