packages feed

dirfiles 0.1.0.0 → 0.1.0.1

raw patch · 2 files changed

+39/−26 lines, 2 files

Files

Data/Library.hs view
@@ -13,7 +13,7 @@ , new , unsafeGetItem , getItem--- , testItem+-- , testItems )  where@@ -23,15 +23,19 @@ import Data.Sequence (Seq, adjust, (|>)) import qualified Data.Sequence as Seq import Data.Time.Clock (UTCTime)-import Data.Library.UUIRI (UUIRI)+import Data.Library.UUIRI (UUIRI, iri, uuid, toUUIRI) import Data.Foldable (toList)-import Data.Aeson (ToJSON(toJSON), FromJSON(parseJSON), object, (.=), (.:), Value(Object, Null))+import Data.Aeson (ToJSON(toJSON), FromJSON(parseJSON), object, (.=), (.:), (.:?), (.!=), Value(Object, Null)) import Data.Aeson.Types (Parser) import Control.Applicative ((<$>), (<*>)) import Control.Monad (mzero) import qualified Data.HashMap.Strict as HM import Data.SafeCopy (deriveSafeCopy, base)+import Data.Maybe (fromMaybe) +import Keys.UUID (nil, UUID(..))+import Data.UUID hiding (nil, UUID)+ -- | A Library is just a rebranded Seq type Library = Seq LibraryItem @@ -150,21 +154,29 @@     }   --- testItem t = Item 0 "teste" (Just "isto é um teste") t t 0 (Left (empty,empty))+{-+testItems t = +    [ Item 0 "teste" (Just "isto eh um teste") t t 0 (Left (IS.singleton 1,IS.singleton 2))+    , f1 t, d1 t+    ]+f1 t = Item 1 "f1" (Just "ficheiro 1") t t 0 (Right $ toUUIRI (UUID $ fromWords 1 2 3 4) "http://blah.com/f1.txt")+d1 t = Item 2 "d1" (Just "dir 1") t t 0 (Left (empty, empty))+-}  instance ToJSON LibraryItem where     toJSON Deleted = Null-    toJSON (Item iid iname idesc icreated imodified iparent itype) = -       object ["id" .= iid, "name" .= iname, "description" .= idesc-              , "created" .= icreated, "modified" .= imodified-              , "parent" .= iparent, "item" .= typeJson-              ] +    toJSON (Item iid iname idesc icreated imodified iparent itype) = object $+        case itype of+            Right f -> ("uuid", toJSON (uuid f)) : ("url", toJSON (iri f)) :+                       ("subdirectories", Null) : ("files",Null) : common+            Left (f,d) -> ("uuid", Null) : ("url", Null) : +                          ("subdirectories", toJSON d) : ("files", toJSON f) : +                          common         where -            typeJson = case itype of-                        Left (f,d) -> object [ "files" .= f -                                             , "subdirectories" .= d-                                             ]-                        Right f -> toJSON f+            common = ["id" .= iid, "name" .= iname, "description" .= idesc+                     , "created" .= icreated, "modified" .= imodified+                     , "parent" .= iparent +                     ]  instance FromJSON LibraryItem where     parseJSON Null = return Deleted@@ -174,17 +186,18 @@                                 <*> i .: "created"                                 <*> i .: "modified"                                 <*> i .: "parent"-                                <*> lookForItem i-        where -              -- WARNING: HM.lookup might change if aeson changes the type -              lookForItem i = maybe emptyDir getType $ HM.lookup "item" i -              getType t = case t of -                            Object o -> toDirectory <$> o .: "files"-                                                    <*> o .: "subdirectories"-                            _ -> Right <$> parseJSON t-              toDirectory :: IntSet -> IntSet -> Either Directory UUIRI-              toDirectory f d = Left (f,d)-              emptyDir = return $ Left (empty, empty)+                                <*> do fuuid  <- i .:? "uuid"+                                       furl   <- i .:? "url"+                                       dfiles <- i .:? "files"+                                       ddirs  <- i .:? "subdirectories"+                                       return $ case furl of+                                         Nothing -> makeDir dfiles ddirs+                                         Just f -> makeUUIRI fuuid f+        where+            makeDir :: Maybe IntSet -> Maybe IntSet -> Either Directory UUIRI+            makeDir i1 i2 = Left (fromMaybe (IS.empty) i1, fromMaybe (IS.empty) i2)+            makeUUIRI :: Maybe UUID -> Text -> Either Directory UUIRI+            makeUUIRI u iri = Right $ toUUIRI (fromMaybe nil u) iri     parseJSON _          = mzero  isDir :: LibraryItem -> Bool
dirfiles.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                dirfiles-version:             0.1.0.0+version:             0.1.0.1 -- synopsis:             description:         Simple datatype to represent a library with files and folders license:             BSD3