Skip to content

Commit 29d3088

Browse files
authored
Merge pull request #40 from marinelli/servant-0.19
Add servant 0.19 support to servant-util
2 parents 04ed46a + 69c3a3e commit 29d3088

File tree

9 files changed

+50
-19
lines changed

9 files changed

+50
-19
lines changed

cabal.project

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages:
2+
./servant-util
3+
./servant-util-beam-pg

servant-util/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased
22
=====
33

4+
* Added support for building the project with `servant` version >= 0.19.
5+
46
0.3
57
===
68

servant-util/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ category: Servant, Web
99
dependencies:
1010
- aeson
1111
- base >= 4.7 && < 5
12+
- bytestring
1213
- constraints
1314
- containers
1415
- data-default

servant-util/servant-util.cabal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: f0ae0577e7cde4002a074bd5fada6d918ea3dc45baee134d38fa8e002ad66d9f
7+
-- hash: 16c019b25a3f668898f483828ea8a0de802a4155bb4601f08b6fe4e095358e04
88

99
name: servant-util
1010
version: 0.3
@@ -111,6 +111,7 @@ library
111111
QuickCheck
112112
, aeson
113113
, base >=4.7 && <5
114+
, bytestring
114115
, constraints
115116
, containers
116117
, data-default
@@ -187,6 +188,7 @@ executable servant-util-examples
187188
QuickCheck
188189
, aeson
189190
, base >=4.7 && <5
191+
, bytestring
190192
, constraints
191193
, containers
192194
, data-default
@@ -275,6 +277,7 @@ test-suite servant-util-test
275277
QuickCheck
276278
, aeson
277279
, base >=4.7 && <5
280+
, bytestring
278281
, constraints
279282
, containers
280283
, data-default

servant-util/src/Servant/Util/Combinators/Filtering/Base.hs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DeriveFunctor #-}
23
{-# LANGUAGE TypeInType #-}
34

@@ -20,8 +21,10 @@ module Servant.Util.Combinators.Filtering.Base
2021
, AreAutoFilters (..)
2122
, FilteringValueParser (..)
2223
, OpsDescriptions
24+
, EncodedQueryParam
2325
, parseFilteringValueAsIs
2426
, unsupportedFilteringValue
27+
, encodeQueryParam
2528
, autoFiltersParsers
2629

2730
, FilteringParamTypesOf
@@ -37,6 +40,10 @@ import Fmt (Buildable (..), Builder)
3740
import GHC.Exts (IsList)
3841
import Servant (FromHttpApiData (..), ToHttpApiData (..))
3942
import Servant.API (NoContent)
43+
#if MIN_VERSION_servant(0,19,0)
44+
import Data.ByteString.Builder (toLazyByteString)
45+
import qualified Data.ByteString.Lazy as BL
46+
#endif
4047

4148
import Servant.Util.Common
4249

@@ -90,6 +97,18 @@ unsupportedFilteringValue errMsg = FilteringValueParser (\_ -> Left errMsg)
9097
-- This is not a 'Map' to prevent developer-defined entries order.
9198
type OpsDescriptions = [(Text, Text)]
9299

100+
-- | Specify the encoding type and function for query parameters.
101+
-- It's required due to this https://github.com/haskell-servant/servant/pull/1432
102+
#if MIN_VERSION_servant(0,19,0)
103+
type EncodedQueryParam = ByteString
104+
encodeQueryParam :: ToHttpApiData a => a -> EncodedQueryParam
105+
encodeQueryParam = BL.toStrict . toLazyByteString . toEncodedUrlPiece
106+
#else
107+
type EncodedQueryParam = Text
108+
encodeQueryParam :: ToHttpApiData a => a -> EncodedQueryParam
109+
encodeQueryParam = toQueryParam
110+
#endif
111+
93112
-- | How auto filters appear in logging.
94113
class BuildableAutoFilter (filter :: Type -> Type) where
95114
buildAutoFilter
@@ -112,7 +131,7 @@ class (Typeable filter, BuildableAutoFilter filter) =>
112131
-- | Encode a filter to query parameter value.
113132
autoFilterEncode
114133
:: ToHttpApiData a
115-
=> filter a -> (Text, Text)
134+
=> filter a -> (Text, EncodedQueryParam)
116135

117136
mapAutoFilterValue
118137
:: (a -> b) -> filter a -> filter b

servant-util/src/Servant/Util/Combinators/Filtering/Client.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Universum hiding (filter)
66

77
import Data.Typeable (cast)
88
import GHC.TypeLits (KnownSymbol)
9-
import Servant (ToHttpApiData (..), toQueryParam, (:>))
9+
import Servant (ToHttpApiData (..), (:>))
1010
import Servant.Client (HasClient (..))
1111
import Servant.Client.Core.Request (Request, appendToQueryString)
1212

@@ -19,10 +19,10 @@ import Servant.Util.Common
1919
-------------------------------------------------------------------------
2020

2121
-- | For given filter return operation name and encoded value.
22-
typeFilterToReq :: ToHttpApiData a => TypeFilter fk a -> (Text, Text)
22+
typeFilterToReq :: ToHttpApiData a => TypeFilter fk a -> (Text, EncodedQueryParam)
2323
typeFilterToReq = \case
2424
TypeAutoFilter (SomeTypeAutoFilter filter) -> autoFilterEncode filter
25-
TypeManualFilter val -> (DefFilteringCmd, toQueryParam val)
25+
TypeManualFilter val -> (DefFilteringCmd, encodeQueryParam val)
2626

2727
-- | Apply filter to a client request being built.
2828
class SomeFilterToReq params where

servant-util/src/Servant/Util/Combinators/Filtering/Filters/General.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Universum
1010
import qualified Data.Map as M
1111
import qualified Data.Text as T
1212
import Fmt (build, listF)
13-
import Servant (FromHttpApiData (..), ToHttpApiData (..))
13+
import Servant (FromHttpApiData (..))
1414

1515
import Servant.Util.Combinators.Filtering.Base
1616

@@ -57,10 +57,12 @@ instance IsAutoFilter FilterMatching where
5757
mapM parseUrlPiece vals
5858

5959
autoFilterEncode = \case
60-
FilterMatching v -> (DefFilteringCmd, toQueryParam v)
61-
FilterNotMatching v -> ("neq", toQueryParam v)
62-
FilterItemsIn vs -> ("in", "[" <> T.intercalate "," (map toQueryParam vs) <> "]")
63-
60+
FilterMatching v -> (DefFilteringCmd, encodeQueryParam v)
61+
FilterNotMatching v -> ("neq", encodeQueryParam v)
62+
FilterItemsIn vs -> ("in", encodeFilterItems vs)
63+
where
64+
encodeFilterItems vs =
65+
"[" <> mconcat (intersperse "," $ map encodeQueryParam vs) <> "]"
6466

6567
-- | Support for @(<)@, @(>)@, @(<=)@ and @(>=)@ operations.
6668
data FilterComparing a
@@ -101,10 +103,10 @@ instance IsAutoFilter FilterComparing where
101103
]
102104

103105
autoFilterEncode = \case
104-
FilterGT v -> ("gt", toQueryParam v)
105-
FilterLT v -> ("lt", toQueryParam v)
106-
FilterGTE v -> ("gte", toQueryParam v)
107-
FilterLTE v -> ("lte", toQueryParam v)
106+
FilterGT v -> ("gt", encodeQueryParam v)
107+
FilterLT v -> ("lt", encodeQueryParam v)
108+
FilterGTE v -> ("gte", encodeQueryParam v)
109+
FilterLTE v -> ("lte", encodeQueryParam v)
108110

109111

110112
-------------------------------------------------------------------------

servant-util/src/Servant/Util/Combinators/Filtering/Filters/Like.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Universum
1515
import qualified Data.Map as M
1616
import qualified Data.Text.Lazy as LT
1717
import Fmt (Buildable (..), (+|), (|+))
18-
import Servant (FromHttpApiData (..), ToHttpApiData (..))
18+
import Servant (FromHttpApiData (..))
1919
import System.Console.Pretty (Color (..), Style (..), color, style)
2020

2121
import Servant.Util.Combinators.Filtering.Base
@@ -117,6 +117,6 @@ instance IsAutoFilter FilterLike where
117117
autoFilterEncode = \case
118118
FilterLike cs (unLikePattern -> pat)
119119
| CaseSensitivity True <- cs
120-
-> ("like", toQueryParam pat)
120+
-> ("like", encodeQueryParam pat)
121121
| otherwise
122-
-> ("ilike", toQueryParam pat)
122+
-> ("ilike", encodeQueryParam pat)

servant-util/src/Servant/Util/Combinators/Sorting/Construction.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module Servant.Util.Combinators.Sorting.Construction
1111
import Universum
1212

1313
import Data.Default (Default (..))
14-
import GHC.Exts (IsList (..))
14+
import qualified GHC.Exts
15+
import GHC.Exts (IsList)
1516
import GHC.TypeLits (ErrorMessage (..), KnownSymbol, Symbol, TypeError)
1617

1718
import Servant.Util.Combinators.Sorting.Base
@@ -79,7 +80,7 @@ sortingSpec = mkSortingSpec [asc #id]
7980
mkSortingSpec
8081
:: ReifySortingItems base
8182
=> [SortingRequestItem provided] -> SortingSpec provided base
82-
mkSortingSpec = fromList
83+
mkSortingSpec = GHC.Exts.fromList
8384

8485
-- | By default 'noSorting' is used.
8586
instance ReifySortingItems base => Default (SortingSpec provided base) where

0 commit comments

Comments
 (0)