From 0897ae55ff1ec961641d4bfe39a291002199082e Mon Sep 17 00:00:00 2001 From: Seongjun Kim Date: Tue, 11 Apr 2017 01:02:58 +0900 Subject: [PATCH] Add QueryParamForm to API --- servant/src/Servant/API.hs | 2 +- servant/src/Servant/API/QueryParam.hs | 14 +++++++++++++- servant/src/Servant/API/TypeLevel.hs | 4 +++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/servant/src/Servant/API.hs b/servant/src/Servant/API.hs index 08594137..075c3c13 100644 --- a/servant/src/Servant/API.hs +++ b/servant/src/Servant/API.hs @@ -71,7 +71,7 @@ import Servant.API.Header (Header (..)) import Servant.API.HttpVersion (HttpVersion (..)) import Servant.API.IsSecure (IsSecure (..)) import Servant.API.QueryParam (QueryFlag, QueryParam, - QueryParams) + QueryParams, QueryParamForm) import Servant.API.Raw (Raw) import Servant.API.RemoteHost (RemoteHost) import Servant.API.ReqBody (ReqBody) diff --git a/servant/src/Servant/API/QueryParam.hs b/servant/src/Servant/API/QueryParam.hs index ca913e17..531528fd 100644 --- a/servant/src/Servant/API/QueryParam.hs +++ b/servant/src/Servant/API/QueryParam.hs @@ -3,7 +3,7 @@ {-# LANGUAGE TypeOperators #-} {-# LANGUAGE PolyKinds #-} {-# OPTIONS_HADDOCK not-home #-} -module Servant.API.QueryParam (QueryFlag, QueryParam, QueryParams) where +module Servant.API.QueryParam (QueryFlag, QueryParam, QueryParams, QueryParamForm) where import Data.Typeable (Typeable) import GHC.TypeLits (Symbol) @@ -42,9 +42,21 @@ data QueryParams (sym :: Symbol) a -- >>> type MyApi = "books" :> QueryFlag "published" :> Get '[JSON] [Book] data QueryFlag (sym :: Symbol) +-- | Lookup the values associated to the query string parameter +-- and try to extract it as a value of type @a@. +-- +-- Example: +-- +-- >>> -- /books?title=&authors[]=<author1>&authors[]=<author2>&... +-- >>> type MyApi = "books" :> QueryParamForm BookSearchParams :> Get '[JSON] [Book] +data QueryParamForm a + deriving Typeable + -- $setup -- >>> import Servant.API -- >>> import Data.Aeson -- >>> import Data.Text +-- >>> import Web.FormUrlEncoded (FromForm) -- >>> data Book -- >>> instance ToJSON Book where { toJSON = undefined } +-- >>> data BookSearchParams diff --git a/servant/src/Servant/API/TypeLevel.hs b/servant/src/Servant/API/TypeLevel.hs index 3cb8076b..90a7afca 100644 --- a/servant/src/Servant/API/TypeLevel.hs +++ b/servant/src/Servant/API/TypeLevel.hs @@ -51,7 +51,8 @@ import GHC.Exts (Constraint) import Servant.API.Alternative (type (:<|>)) import Servant.API.Capture (Capture, CaptureAll) import Servant.API.Header (Header) -import Servant.API.QueryParam (QueryFlag, QueryParam, QueryParams) +import Servant.API.QueryParam (QueryFlag, QueryParam, + QueryParams, QueryParamForm) import Servant.API.ReqBody (ReqBody) import Servant.API.Sub (type (:>)) import Servant.API.Verbs (Verb) @@ -123,6 +124,7 @@ type family IsElem endpoint api :: Constraint where = IsElem sa sb IsElem sa (QueryParam x y :> sb) = IsElem sa sb IsElem sa (QueryParams x y :> sb) = IsElem sa sb + IsElem sa (QueryParamForm x :> sb) = IsElem sa sb IsElem sa (QueryFlag x :> sb) = IsElem sa sb IsElem (Verb m s ct typ) (Verb m s ct' typ) = IsSubList ct ct'