@@ -21,7 +21,6 @@ module Servant.Foreign
2121 , HeaderArg(..)
2222 , ArgType(..)
2323 , Req
24− , toValidFunctionName
2524 , captureArg
2625 , defReq
2726 , concatCase
@@ -48,12 +47,8 @@ import Control.Applicative
4847#endif
4948import Control.Lens hiding (List)
5049import Data.Char (toLower, toUpper)
51−import qualified Data.CharSet as Set
52−import qualified Data.CharSet.Unicode.Category as Set
5350import Data.List
54−import Data.Monoid
5551import Data.Proxy
56−import qualified Data.Text as T
5752import GHC.Exts (Constraint)
5853import GHC.TypeLits
5954import Servant.API
@@ -105,58 +100,8 @@ data HeaderArg = HeaderArg
105100 | ReplaceHeaderArg
106101 { headerArgName :: String
107102 , headerPattern :: String
108− } deriving (Eq)
109−
110−
111−-- |
112−instance Show HeaderArg where
113− show (HeaderArg n) = toValidFunctionName ("header" <> n)
114− show (ReplaceHeaderArg n p)
115− | pn `isPrefixOf` p = pv <> " + \"" <> rp <> "\""
116− | pn `isSuffixOf` p = "\"" <> rp <> "\" + " <> pv
117− | pn `isInfixOf` p = "\"" <> (replace pn ("\" + " <> pv <> " + \"") p)
118− <> "\""
119− | otherwise = p
120− where
121− pv = toValidFunctionName ("header" <> n)
122− pn = "{" <> n <> "}"
123− rp = replace pn "" p
124− -- Use replace method from Data.Text
125− replace old new = T.unpack .
126− T.replace (T.pack old) (T.pack new) .
127− T.pack
128−
129−-- | Attempts to reduce the function name provided to that allowed by @'Foreign'@.
130−--
131−-- Here we are making an assumption that js identifiers are common enough.
132−-- https://mathiasbynens.be/notes/javascript-identifiers
133−-- Couldn't work out how to handle zero-width characters.
134−-- TODO: compare it with other generated languages(such as ruby via lackey)
135−-- and generalize.
136−--
137−-- @TODO: specify better default function name, or throw error?
138−toValidFunctionName :: String -> String
139−toValidFunctionName (x:xs) = [setFirstChar x] <> filter remainder xs
140− where
141− setFirstChar c = if firstChar c
142− then c
143− else '_'
144− firstChar c = prefixOK c || any (Set.member c) firstLetterOK
145− remainder c = prefixOK c || any (Set.member c) remainderOK
146− -- Valid prefixes
147− prefixOK c = c `elem` ['$','_']
148− -- Unicode character sets
149− firstLetterOK = [ Set.lowercaseLetter
150− , Set.uppercaseLetter
151− , Set.titlecaseLetter
152− , Set.modifierLetter
153− , Set.otherLetter
154− , Set.letterNumber ]
155− remainderOK = firstLetterOK <> [ Set.nonSpacingMark
156− , Set.spacingCombiningMark
157− , Set.decimalNumber
158− , Set.connectorPunctuation ]
159−toValidFunctionName [] = "_"
103+ } deriving (Eq, Show)
104+
160105
161106type MatrixArg = QueryArg
162107