on master help sign in
doc(servant-foreign): Inflection docs & module docs
c3a517cPhilip Patsch
servant-foreign/src/Servant/Foreign.hs+2
@@ -1,5 +1,7 @@
1-- | Generalizes all the data needed to make code generation work with
1-- | Generalizes all the data needed to make code generation work with
2-- arbitrary programming languages.
2-- arbitrary programming languages.
3+--
4+-- See documentation of 'HasForeignType' for a simple example. 'listFromAPI' returns a list of all your endpoints and their foreign types, given a mapping from Haskell types to foreign types (conventionally called `ftypes` below).
3module Servant.Foreign
5module Servant.Foreign
4 ( ArgType(..)
6 ( ArgType(..)
5 , HeaderArg(..)
7 , HeaderArg(..)
servant-foreign/src/Servant/Foreign/Inflections.hs+18−12
@@ -20,20 +20,31 @@ import Prelude hiding
20 (head, tail)
20 (head, tail)
21import Servant.Foreign.Internal
21import Servant.Foreign.Internal
22
22
23+-- | Simply concat each part of the FunctionName together.
24+--
25+-- @[ "get", "documents", "by", "id" ] → "getdocumentsbyid"@
26+concatCase :: FunctionName -> Text
27+concatCase = view concatCaseL
28+
23concatCaseL :: Getter FunctionName Text
29concatCaseL :: Getter FunctionName Text
24concatCaseL = _FunctionName . to mconcat
30concatCaseL = _FunctionName . to mconcat
25
31
26−-- | Function name builder that simply concat each part together
32+-- | Use the snake_case convention.
33+-- Each part is separated by a single underscore character.
34+--
35+-- @[ "get", "documents", "by", "id" ] → "get_documents_by_id"@
27−concatCase :: FunctionName -> Text
36+snakeCase :: FunctionName -> Text
28−concatCase = view concatCaseL
37+snakeCase = view snakeCaseL
29
38
30snakeCaseL :: Getter FunctionName Text
39snakeCaseL :: Getter FunctionName Text
31snakeCaseL = _FunctionName . to (intercalate "_")
40snakeCaseL = _FunctionName . to (intercalate "_")
32
41
33−-- | Function name builder using the snake_case convention.
42+-- | Use the camelCase convention.
34−-- each part is separated by a single underscore character.
43+-- The first part is lower case, every other part starts with an upper case character.
44+--
45+-- @[ "get", "documents", "by", "id" ] → "getDocumentsById"@
35−snakeCase :: FunctionName -> Text
46+camelCase :: FunctionName -> Text
36−snakeCase = view snakeCaseL
47+camelCase = view camelCaseL
37
48
38camelCaseL :: Getter FunctionName Text
49camelCaseL :: Getter FunctionName Text
39camelCaseL = _FunctionName . to convert
50camelCaseL = _FunctionName . to convert
@@ -42,8 +53,3 @@ camelCaseL = _FunctionName . to convert
42 convert (p:ps) = mconcat $ p : map capitalize ps
53 convert (p:ps) = mconcat $ p : map capitalize ps
43 capitalize "" = ""
54 capitalize "" = ""
44 capitalize name = C.toUpper (head name) `cons` tail name
55 capitalize name = C.toUpper (head name) `cons` tail name
45−
46−-- | Function name builder using the CamelCase convention.
47−-- each part begins with an upper case character.
48−camelCase :: FunctionName -> Text
49−camelCase = view camelCaseL
servant-foreign/src/Servant/Foreign/Internal.hs+3−2
@@ -13,8 +13,6 @@
13{-# LANGUAGE TypeOperators #-}
13{-# LANGUAGE TypeOperators #-}
14{-# LANGUAGE UndecidableInstances #-}
14{-# LANGUAGE UndecidableInstances #-}
15
15
16−-- | Generalizes all the data needed to make code generation work with
17−-- arbitrary programming languages.
18module Servant.Foreign.Internal where
16module Servant.Foreign.Internal where
19
17
20import Prelude ()
18import Prelude ()
@@ -40,6 +38,9 @@ import Servant.API.Modifiers
40 (RequiredArgument)
38 (RequiredArgument)
41import Servant.API.TypeLevel
39import Servant.API.TypeLevel
42
40
41+-- | Canonical name of the endpoint, can be used to generate a function name.
42+--
43+-- You can use the functions in "Servant.Foreign.Inflections", like 'Servant.Foreign.Inflections.camelCase' to transform to `Text`.
43newtype FunctionName = FunctionName { unFunctionName :: [Text] }
44newtype FunctionName = FunctionName { unFunctionName :: [Text] }
44 deriving (Data, Show, Eq, Semigroup, Monoid, Typeable)
45 deriving (Data, Show, Eq, Semigroup, Monoid, Typeable)
45
46