{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Graphula.Key
( onlyKey
, keys
, Keys
) where
import Database.Persist
import GHC.TypeLits (ErrorMessage (..), TypeError)
import Graphula (Only (..), only)
class EntityKeys a where
type Keys a
keys :: a -> Keys a
instance
TypeError
( 'Text "Cannot use naked ‘"
':<>: 'ShowType (Entity a)
':<>: 'Text "’ as argument to ‘keys’."
':$$: 'Text "Did you mean ‘Only ("
':<>: 'ShowType (Entity a)
':<>: 'Text ")’?"
)
=> EntityKeys (Entity a)
where
type Keys (Entity a) = Key a
keys :: Entity a -> Keys (Entity a)
keys = forall record. Entity record -> Key record
entityKey
onlyKey :: Entity a -> Only (Key a)
onlyKey :: forall a. Entity a -> Only (Key a)
onlyKey = forall a. EntityKeys a => a -> Keys a
keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Only a
only
instance EntityKeys (Only (Entity a)) where
type Keys (Only (Entity a)) = Only (Key a)
keys :: Only (Entity a) -> Keys (Only (Entity a))
keys (Only Entity a
a) = forall a. a -> Only a
Only (forall record. Entity record -> Key record
entityKey Entity a
a)
instance EntityKeys (Entity a, Entity b) where
type Keys (Entity a, Entity b) = (Key a, Key b)
keys :: (Entity a, Entity b) -> Keys (Entity a, Entity b)
keys (Entity a
a, Entity b
b) = (forall record. Entity record -> Key record
entityKey Entity a
a, forall record. Entity record -> Key record
entityKey Entity b
b)
instance EntityKeys (Entity a, Entity b, Entity c) where
type Keys (Entity a, Entity b, Entity c) = (Key a, Key b, Key c)
keys :: (Entity a, Entity b, Entity c)
-> Keys (Entity a, Entity b, Entity c)
keys (Entity a
a, Entity b
b, Entity c
c) = (forall record. Entity record -> Key record
entityKey Entity a
a, forall record. Entity record -> Key record
entityKey Entity b
b, forall record. Entity record -> Key record
entityKey Entity c
c)
instance EntityKeys (Entity a, Entity b, Entity c, Entity d) where
type
Keys (Entity a, Entity b, Entity c, Entity d) =
(Key a, Key b, Key c, Key d)
keys :: (Entity a, Entity b, Entity c, Entity d)
-> Keys (Entity a, Entity b, Entity c, Entity d)
keys (Entity a
a, Entity b
b, Entity c
c, Entity d
d) = (forall record. Entity record -> Key record
entityKey Entity a
a, forall record. Entity record -> Key record
entityKey Entity b
b, forall record. Entity record -> Key record
entityKey Entity c
c, forall record. Entity record -> Key record
entityKey Entity d
d)