@@ -21,7 +21,6 @@ module Servant.Foreign
21 , HeaderArg(..)
21 , HeaderArg(..)
22 , ArgType(..)
22 , ArgType(..)
24− , toValidFunctionName
25 , captureArg
24 , captureArg
27 , concatCase
26 , concatCase
@@ -48,12 +47,8 @@ import Control.Applicative
49import Control.Lens hiding (List)
48import Control.Lens hiding (List)
50import Data.Char (toLower, toUpper)
49import Data.Char (toLower, toUpper)
51−import qualified Data.CharSet as Set
52−import qualified Data.CharSet.Unicode.Category as Set
53import Data.List
50import Data.List
55import Data.Proxy
51import Data.Proxy
56−import qualified Data.Text as T
57import GHC.Exts (Constraint)
52import GHC.Exts (Constraint)
58import GHC.TypeLits
53import GHC.TypeLits
59import Servant.API
54import Servant.API
@@ -105,58 +100,8 @@ data HeaderArg = HeaderArg
105 | ReplaceHeaderArg
100 | ReplaceHeaderArg
106 { headerArgName :: String
101 { headerArgName :: String
107 , headerPattern :: String
102 , headerPattern :: String
108− } deriving (Eq)
103+ } deriving (Eq, Show)
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)
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) .
129−-- | Attempts to reduce the function name provided to that allowed by @'Foreign'@.
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)
137−-- @TODO: specify better default function name, or throw error?
138−toValidFunctionName :: String -> String
139−toValidFunctionName (x:xs) = [setFirstChar x] <> filter remainder xs
141− setFirstChar c = if firstChar c
144− firstChar c = prefixOK c || any (Set.member c) firstLetterOK
145− remainder c = prefixOK c || any (Set.member c) remainderOK
147− prefixOK c = c `elem` ['$','_']
148− -- Unicode character sets
149− firstLetterOK = [ Set.lowercaseLetter
150− , Set.uppercaseLetter
151− , Set.titlecaseLetter
152− , Set.modifierLetter
154− , Set.letterNumber ]
155− remainderOK = firstLetterOK <> [ Set.nonSpacingMark
156− , Set.spacingCombiningMark
158− , Set.connectorPunctuation ]
159−toValidFunctionName [] = "_"
161type MatrixArg = QueryArg
106type MatrixArg = QueryArg