thurstonsand / scala-cass

a wrapper for the Java Cassandra driver that allows extraction from a Row with Scala types, or directly into a case class. Also has utility functions for the Session to read/write to Cassandra directly to/from a case class.
MIT License
21 stars 10 forks source link

provide ability to write a NULL #28

Open thurstonsand opened 7 years ago

thurstonsand commented 7 years ago

None will always remove the write. provide a way to write down a NULL

thurstonsand commented 7 years ago

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)
  }
}
thurstonsand commented 7 years ago

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
thurstonsand commented 7 years ago

this is implemented. no docs yet, so leaving this here until then

holinov commented 5 years ago

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

thurstonsand commented 5 years ago

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

thurstonsand commented 5 years ago

also, thanks for reminding me about this. it shouldn't take much to add this documentation, so i should just do it