Skip to content

Commit 177a720

Browse files
committed
Add missing HasLink instance for DeepQuery (#1784)
1 parent 0d679b2 commit 177a720

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎changelog.d/issue-1784‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
synopsis: Add missing HasLink instance for DeepQuery
2+
issues: #1784
3+
4+
description {
5+
Adds missing `HasLink` instance for the `DeepQuery` type.
6+
}

‎servant/src/Servant/Links.hs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ import Data.Kind
122122
(Type)
123123
import qualified Data.List as List
124124
import Data.Constraint
125+
import Data.Foldable
126+
(foldl')
127+
import Data.Maybe
128+
(fromMaybe)
125129
import Data.Proxy
126130
(Proxy (..))
127131
import Data.Singletons.Bool
@@ -162,6 +166,8 @@ import Servant.API.NamedRoutes
162166
(NamedRoutes)
163167
import Servant.API.QueryParam
164168
(QueryFlag, QueryParam', QueryParams)
169+
import Servant.API.QueryString
170+
(ToDeepQuery, DeepQuery, generateDeepParam, toDeepQuery)
165171
import Servant.API.Raw
166172
(Raw, RawM)
167173
import Servant.API.RemoteHost
@@ -683,3 +689,28 @@ instance {-# OVERLAPPABLE #-} TypeError (NoInstanceFor (HasLink api)) => HasLink
683689
instance HasLink (MultiVerb method cs as r) where
684690
type MkLink (MultiVerb method cs as r) a = a
685691
toLink toA _ = toA
692+
693+
instance (KnownSymbol sym, ToDeepQuery record, HasLink sub) => HasLink (DeepQuery sym record :> sub) where
694+
type MkLink (DeepQuery sym record :> sub) a =
695+
record -> MkLink sub a
696+
697+
toLink :: (KnownSymbol sym, ToDeepQuery record, HasLink sub) =>
698+
(Link -> a)
699+
-> Proxy (DeepQuery sym record :> sub)
700+
-> Link
701+
-> MkLink (DeepQuery sym record :> sub) a
702+
toLink toA _ lnk record =
703+
toLink toA (Proxy @sub) $ addParams lnk
704+
where
705+
k :: Text.Text
706+
k = Text.pack $ symbolVal (Proxy @sym)
707+
708+
mkSingleParam :: ([Text.Text], Maybe Text.Text) -> Param
709+
mkSingleParam x =
710+
let (a, b) = generateDeepParam k x
711+
in SingleParam (Text.unpack a) (Text.pack $ escapeURIString isUnreserved $ Text.unpack $ fromMaybe "" b)
712+
713+
addParams :: Link -> Link
714+
addParams link =
715+
foldl' (flip (addQueryParam . mkSingleParam)) link $
716+
toDeepQuery record

0 commit comments

Comments
 (0)