Replace use of ToByteString with HttpApiData for GetHeaders, fixes servant/#581

* Version bump because this changes the API for GetHeaders
This commit is contained in:
Alex Mason 2016-09-02 19:46:49 +10:00
parent 9e7356c6d3
commit a1b23018f9
4 changed files with 15 additions and 12 deletions

View file

@ -8,9 +8,7 @@
module Servant.MockSpec where module Servant.MockSpec where
import Data.Aeson as Aeson import Data.Aeson as Aeson
import Data.ByteString.Conversion.To
import Data.Proxy import Data.Proxy
import Data.String
import GHC.Generics import GHC.Generics
import Network.Wai import Network.Wai
import Servant.API import Servant.API
@ -40,8 +38,11 @@ data TestHeader
| ArbitraryHeader | ArbitraryHeader
deriving (Show) deriving (Show)
instance ToByteString TestHeader where instance ToHttpApiData TestHeader where
builder = fromString . show toHeader = toHeader . show
toUrlPiece _ = error "ToHttpApiData.toUrlPiece not implemented for TestHeader"
toQueryParam _ = error "ToHttpApiData.toQueryParam not implemented for TestHeader"
instance Arbitrary TestHeader where instance Arbitrary TestHeader where
arbitrary = return ArbitraryHeader arbitrary = return ArbitraryHeader

View file

@ -2,6 +2,7 @@ next
---- ----
* Add `CaptureAll` combinator. Captures all of the remaining segments in a URL. * Add `CaptureAll` combinator. Captures all of the remaining segments in a URL.
* replace use of `ToByteString` with `HttpApiData` for `GetHeaders`
0.8 0.8
--- ---

View file

@ -1,5 +1,5 @@
name: servant name: servant
version: 0.8 version: 0.8.1
synopsis: A family of combinators for defining webservices APIs synopsis: A family of combinators for defining webservices APIs
description: description:
A family of combinators for defining webservices APIs and serving them A family of combinators for defining webservices APIs and serving them

View file

@ -31,8 +31,9 @@ module Servant.API.ResponseHeaders
) where ) where
import Data.ByteString.Char8 as BS (pack, unlines, init) import Data.ByteString.Char8 as BS (pack, unlines, init)
import Data.ByteString.Conversion (ToByteString, toByteString', import Data.ByteString.Conversion (--ToByteString, toByteString',
FromByteString, fromByteString) FromByteString, fromByteString)
import Web.HttpApiData (ToHttpApiData,toHeader)
import qualified Data.CaseInsensitive as CI import qualified Data.CaseInsensitive as CI
import Data.Proxy import Data.Proxy
import GHC.TypeLits (KnownSymbol, symbolVal) import GHC.TypeLits (KnownSymbol, symbolVal)
@ -88,18 +89,18 @@ class GetHeaders ls where
instance OVERLAPPING_ GetHeaders (HList '[]) where instance OVERLAPPING_ GetHeaders (HList '[]) where
getHeaders _ = [] getHeaders _ = []
instance OVERLAPPABLE_ ( KnownSymbol h, ToByteString x, GetHeaders (HList xs) ) instance OVERLAPPABLE_ ( KnownSymbol h, ToHttpApiData x, GetHeaders (HList xs) )
=> GetHeaders (HList (Header h x ': xs)) where => GetHeaders (HList (Header h x ': xs)) where
getHeaders hdrs = case hdrs of getHeaders hdrs = case hdrs of
Header val `HCons` rest -> (headerName , toByteString' val):getHeaders rest Header val `HCons` rest -> (headerName , toHeader val):getHeaders rest
UndecodableHeader h `HCons` rest -> (headerName, h) : getHeaders rest UndecodableHeader h `HCons` rest -> (headerName, h) :getHeaders rest
MissingHeader `HCons` rest -> getHeaders rest MissingHeader `HCons` rest -> getHeaders rest
where headerName = CI.mk . pack $ symbolVal (Proxy :: Proxy h) where headerName = CI.mk . pack $ symbolVal (Proxy :: Proxy h)
instance OVERLAPPING_ GetHeaders (Headers '[] a) where instance OVERLAPPING_ GetHeaders (Headers '[] a) where
getHeaders _ = [] getHeaders _ = []
instance OVERLAPPABLE_ ( KnownSymbol h, GetHeaders (HList rest), ToByteString v ) instance OVERLAPPABLE_ ( KnownSymbol h, GetHeaders (HList rest), ToHttpApiData v )
=> GetHeaders (Headers (Header h v ': rest) a) where => GetHeaders (Headers (Header h v ': rest) a) where
getHeaders hs = getHeaders $ getHeadersHList hs getHeaders hs = getHeaders $ getHeadersHList hs
@ -111,11 +112,11 @@ class AddHeader h v orig new
addHeader :: v -> orig -> new -- ^ N.B.: The same header can't be added multiple times addHeader :: v -> orig -> new -- ^ N.B.: The same header can't be added multiple times
instance OVERLAPPING_ ( KnownSymbol h, ToByteString v ) instance OVERLAPPING_ ( KnownSymbol h, ToHttpApiData v )
=> AddHeader h v (Headers (fst ': rest) a) (Headers (Header h v ': fst ': rest) a) where => AddHeader h v (Headers (fst ': rest) a) (Headers (Header h v ': fst ': rest) a) where
addHeader a (Headers resp heads) = Headers resp (HCons (Header a) heads) addHeader a (Headers resp heads) = Headers resp (HCons (Header a) heads)
instance OVERLAPPABLE_ ( KnownSymbol h, ToByteString v instance OVERLAPPABLE_ ( KnownSymbol h, ToHttpApiData v
, new ~ (Headers '[Header h v] a) ) , new ~ (Headers '[Header h v] a) )
=> AddHeader h v a new where => AddHeader h v a new where
addHeader a resp = Headers resp (HCons (Header a) HNil) addHeader a resp = Headers resp (HCons (Header a) HNil)