Never bother with `Value`s again?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 15/100
Research direction
This is a design proposal, not a defined task. The entry points are the select and selectOne wrappers over Database.Esqueleto.Experimental and the UnpackValue and CanCoerceFromValue type machinery in the issue body. Start by reading how Value is used in the library's existing select signatures and by checking the aggregate return-type problems the author describes. Done means maintainers have decided whether to integrate this, and on what API, so a newcomer should first ask whether a direction has been chosen.
Written by the indexing model from the issue text.
Description
I was fed up enough with having to pattern match on Values and using unValue when it is only a solo tuple in essence, and can just be coerced away.
So I made a small wrapper around select and selectOne that makes sure to coerce all the Value as to their respective as. That way it's much more ergonomic when doing SELECT table.column FROM and not having to unwrap the Values all the time.
I want to present my implementation here, so that either:
- It can be critiqued and I can improve it; and/or,
- It could be integrated in some way so that the API of
esqueletomight become more usable.
I haven't found any issues with this implementation, other than a few type errors because of too lax (mostly aggregate) function return types. e.g. countRows's return type is a Num a, so this has to be made concrete either in the query, or at the usage site of the returned (Value) a. Or when making helper functions, some type applications might be necessary.
{-# LANGUAGE TypeFamilies #-}
import Database.Esqueleto.Experimental qualified as E
type SqlSelectUnpack a r b =
( SqlSelect a r -- Converts the `SqlExpr (Value a)` to the resulting `[r]` of select
, CanCoerceFromValue r ~ b -- The check that we're only coercing `Value a` to `a`
, Coercible r b
, UnpackValue r b
)
-- | Constraint to remove 'Value' from 'select' return values.
class UnpackValue a b where
unpackValue :: a -> b
-- | Any value can be coerced, either to itself, or @Value a -> a@
instance (Coercible a b, CanCoerceFromValue a ~ b) => UnpackValue a b where
unpackValue = coerce
{-# INLINE unpackValue #-}
{- |
Checks that we're only trying to coerce a @Value a -> a@.
Any other value will not be touched.
-}
type family CanCoerceFromValue a where
CanCoerceFromValue [a] = [CanCoerceFromValue a]
CanCoerceFromValue (Maybe a) = Maybe (CanCoerceFromValue a)
-- This is the one that actually changes the type by removing the 'Value'
CanCoerceFromValue (Value a) = CanCoerceFromValue a
-- ALL THE TUPLE INSTANCES
CanCoerceFromValue (a, b) = (CanCoerceFromValue a, CanCoerceFromValue b)
CanCoerceFromValue (a, b, c) =
(CanCoerceFromValue a, CanCoerceFromValue b, CanCoerceFromValue c)
CanCoerceFromValue (a, b, c, d) =
(CanCoerceFromValue a, CanCoerceFromValue b, CanCoerceFromValue c, CanCoerceFromValue d)
-- ... more tuple instances
-- The default case
CanCoerceFromValue a = a
-- | @SELECT@ from the database (cf. 'Database.Esqueleto.select')
select ::
forall a b r m backend.
( MonadIO m
, SqlBackendCanRead backend
, SqlSelectUnpack a r b
) =>
SqlQuery a -> ReaderT backend m [b]
select query =
unpackValue <$> E.select query
-- | @SELECT@ with @LIMIT 1@ from the database (cf. 'Database.Esqueleto.selectOne')
selectOne ::
forall a b r m backend.
( MonadIO m
, SqlBackendCanRead backend
, SqlSelectUnpack a r b
) =>
SqlQuery a -> ReaderT backend m (Maybe b)
selectOne query =
unpackValue <$> E.selectOne query
- Dominant language
- Haskell
- Stars
- 401
- Forks
- 108
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bitemyapp/esqueleto
-
Difficulty 5/5 Over a week Newbie friendliness 8/100
-
Difficulty 4/5 3-5 days Newbie friendliness 64/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
All issues in bitemyapp/esqueleto
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
haskell/bytestring#730 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
seal-harness/seal-harness#399 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
fmidue/codeworld-tasks#124 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
reflex-frp/reflex#563 ·
-
The codecov job never gets a runner on macos-26 and is cancelledPossibly taken @yegor256 claimed this 1 day ago. Openbug
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
objectionary/phino#2524 · 1 comment ·
Maintainers usually reply within 1 day