Limit embeded items

This commit is contained in:
Ruslan Talpa
2016-05-26 09:56:28 +03:00
parent e76de196e0
commit 7c83edc402
11 changed files with 208 additions and 63 deletions
+23 -5
View File
@@ -4,13 +4,16 @@ module PostgREST.RangeQuery (
, rangeLimit
, rangeOffset
, restrictRange
, rangeGeq
, NonnegRange
, limitToRange
, toRange
) where
import Control.Applicative
import Network.HTTP.Types.Header
import PostgREST.Types ()
import Data.Monoid ((<>))
import qualified Data.ByteString.Char8 as BS
import Data.Ranged.Boundaries
@@ -38,12 +41,13 @@ rangeParse range = do
rangeIntersection lower upper
Nothing -> rangeGeq 0
rangeRequested :: RequestHeaders -> NonnegRange
rangeRequested = rangeParse . fromMaybe "" . lookup hRange
rangeRequested :: RequestHeaders -> Maybe NonnegRange
rangeRequested headers = rangeParse <$> lookup hRange headers
restrictRange :: Maybe Integer -> NonnegRange -> NonnegRange
restrictRange :: Maybe Integer -> Maybe NonnegRange -> Maybe NonnegRange
restrictRange Nothing r = r
restrictRange (Just limit) r =
restrictRange (Just limit) Nothing = Just $ rangeIntersection (rangeGeq 0) (rangeLeq (limit - 1))
restrictRange (Just limit) (Just r) = Just $
rangeIntersection r $
Range BoundaryBelowAll (BoundaryAbove $ rangeOffset r + limit - 1)
@@ -66,3 +70,17 @@ rangeGeq n =
rangeLeq :: Integer -> NonnegRange
rangeLeq n =
Range BoundaryBelowAll (BoundaryAbove n)
limitToRange :: BS.ByteString -> NonnegRange
limitToRange l = rangeParse ("0-" <> cs (show (l' - 1)))
where l' = fromMaybe 0 (readMaybe $ cs l)::Integer
toRange :: Maybe String -> Maybe String -> Maybe NonnegRange
toRange Nothing Nothing = Nothing
toRange Nothing (Just o) = Just $ rangeParse $ cs $ show o' <> "-"
where o' = fromMaybe 0 (readMaybe $ cs o)::Integer
toRange (Just l) Nothing = Just $ limitToRange $ cs l
toRange (Just l) (Just o) = Just $ rangeParse $ cs $ show o' <> "-" <> show (o' + l' - 1)
where
l' = fromMaybe 0 (readMaybe $ cs l)::Integer
o' = fromMaybe 0 (readMaybe $ cs o)::Integer