packages feed

json-api-lib 0.1.1.0 → 0.1.1.1

raw patch · 9 files changed

+109/−57 lines, 9 filesdep ~lensdep ~lens-aesonPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependency ranges changed: lens, lens-aeson

API changes (from Hackage documentation)

- Network.JSONApi: [currentPage] :: Pagination -> Maybe Int
- Network.JSONApi: [pageSize] :: Pagination -> Maybe Int
- Network.JSONApi: [totalDocuments] :: Pagination -> Maybe Int
- Network.JSONApi.Meta: Pagination :: Maybe Int -> Maybe Int -> Maybe Int -> Pagination
- Network.JSONApi.Meta: [currentPage] :: Pagination -> Maybe Int
- Network.JSONApi.Meta: [pageSize] :: Pagination -> Maybe Int
- Network.JSONApi.Meta: [totalDocuments] :: Pagination -> Maybe Int
- Network.JSONApi.Meta: data Pagination
- Network.JSONApi.Meta: instance Data.Aeson.Types.FromJSON.FromJSON Network.JSONApi.Meta.Pagination
- Network.JSONApi.Meta: instance Data.Aeson.Types.ToJSON.ToJSON Network.JSONApi.Meta.Pagination
- Network.JSONApi.Meta: instance GHC.Classes.Eq Network.JSONApi.Meta.Pagination
- Network.JSONApi.Meta: instance GHC.Show.Show Network.JSONApi.Meta.Pagination
- Network.JSONApi.Meta: instance Network.JSONApi.Meta.MetaObject Network.JSONApi.Meta.Pagination
- Network.JSONApi.Resource: PageNum :: Int -> PageNum
- Network.JSONApi.Resource: PageSize :: Int -> PageSize
- Network.JSONApi.Resource: ResourceCount :: Int -> ResourceCount
- Network.JSONApi.Resource: [getPageNum] :: PageNum -> Int
- Network.JSONApi.Resource: [getPageSize] :: PageSize -> Int
- Network.JSONApi.Resource: [getResourceCount] :: ResourceCount -> Int
- Network.JSONApi.Resource: instance GHC.Show.Show Network.JSONApi.Resource.PageNum
- Network.JSONApi.Resource: instance GHC.Show.Show Network.JSONApi.Resource.PageSize
- Network.JSONApi.Resource: instance GHC.Show.Show Network.JSONApi.Resource.ResourceCount
- Network.JSONApi.Resource: newtype PageNum
- Network.JSONApi.Resource: newtype PageSize
- Network.JSONApi.Resource: newtype ResourceCount
+ Network.JSONApi: [getPaginationPageNum] :: Pagination -> PageNum
+ Network.JSONApi: [getPaginationPageSize] :: Pagination -> PageSize
+ Network.JSONApi: [getPaginationResourceCount] :: Pagination -> ResourceCount
+ Network.JSONApi: mkSimpleDocument :: ResourcefulEntity a => [a] -> Document a
+ Network.JSONApi: mkSimpleDocument' :: ResourceData a -> Document a
+ Network.JSONApi.Document: mkSimpleDocument :: ResourcefulEntity a => [a] -> Document a
+ Network.JSONApi.Document: mkSimpleDocument' :: ResourceData a -> Document a
+ Network.JSONApi.Pagination: PageNum :: Int -> PageNum
+ Network.JSONApi.Pagination: PageSize :: Int -> PageSize
+ Network.JSONApi.Pagination: Pagination :: PageSize -> PageNum -> ResourceCount -> Pagination
+ Network.JSONApi.Pagination: ResourceCount :: Int -> ResourceCount
+ Network.JSONApi.Pagination: [getPageNum] :: PageNum -> Int
+ Network.JSONApi.Pagination: [getPageSize] :: PageSize -> Int
+ Network.JSONApi.Pagination: [getPaginationPageNum] :: Pagination -> PageNum
+ Network.JSONApi.Pagination: [getPaginationPageSize] :: Pagination -> PageSize
+ Network.JSONApi.Pagination: [getPaginationResourceCount] :: Pagination -> ResourceCount
+ Network.JSONApi.Pagination: [getResourceCount] :: ResourceCount -> Int
+ Network.JSONApi.Pagination: data Pagination
+ Network.JSONApi.Pagination: instance Data.Aeson.Types.ToJSON.ToJSON Network.JSONApi.Pagination.Pagination
+ Network.JSONApi.Pagination: instance GHC.Show.Show Network.JSONApi.Pagination.PageNum
+ Network.JSONApi.Pagination: instance GHC.Show.Show Network.JSONApi.Pagination.PageSize
+ Network.JSONApi.Pagination: instance GHC.Show.Show Network.JSONApi.Pagination.ResourceCount
+ Network.JSONApi.Pagination: instance Network.JSONApi.Meta.MetaObject Network.JSONApi.Pagination.Pagination
+ Network.JSONApi.Pagination: newtype PageNum
+ Network.JSONApi.Pagination: newtype PageSize
+ Network.JSONApi.Pagination: newtype ResourceCount
- Network.JSONApi: Pagination :: Maybe Int -> Maybe Int -> Maybe Int -> Pagination
+ Network.JSONApi: Pagination :: PageSize -> PageNum -> ResourceCount -> Pagination
- Network.JSONApi: indexLinks :: ResourcefulEntity e => e -> Maybe Text -> PageSize -> PageNum -> ResourceCount -> Links
+ Network.JSONApi: indexLinks :: Text -> Pagination -> Links
- Network.JSONApi.Resource: indexLinks :: ResourcefulEntity e => e -> Maybe Text -> PageSize -> PageNum -> ResourceCount -> Links
+ Network.JSONApi.Resource: indexLinks :: Text -> Pagination -> Links

Files

json-api-lib.cabal view
@@ -1,7 +1,7 @@ cabal-version:       2.0  name:                json-api-lib-version:             0.1.1.0+version:             0.1.1.1 homepage:            https://github.com/shirren/json-api-lib bug-reports:         https://github.com/shirren/json-api-lib/issues license:             MIT@@ -48,6 +48,7 @@     Network.JSONApi.Identifier     Network.JSONApi.Meta     Network.JSONApi.Link+    Network.JSONApi.Pagination     Network.JSONApi.Resource    other-modules:@@ -99,6 +100,7 @@     Network.JSONApi.DocumentSpec     Network.JSONApi.IdentifierSpec     Network.JSONApi.MetaSpec+    Network.JSONApi.PaginationSpec     Network.JSONApi.ResourceSpec     TestHelpers 
src/Network/JSONApi.hs view
@@ -7,11 +7,8 @@ , D.ErrorDocument (..) , D.Included , E.Error (..)-, R.PageNum (..)-, R.PageSize (..) , R.Relationship , R.Resource (..)-, R.ResourceCount (..) , R.Relationships , R.ResourcefulEntity (..) , I.HasIdentifier (..)@@ -19,8 +16,11 @@ , L.Links , M.Meta , M.MetaObject (..)-, M.Pagination (..) , L.mkLinks+, P.Pagination (..)+, P.PageNum (..)+, P.PageSize (..)+, P.ResourceCount (..) , R.indexLinks , R.mkRelationship , R.mkRelationships@@ -32,6 +32,8 @@ , D.mkCompoundDocument , D.mkCompoundDocument' , D.mkIncludedResource+, D.mkSimpleDocument+, D.mkSimpleDocument' , M.mkMeta ) where @@ -40,5 +42,5 @@ import qualified Network.JSONApi.Identifier as I import qualified Network.JSONApi.Link as L import qualified Network.JSONApi.Meta as M+import qualified Network.JSONApi.Pagination as P import qualified Network.JSONApi.Resource as R-
src/Network/JSONApi/Document.hs view
@@ -8,6 +8,8 @@   , Included   , mkDocument   , mkDocument'+  , mkSimpleDocument+  , mkSimpleDocument'   , singleton   , list   , mkCompoundDocument@@ -108,6 +110,18 @@     , _meta = meta     , _included = []     }++{- |+A function for document which do not require links or Meta data.+-}+mkSimpleDocument :: ResourcefulEntity a => [a] -> Document a+mkSimpleDocument res = mkDocument res Nothing Nothing++{- |+A function for document which do not require links or Meta data.+-}+mkSimpleDocument' :: ResourceData a -> Document a+mkSimpleDocument' res = mkDocument' res Nothing Nothing  {- | Constructor function for the Document data type.
src/Network/JSONApi/Meta.hs view
@@ -6,12 +6,10 @@ module Network.JSONApi.Meta ( Meta , MetaObject (..)-, Pagination (..) , mkMeta )where  import           Data.Aeson (ToJSON, FromJSON, Object, toJSON)-import           Data.Aeson.TH import           Data.HashMap.Strict as HM import           Data.Text (Text) @@ -72,19 +70,3 @@ -} mkMeta :: (MetaObject a) => a -> Meta mkMeta obj = Meta $ HM.singleton (typeName obj) (toJSON obj)--{- |-Pagination is arguably a meta object not covered by the Spec. The spec instead opts for-links which are supported by this library. However if you would like to throw a generic-Pagination meta object into your response payload this type may be used.--}-data Pagination = Pagination {-  pageSize :: Maybe Int-  , currentPage :: Maybe Int-  , totalDocuments :: Maybe Int-} deriving (Eq, Show)--$(deriveJSON defaultOptions ''Pagination)--instance MetaObject Pagination where-  typeName _ = "pagination"
+ src/Network/JSONApi/Pagination.hs view
@@ -0,0 +1,50 @@+module Network.JSONApi.Pagination (+    Pagination (..)+  , PageNum (..)+  , PageSize (..)+  , ResourceCount (..)+) where++import Data.Aeson ((.=), ToJSON, object, toJSON)++import Network.JSONApi.Meta (MetaObject (..))++{- |+Wrapper type for the various components of pagination being page size, page number+and the number of resources in total.+-}+data Pagination = Pagination {+    getPaginationPageSize :: PageSize+  , getPaginationPageNum :: PageNum+  , getPaginationResourceCount :: ResourceCount+}++instance ToJSON Pagination where+  toJSON (Pagination (PageSize size) (PageNum num) (ResourceCount count)) =+    object [+        "pageSize" .= size+      , "currentPage" .= num+      , "totalDocuments" .= count+      ]++{- |+Pagination can be used as a meta object if required in addition to the links generated+for paging.+-}+instance MetaObject Pagination where+  typeName _ = "pagination"++{- |+We can specify limits on the number of rows we would like back from the database+-}+newtype PageSize = PageSize {+  getPageSize :: Int+} deriving Show++newtype PageNum = PageNum {+  getPageNum :: Int+} deriving Show++newtype ResourceCount = ResourceCount {+  getResourceCount :: Int+} deriving Show
src/Network/JSONApi/Resource.hs view
@@ -4,10 +4,7 @@ Specification: <http://jsonapi.org/format/#document-resource-objects> -} module Network.JSONApi.Resource-( PageNum (..)-, PageSize (..)-, Resource (..)-, ResourceCount (..)+( Resource (..) , Relationships , ResourcefulEntity (..) , Relationship@@ -24,7 +21,6 @@ import           Data.Aeson.Types (fieldLabelModifier) import           Data.Map (Map) import qualified Data.Map as Map-import           Data.Maybe (fromMaybe) import           Data.Text (Text, pack)  import           GHC.Generics hiding (Meta)@@ -32,6 +28,7 @@ import           Network.JSONApi.Identifier (HasIdentifier (..), Identifier (..)) import           Network.JSONApi.Link (Links, mkLinks) import           Network.JSONApi.Meta (Meta)+import           Network.JSONApi.Pagination (Pagination (..), PageSize (..), PageNum (..), ResourceCount (..)) import           Network.URI.Encode (encodeText)  import           Prelude hiding (id)@@ -150,27 +147,12 @@     buildLink = "/" <> resourceType resource <> "/" <> resourceIdentifier resource  {- |-We can specify limits on the number of rows we would like back from the database--}-newtype PageSize = PageSize {-  getPageSize :: Int-} deriving Show--newtype PageNum = PageNum {-  getPageNum :: Int-} deriving Show--newtype ResourceCount = ResourceCount {-  getResourceCount :: Int-} deriving Show--{- | Helper function to beuild relative links for a collection of resources of type ResourceEntity.  This helper function assumes that the first page is always page 0. -}-indexLinks :: ResourcefulEntity e => e -> Maybe Text -> PageSize -> PageNum -> ResourceCount -> Links-indexLinks resource baseUrl pageSize pageNum resourceCount = mkLinks [+indexLinks :: Text -> Pagination -> Links+indexLinks baseUrl (Pagination pageSize pageNum resourceCount) = mkLinks [      ("self", genLink pgNum)     ,("first", genLink (0 :: Int))     ,("prev", genLink (if pgNum - 1 < 0 then 0 else pgNum - 1))@@ -180,6 +162,5 @@     pgNum = if getPageNum pageNum < 0 then 0 else getPageNum pageNum     pgSize = if getPageSize pageSize <= 0 then 1 else getPageSize pageSize     resCount = if getResourceCount resourceCount < 0 then 0 else getResourceCount resourceCount-    genLink no = fromMaybe "" baseUrl <> "/" <> resourceType resource <> "?" <>-                encodeText "page[number]" <> "=" <> (pack . show) no <>-                "&" <> encodeText "page[size]" <> "=" <> (pack . show) pgSize+    genLink no =  baseUrl <> "?" <> encodeText "page[number]" <> "=" <> (pack . show) no <>+                  "&" <> encodeText "page[size]" <> "=" <> (pack . show) pgSize
test/Network/JSONApi/ErrorSpec.hs view
@@ -14,7 +14,7 @@  spec :: Spec spec = do-  describe "Defaults" $ do+  describe "Defaults" $     it "provides defaults" $       let expectedDefault = Error             { id     = Nothing
+ test/Network/JSONApi/PaginationSpec.hs view
@@ -0,0 +1,21 @@+module Network.JSONApi.PaginationSpec where++import qualified Data.ByteString.Lazy.Char8 as BS++import           Network.JSONApi++import           Prelude hiding (id)++import           TestHelpers (prettyEncode)+import           Test.Hspec++main :: IO ()+main = hspec spec++spec :: Spec+spec =+  describe "JSON serialization" $+    it "provides ToJSON instances" $ do+      let pagination = Pagination (PageSize 1) (PageNum 5) (ResourceCount 20)+      let encJson = BS.unpack . prettyEncode $ pagination+      encJson `shouldBe` "{\n    \"currentPage\": 5,\n    \"pageSize\": 1,\n    \"totalDocuments\": 20\n}"
test/Network/JSONApi/ResourceSpec.hs view
@@ -30,7 +30,7 @@    describe "Index links" $ do     it "should always start at page 0" $ do-      let links = indexLinks testObject Nothing (PageSize 10) (PageNum 1) (ResourceCount 20)+      let links = indexLinks "/TestObject" $ Pagination (PageSize 10) (PageNum 1) (ResourceCount 20)       links `shouldBe` mkLinks [           ("first","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=10")         , ("last","/TestObject?page%5Bnumber%5D=1&page%5Bsize%5D=10")@@ -39,7 +39,7 @@         , ("self","/TestObject?page%5Bnumber%5D=1&page%5Bsize%5D=10")]      it "should never have a negative prev page" $ do-      let links = indexLinks testObject Nothing (PageSize 10) (PageNum 0) (ResourceCount 20)+      let links = indexLinks "/TestObject" $ Pagination (PageSize 10) (PageNum 0) (ResourceCount 20)       links `shouldBe` mkLinks [           ("first","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=10")         , ("last","/TestObject?page%5Bnumber%5D=1&page%5Bsize%5D=10")@@ -48,7 +48,7 @@         , ("self","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=10")]      it "should handle a page size of 0" $ do-      let links = indexLinks testObject Nothing (PageSize 0) (PageNum 0) (ResourceCount 2)+      let links = indexLinks "/TestObject" $ Pagination (PageSize 0) (PageNum 0) (ResourceCount 2)       links `shouldBe` mkLinks [           ("first","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=1")         , ("last","/TestObject?page%5Bnumber%5D=1&page%5Bsize%5D=1")@@ -57,7 +57,7 @@         , ("self","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=1")]      it "should handle a negative page num" $ do-      let links = indexLinks testObject Nothing (PageSize 0) (PageNum $ -1) (ResourceCount 2)+      let links = indexLinks "/TestObject" $ Pagination (PageSize 0) (PageNum $ -1) (ResourceCount 2)       links `shouldBe` mkLinks [           ("first","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=1")         , ("last","/TestObject?page%5Bnumber%5D=1&page%5Bsize%5D=1")@@ -66,7 +66,7 @@         , ("self","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=1")]      it "should handle a negative resource count" $ do-      let links = indexLinks testObject Nothing (PageSize 1) (PageNum 0) (ResourceCount $ -1)+      let links = indexLinks "/TestObject" $ Pagination (PageSize 1) (PageNum 0) (ResourceCount $ -1)       links `shouldBe` mkLinks [           ("first","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=1")         , ("last","/TestObject?page%5Bnumber%5D=-1&page%5Bsize%5D=1")@@ -75,7 +75,7 @@         , ("self","/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=1")]      it "should handle nested resource paths" $ do-      let links = indexLinks testObject (Just "/ParentObject/1") (PageSize 10) (PageNum 1) (ResourceCount 20)+      let links = indexLinks "/ParentObject/1/TestObject" $ Pagination (PageSize 10) (PageNum 1) (ResourceCount 20)       links `shouldBe` mkLinks [           ("first","/ParentObject/1/TestObject?page%5Bnumber%5D=0&page%5Bsize%5D=10")         , ("last","/ParentObject/1/TestObject?page%5Bnumber%5D=1&page%5Bsize%5D=10")@@ -119,7 +119,7 @@           ]  myResourceMetaData :: Meta-myResourceMetaData = mkMeta (Pagination (Just 1) (Just 1) (Just 14))+myResourceMetaData = mkMeta (Pagination (PageSize 1) (PageNum 1) (ResourceCount 14))  testObject :: TestObject testObject = TestObject 1 "Fred Armisen" 49 "Pizza"