@@ -12,6 +12,7 @@
1212{-# LANGUAGE TypeFamilies #-}
1313{-# LANGUAGE TypeOperators #-}
1414{-# LANGUAGE UndecidableInstances #-}
15+{-# LANGUAGE OverloadedStrings #-}
1516
1617-- | Generalizes all the data needed to make code generation work with
1718-- arbitrary programming languages.
@@ -51,38 +52,37 @@ import Control.Applicative
5152#endif
5253import Control.Lens (makeLenses, (%~), (&), (.~),
5354 (<>~), _last)
54−import Data.Char (toLower, toUpper)
55−import Data.List
55+import Data.Monoid ((<>))
56+import Data.Text
5657import Data.Proxy
5758import GHC.Exts (Constraint)
5859import GHC.TypeLits
5960import Servant.API
61+import Prelude hiding (concat)
6062
6163-- | Function name builder that simply concat each part together
62−concatCase :: FunctionName -> String
64+concatCase :: FunctionName -> Text
6365concatCase = concat
6466
6567-- | Function name builder using the snake_case convention.
6668-- each part is separated by a single underscore character.
67−snakeCase :: FunctionName -> String
69+snakeCase :: FunctionName -> Text
6870snakeCase = intercalate "_"
6971
7072-- | Function name builder using the CamelCase convention.
7173-- each part begins with an upper case character.
72−camelCase :: FunctionName -> String
74+camelCase :: FunctionName -> Text
7375camelCase [] = ""
7476camelCase (p:ps) = concat $ p : camelCase' ps
7577 where camelCase' [] = []
76− camelCase' (r:rs) = capitalize r : camelCase' rs
77− capitalize [] = []
78− capitalize (x:xs) = toUpper x : xs
78+ camelCase' (r:rs) = toUpper r : camelCase' rs
7979
80−type Arg = String
80+type Arg = Text
8181
8282data Segment = Segment { _segment :: SegmentType, _matrix :: [MatrixArg] }
8383 deriving (Eq, Show)
8484
85−data SegmentType = Static String -- ^ a static path segment. like "/foo"
85+data SegmentType = Static Text -- ^ a static path segment. like "/foo"
8686 | Cap Arg -- ^ a capture. like "/:userid"
8787 deriving (Eq, Show)
8888
@@ -100,11 +100,11 @@ data QueryArg = QueryArg
100100 } deriving (Eq, Show)
101101
102102data HeaderArg = HeaderArg
103− { headerArgName :: String
103+ { headerArgName :: Text
104104 }
105105 | ReplaceHeaderArg
106− { headerArgName :: String
107− , headerPattern :: String
106+ { headerArgName :: Text
107+ , headerPattern :: Text
108108 } deriving (Eq, Show)
109109
110110
@@ -118,8 +118,8 @@ data Url = Url
118118defUrl :: Url
119119defUrl = Url [] []
120120
121−type FunctionName = [String]
122−type Method = String
121+type FunctionName = [Text]
122+type Method = Text
123123
124124data Req = Req
125125 { _reqUrl :: Url
@@ -175,7 +175,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
175175 req & reqUrl.path <>~ [Segment (Cap str) []]
176176 & funcName %~ (++ ["by", str])
177177
178− where str = symbolVal (Proxy :: Proxy sym)
178+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
179179
180180instance Elem JSON list => HasForeign (Delete list a) where
181181 type Foreign (Delete list a) = Req
@@ -198,7 +198,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
198198 foreignFor Proxy req =
199199 foreignFor subP (req & reqHeaders <>~ [HeaderArg hname])
200200
201− where hname = symbolVal (Proxy :: Proxy sym)
201+ where hname = pack . symbolVal $ (Proxy :: Proxy sym)
202202 subP = Proxy :: Proxy sublayout
203203
204204instance Elem JSON list => HasForeign (Post list a) where
@@ -223,7 +223,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
223223 foreignFor (Proxy :: Proxy sublayout) $
224224 req & reqUrl.queryStr <>~ [QueryArg str Normal]
225225
226− where str = symbolVal (Proxy :: Proxy sym)
226+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
227227
228228instance (KnownSymbol sym, HasForeign sublayout)
229229 => HasForeign (QueryParams sym a :> sublayout) where
@@ -233,7 +233,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
233233 foreignFor (Proxy :: Proxy sublayout) $
234234 req & reqUrl.queryStr <>~ [QueryArg str List]
235235
236− where str = symbolVal (Proxy :: Proxy sym)
236+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
237237
238238instance (KnownSymbol sym, HasForeign sublayout)
239239 => HasForeign (QueryFlag sym :> sublayout) where
@@ -243,7 +243,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
243243 foreignFor (Proxy :: Proxy sublayout) $
244244 req & reqUrl.queryStr <>~ [QueryArg str Flag]
245245
246− where str = symbolVal (Proxy :: Proxy sym)
246+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
247247
248248instance (KnownSymbol sym, HasForeign sublayout)
249249 => HasForeign (MatrixParam sym a :> sublayout) where
@@ -253,8 +253,8 @@ instance (KnownSymbol sym, HasForeign sublayout)
253253 foreignFor (Proxy :: Proxy sublayout) $
254254 req & reqUrl.path._last.matrix <>~ [QueryArg strArg Normal]
255255
256− where str = symbolVal (Proxy :: Proxy sym)
257− strArg = str ++ "Value"
256+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
257+ strArg = str <> "Value"
258258
259259instance (KnownSymbol sym, HasForeign sublayout)
260260 => HasForeign (MatrixParams sym a :> sublayout) where
@@ -264,7 +264,7 @@ instance (KnownSymbol sym, HasForeign sublayout)
264264 foreignFor (Proxy :: Proxy sublayout) $
265265 req & reqUrl.path._last.matrix <>~ [QueryArg str List]
266266
267− where str = symbolVal (Proxy :: Proxy sym)
267+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
268268
269269instance (KnownSymbol sym, HasForeign sublayout)
270270 => HasForeign (MatrixFlag sym :> sublayout) where
@@ -274,13 +274,13 @@ instance (KnownSymbol sym, HasForeign sublayout)
274274 foreignFor (Proxy :: Proxy sublayout) $
275275 req & reqUrl.path._last.matrix <>~ [QueryArg str Flag]
276276
277− where str = symbolVal (Proxy :: Proxy sym)
277+ where str = pack . symbolVal $ (Proxy :: Proxy sym)
278278
279279instance HasForeign Raw where
280280 type Foreign Raw = Method -> Req
281281
282282 foreignFor Proxy req method =
283− req & funcName %~ ((toLower <$> method) :)
283+ req & funcName %~ ((toLower method) :)
284284 & reqMethod .~ method
285285
286286instance (Elem JSON list, HasForeign sublayout) => HasForeign (ReqBody list a :> sublayout) where
@@ -299,7 +299,7 @@ instance (KnownSymbol path, HasForeign sublayout)
299299 req & reqUrl.path <>~ [Segment (Static str) []]
300300 & funcName %~ (++ [str])
301301
302− where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)
302+ where str = Data.Text.map (\c -> if c == '.' then '_' else c) . pack . symbolVal $ (Proxy :: Proxy path)
303303
304304instance HasForeign sublayout => HasForeign (RemoteHost :> sublayout) where
305305 type Foreign (RemoteHost :> sublayout) = Foreign sublayout