packages feed

persistent-generic (empty) → 0.1.0.0

raw patch · 5 files changed

+135/−0 lines, 5 filesdep +basedep +persistentdep +textsetup-changed

Dependencies added: base, persistent, text

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for persistent-generic++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,10 @@+Copyright (c) 2020-2021, David M. Johnson+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.+3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ persistent-generic.cabal view
@@ -0,0 +1,30 @@+cabal-version:       >=1.10+name:                persistent-generic+version:             0.1.0.0+synopsis:            Derive Persistent classes generically+description:         Generic facilities for working with Peristent.+bug-reports:         https://github.com/dmjio/persistent-generic+license:             BSD3+license-file:        LICENSE+author:              David Johnson+maintainer:          djohnson.m@gmail.com+copyright:           David Johnson (c) 2020+category:            Database+build-type:          Simple+extra-source-files:  CHANGELOG.md++library+  exposed-modules:+    Database.Persist.Generic+  build-depends:+    base < 5,+    persistent,+    text+  hs-source-dirs:+    src+  default-language:+    Haskell2010++source-repository head+  type: git+  location: https://github.com/dmjio/persistent-generic.git
+ src/Database/Persist/Generic.hs view
@@ -0,0 +1,88 @@+{-# LANGUAGE FlexibleInstances    #-}+{-# LANGUAGE FlexibleContexts     #-}+{-# LANGUAGE DataKinds            #-}+{-# LANGUAGE TypeOperators        #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE TypeApplications     #-}+{-# LANGUAGE ScopedTypeVariables  #-}+--------------------------------------------------------------------------------+-- |+-- Module      : Database.Persist.Generic+-- Copyright   : (c) 2020 David Johnson+-- License     : All Rights Reserved+-- Maintainer  : David Johnson <djohnson.m@gmail.com>+-- Stability   : Experimental+-- Portability : GHC+--+-- Generic facilities for dealing with Persistent classes.+--+--------------------------------------------------------------------------------+module Database.Persist.Generic+  ( -- * Classes+    GToPersistValue (..)+  , GFromPersistValue (..)+    -- * Methods+  , genericToPersistValue+  , genericFromPersistValue+  ) where++import           Control.Applicative  ((<|>))+import           Data.Bifunctor       (first)+import           Data.Proxy           (Proxy (..))+import           Data.Text            (Text)+import qualified Data.Text            as T+import           Database.Persist.Sql (PersistValue (PersistText), toPersistValue, fromPersistValue)+import           GHC.Generics         (Generic (..), D1, C1, M1(..), (:+:)(..), Meta(..), U1(..))+import           GHC.TypeLits         (KnownSymbol, symbolVal)++-- | Generic deriving of 'toPersistValue'+genericToPersistValue :: (Generic a, GToPersistValue (Rep a)) => a -> PersistValue+genericToPersistValue = gToPersistValue . from++-- | Generic deriving of 'fromPersistValue'+genericFromPersistValue+  :: (Generic a, GFromPersistValue (Rep a))+  => PersistValue+  -> Either Text a+genericFromPersistValue v =+  first T.pack (to <$> gFromPersistValue v)++-- | Generic class for deriving 'PersistValue'+class GToPersistValue f where+  gToPersistValue :: f a -> PersistValue++instance GToPersistValue a => GToPersistValue (D1 f a) where+  gToPersistValue (M1 x) = gToPersistValue x++instance KnownSymbol name => GToPersistValue (C1 ('MetaCons name x y) U1) where+  gToPersistValue (M1 _) = PersistText (T.pack name)+    where+      name = symbolVal (Proxy @ name)++instance (GToPersistValue l, GToPersistValue r) => GToPersistValue (l :+: r) where+  gToPersistValue (L1 x) = gToPersistValue x+  gToPersistValue (R1 x) = gToPersistValue x++-- | Generic class for parsing 'PersistValue'+class GFromPersistValue f where+  gFromPersistValue :: PersistValue -> Either String (f a)++instance GFromPersistValue a => GFromPersistValue (D1 f a) where+  gFromPersistValue x = M1 <$> gFromPersistValue x++instance KnownSymbol name => GFromPersistValue (C1 ('MetaCons name x y) U1) where+  gFromPersistValue (PersistText v) =+    if T.unpack v == name+      then pure (M1 U1)+      else Left $ "Parse error: " <> name+    where+      name = symbolVal (Proxy @ name)+  gFromPersistValue _ = Left $ "Invalid Type: " <> name+    where+      name = symbolVal (Proxy @ name)++instance (GFromPersistValue l, GFromPersistValue r) => GFromPersistValue (l :+: r) where+  gFromPersistValue x = l <|> r+    where+      l = L1 <$> gFromPersistValue x+      r = R1 <$> gFromPersistValue x