on master help sign in
Merge pull request #171 from freezeboy/transform-function-name

Replace functionName with an array, and use functionRenamer to build …

commitdc902e2f58a986905876299f7815c4ff0fa728d9 authorAlp Mestanogullari <alpmestan@gmail.com> dateWed 29 Jul 2015, 12:03 (11 years ago) parent4ff9697116d7bf
9 files changed+50−25
servant-js/examples/www/angular/index.html+2−2 servant-js/examples/www/angular/service.html+2−2 servant-js/examples/www/jquery/index.html+3−3 servant-js/examples/www/vanilla/index.html+3−3 servant-js/src/Servant/JS.hs+5 servant-js/src/Servant/JS/Angular.hs+1−1 servant-js/src/Servant/JS/Internal.hs+32−12 servant-js/src/Servant/JS/JQuery.hs+1−1 servant-js/src/Servant/JS/Vanilla.hs+1−1
servant-js/examples/www/angular/index.html+2−2
@@ -21,11 +21,11 @@ counterApp.controller('CounterCtrl', ['$scope', '$http', '$interval', function (
2121 var publishCounter = function (response) { $scope.counter = response.value; };
2222
2323 $scope.getCounter = function() {
24− getcounter($http)
24+ getCounter($http)
2525 .success(publishCounter);
2626 }
2727 $scope.incCounter = function() {
28− postcounter($http)
28+ postCounter($http)
2929 .success(publishCounter);
3030 }
3131
servant-js/examples/www/angular/service.html+2−2
@@ -20,11 +20,11 @@ counterApp.controller('CounterCtrl', function ($scope,counterSvc, $interval) {
2020 var publishCounter = function (response) { $scope.counter = response.value; };
2121
2222 $scope.getCounter = function() {
23− counterSvc.getcounter()
23+ counterSvc.getCounter()
2424 .success(publishCounter);
2525 }
2626 $scope.incCounter = function() {
27− counterSvc.postcounter()
27+ counterSvc.postCounter()
2828 .success(publishCounter);
2929 }
3030
servant-js/examples/www/jquery/index.html+3−3
@@ -17,11 +17,11 @@
1717<script type="text/javascript">
1818$(document).ready(function() {
1919 // we get the current value stored by the server when the page is loaded
20− getcounter(updateCounter, alert);
20+ getCounter(updateCounter, alert);
2121
2222 // we update the value every 1sec, in the same way
2323 window.setInterval(function() {
24− getcounter(updateCounter, alert);
24+ getCounter(updateCounter, alert);
2525 }, 1000);
2626});
2727
@@ -33,7 +33,7 @@ function updateCounter(response)
3333// when the button is clicked, ask the server to increase
3434// the value by one
3535$('#inc').click(function() {
36− postcounter(updateCounter, alert);
36+ postCounter(updateCounter, alert);
3737});
3838</script>
3939</body>
servant-js/examples/www/vanilla/index.html+3−3
@@ -16,11 +16,11 @@
1616<script type="text/javascript">
1717window.addEventListener('load', function() {
1818 // we get the current value stored by the server when the page is loaded
19− getcounter(updateCounter, alert);
19+ getCounter(updateCounter, alert);
2020
2121 // we update the value every 1sec, in the same way
2222 window.setInterval(function() {
23− getcounter(updateCounter, alert);
23+ getCounter(updateCounter, alert);
2424 }, 1000);
2525});
2626
@@ -32,7 +32,7 @@ function updateCounter(response)
3232// when the button is clicked, ask the server to increase
3333// the value by one
3434document.getElementById('inc').addEventListener('click', function() {
35− postcounter(updateCounter, alert);
35+ postCounter(updateCounter, alert);
3636});
3737</script>
3838</body>
servant-js/src/Servant/JS.hs+5
@@ -79,6 +79,11 @@ module Servant.JS
7979 CommonGeneratorOptions(..)
8080 , defCommonGeneratorOptions
8181
82+ , -- * Function renamers
83+ concatCase
84+ , snakeCase
85+ , camelCase
86+
8287 , -- * Vanilla Javascript code generation
8388 vanillaJS
8489 , vanillaJSWith
servant-js/src/Servant/JS/Angular.hs+1−1
@@ -123,7 +123,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
123123
124124 fsep = if hasService then ":" else " ="
125125
126− fname = namespace <> (functionRenamer opts $ req ^. funcName)
126+ fname = namespace <> (functionNameBuilder opts $ req ^. funcName)
127127
128128 method = req ^. reqMethod
129129 url = if url' == "'" then "'/'" else url'
servant-js/src/Servant/JS/Internal.hs+32−12
@@ -14,7 +14,7 @@ module Servant.JS.Internal where
1414import Control.Applicative
1515#endif
1616import Control.Lens
17−import Data.Char (toLower)
17+import Data.Char (toLower, toUpper)
1818import qualified Data.CharSet as Set
1919import qualified Data.CharSet.Unicode.Category as Set
2020import Data.List
@@ -29,7 +29,7 @@ import Servant.API
2929-- customize the output
3030data CommonGeneratorOptions = CommonGeneratorOptions
3131 {
32− functionRenamer :: String -> String -- ^ function transforming function names
32+ functionNameBuilder :: FunctionName -> String -- ^ function generating function names
3333 , requestBody :: String -- ^ name used when a user want to send the request body (to let you redefine it)
3434 , successCallback :: String -- ^ name of the callback parameter when the request was successful
3535 , errorCallback :: String -- ^ name of the callback parameter when the request reported an error
@@ -41,7 +41,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions
4141--
4242-- @
4343-- > defCommonGeneratorOptions = CommonGeneratorOptions
44−-- > { functionRenamer = id
44+-- > { functionNameBuilder = camelCase
4545-- > , requestBody = "body"
4646-- > , successCallback = "onSuccess"
4747-- > , errorCallback = "onError"
@@ -52,13 +52,32 @@ data CommonGeneratorOptions = CommonGeneratorOptions
5252defCommonGeneratorOptions :: CommonGeneratorOptions
5353defCommonGeneratorOptions = CommonGeneratorOptions
5454 {
55− functionRenamer = id
55+ functionNameBuilder = camelCase
5656 , requestBody = "body"
5757 , successCallback = "onSuccess"
5858 , errorCallback = "onError"
5959 , moduleName = ""
6060 , urlPrefix = ""
6161 }
62+
63+-- | Function name builder that simply concat each part together
64+concatCase :: FunctionName -> String
65+concatCase = concat
66+
67+-- | Function name builder using the snake_case convention.
68+-- each part is separated by a single underscore character.
69+snakeCase :: FunctionName -> String
70+snakeCase = intercalate "_"
71+
72+-- | Function name builder using the CamelCase convention.
73+-- each part begins with an upper case character.
74+camelCase :: FunctionName -> String
75+camelCase [] = ""
76+camelCase (p:ps) = concat $ p : camelCase' ps
77+ where camelCase' [] = []
78+ camelCase' (r:rs) = capitalize r : camelCase' rs
79+ capitalize [] = []
80+ capitalize (x:xs) = toUpper x : xs
6281
6382type Arg = String
6483
@@ -149,7 +168,7 @@ data Url = Url
149168defUrl :: Url
150169defUrl = Url [] []
151170
152−type FunctionName = String
171+type FunctionName = [String]
153172type Method = String
154173
155174data AjaxReq = AjaxReq
@@ -226,7 +245,7 @@ paramToStr qarg notTheEnd =
226245 where name = qarg ^. argName
227246
228247defReq :: AjaxReq
229−defReq = AjaxReq defUrl "GET" [] False ""
248+defReq = AjaxReq defUrl "GET" [] False []
230249
231250type family Elem (a :: *) (ls::[*]) :: Constraint where
232251 Elem a '[] = 'False ~ 'True
@@ -252,6 +271,7 @@ instance (KnownSymbol sym, HasJS sublayout)
252271 javascriptFor Proxy req =
253272 javascriptFor (Proxy :: Proxy sublayout) $
254273 req & reqUrl.path <>~ [Segment (Cap str) []]
274+ & funcName %~ (++ ["by", str])
255275
256276 where str = symbolVal (Proxy :: Proxy sym)
257277
@@ -259,14 +279,14 @@ instance Elem JSON list => HasJS (Delete list a) where
259279 type JS (Delete list a) = AjaxReq
260280
261281 javascriptFor Proxy req =
262− req & funcName %~ ("delete" <>)
282+ req & funcName %~ ("delete" :)
263283 & reqMethod .~ "DELETE"
264284
265285instance Elem JSON list => HasJS (Get list a) where
266286 type JS (Get list a) = AjaxReq
267287
268288 javascriptFor Proxy req =
269− req & funcName %~ ("get" <>)
289+ req & funcName %~ ("get" :)
270290 & reqMethod .~ "GET"
271291
272292instance (KnownSymbol sym, HasJS sublayout)
@@ -283,14 +303,14 @@ instance Elem JSON list => HasJS (Post list a) where
283303 type JS (Post list a) = AjaxReq
284304
285305 javascriptFor Proxy req =
286− req & funcName %~ ("post" <>)
306+ req & funcName %~ ("post" :)
287307 & reqMethod .~ "POST"
288308
289309instance Elem JSON list => HasJS (Put list a) where
290310 type JS (Put list a) = AjaxReq
291311
292312 javascriptFor Proxy req =
293− req & funcName %~ ("put" <>)
313+ req & funcName %~ ("put" :)
294314 & reqMethod .~ "PUT"
295315
296316instance (KnownSymbol sym, HasJS sublayout)
@@ -358,7 +378,7 @@ instance HasJS Raw where
358378 type JS Raw = Method -> AjaxReq
359379
360380 javascriptFor Proxy req method =
361− req & funcName %~ ((toLower <$> method) <>)
381+ req & funcName %~ ((toLower <$> method) :)
362382 & reqMethod .~ method
363383
364384instance (Elem JSON list, HasJS sublayout) => HasJS (ReqBody list a :> sublayout) where
@@ -375,7 +395,7 @@ instance (KnownSymbol path, HasJS sublayout)
375395 javascriptFor Proxy req =
376396 javascriptFor (Proxy :: Proxy sublayout) $
377397 req & reqUrl.path <>~ [Segment (Static str) []]
378− & funcName %~ (str <>)
398+ & funcName %~ (++ [str])
379399
380400 where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)
381401
servant-js/src/Servant/JS/JQuery.hs+1−1
@@ -76,7 +76,7 @@ generateJQueryJSWith opts req = "\n" <>
7676 namespace = if null (moduleName opts)
7777 then "var "
7878 else (moduleName opts) <> "."
79− fname = namespace <> (functionRenamer opts $ req ^. funcName)
79+ fname = namespace <> (functionNameBuilder opts $ req ^. funcName)
8080
8181 method = req ^. reqMethod
8282 url = if url' == "'" then "'/'" else url'
servant-js/src/Servant/JS/Vanilla.hs+1−1
@@ -83,7 +83,7 @@ generateVanillaJSWith opts req = "\n" <>
8383 namespace = if null (moduleName opts)
8484 then "var "
8585 else (moduleName opts) <> "."
86− fname = namespace <> (functionRenamer opts $ req ^. funcName)
86+ fname = namespace <> (functionNameBuilder opts $ req ^. funcName)
8787
8888 method = req ^. reqMethod
8989 url = if url' == "'" then "'/'" else url'