2015-09-22 13:02:52 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2015-09-23 20:39:46 +02:00
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
|
|
|
{-# LANGUAGE NullaryTypeClasses #-}
|
|
|
|
#endif
|
2015-09-22 13:02:52 +02:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2015-10-02 10:23:57 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-09-21 12:31:00 +02:00
|
|
|
|
|
|
|
-- | Generalizes all the data needed to make code generation work with
|
|
|
|
-- arbitrary programming languages.
|
2015-11-16 18:40:15 +01:00
|
|
|
module Servant.Foreign.Internal where
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-10-08 23:33:32 +02:00
|
|
|
import Control.Lens (makeLenses, (%~), (&), (.~), (<>~))
|
2015-11-04 12:16:39 +01:00
|
|
|
import qualified Data.Char as C
|
2015-10-08 23:33:32 +02:00
|
|
|
import Data.Proxy
|
|
|
|
import Data.Text
|
|
|
|
import GHC.Exts (Constraint)
|
|
|
|
import GHC.TypeLits
|
|
|
|
import Prelude hiding (concat)
|
|
|
|
import Servant.API
|
2015-09-21 12:31:00 +02:00
|
|
|
|
|
|
|
-- | Function name builder that simply concat each part together
|
2015-10-02 10:23:57 +02:00
|
|
|
concatCase :: FunctionName -> Text
|
2015-09-21 12:31:00 +02:00
|
|
|
concatCase = concat
|
|
|
|
|
|
|
|
-- | Function name builder using the snake_case convention.
|
|
|
|
-- each part is separated by a single underscore character.
|
2015-10-02 10:23:57 +02:00
|
|
|
snakeCase :: FunctionName -> Text
|
2015-09-21 12:31:00 +02:00
|
|
|
snakeCase = intercalate "_"
|
|
|
|
|
|
|
|
-- | Function name builder using the CamelCase convention.
|
|
|
|
-- each part begins with an upper case character.
|
2015-10-02 10:23:57 +02:00
|
|
|
camelCase :: FunctionName -> Text
|
2015-11-04 12:16:39 +01:00
|
|
|
camelCase = camelCase' . Prelude.map (replace "-" "")
|
|
|
|
where camelCase' [] = ""
|
|
|
|
camelCase' (p:ps) = concat $ p : Prelude.map capitalize ps
|
|
|
|
capitalize "" = ""
|
|
|
|
capitalize name = C.toUpper (Data.Text.head name) `cons` Data.Text.tail name
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
type ForeignType = Text
|
|
|
|
type Arg = (Text, ForeignType)
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-10-08 23:33:32 +02:00
|
|
|
newtype Segment = Segment { _segment :: SegmentType }
|
2015-09-21 12:31:00 +02:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2015-10-02 10:23:57 +02:00
|
|
|
data SegmentType = Static Text -- ^ a static path segment. like "/foo"
|
2015-09-21 12:31:00 +02:00
|
|
|
| Cap Arg -- ^ a capture. like "/:userid"
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
type Path = [Segment]
|
|
|
|
|
|
|
|
data ArgType =
|
|
|
|
Normal
|
|
|
|
| Flag
|
|
|
|
| List
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
data QueryArg = QueryArg
|
|
|
|
{ _argName :: Arg
|
|
|
|
, _argType :: ArgType
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
data HeaderArg = HeaderArg
|
2015-11-28 09:13:26 +01:00
|
|
|
{ headerArg :: Arg
|
2015-09-21 12:31:00 +02:00
|
|
|
}
|
|
|
|
| ReplaceHeaderArg
|
2015-11-28 09:13:26 +01:00
|
|
|
{ headerArg :: Arg
|
2015-10-02 10:23:57 +02:00
|
|
|
, headerPattern :: Text
|
2015-09-22 12:17:43 +02:00
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
|
|
|
|
data Url = Url
|
|
|
|
{ _path :: Path
|
|
|
|
, _queryStr :: [QueryArg]
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
defUrl :: Url
|
|
|
|
defUrl = Url [] []
|
|
|
|
|
2015-10-02 10:23:57 +02:00
|
|
|
type FunctionName = [Text]
|
|
|
|
type Method = Text
|
2015-09-21 12:31:00 +02:00
|
|
|
|
|
|
|
data Req = Req
|
2015-11-28 09:13:26 +01:00
|
|
|
{ _reqUrl :: Url
|
|
|
|
, _reqMethod :: Method
|
|
|
|
, _reqHeaders :: [HeaderArg]
|
|
|
|
, _reqBody :: Maybe ForeignType
|
|
|
|
, _reqReturnType :: ForeignType
|
|
|
|
, _funcName :: FunctionName
|
2015-09-21 12:31:00 +02:00
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
|
|
|
makeLenses ''QueryArg
|
|
|
|
makeLenses ''Segment
|
|
|
|
makeLenses ''Url
|
|
|
|
makeLenses ''Req
|
|
|
|
|
|
|
|
isCapture :: Segment -> Bool
|
2015-10-08 23:33:32 +02:00
|
|
|
isCapture (Segment (Cap _)) = True
|
|
|
|
isCapture _ = False
|
2015-09-21 12:31:00 +02:00
|
|
|
|
|
|
|
captureArg :: Segment -> Arg
|
2015-10-08 23:33:32 +02:00
|
|
|
captureArg (Segment (Cap s)) = s
|
|
|
|
captureArg _ = error "captureArg called on non capture"
|
2015-09-21 12:31:00 +02:00
|
|
|
|
|
|
|
defReq :: Req
|
2015-11-28 09:13:26 +01:00
|
|
|
defReq = Req defUrl "GET" [] Nothing "" []
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-09-22 13:02:52 +02:00
|
|
|
-- | To be used exclusively as a "negative" return type/constraint
|
|
|
|
-- by @'Elem`@ type family.
|
|
|
|
class NotFound
|
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
type family Elem (a :: *) (ls::[*]) :: Constraint where
|
2015-09-22 13:02:52 +02:00
|
|
|
Elem a '[] = NotFound
|
2015-09-21 12:31:00 +02:00
|
|
|
Elem a (a ': list) = ()
|
|
|
|
Elem a (b ': list) = Elem a list
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
class HasForeignType lang a where
|
|
|
|
typeFor :: Proxy lang -> Proxy a -> ForeignType
|
2015-11-28 09:13:26 +01:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
class HasForeign lang (layout :: *) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign layout :: *
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor :: Proxy lang -> Proxy layout -> Req -> Foreign layout
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (HasForeign lang a, HasForeign lang b)
|
|
|
|
=> HasForeign lang (a :<|> b) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (a :<|> b) = Foreign a :<|> Foreign b
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy a) req
|
|
|
|
:<|> foreignFor lang (Proxy :: Proxy b) req
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (Capture sym a :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Capture sym a :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) $
|
2015-11-28 09:13:26 +01:00
|
|
|
req & reqUrl.path <>~ [Segment (Cap arg)]
|
2015-09-21 12:31:00 +02:00
|
|
|
& funcName %~ (++ ["by", str])
|
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
where
|
|
|
|
str = pack . symbolVal $ (Proxy :: Proxy sym)
|
2015-11-29 05:53:50 +01:00
|
|
|
arg = (str, typeFor lang (Proxy :: Proxy a))
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (Elem JSON list, HasForeignType lang a)
|
|
|
|
=> HasForeign lang (Delete list a) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Delete list a) = Req
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
2015-11-28 09:13:26 +01:00
|
|
|
req & funcName %~ ("delete" :)
|
|
|
|
& reqMethod .~ "DELETE"
|
|
|
|
& reqReturnType .~ retType
|
|
|
|
where
|
2015-11-29 05:53:50 +01:00
|
|
|
retType = typeFor lang (Proxy :: Proxy a)
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (Elem JSON list, HasForeignType lang a)
|
|
|
|
=> HasForeign lang (Get list a) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Get list a) = Req
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
2015-09-21 12:31:00 +02:00
|
|
|
req & funcName %~ ("get" :)
|
|
|
|
& reqMethod .~ "GET"
|
2015-11-28 09:13:26 +01:00
|
|
|
& reqReturnType .~ retType
|
|
|
|
where
|
2015-11-29 05:53:50 +01:00
|
|
|
retType = typeFor lang (Proxy :: Proxy a)
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (Header sym a :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Header sym a :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang subP $ req
|
2015-11-28 09:13:26 +01:00
|
|
|
& reqHeaders <>~ [HeaderArg arg]
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
where
|
|
|
|
hname = pack . symbolVal $ (Proxy :: Proxy sym)
|
2015-11-29 05:53:50 +01:00
|
|
|
arg = (hname, typeFor lang (Proxy :: Proxy a))
|
2015-11-28 09:13:26 +01:00
|
|
|
subP = Proxy :: Proxy sublayout
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (Elem JSON list, HasForeignType lang a)
|
|
|
|
=> HasForeign lang (Post list a) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Post list a) = Req
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
2015-09-21 12:31:00 +02:00
|
|
|
req & funcName %~ ("post" :)
|
|
|
|
& reqMethod .~ "POST"
|
2015-11-28 09:13:26 +01:00
|
|
|
& reqReturnType .~ retType
|
|
|
|
where
|
2015-11-29 05:53:50 +01:00
|
|
|
retType = typeFor lang (Proxy :: Proxy a)
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (Elem JSON list, HasForeignType lang a)
|
|
|
|
=> HasForeign lang (Put list a) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Put list a) = Req
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
2015-09-21 12:31:00 +02:00
|
|
|
req & funcName %~ ("put" :)
|
|
|
|
& reqMethod .~ "PUT"
|
2015-11-28 09:13:26 +01:00
|
|
|
& reqReturnType .~ retType
|
|
|
|
where
|
2015-11-29 05:53:50 +01:00
|
|
|
retType = typeFor lang (Proxy :: Proxy a)
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (QueryParam sym a :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (QueryParam sym a :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) $
|
2015-11-28 09:13:26 +01:00
|
|
|
req & reqUrl.queryStr <>~ [QueryArg arg Normal]
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
where
|
|
|
|
str = pack . symbolVal $ (Proxy :: Proxy sym)
|
2015-11-29 05:53:50 +01:00
|
|
|
arg = (str, typeFor lang (Proxy :: Proxy a))
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (QueryParams sym a :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (QueryParams sym a :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) $
|
2015-11-28 09:13:26 +01:00
|
|
|
req & reqUrl.queryStr <>~ [QueryArg arg List]
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
where
|
|
|
|
str = pack . symbolVal $ (Proxy :: Proxy sym)
|
2015-11-29 05:53:50 +01:00
|
|
|
arg = (str, typeFor lang (Proxy :: Proxy a))
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (KnownSymbol sym, HasForeignType lang a, a ~ Bool, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (QueryFlag sym :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (QueryFlag sym :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) $
|
2015-11-28 09:13:26 +01:00
|
|
|
req & reqUrl.queryStr <>~ [QueryArg arg Flag]
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
where
|
|
|
|
str = pack . symbolVal $ (Proxy :: Proxy sym)
|
2015-11-29 05:53:50 +01:00
|
|
|
arg = (str, typeFor lang (Proxy :: Proxy a))
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance HasForeign lang Raw where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign Raw = Method -> Req
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor _ Proxy req method =
|
2015-10-02 10:23:57 +02:00
|
|
|
req & funcName %~ ((toLower method) :)
|
2015-09-21 12:31:00 +02:00
|
|
|
& reqMethod .~ method
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (Elem JSON list, HasForeignType lang a, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (ReqBody list a :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (ReqBody list a :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) $
|
|
|
|
req & reqBody .~ (Just $ typeFor lang (Proxy :: Proxy a))
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance (KnownSymbol path, HasForeign lang sublayout)
|
|
|
|
=> HasForeign lang (path :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (path :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) $
|
2015-10-08 23:33:32 +02:00
|
|
|
req & reqUrl.path <>~ [Segment (Static str)]
|
2015-09-21 12:31:00 +02:00
|
|
|
& funcName %~ (++ [str])
|
|
|
|
|
2015-11-28 09:13:26 +01:00
|
|
|
where
|
|
|
|
str = Data.Text.map (\c -> if c == '.' then '_' else c)
|
|
|
|
. pack . symbolVal $ (Proxy :: Proxy path)
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance HasForeign lang sublayout => HasForeign lang (RemoteHost :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (RemoteHost :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) req
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance HasForeign lang sublayout => HasForeign lang (IsSecure :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (IsSecure :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) req
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance HasForeign lang sublayout => HasForeign lang (Vault :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (Vault :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) req
|
2015-09-21 12:31:00 +02:00
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
instance HasForeign lang sublayout => HasForeign lang (HttpVersion :> sublayout) where
|
2015-09-21 12:31:00 +02:00
|
|
|
type Foreign (HttpVersion :> sublayout) = Foreign sublayout
|
|
|
|
|
2015-11-29 05:53:50 +01:00
|
|
|
foreignFor lang Proxy req =
|
|
|
|
foreignFor lang (Proxy :: Proxy sublayout) req
|
2015-12-02 12:21:37 +01:00
|
|
|
|
|
|
|
-- | Utility class used by 'listFromAPI' which computes
|
|
|
|
-- the data needed to generate a function for each endpoint
|
|
|
|
-- and hands it all back in a list.
|
|
|
|
class GenerateList reqs where
|
|
|
|
generateList :: reqs -> [Req]
|
|
|
|
|
|
|
|
instance GenerateList Req where
|
|
|
|
generateList r = [r]
|
|
|
|
|
|
|
|
instance (GenerateList start, GenerateList rest) => GenerateList (start :<|> rest) where
|
|
|
|
generateList (start :<|> rest) = (generateList start) ++ (generateList rest)
|
|
|
|
|
|
|
|
-- | Generate the necessary data for codegen as a list, each 'Req'
|
|
|
|
-- describing one endpoint from your API type.
|
|
|
|
listFromAPI :: (HasForeign lang api, GenerateList (Foreign api)) => Proxy lang -> Proxy api -> [Req]
|
|
|
|
listFromAPI lang p = generateList (foreignFor lang p defReq)
|
|
|
|
|