servant/servant-foreign/src/Servant/Foreign/Internal.hs

371 lines
11 KiB
Haskell
Raw Normal View History

2016-02-17 22:47:30 +01:00
{-# LANGUAGE CPP #-}
2015-09-23 20:39:46 +02:00
#if !MIN_VERSION_base(4,8,0)
2016-02-17 22:47:30 +01:00
{-# LANGUAGE NullaryTypeClasses #-}
2015-09-23 20:39:46 +02:00
#endif
-- | 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
2016-03-12 10:51:11 +01:00
import Control.Lens hiding (cons, List)
2016-03-01 09:59:00 +01:00
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid
#endif
import Data.Proxy
2016-02-17 22:47:30 +01:00
import Data.String
import Data.Text
import Data.Text.Encoding (decodeUtf8)
import GHC.Exts (Constraint)
import GHC.TypeLits
import qualified Network.HTTP.Types as HTTP
import Prelude hiding (concat)
import Servant.API
2016-02-17 22:47:30 +01:00
newtype FunctionName = FunctionName { unFunctionName :: [Text] }
deriving (Show, Eq, Monoid)
2016-02-17 22:47:30 +01:00
makePrisms ''FunctionName
newtype ForeignType f = ForeignType { unForeignType :: f }
deriving instance Show f => Show (ForeignType f)
deriving instance Eq f => Eq (ForeignType f)
deriving instance IsString f => IsString (ForeignType f)
deriving instance Monoid f => Monoid (ForeignType f)
2016-02-17 22:47:30 +01:00
makePrisms ''ForeignType
2016-02-17 22:47:30 +01:00
newtype PathSegment = PathSegment { unPathSegment :: Text }
deriving (Show, Eq, IsString, Monoid)
makePrisms ''PathSegment
data Arg f = Arg
{ _argName :: PathSegment
, _argType :: ForeignType f }
deriving instance Eq f => Eq (Arg f)
deriving instance Show f => Show (Arg f)
2016-02-17 22:47:30 +01:00
makeLenses ''Arg
argPath :: Getter (Arg f) Text
argPath = argName . _PathSegment
data SegmentType f
2016-02-17 22:47:30 +01:00
= Static PathSegment
-- ^ a static path segment. like "/foo"
| Cap (Arg f)
-- ^ a capture. like "/:userid"
deriving instance Eq f => Eq (SegmentType f)
deriving instance Show f => Show (SegmentType f)
makePrisms ''SegmentType
newtype Segment f = Segment { unSegment :: SegmentType f }
deriving instance Eq f => Eq (Segment f)
deriving instance Show f => Show (Segment f)
makePrisms ''Segment
isCapture :: Segment f -> Bool
2016-02-17 22:47:30 +01:00
isCapture (Segment (Cap _)) = True
isCapture _ = False
captureArg :: Segment f -> Arg f
2016-02-17 22:47:30 +01:00
captureArg (Segment (Cap s)) = s
captureArg _ = error "captureArg called on non capture"
type Path f = [Segment f]
data ArgType
= Normal
| Flag
| List
deriving (Eq, Show)
makePrisms ''ArgType
data QueryArg f = QueryArg
{ _queryArgName :: Arg f
, _queryArgType :: ArgType
}
deriving instance Eq f => Eq (QueryArg f)
deriving instance Show f => Show (QueryArg f)
makeLenses ''QueryArg
data HeaderArg f = HeaderArg
{ _headerArg :: Arg f }
| ReplaceHeaderArg
{ _headerArg :: Arg f
2016-02-17 22:47:30 +01:00
, _headerPattern :: Text
}
deriving instance Eq f => Eq (HeaderArg f)
deriving instance Show f => Show (HeaderArg f)
makeLenses ''HeaderArg
makePrisms ''HeaderArg
data Url f = Url
{ _path :: Path f
, _queryStr :: [QueryArg f]
}
deriving instance Eq f => Eq (Url f)
deriving instance Show f => Show (Url f)
defUrl :: Url f
defUrl = Url [] []
makeLenses ''Url
data Req f = Req
{ _reqUrl :: Url f
2016-01-06 18:20:20 +01:00
, _reqMethod :: HTTP.Method
, _reqHeaders :: [HeaderArg f]
, _reqBody :: Maybe (ForeignType f)
, _reqReturnType :: ForeignType f
, _reqFuncName :: FunctionName
}
deriving instance Eq f => Eq (Req f)
deriving instance Show f => Show (Req f)
makeLenses ''Req
defReq :: Req Text
2016-02-17 22:47:30 +01:00
defReq = Req defUrl "GET" [] Nothing (ForeignType "") (FunctionName [])
-- | To be used exclusively as a "negative" return type/constraint
-- by @'Elem`@ type family.
class NotFound
type family Elem (a :: *) (ls::[*]) :: Constraint where
Elem a '[] = NotFound
Elem a (a ': list) = ()
Elem a (b ': list) = Elem a list
2015-12-02 15:10:30 +01:00
-- | 'HasForeignType' maps Haskell types with types in the target
-- language of your backend. For example, let's say you're
-- implementing a backend to some language __X__:
--
-- > -- First you need to create a dummy type to parametrize your
-- > -- instances.
-- > data LangX
-- >
-- > -- Otherwise you define instances for the types you need
-- > instance HasForeignType LangX Int where
-- > typeFor _ _ = "intX"
-- >
-- > -- Or for example in case of lists
-- > instance HasForeignType LangX a => HasForeignType LangX [a] where
-- > typeFor lang _ = "listX of " <> typeFor lang (Proxy :: Proxy a)
--
-- Finally to generate list of information about all the endpoints for
-- an API you create a function of a form:
--
-- > getEndpoints :: (HasForeign LangX api, GenerateList (Foreign api))
-- > => Proxy api -> [Req]
-- > getEndpoints api = listFromAPI (Proxy :: Proxy LangX) api
--
-- > -- If language __X__ is dynamically typed then you can use
-- > -- a predefined NoTypes parameter
-- > getEndpoints :: (HasForeign NoTypes api, GenerateList (Foreign api))
-- > => Proxy api -> [Req]
-- > getEndpoints api = listFromAPI (Proxy :: Proxy NoTypes) api
-- >
--
2015-11-29 05:53:50 +01:00
class HasForeignType lang a where
typeFor :: Proxy lang -> Proxy a -> ForeignType Text
data NoTypes
instance HasForeignType NoTypes ftype where
2016-02-17 22:47:30 +01:00
typeFor _ _ = ForeignType empty
type HasNoForeignType = HasForeignType NoTypes
2015-11-29 05:53:50 +01:00
class HasForeign lang (layout :: *) where
type Foreign layout :: *
foreignFor :: Proxy lang -> Proxy layout -> Req Text -> Foreign layout
2015-11-29 05:53:50 +01:00
instance (HasForeign lang a, HasForeign lang b)
=> HasForeign lang (a :<|> b) where
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
instance (KnownSymbol sym, HasForeignType lang ftype, HasForeign lang sublayout)
=> HasForeign lang (Capture sym ftype :> sublayout) where
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) $
2016-02-17 22:47:30 +01:00
req & reqUrl . path <>~ [Segment (Cap arg)]
& reqFuncName . _FunctionName %~ (++ ["by", str])
where
2016-02-17 22:47:30 +01:00
str = pack . symbolVal $ (Proxy :: Proxy sym)
ftype = typeFor lang (Proxy :: Proxy ftype)
arg = Arg
{ _argName = PathSegment str
, _argType = ftype }
2016-01-06 18:20:20 +01:00
instance (Elem JSON list, HasForeignType lang a, ReflectMethod method)
=> HasForeign lang (Verb method status list a) where
type Foreign (Verb method status list a) = Req Text
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req =
2016-02-17 22:47:30 +01:00
req & reqFuncName . _FunctionName %~ (methodLC :)
2016-01-06 18:20:20 +01:00
& reqMethod .~ method
& reqReturnType .~ retType
where
retType = typeFor lang (Proxy :: Proxy a)
method = reflectMethod (Proxy :: Proxy method)
methodLC = toLower $ decodeUtf8 method
2015-11-29 05:53:50 +01:00
instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
=> HasForeign lang (Header sym a :> sublayout) where
type Foreign (Header sym a :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req =
2016-02-17 22:47:30 +01:00
foreignFor lang subP $ req & reqHeaders <>~ [HeaderArg arg]
where
hname = pack . symbolVal $ (Proxy :: Proxy sym)
2016-02-17 22:47:30 +01:00
arg = Arg
{ _argName = PathSegment hname
, _argType = typeFor lang (Proxy :: Proxy a) }
2016-02-17 22:47:30 +01:00
subP = Proxy :: Proxy sublayout
2015-11-29 05:53:50 +01:00
instance (KnownSymbol sym, HasForeignType lang a, HasForeign lang sublayout)
=> HasForeign lang (QueryParam sym a :> sublayout) where
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) $
req & reqUrl.queryStr <>~ [QueryArg arg Normal]
where
str = pack . symbolVal $ (Proxy :: Proxy sym)
2016-02-17 22:47:30 +01:00
arg = Arg
{ _argName = PathSegment str
, _argType = typeFor lang (Proxy :: Proxy a) }
instance
(KnownSymbol sym, HasForeignType lang [a], HasForeign lang sublayout)
=> HasForeign lang (QueryParams sym a :> sublayout) where
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) $
req & reqUrl.queryStr <>~ [QueryArg arg List]
where
str = pack . symbolVal $ (Proxy :: Proxy sym)
2016-02-17 22:47:30 +01:00
arg = Arg
{ _argName = PathSegment str
, _argType = typeFor lang (Proxy :: Proxy [a]) }
instance
(KnownSymbol sym, HasForeignType lang Bool, HasForeign lang sublayout)
=> HasForeign lang (QueryFlag sym :> sublayout) where
type Foreign (QueryFlag sym :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req =
foreignFor lang (Proxy :: Proxy sublayout) $
req & reqUrl.queryStr <>~ [QueryArg arg Flag]
where
str = pack . symbolVal $ (Proxy :: Proxy sym)
2016-02-17 22:47:30 +01:00
arg = Arg
{ _argName = PathSegment str
, _argType = typeFor lang (Proxy :: Proxy Bool) }
2015-11-29 05:53:50 +01:00
instance HasForeign lang Raw where
type Foreign Raw = HTTP.Method -> Req Text
2015-11-29 05:53:50 +01:00
foreignFor _ Proxy req method =
2016-02-17 22:47:30 +01:00
req & reqFuncName . _FunctionName %~ ((toLower $ decodeUtf8 method) :)
& 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
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-11-29 05:53:50 +01:00
instance (KnownSymbol path, HasForeign lang sublayout)
=> HasForeign lang (path :> sublayout) where
type Foreign (path :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req =
foreignFor lang (Proxy :: Proxy sublayout) $
2016-02-17 22:47:30 +01:00
req & reqUrl . path <>~ [Segment (Static (PathSegment str))]
& reqFuncName . _FunctionName %~ (++ [str])
where
str =
Data.Text.map (\c -> if c == '.' then '_' else c)
. pack . symbolVal $ (Proxy :: Proxy path)
instance HasForeign lang sublayout
=> HasForeign lang (RemoteHost :> sublayout) where
type Foreign (RemoteHost :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req =
foreignFor lang (Proxy :: Proxy sublayout) req
instance HasForeign lang sublayout
=> HasForeign lang (IsSecure :> sublayout) where
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-11-29 05:53:50 +01:00
instance HasForeign lang sublayout => HasForeign lang (Vault :> sublayout) where
type Foreign (Vault :> sublayout) = Foreign sublayout
2015-11-29 05:53:50 +01:00
foreignFor lang Proxy req =
foreignFor lang (Proxy :: Proxy sublayout) req
instance HasForeign lang sublayout =>
2016-02-28 23:23:32 +01:00
HasForeign lang (WithNamedContext name context sublayout) where
2016-02-28 23:23:32 +01:00
type Foreign (WithNamedContext name context sublayout) = Foreign sublayout
foreignFor lang Proxy = foreignFor lang (Proxy :: Proxy sublayout)
instance HasForeign lang sublayout
=> HasForeign lang (HttpVersion :> sublayout) where
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 Text]
2015-12-02 12:21:37 +01:00
instance GenerateList (Req Text) where
2015-12-02 12:21:37 +01:00
generateList r = [r]
instance (GenerateList start, GenerateList rest)
=> GenerateList (start :<|> rest) where
2015-12-02 12:21:37 +01:00
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 Text]
2015-12-02 12:21:37 +01:00
listFromAPI lang p = generateList (foreignFor lang p defReq)