Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

implicit conversion of value contained in an Option container #6

Open
crumbpicker opened this issue Feb 1, 2012 · 2 comments
Open

Comments

@crumbpicker
Copy link

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 ?

@jpersson
Copy link
Owner

jpersson commented Feb 2, 2012

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?

@crumbpicker
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants