Skip to content

Commit a975cfc

Browse files
authored
Add details about AddHeaders instances (#1490)
* Add details about the instances of AddHeader Also: * Cleanup of extensions and imports
1 parent 9a39799 commit a975cfc

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ doc/venv
3131
doc/tutorial/static/api.js
3232
doc/tutorial/static/jq.js
3333
shell.nix
34+
.hspec-failures
3435

3536
# nix
3637
result*

servant/src/Servant/API/ResponseHeaders.hs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
{-# LANGUAGE DeriveFunctor #-}
44
{-# LANGUAGE FlexibleContexts #-}
55
{-# LANGUAGE FlexibleInstances #-}
6-
{-# LANGUAGE FunctionalDependencies #-}
76
{-# LANGUAGE GADTs #-}
8-
{-# LANGUAGE KindSignatures #-}
9-
{-# LANGUAGE MultiParamTypeClasses #-}
7+
{-# LANGUAGE FunctionalDependencies #-}
108
{-# LANGUAGE PolyKinds #-}
119
{-# LANGUAGE ScopedTypeVariables #-}
1210
{-# LANGUAGE TypeFamilies #-}
@@ -51,9 +49,6 @@ import Web.HttpApiData
5149

5250
import Prelude ()
5351
import Prelude.Compat
54-
import Servant.API.ContentTypes
55-
(JSON, PlainText, FormUrlEncoded, OctetStream,
56-
MimeRender(..))
5752
import Servant.API.Header
5853
(Header)
5954

@@ -117,7 +112,7 @@ instance {-# OVERLAPPABLE #-} ( FromHttpApiData v, BuildHeadersTo xs, KnownSymbo
117112
`HCons` buildHeadersTo headers
118113
Right h -> Header h `HCons` buildHeadersTo headers
119114

120-
-- * Getting
115+
-- * Getting headers
121116

122117
class GetHeaders ls where
123118
getHeaders :: ls -> [HTTP.Header]
@@ -158,20 +153,20 @@ instance (KnownSymbol h, GetHeadersFromHList rest, ToHttpApiData v)
158153
where
159154
getHeaders' hs = getHeadersFromHList $ getHeadersHList hs
160155

161-
-- * Adding
156+
-- * Adding headers
162157

163158
-- We need all these fundeps to save type inference
164159
class AddHeader h v orig new
165160
| h v orig -> new, new -> h, new -> v, new -> orig where
166161
addOptionalHeader :: ResponseHeader h v -> orig -> new -- ^ N.B.: The same header can't be added multiple times
167162

168-
163+
-- In this instance, we add a Header on top of something that is already decorated with some headers
169164
instance {-# OVERLAPPING #-} ( KnownSymbol h, ToHttpApiData v )
170165
=> AddHeader h v (Headers (fst ': rest) a) (Headers (Header h v ': fst ': rest) a) where
171166
addOptionalHeader hdr (Headers resp heads) = Headers resp (HCons hdr heads)
172167

173-
instance {-# OVERLAPPABLE #-} ( KnownSymbol h, ToHttpApiData v
174-
, new ~ (Headers '[Header h v] a) )
168+
-- In this instance, 'a' parameter is decorated with a Header.
169+
instance {-# OVERLAPPABLE #-} ( KnownSymbol h, ToHttpApiData v , new ~ Headers '[Header h v] a)
175170
=> AddHeader h v a new where
176171
addOptionalHeader hdr resp = Headers resp (HCons hdr HNil)
177172

0 commit comments

Comments
 (0)