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

None.get on nested case class mapping #1790

Open
greenhost87 opened this issue Oct 4, 2017 · 4 comments
Open

None.get on nested case class mapping #1790

greenhost87 opened this issue Oct 4, 2017 · 4 comments

Comments

@greenhost87
Copy link

greenhost87 commented Oct 4, 2017

Simple code to reproduce error on last Slick 3.2.1 version:
https://github.com/snap-swap/slick-none-get-exception

Example code is: https://github.com/snap-swap/slick-none-get-exception/blob/master/src/main/scala/com/snapswap/slick/SlickNoneGetError.scala

If you run code you get exception:

Exception in thread "main" java.util.concurrent.TimeoutException: Futures timed out after [10 seconds]
	at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219)
	at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:223)
	at scala.concurrent.Await$$anonfun$result$1.apply(package.scala:190)
	at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
	at scala.concurrent.Await$.result(package.scala:190)
	at com.snapswap.slick.SlickNoneGetError$.delayedEndpoint$com$snapswap$slick$SlickNoneGetError$1(SlickNoneGetError.scala:72)
	at com.snapswap.slick.SlickNoneGetError$delayedInit$body.apply(SlickNoneGetError.scala:12)
	at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
	at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
	at scala.App$$anonfun$main$1.apply(App.scala:76)
	at scala.App$$anonfun$main$1.apply(App.scala:76)
	at scala.collection.immutable.List.foreach(List.scala:381)
	at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
	at scala.App$class.main(App.scala:76)
	at com.snapswap.slick.SlickNoneGetError$.main(SlickNoneGetError.scala:12)
	at com.snapswap.slick.SlickNoneGetError.main(SlickNoneGetError.scala)

Interesting thing: if option field error setted no error happens (insert a1 as example), but if that field is empty code ruins with error.

As work around you can introduce some of Database Object (DO) and then cover your nested model object to flat DO case class.

@d6y
Copy link
Contributor

d6y commented Oct 4, 2017

Hello @greenhost87 - thanks for the example.

It looks to me like your mapping function needs to return Some(None) on line 34.

However, there's a good case that the meaning of that R => Option[U] function should be explained in the manual. It's not clear what returning None could mean.

As an alternative, you might be able to use:

case class ErrorInfo(value: String) extends MappedTo[String]

...and then preserve the ErrorInfo tpe you want in the column:

def errorCode = column[Option[ErrorInfo]]("error_code")

...and then directly reference errorCode in your projection:

def * = (id, `type`, errorCode).mapTop[Action]

@greenhost87
Copy link
Author

@d6y Thank, add Some(None) fixing the problem, but it's totally unclear why :), for strong type safe mapping. like Slick, this case need more clear way to solve.

Alternative way is work, but if you have only one model. without separation DO and Application models, your whole application begin know about slick mapping specific things.

@d6y
Copy link
Contributor

d6y commented Oct 5, 2017

Thanks for confirming that @greenhost87. I'll leave this ticket open as a note to improve the documentation.

In case it helps regarding the alternative, you can separate the mapping from the model. E.g.,

object adapters {
  implicit val errorInfoMapper = 
     MappedColumnType.base[ErrorInfo, String](_.code, ErrorInfo.apply) 
}

And then import adapters._ where you need them.

@d6y
Copy link
Contributor

d6y commented Oct 5, 2017

What I think would help here is an explanation of why <> has a second argument of a function to Option[T], and what it means if that is None.

@hvesalai hvesalai modified the milestones: Future, Feature ideas, Documentation ideas Feb 28, 2018
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

3 participants