polysemy-kvstore-jsonfile 0.1.0.0 → 0.1.1.0
raw patch · 2 files changed
+44/−28 lines, 2 filesdep +polysemy-kvstoredep −polysemy-zoodep ~aesondep ~basedep ~containersPVP ok
version bump matches the API change (PVP)
Dependencies added: polysemy-kvstore
Dependencies removed: polysemy-zoo
Dependency ranges changed: aeson, base, containers, exceptions, extra, path, polysemy, unliftio-path
API changes (from Hackage documentation)
Files
- polysemy-kvstore-jsonfile.cabal +11/−11
- src/Polysemy/JSONFileKVStore.hs +33/−17
polysemy-kvstore-jsonfile.cabal view
@@ -1,11 +1,11 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.34.2.+-- This file has been generated from package.yaml by hpack version 0.34.4. -- -- see: https://github.com/sol/hpack name: polysemy-kvstore-jsonfile-version: 0.1.0.0+version: 0.1.1.0 synopsis: Run a KVStore as a single json file in polysemy. category: Polysemy author: Daniel Firth@@ -30,13 +30,13 @@ hs-source-dirs: src build-depends:- aeson- , base >=4.7 && <5- , containers- , exceptions- , extra- , path- , polysemy- , polysemy-zoo- , unliftio-path+ aeson >=1.0 && <1.6+ , base >=4.7 && <4.16+ , containers >=0.5 && <0.7+ , exceptions >=0.10.0 && <0.11+ , extra >=1.0 && <1.8+ , path >=0.7 && <0.10+ , polysemy >=1.3.0.0 && <1.7+ , polysemy-kvstore >=0.1.2.0 && <0.2+ , unliftio-path >=0.0.2.0 && <0.1 default-language: Haskell2010
src/Polysemy/JSONFileKVStore.hs view
@@ -1,26 +1,31 @@-{-# LANGUAGE BlockArguments #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE DeriveGeneric #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE LambdaCase #-}-{-# LANGUAGE PolyKinds #-}+{-# LANGUAGE BlockArguments #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE GADTs #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE PolyKinds #-} {-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE TypeApplications #-}-{-# LANGUAGE TypeOperators #-}-module Polysemy.JSONFileKVStore where+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE TypeOperators #-} +module Polysemy.JSONFileKVStore+ ( JSONParseException (..),+ runKVStoreAsJSONFileStore,+ eitherDecodeOrCreate,+ )+where+ import Control.Monad.Catch import Control.Monad.Extra import Control.Monad.IO.Class import Data.Aeson-import qualified Data.Map as Map import Data.Map (Map)+import qualified Data.Map as Map import GHC.Generics+import Path import Polysemy import Polysemy.Error-import Polysemy.Input import Polysemy.KVStore-import Path import qualified UnliftIO.Path.Directory as U newtype JSONParseException = JSONParseException String@@ -34,18 +39,29 @@ whenM (fmap not . U.doesFileExist $ f) $ liftIO $ encodeFile (toFilePath f) x liftIO $ eitherDecodeFileStrict' (toFilePath f) -runKVStoreAsJSONFileStore :: (Members '[Embed IO, Error JSONParseException] r,- FromJSONKey k, ToJSONKey k, FromJSON v, ToJSON v, Ord k)- => Path b File- -> Sem (KVStore k v ': r) a -> Sem r a+-- | Run a `KVStore` as a json file stored as a `Map`.+--+-- @since 0.1.0.0+runKVStoreAsJSONFileStore ::+ ( Members '[Embed IO, Error JSONParseException] r,+ FromJSONKey k,+ ToJSONKey k,+ FromJSON v,+ ToJSON v,+ Ord k+ ) =>+ Path b File ->+ Sem (KVStore k v ': r) a ->+ Sem r a runKVStoreAsJSONFileStore d = interpret \case LookupKV k -> do z <- eitherDecodeOrCreate d mempty case z of- Left x -> throw @JSONParseException $ JSONParseException x+ Left x -> throw @JSONParseException $ JSONParseException x Right x -> return $ Map.lookup k x UpdateKV k v -> do z <- eitherDecodeOrCreate d mempty case z of Left x -> throw $ JSONParseException x Right (x :: Map k v) -> embed $ encodeFile (toFilePath d) (Map.alter (const v) k x)+{-# INLINE runKVStoreAsJSONFileStore #-}