on develop help sign in
Adding concept of repeated path parameters
commitd71bad19fee6ae75b3624736dfa10e82b8c7d709 authorBrendan Hay <brendan.g.hay@gmail.com> dateSun 4 Oct 2015, 11:48 (11 years ago) parentab17b84
4 files changed+105−28
core/src/Network/Google/Data/JSON.hs+16−2 core/src/Network/Google/Prelude.hs+67−13 gen/src/Gen/Syntax.hs+5 gen/src/Gen/Types/Id.hs+17−13
core/src/Network/Google/Data/JSON.hs+16−2
@@ -10,9 +10,23 @@
1010-- Stability : provisional
1111-- Portability : non-portable (GHC extensions)
1212--
13−module Network.Google.Data.JSON where
13+module Network.Google.Data.JSON
14+ ( module Network.Google.Data.JSON
1415
15−import Control.Lens
16+ , FromJSON (..)
17+ , ToJSON (..)
18+
19+ , Value (Object)
20+ , withObject
21+ , object
22+
23+ , (.=)
24+ , (.:)
25+ , (.:?)
26+ , (.!=)
27+ ) where
28+
29+import Control.Lens hiding ((.=))
1630import Data.Aeson
1731import Data.Aeson.Types
1832import Servant.API
core/src/Network/Google/Prelude.hs+67−13
@@ -2,8 +2,11 @@
22{-# LANGUAGE DeriveDataTypeable #-}
33{-# LANGUAGE DeriveGeneric #-}
44{-# LANGUAGE FlexibleContexts #-}
5+{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+{-# LANGUAGE MultiParamTypeClasses #-}
68{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE ScopedTypeVariables #-}
710{-# LANGUAGE TypeFamilies #-}
811{-# LANGUAGE TypeOperators #-}
912
@@ -21,50 +24,57 @@ module Network.Google.Prelude
2124 ) where
2225
2326import Control.Lens as Export (Lens', lens, mapping,
24− _Just)
27+ (^.), _Just)
2528import Control.Lens hiding (coerce)
2629import Control.Monad.Trans.Either as Export (EitherT)
27−import Data.Aeson as Export
2830import Data.Coerce
2931import Data.Data as Export (Data, Typeable)
3032import Data.Hashable as Export
3133import Data.Int as Export (Int32, Int64)
3234import Data.Maybe as Export
35+import Data.Monoid
3336import Data.Proxy as Export
3437import Data.Text as Export (Text)
35−import Data.Time as Export (UTCTime)
38+import qualified Data.Text as Text
39+import Data.Time as Export
3640import Data.Word as Export (Word32, Word64, Word8)
3741import GHC.Generics as Export (Generic)
42+import GHC.TypeLits
3843import Network.Google.Data.JSON as Export
3944import Network.Google.Data.Numeric as Export
40−import Network.HTTP.Media
45+-- import Network.HTTP.Media
4146import Numeric.Natural as Export (Natural)
4247import Servant.API as Export
4348import Servant.Client as Export
4449import Servant.Common.Req as Export (Req, defReq)
50+import Servant.Common.Req (appendToPath)
4551import Servant.Utils.Links as Export
4652
4753_Coerce :: (Coercible a b, Coercible b a) => Iso' a b
4854_Coerce = iso coerce coerce
4955
50−-- | Invalid Iso, should be a Prism but exists for ease of composition
51−-- with the current 'Lens . Iso' chaining to hide internal types from the user.
56+-- | Invalid Iso, exists for ease of composition with the current 'Lens . Iso'
57+-- chaining to hide internal types from the user.
5258_Default :: Monoid a => Iso' (Maybe a) a
5359_Default = iso f Just
5460 where
5561 f (Just x) = x
5662 f Nothing = mempty
5763
58−newtype Time' = Time' Text
59− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToText, FromText)
64+_Time = undefined
65+_Date = undefined
66+_DateTime = undefined
6067
61−newtype Date' = Date' Text
62− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToText, FromText)
68+newtype Time' = Time' Text -- TimeOfDay
69+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, FromText, ToText, FromJSON, ToJSON)
6370
64−newtype DateTime' = DateTime' Text
65− deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, ToText, FromText)
71+newtype Date' = Date' Text -- LocalTime
72+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, FromText, ToText, FromJSON, ToJSON)
73+
74+newtype DateTime' = DateTime' Text -- UTCTime
75+ deriving (Eq, Ord, Show, Read, Generic, Data, Typeable, FromText, ToText, FromJSON, ToJSON)
6676
67−data AltJSON = AltJSON
77+data AltJSON = AltJSON
6878 deriving (Eq, Ord, Show, Read, Generic, Data, Typeable)
6979
7080instance ToText AltJSON where
@@ -105,8 +115,52 @@ instance GoogleAuth a => GoogleAuth (Download a) where
105115data Body = Body -- MediaType
106116 deriving (Eq, Show, Generic, Data, Typeable)
107117
118+data Captures (capture :: Symbol) a
119+ deriving (Typeable)
120+
121+instance (KnownSymbol capture, ToText a, HasClient sublayout)
122+ => HasClient (Captures capture a :> sublayout) where
123+ type Client (Captures capture a :> sublayout) = [a] -> Client sublayout
124+
125+ clientWithRoute Proxy req baseUrl xs =
126+ clientWithRoute (Proxy :: Proxy sublayout) (appendToPath vs req) baseUrl
127+ where
128+ vs = Text.unpack $ Text.intercalate "/" (map toText xs)
129+
130+data CaptureMode (capture :: Symbol) (mode :: Symbol) a
131+ deriving (Typeable)
132+
133+instance (KnownSymbol capture, KnownSymbol mode, ToText a, HasClient sublayout)
134+ => HasClient (CaptureMode capture mode a :> sublayout) where
135+ type Client (CaptureMode capture mode a :> sublayout) = a -> Client sublayout
136+
137+ clientWithRoute Proxy req baseUrl val =
138+ clientWithRoute (Proxy :: Proxy sublayout) (appendToPath p req) baseUrl
139+ where
140+ p = Text.unpack (toText val) <> m
141+ m = symbolVal (Proxy :: Proxy mode)
142+
143+instance MimeUnrender OctetStream Body
108144
109145data MultipartRelated (metatypes :: [*]) meta media
146+ deriving (Typeable)
147+
148+instance (MimeUnrender ct a, HasClient sublayout)
149+ => HasClient (MultipartRelated (ct ': cts) a b :> sublayout) where
150+
151+ type Client (MultipartRelated (ct ': cts) a b :> sublayout) =
152+ a -> b -> Client sublayout
153+
154+ clientWithRoute Proxy req baseurl meta media = undefined
155+
156+ -- clientWithRoute (Proxy :: Proxy sublayout)
157+ -- (let ctProxy = Proxy :: Proxy ct
158+ -- in setRQBody (mimeRender ctProxy body)
159+ -- (contentType ctProxy)
160+ -- req
161+ -- )
162+ -- baseurl
163+
110164
111165-- IsElem sa (ReqBody y x :> sb) = IsElem sa sb
112166
gen/src/Gen/Syntax.hs+5
@@ -71,6 +71,11 @@ verbAlias n m
7171 Nothing -> error $ "Unable to find path parameter " ++ show l
7272 Just x ->
7373 case c of
74+ Nothing | x ^. iRepeated ->
75+ TyApp (TyApp (TyCon "Captures")
76+ (sing (local l)))
77+ (terminalType (_type (_pParam x)))
78+
7479 Nothing ->
7580 TyApp (TyApp (TyCon "Capture")
7681 (sing (local l)))
gen/src/Gen/Types/Id.hs+17−13
@@ -4,7 +4,7 @@
44{-# LANGUAGE LambdaCase #-}
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE ScopedTypeVariables #-}
7−{-# LANGUAGE ViewPatterns #-}
7+{-# LANGUAGE TupleSections #-}
88
99-- Module : Gen.Types.Id
1010-- Copyright : (c) 2015 Brendan Hay
@@ -51,7 +51,10 @@ module Gen.Types.Id
5151 , pname
5252 ) where
5353
54+import Control.Applicative
55+import Control.Monad
5456import Data.Aeson hiding (Bool, String)
57+import qualified Data.Attoparsec.Text as A
5558import Data.CaseInsensitive (CI)
5659import qualified Data.CaseInsensitive as CI
5760import Data.Coerce
@@ -168,22 +171,23 @@ globalise :: Local -> Global
168171globalise = Global . (:[]) . local
169172
170173extractPath :: Text -> [Either Text (Local, Maybe Text)]
171−extractPath = map match . filter (not . Text.null) . Text.split (== '/')
174+extractPath = either (error . show) id . A.parseOnly path
172175 where
173− match x =
174− case Text.uncons x of
175− Just (c, Text.dropWhile (== '+') -> t)
176− | c == '{' -> Right $
177− let y = toKey (Text.takeWhile (/= '}') t)
178− in case Text.dropWhile (/= ':') t of
179− "" -> (y, Nothing)
180− z -> (y, Just z)
176+ path = A.many1 (seg <|> repeat <|> var) <* A.endOfInput
181177
182− -- Resource "editId" "commit"
183− -- "path": "{packageName}/edits/{editId}:commit",
178+ seg = fmap Left $
179+ optional (A.char '/') *> A.takeWhile1 (A.notInClass "/{+*}:")
184180
185− _ -> Left x
181+ repeat = fmap Right $ do
182+ A.string "{/"
183+ (,Nothing) <$> fmap Local (A.takeWhile1 (/= '*'))
184+ <* A.string "*}"
186185
186+ var = fmap Right $ do
187+ void $ optional (A.char '/') *> A.char '{'
188+ (,) <$> fmap Local (A.takeWhile1 (A.notInClass "/{+*}:"))
189+ <* A.char '}'
190+ <*> optional (A.char ':' *> A.takeWhile1 (A.notInClass "/{+*}:"))
187191
188192orderParams :: (a -> Local) -> [a] -> [Local] -> [a]
189193orderParams f xs ys = orderBy f zs (del zs [] ++ global)