@@ -14,17 +14,18 @@ module Gen.Syntax where
1414
1515import Control.Lens hiding (iso, mapping, op, pre,
1616 strict)
17+import Data.Either
1718import Data.Foldable (find, foldl', foldr')
1819import Data.Hashable
1920import qualified Data.HashMap.Strict as Map
20−import Data.List (delete)
21−import Data.Maybe
21+import Data.List (delete, nub)
2222import Data.Maybe
2323import Data.Semigroup ((<>))
2424import Data.String
2525import Data.Text (Text)
2626import qualified Data.Text as Text
2727import Data.Text.Manipulate
28+import Debug.Trace
2829import Gen.Orphans
2930import Gen.Text
3031import Gen.Types
@@ -56,26 +57,31 @@ verbAlias n m
5657 where
5758 rest = alias verb (path ++ qry params ++ media)
5859 download = TyInfix rest (UnQual (sym ":<|>")) $
59− alias (TyApp (TyApp meth octet) (TyCon "Stream"))
60+ alias (TyApp (TyApp meth octet) (TyCon "Body"))
6061 (path ++ qry (Map.delete "alt" (_mParameters m)) ++ [alt] ++ media)
6162
6263 alias = foldr' (\l r -> TyInfix l (UnQual (sym ":>")) r)
6364
6465 path :: [Type]
65− path = map match
66− . filter (not . Text.null)
67− $ Text.split (== '/') (_mPath m)
66+ path = map match $ extractPath (_mPath m)
6867 where
69− -- FIXME: should error if '{' '}' is found and cannot find the ref.
70− match x
71− | Just (c, t) <- Text.uncons x
72− , c == '{'
73− , Just k <- Map.lookup (toKey (Text.init t)) params
74− , _pLocation k == Path
75− = TyApp (TyApp (TyCon "Capture")
76− (sing (Text.init t)))
77− (terminalType (_type (_pParam k)))
78− | otherwise = sing x
68+ match (Left t) = sing t
69+ match (Right (l, c)) =
70+ case Map.lookup l (_mParameters m) of
71+ Nothing -> error $ "Unable to find path parameter " ++ show l
72+ Just x ->
73+ case c of
74+ Nothing ->
75+ TyApp (TyApp (TyCon "Capture")
76+ (sing (local l)))
77+ (terminalType (_type (_pParam x)))
78+
79+ Just y ->
80+ TyApp (TyApp (TyApp (TyCon "CaptureMode")
81+ (sing (local l)))
82+ (sing y))
83+ (terminalType (_type (_pParam x)))
84+
7985
8086 qry xs = mapMaybe f $ orderParams fst (Map.toList xs) (_mParameterOrder m)
8187 where
@@ -106,9 +112,9 @@ verbAlias n m
106112 octet = tylist ["OctetStream"]
107113 json = tylist ["JSON"]
108114
109− alt = TyApp (TyApp (TyCon "QueryParam") (sing "alt")) (TyCon "Media")
115+ alt = TyApp (TyApp (TyCon "QueryParam") (sing "alt")) (TyCon "AltMedia")
110116
111− params = _mParameters m
117+ params = Map.filter ((/= Path) . _pLocation) (_mParameters m)
112118
113119downloadDecl :: Global
114120 -> Prefix
@@ -122,7 +128,7 @@ downloadDecl n p api url fs m =
122128 where
123129 rs = InsType noLoc
124130 (TyApp (TyCon "Rs") (TyApp (TyCon "Download") (tycon n)))
125− (TyCon "Stream")
131+ (TyCon "Body")
126132
127133 alt = Just (var "Media")
128134
@@ -191,10 +197,13 @@ googleRequestDecl g n assoc alt p api url fields m =
191197 ps = _mParameters m
192198 v = var . fname p
193199
194− (fs', rq) | Just r <- _mRequest m = let x = localise (ref r) in (delete x fs, Just x)
200+ (fs', rq) | Just _ <- _mRequest m = (delete "payload" fs, Just "payload")
195201 | otherwise = (fs, Nothing)
196202
197− fs = orderParams id fields (_mParameterOrder m)
203+ fs = delete "alt"
204+ . orderParams id (Map.keys (_mParameters m))
205+ . nub
206+ $ map fst (rights (extractPath (_mPath m))) ++ _mParameterOrder m
198207
199208 pats = [pvar "r", pvar "u", prec]
200209 prec = PRec (UnQual (dname g)) [PFieldWildcard]