Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undesirable runtime errors when creating custom types for Array #244

Closed
mdedetrich opened this issue Dec 16, 2015 · 9 comments
Closed

Undesirable runtime errors when creating custom types for Array #244

mdedetrich opened this issue Dec 16, 2015 · 9 comments

Comments

@mdedetrich
Copy link

When creating custom types for pg-array's type, its possible to get runtime cast errors (i.e. the code compiles fine)

As an example (and we had this happen to us), you can define a List[BigDecimal] mapping like this

implicit val bigDecimalTypeWrapper = new SimpleArrayJdbcType[BigDecimal]("numeric").to(_.toList)

You get a class cast exception at runtime (unable to cast a java.math.BigDecimal to a scala.mathBigDecimal). The following version is what you need to do

implicit val bigDecimalTypeWrapper = new SimpleArrayJdbcType[scala.math.BigDecimal]("numeric")
  .basedOn[java.math.BigDecimal]({ scalaBigDecimal: scala.math.BigDecimal => scalaBigDecimal.bigDecimal }, 
   javaBigDecimal => scala.math.BigDecimal(javaBigDecimal)).to(_.toList)

Both versions compile fine. The former should throw a compile time error (or work as desired)

@tminglei
Copy link
Owner

Yes, we should enhance it.

@tminglei
Copy link
Owner

Hi @mdedetrich, I added type checking logic, but it only takes effect when running instead of compiling.

If you have a better idea, pls don't hesitate to tell me.

@mdedetrich
Copy link
Author

@tminglei I will have a better look as to why this is happening, is there a reason why this happens in slick-pg and not slick.

Maybe the only thing we need to do is to add implicit conversions from all of the Java JDBC types, which is another solution to the problem

@tminglei
Copy link
Owner

@mdedetrich, you mean, define another conversion interface, and make implementations for every supported Java JDBC types?

@mdedetrich
Copy link
Author

@tminglei Yes. So we would create default conversions for all of the JDBC types (i.e. java.math.BigDecimal to scala.math.BigDecimal, java.lang.Long to scala.Long). Any custom types should fall outside of that

@tminglei
Copy link
Owner

Ok, I'll try on it.

@tminglei
Copy link
Owner

@mdedetrich enhanced. Can you help check it again?

Thanks!

@mdedetrich
Copy link
Author

@tminglei Looking at it now

@mdedetrich
Copy link
Author

@tminglei All good, seems to be working fine now with zero problems!

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

No branches or pull requests

2 participants