prequel icon indicating copy to clipboard operation
prequel copied to clipboard

implicit conversion of value contained in an Option container

Open crumbpicker opened this issue 13 years ago • 2 comments

The SQLFormatterImplicits doesn't provide a support for values encapsulated in an Option. Here is the workaround I'm currently using :

import net.noerd.prequel.Nullable
import net.noerd.prequel.IntFormattable
import net.noerd.prequel.DoubleFormattable
import net.noerd.prequel.SQLFormatter


class NullFormattable( override val value: Option[Null] ) extends Nullable( value ) {
    override def escaped( formatter: SQLFormatter ): String = "null"
}
object NullFormattable{
    def apply( value: Option[Null] ) = new NullFormattable( value )
}

object SQLFormatterImplicits {
  implicit def someNull2Formattable( wrapped: Option[Null] ) = NullFormattable( wrapped )
  implicit def someInt2Formattable( wrapped: Option[Int] )= wrapped match {
                                                              case None => NullFormattable(Some(null))
                                                              case Some(value) => IntFormattable(value)
                                                            }
  implicit def someDouble2Formattable( wrapped: Option[Double] )= wrapped match {
                                                              case None => NullFormattable(Some(null))
                                                              case Some(value) => DoubleFormattable(value)
                                                            }
}

Do you have a better idea ? Could it be added in the master branch ?

crumbpicker avatar Feb 01 '12 13:02 crumbpicker

there is the Nullable formattable which allows you to have use an Option when formatting. it will result in 'null' in the case of None and otherwise the formatted value.

does this help?

jpersson avatar Feb 02 '12 22:02 jpersson

I don't know why but it doesn't work for me wihthout this new definitions. When I try to insert an Option[Int] Scala throws the following error : type mismatch; found: Option[Int] required: net.noerd.prequel.Formattable

crumbpicker avatar Feb 03 '12 08:02 crumbpicker