Skip to content

Commit

Permalink
Fix withTyCon
Browse files Browse the repository at this point in the history
  • Loading branch information
Martoon-00 committed Dec 11, 2018
1 parent 287341f commit d4a3c4d
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions beam-migrate/Database/Beam/Migrate/Checks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Data.Aeson ((.:), (.=), withObject, object)
import Data.Aeson.Types (Parser, Value)
import Data.Hashable (Hashable(..))
import Data.Text (Text)
import Data.Typeable (typeOf, typeRepTyCon, typeRep, Typeable, cast)
import Data.Typeable (Typeable, cast, splitTyConApp, typeOf, typeRep)
#if !MIN_VERSION_base(4, 11, 0)
import Data.Semigroup
#endif
Expand Down Expand Up @@ -165,12 +165,15 @@ instance DatabasePredicate TableHasIndex where
-- | Match a given item's type against a type-level application with the given
-- type constructor. Applies the given function and returns 'Just' its result on match,
-- 'Nothing' otherwise.
-- Unlike 'cast', this function does not require @a@ type to be instance of 'Typeable'.
withTyCon
:: forall (con :: * -> *) (item :: *) r.
(Typeable con, Typeable item)
=> (forall a. con a -> r) -> item -> Maybe r
withTyCon f x = do
guard (typeRepTyCon (typeRep (Proxy @item)) == typeRepTyCon (typeOf x))
(itemTyCon, itemTyArgs@(_ : _)) <- pure $ splitTyConApp (typeOf x)
(conTyCon, conTyArgs) <- pure $ splitTyConApp (typeRep (Proxy @con))
guard (itemTyCon == conTyCon && init itemTyArgs == conTyArgs)
return (f $ unsafeCoerce x)

-- | Convert gathered indices into checks.
Expand Down

0 comments on commit d4a3c4d

Please sign in to comment.