prowdsponsor/esqueleto — pull request #69 by erikd (2014-07-07 10:14:18+00:00)
Title: Add function keyE.

Give a value, keyE lifts the Key for that value into the query expression.

keyE :: Esqueleto query expr backend =>
Value (Key entity) -> expr (Value (Key entity))

This function (not totally happy with the name) solves a particular problem in my code base. I have a function `unValueKey` like this:

unValueKey :: Value (Key entity) -> Int64
unValueKey k =
case (unKey . unValue) k of
PersistInt64 x -> x
_ -> error "unValueKey : Bad PersistValue."

and some times in a `where_` clause I would do:

t ^. TableChairId ==. valkey (unValueKey kid)

The problem is the composition of `valkey` with `unValueKey`. Given this function composition, GHC infers a type of:

Esqueleto query expr backend => Value (Key entity1) -> expr (Value (Key entity))

which does not constrain the `entity` type parameter of the output to be the same as the input. Without that constraint, the above is non-sensical.

To fix this, the type signature of `keyE` requires the input and output `entity` type paramer to be identical.

I would have added a test, but I really don't think this can go wrong :-).