git.iko.soy/typeable/servant
on master help sign in
doc(servant-foreign): Inflection docs & module docs
commitc3a517cb4f7efd89a031f134dab43b4f3d85217b authorPhilip Patsch <philip.patsch@tweag.io> dateWed 3 Feb 2021, 07:52 (6 years ago) parent0743ca7
3 files changed+23−14
servant-foreign/src/Servant/Foreign.hs+2 servant-foreign/src/Servant/Foreign/Inflections.hs+18−12 servant-foreign/src/Servant/Foreign/Internal.hs+3−2
servant-foreign/src/Servant/Foreign.hs+2
@@ -1,5 +1,7 @@
11-- | Generalizes all the data needed to make code generation work with
22-- 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).
35module Servant.Foreign
46 ( ArgType(..)
57 , HeaderArg(..)
servant-foreign/src/Servant/Foreign/Inflections.hs+18−12
@@ -20,20 +20,31 @@ import Prelude hiding
2020 (head, tail)
2121import Servant.Foreign.Internal
2222
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+
2329concatCaseL :: Getter FunctionName Text
2430concatCaseL = _FunctionName . to mconcat
2531
26−-- | Function name builder that simply concat each part together
27−concatCase :: FunctionName -> Text
28−concatCase = view concatCaseL
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"@
36+snakeCase :: FunctionName -> Text
37+snakeCase = view snakeCaseL
2938
3039snakeCaseL :: Getter FunctionName Text
3140snakeCaseL = _FunctionName . to (intercalate "_")
3241
33−-- | Function name builder using the snake_case convention.
34−-- each part is separated by a single underscore character.
35−snakeCase :: FunctionName -> Text
36−snakeCase = view snakeCaseL
42+-- | Use the camelCase convention.
43+-- The first part is lower case, every other part starts with an upper case character.
44+--
45+-- @[ "get", "documents", "by", "id" ] → "getDocumentsById"@
46+camelCase :: FunctionName -> Text
47+camelCase = view camelCaseL
3748
3849camelCaseL :: Getter FunctionName Text
3950camelCaseL = _FunctionName . to convert
@@ -42,8 +53,3 @@ camelCaseL = _FunctionName . to convert
4253 convert (p:ps) = mconcat $ p : map capitalize ps
4354 capitalize "" = ""
4455 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 @@
1313{-# LANGUAGE TypeOperators #-}
1414{-# LANGUAGE UndecidableInstances #-}
1515
16−-- | Generalizes all the data needed to make code generation work with
17−-- arbitrary programming languages.
1816module Servant.Foreign.Internal where
1917
2018import Prelude ()
@@ -40,6 +38,9 @@ import Servant.API.Modifiers
4038 (RequiredArgument)
4139import Servant.API.TypeLevel
4240
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`.
4344newtype FunctionName = FunctionName { unFunctionName :: [Text] }
4445 deriving (Data, Show, Eq, Semigroup, Monoid, Typeable)
4546