-
Notifications
You must be signed in to change notification settings - Fork 348
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
add support for auto encoding of wrapped types #199
Conversation
case class Entity(x: Wrapped) | ||
|
||
val q = quote { | ||
(x: Wrapped) => query[Wrapped].filter(_.value == x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is filter
comparing an Int
(_.value
) to a Wrapped
instance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is, but should not, I'll update PR, all the queries should be against Entity
with wrapped param as argument.
…ly on `WrappedValue` instance to make Codecov happy.
@godenji what problem does this PR solve? |
@gustavoamigo automatic encoding of, for example, Id types (or any single value constructor that extends Can also be used as basis for trait Id[T] extends Any with MappedValue[T] {
def empty: Boolean = value == Id.zero
}
object Id{
private val(intZero,doubleZero,longZero,stringZero,uuidZero) = (
0, 0.0, 0L, "", new java.util.UUID(0L,0L)
)
/** default Id value's type */
type Value = Int
/** default Id */
type Default = Id[Value]
/** default zero value */
val zero: Value = intZero
}
case class UserId(value: Id.Default) extends Id[Id.Default]
case class CompanyId(value: Id.Default) extends Id[Id.Default]
... Although there's probably a better approach (i.e. not relying on static object). |
@gustavoamigo @fwbrasil @jilen @lvicentesanchez can you guys take another look at this? Maybe I didn't explain clearly enough: this PR gives us mapping of wrapped types for free From the updated README section on encoding import io.getquill.sources._
case class UserId(value: Int) extends AnyVal with WrappedValue[Int]
case class User(id: UserId, name: String)
val q = quote {
(id: UserId) => for {
u <- query[User] if u.id == id
} yield u
}
db.run(q)(UserId(1))
// SELECT u.id, u.name FROM User u WHERE (u.id = 1) Pretty simple implementation, just a couple of traits and an implicit conversion. Scala's a strongly typed language, let's use types to our advantage 😄 |
I need to look at this more carefully but as I use value classes a lot not having to write wrappers would be great. I'm going to check out the PR tomorrow and use it on my pet project. Sent from my iPad
|
@lvicentesanchez exactly, got 50+ tables in a production app and definitely don't want to write implicit encoders for |
@lvicentesanchez Do you mind if we merge this change and you test it on master? |
@fwbrasil @lvicentesanchez @gustavoamigo @jilen looks like updating the branch requires committers to re-approve the PR? That or there's some kind of Otherwise, good to go for merging. |
👍
|
add support for auto encoding of wrapped types
This PR addresses ID handling issue