Skip to content

Commit

Permalink
no sqlcoerce yet
Browse files Browse the repository at this point in the history
  • Loading branch information
parsonsmatt committed Jan 13, 2025
1 parent bcd849c commit 9fd81b5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 48 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
- The ability to `coerce` `SqlExpr` was removed. Instead, use
`veryUnsafeCoerceSqlExpr`. See the documentation on
`veryUnsafeCoerceSqlExpr` for safe use example.
- `unsafeCeorceSqlExpr` is provided as an option when the underlying
Haskell types are coercible. This is still unsafe, as different
`PersistFieldSql` instances may be at play.
- [#420](https://github.com/bitemyapp/esqueleto/pull/421)
- The `LockingKind` constructors are deprecated, and will be removed
from non-Internal modules in a future release. Smart constructors
Expand Down
48 changes: 0 additions & 48 deletions src/Database/Esqueleto/Internal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2523,54 +2523,6 @@ veryUnsafeCoerceSqlExpr (ERaw m k) = ERaw m k
unsafeCoerceSqlExpr :: (Coercible a b) => SqlExpr a -> SqlExpr b
unsafeCoerceSqlExpr = veryUnsafeCoerceSqlExpr

-- | This type class provides a relation between two types @arg@ and @result@
-- such that @arg@ can be coerced into @result@ safely.
--
-- For this to be generally true, you should ensure that the 'sqlType' method
-- agrees for each type. See 'testSqlCoerce' for a function which does this.
--
-- You will likely want to write bidirectional instances. That is, for two types
-- @A@ and @B@, you'll likely want to write:
--
-- @
-- instance SqlCoerce A B
--
-- instance SqlCoerce B A
-- @
--
-- That way, you can coerce along either way.
--
-- @since 3.6.0.0
class SqlCoerce arg result where
-- | A Haskell value-level witness that you can safely
--
-- @since 3.6.0.0
sqlCoerceWitness :: arg -> result

-- | Included as a class member to make importing the function easier.
-- However, this is defaulted to 'veryUnsafeCoerceSqlExpr', so you
-- probably don't want to define your own implementations.
sqlCoerce :: SqlExpr arg -> SqlExpr result
sqlCoerce = veryUnsafeCoerceSqlExpr

-- | This function can be used to test whether or not a 'SqlCoerce' instance is
-- safe or not. If the two underlying 'sqlType' are equal, then this returns
-- @'Right' ()@. Otherwise, it returns a 'Left' with the two 'TypeRep's,
-- allowing you to render failure how you'd like.
--
-- @since 3.6.0.0
testSqlCoerce
:: forall arg result
. ( Typeable arg, PersistFieldSql arg
, Typeable result, PersistFieldSql result
, SqlCoerce arg result
)
=> Either (TypeRep, TypeRep) ()
testSqlCoerce =
if sqlType (Proxy @arg) == sqlType (Proxy @result)
then pure ()
else Left (typeRep (Proxy @arg), typeRep (Proxy @result))

-- | Folks often want the ability to promote a Haskell function into the
-- 'SqlExpr' expression language - and naturally reach for 'fmap'.
-- Unfortunately, this is impossible. We cannot send *functions* to the
Expand Down

0 comments on commit 9fd81b5

Please sign in to comment.