doc(servant-foreign): Inflection docs & module docs

This commit is contained in:
Philip Patsch 2021-02-03 07:52:28 +01:00
parent 0743ca724d
commit c3a517cb4f
3 changed files with 23 additions and 14 deletions

View file

@ -1,5 +1,7 @@
-- | Generalizes all the data needed to make code generation work with -- | Generalizes all the data needed to make code generation work with
-- arbitrary programming languages. -- arbitrary programming languages.
--
-- 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).
module Servant.Foreign module Servant.Foreign
( ArgType(..) ( ArgType(..)
, HeaderArg(..) , HeaderArg(..)

View file

@ -20,20 +20,31 @@ import Prelude hiding
(head, tail) (head, tail)
import Servant.Foreign.Internal import Servant.Foreign.Internal
-- | Simply concat each part of the FunctionName together.
--
-- @[ "get", "documents", "by", "id" ] → "getdocumentsbyid"@
concatCase :: FunctionName -> Text
concatCase = view concatCaseL
concatCaseL :: Getter FunctionName Text concatCaseL :: Getter FunctionName Text
concatCaseL = _FunctionName . to mconcat concatCaseL = _FunctionName . to mconcat
-- | Function name builder that simply concat each part together -- | Use the snake_case convention.
concatCase :: FunctionName -> Text -- Each part is separated by a single underscore character.
concatCase = view concatCaseL --
-- @[ "get", "documents", "by", "id" ] → "get_documents_by_id"@
snakeCase :: FunctionName -> Text
snakeCase = view snakeCaseL
snakeCaseL :: Getter FunctionName Text snakeCaseL :: Getter FunctionName Text
snakeCaseL = _FunctionName . to (intercalate "_") snakeCaseL = _FunctionName . to (intercalate "_")
-- | Function name builder using the snake_case convention. -- | Use the camelCase convention.
-- each part is separated by a single underscore character. -- The first part is lower case, every other part starts with an upper case character.
snakeCase :: FunctionName -> Text --
snakeCase = view snakeCaseL -- @[ "get", "documents", "by", "id" ] → "getDocumentsById"@
camelCase :: FunctionName -> Text
camelCase = view camelCaseL
camelCaseL :: Getter FunctionName Text camelCaseL :: Getter FunctionName Text
camelCaseL = _FunctionName . to convert camelCaseL = _FunctionName . to convert
@ -42,8 +53,3 @@ camelCaseL = _FunctionName . to convert
convert (p:ps) = mconcat $ p : map capitalize ps convert (p:ps) = mconcat $ p : map capitalize ps
capitalize "" = "" capitalize "" = ""
capitalize name = C.toUpper (head name) `cons` tail name capitalize name = C.toUpper (head name) `cons` tail name
-- | Function name builder using the CamelCase convention.
-- each part begins with an upper case character.
camelCase :: FunctionName -> Text
camelCase = view camelCaseL

View file

@ -13,8 +13,6 @@
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableInstances #-}
-- | Generalizes all the data needed to make code generation work with
-- arbitrary programming languages.
module Servant.Foreign.Internal where module Servant.Foreign.Internal where
import Prelude () import Prelude ()
@ -40,6 +38,9 @@ import Servant.API.Modifiers
(RequiredArgument) (RequiredArgument)
import Servant.API.TypeLevel import Servant.API.TypeLevel
-- | Canonical name of the endpoint, can be used to generate a function name.
--
-- You can use the functions in "Servant.Foreign.Inflections", like 'Servant.Foreign.Inflections.camelCase' to transform to `Text`.
newtype FunctionName = FunctionName { unFunctionName :: [Text] } newtype FunctionName = FunctionName { unFunctionName :: [Text] }
deriving (Data, Show, Eq, Semigroup, Monoid, Typeable) deriving (Data, Show, Eq, Semigroup, Monoid, Typeable)