on develop help sign in
Adding ghetto parameter ordering
commitcf863cd16c9ba688ddce59bbc4b3b684e56a8d45 authorBrendan Hay <brendan.g.hay@gmail.com> dateFri 2 Oct 2015, 16:17 (11 years ago) parentd4d6c9c
2 files changed+35−4
gen/src/Gen/Syntax.hs+4−4 gen/src/Gen/Types/Id.hs+31
gen/src/Gen/Syntax.hs+4−4
@@ -77,9 +77,7 @@ verbAlias n m
7777 (terminalType (_type (_pParam k)))
7878 | otherwise = sing x
7979
80− -- FIXME: order by _mParameterOrder
81−-- qry :: [Type]
82− qry = mapMaybe f . Map.toList
80+ qry xs = mapMaybe f $ orderParams fst (Map.toList xs) (_mParameterOrder m)
8381 where
8482 f (k, x) =
8583 let t = terminalType (_type (_pParam x))
@@ -154,7 +152,7 @@ googleRequestDecl :: Global
154152 -> [Local]
155153 -> Method Solved
156154 -> Decl
157−googleRequestDecl g n assoc alt p api url fs m =
155+googleRequestDecl g n assoc alt p api url fields m =
158156 InstDecl noLoc Nothing [] [] (unqual "GoogleRequest") [n]
159157 [ assoc
160158 , request
@@ -196,6 +194,8 @@ googleRequestDecl g n assoc alt p api url fs m =
196194 (fs', rq) | Just r <- _mRequest m = let x = localise (ref r) in (delete x fs, Just x)
197195 | otherwise = (fs, Nothing)
198196
197+ fs = orderParams id fields (_mParameterOrder m)
198+
199199 pats = [pvar "r", pvar "u", prec]
200200 prec = PRec (UnQual (dname g)) [PFieldWildcard]
201201
gen/src/Gen/Types/Id.hs+31
@@ -3,6 +3,7 @@
33{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44{-# LANGUAGE LambdaCase #-}
55{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE ScopedTypeVariables #-}
67
78-- Module : Gen.Types.Id
89-- Copyright : (c) 2015 Brendan Hay
@@ -30,6 +31,8 @@ module Gen.Types.Id
3031
3132 , reference
3233
34+ , orderParams
35+
3336 -- * Naming
3437 , aname
3538 , mname
@@ -44,8 +47,12 @@ module Gen.Types.Id
4447 ) where
4548
4649import Data.Aeson hiding (Bool, String)
50+import Data.CaseInsensitive (CI)
4751import qualified Data.CaseInsensitive as CI
52+import Data.Coerce
53+import Data.Foldable (foldl')
4854import Data.Hashable
55+import Data.List (elemIndex, sortOn)
4956import Data.Semigroup hiding (Sum)
5057import Data.String
5158import Data.Text (Text)
@@ -146,3 +153,27 @@ reference (Global g) (Local l) = Global (g <> Text.split (== '.') l)
146153
147154localise :: Global -> Local
148155localise = Local . global
156+
157+orderParams :: (a -> Local) -> [a] -> [Local] -> [a]
158+orderParams f xs ys = orderBy f zs (del zs [] ++ global)
159+ where
160+ zs = orderBy f (sortOn f xs) ys
161+
162+ del _ [] = []
163+ del [] rs = reverse rs
164+ del (r:qs) rs
165+ | f r `elem` global = del qs rs
166+ | otherwise = del qs (f r:rs)
167+
168+ global =
169+ [ "quotaUser"
170+ , "prettyPrint"
171+ , "userIp"
172+ , "fields"
173+ , "key"
174+ , "oauth_token"
175+ , "alt"
176+ ]
177+
178+orderBy :: Eq b => (a -> b) -> [a] -> [b] -> [a]
179+orderBy g xs ys = sortOn (flip elemIndex ys . g) xs