gitson 0.4.1 → 0.5.0
raw patch · 3 files changed
+58/−17 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Gitson: saveDocumentById :: (MonadIO i, ToJSON a) => FilePath -> Int -> a -> TransactionWriter i ()
+ Gitson: saveDocumentByName :: (MonadIO i, ToJSON a) => FilePath -> String -> a -> TransactionWriter i ()
Files
- gitson.cabal +3/−3
- library/Gitson.hs +35/−14
- test-suite/GitsonSpec.hs +20/−0
gitson.cabal view
@@ -1,12 +1,12 @@ name: gitson-version: 0.4.1+version: 0.5.0 synopsis: A document store library for Git + JSON. description: A simple document store library for Git + JSON, based on Aeson. Uses command line git. Transactions use flock, so it's safe even across completely separate programs! category: Database, JSON, Git homepage: https://github.com/myfreeweb/gitson author: Greg V-copyright: 2014 Greg V <floatboth@me.com>-maintainer: floatboth@me.com+copyright: 2014-2015 Greg V <greg@unrelenting.technology>+maintainer: greg@unrelenting.technology license: Apache-2.0 license-file: COPYING build-type: Simple
library/Gitson.hs view
@@ -7,6 +7,8 @@ , transaction , saveDocument , saveNextDocument+, saveDocumentById+, saveDocumentByName , listCollections , listDocumentKeys , listEntries@@ -39,6 +41,19 @@ type FileName = String type Finder = ([(IdAndName, FileName)] -> Maybe (IdAndName, FileName)) +splitFindDocument :: (MonadIO i) => FilePath -> Finder -> i (Maybe (IdAndName, FileName))+splitFindDocument collection finder = listDocumentKeys collection >>=+ return . finder . catMaybes . map (\x -> intoFunctor (maybeReadIntString x) x)++documentFullKey :: (MonadIO i) => FilePath -> Finder -> i (Maybe FileName)+documentFullKey collection finder = splitFindDocument collection finder >>= return . (snd <$>)++findById :: Int -> Finder+findById i = find $ (== i) . fst . fst++findByName :: String -> Finder+findByName n = find $ (isSuffixOf n) . snd . fst+ -- | Creates a git repository under a given path. createRepo :: FilePath -> IO () createRepo path = do@@ -81,6 +96,24 @@ return . nextKeyId >>= \nextId -> writeDocument collection (combineKey (nextId, key)) content] +-- | Adds a write action to a transaction.+-- Will update the document with the given numeric id.+saveDocumentById :: (MonadIO i, ToJSON a) => FilePath -> Int -> a -> TransactionWriter i ()+saveDocumentById collection i content = do+ tell [documentFullKey collection (findById i) >>=+ \k -> case k of+ Just key -> writeDocument collection key content+ Nothing -> return ()]++-- | Adds a write action to a transaction.+-- Will update the document with the given numeric id.+saveDocumentByName :: (MonadIO i, ToJSON a) => FilePath -> String -> a -> TransactionWriter i ()+saveDocumentByName collection n content = do+ tell [documentFullKey collection (findByName n) >>=+ \k -> case k of+ Just key -> writeDocument collection key content+ Nothing -> return ()]+ -- | Lists collections in the current repository. listCollections :: (MonadIO i) => i [FilePath] listCollections = liftIO $ do@@ -111,28 +144,16 @@ Just key -> readDocument collection key Nothing -> return Nothing -splitFindDocument :: (MonadIO i) => FilePath -> Finder -> i (Maybe (IdAndName, FileName))-splitFindDocument collection finder = listDocumentKeys collection >>=- return . finder . catMaybes . map (\x -> intoFunctor (maybeReadIntString x) x)--findById :: Int -> Finder-findById i = find $ (== i) . fst . fst--findByName :: String -> Finder-findByName n = find $ (isSuffixOf n) . snd . fst- -- | Reads a document from a collection by numeric id (for example, key "00001-hello" has id 1). readDocumentById :: (MonadIO i, FromJSON a) => FilePath -> Int -> i (Maybe a) readDocumentById collection i =- splitFindDocument collection (findById i) >>=- return . (snd <$>) >>=+ documentFullKey collection (findById i) >>= readDocument' collection -- | Reads a document from a collection by name (for example, key "00001-hello" has name "hello"). readDocumentByName :: (MonadIO i, FromJSON a) => FilePath -> String -> i (Maybe a) readDocumentByName collection n =- splitFindDocument collection (findByName n) >>=- return . (snd <$>) >>=+ documentFullKey collection (findByName n) >>= readDocument' collection -- | Returns a document's id by name (for example, "hello" will return 23 when key "00023-hello" exists).
test-suite/GitsonSpec.hs view
@@ -44,6 +44,26 @@ second <- readFile "things/000002-world.json" second `shouldBe` "{\n \"val\": 2\n}" + describe "saveDocumentById" $ do+ it "updates an entry by specified id" $ do+ createDirectoryIfMissing True "tmp/repo/things"+ _ <- liftIO $ writeFile "tmp/repo/things/000004-second-thing.json" "{\"val\":1}"+ transaction "tmp/repo" $ do+ saveDocumentById "things" 4 Thing {val = 2}+ insideDirectory "tmp/repo" $ do+ second <- readFile "things/000004-second-thing.json"+ second `shouldBe` "{\n \"val\": 2\n}"++ describe "saveDocumentByName" $ do+ it "updates an entry by specified name" $ do+ createDirectoryIfMissing True "tmp/repo/things"+ _ <- liftIO $ writeFile "tmp/repo/things/000004-second-thing.json" "{\"val\":1}"+ transaction "tmp/repo" $ do+ saveDocumentByName "things" "second-thing" Thing {val = 2}+ insideDirectory "tmp/repo" $ do+ second <- readFile "things/000004-second-thing.json"+ second `shouldBe` "{\n \"val\": 2\n}"+ describe "readDocument" $ do it "returns Just the document when reading an document by key" $ do createDirectoryIfMissing True "tmp/repo/things"