@@ -35,10 +35,12 @@ import Servant.API
3535import Servant.API.ContentTypes (AllMimeRender, allMimeRender)
3636import Servant.Client
3737-- import Test.QuickCheck (Arbitrary, Gen, elements, frequency)
38−import Data.Dynamic (Dynamic)
38+
39+import Data.Dynamic (Dynamic, fromDynamic)
3940import Data.Map.Strict (Map)
41+import qualified Data.Map.Strict as Map
4042import Data.Maybe
41−import Data.Typeable (TypeRep)
43+import Data.Typeable (TypeRep, Typeable, typeRep)
4244import Hedgehog
4345import qualified Hedgehog.Gen as Gen
4446import Roboservant.Hedgehog
@@ -67,6 +69,14 @@ instance ( HasContextualGenRequest a
6769
6870withGeneratable p store cont = cont <$> genContextualRequest p store
6971
72+getCandidates p st cont =
73+ case Map.lookup (typeRep p) st of
74+ Nothing -> (0,error "shouldn't ever be invoked")
75+ Just x -> do
76+ case fromDynamic x of
77+ Nothing -> (0,error "shouldn't ever be invoked")
78+ Just candidates -> cont candidates
79+
7080instance (KnownSymbol path, HasContextualGenRequest b )
7181 => HasContextualGenRequest (path :> b) where
7282 genContextualRequest _ store =
@@ -81,42 +91,48 @@ instance (KnownSymbol path, HasContextualGenRequest b )
8191 in r { path = "/" <> BS.intercalate "/" paths }
8292
8393 )
84−-- , pure ( \burl -> do
85−-- _
86− -- fmap (Just . (1,)) $ return $ \burl st ->
8794 where
8895 -- (oldf, old) = genContextualRequest (Proxy :: Proxy b) store
8996 new = cs $ symbolVal (Proxy :: Proxy path)
9097
91−-- instance HasContextualGenRequest EmptyAPI st where
92−-- genContextualRequest _ = (0, error "EmptyAPIs cannot be queried.")
98+instance HasContextualGenRequest EmptyAPI where
99+ genContextualRequest _ _ = Nothing
93100
94−-- instance HasContextualGenRequest api st => HasContextualGenRequest (Summary d :> api) st where
95−-- genContextualRequest _ = genContextualRequest (Proxy :: Proxy api)
101+instance HasContextualGenRequest api => HasContextualGenRequest (Summary d :> api) where
102+ genContextualRequest _ = genContextualRequest (Proxy :: Proxy api)
96103
97−-- instance HasContextualGenRequest api st => HasContextualGenRequest (Description d :> api) st where
98−-- genContextualRequest _ = genContextualRequest (Proxy :: Proxy api)
104+instance HasContextualGenRequest api => HasContextualGenRequest (Description d :> api) where
105+ genContextualRequest _ = genContextualRequest (Proxy :: Proxy api)
99106
100−-- instance (HasContextualGenRequest b st, ToHttpApiData c )
101−-- => HasContextualGenRequest (Capture' mods x c :> b) st where
102−-- genContextualRequest _ = (oldf, do
103−-- old' <- old
104−-- new' <- toUrlPiece <$> _generator
105−-- return $ \burl st -> let r = old' burl st in r { path = cs new' <> path r })
106−-- where
107−-- (oldf, old) = genContextualRequest (Proxy :: Proxy b)
107+instance (HasContextualGenRequest b, Typeable c, ToHttpApiData c )
108+ => HasContextualGenRequest (Capture' mods x c :> b) where
109+ genContextualRequest _ st = withGeneratable (Proxy :: Proxy b) st $ \(oldf,old) ->
110+ getCandidates (Proxy :: Proxy c) st $ \candidates ->
111+ (oldf
112+ ,do
113+ piece :: c <- Gen.choice candidates
114+ old' <- old
115+ pure $ \burl ->
116+ let r = old' burl
117+ in r { path = cs (toUrlPiece piece) <> path r })
108118
109−-- instance (HasContextualGenRequest b st, ToHttpApiData c )
110−-- => HasContextualGenRequest (CaptureAll x c :> b) st where
111−-- genContextualRequest _ = (oldf, do
112−-- old' <- old
113−-- new' <- fmap (cs . toUrlPiece) <$> new
114−-- let new'' = BS.intercalate "/" new'
115−-- return $ \burl st -> let r = old' burl st
116−-- in r { path = new'' <> path r })
117−-- where
118−-- (oldf, old) = genContextualRequest (Proxy :: Proxy b)
119−-- new = _arbitrary :: Gen [c]
119+-- unsure how CaptureAll works, finish later.
120+-- instance (HasContextualGenRequest b, ToHttpApiData c, Typeable c )
121+-- => HasContextualGenRequest (CaptureAll x c :> b) where
122+-- genContextualRequest _ st = withGeneratable (Proxy :: Proxy b) st $ \(oldf,old) ->
123+-- getCandidates (Proxy :: Proxy c) st $ \candidates ->
124+-- (oldf, do
125+-- old' <- old
126+-- piece :: c <- Gen.choice candidates
127+-- let new'' = BS.intercalate "/" . fmap (cs . toUrlPiece) $ piece
128+
129+
130+
131+-- return $ \burl -> let r = old' burl
132+-- in r { path = new'' <> path r })
133+
134+-- -- (oldf, old) = genContextualRequest (Proxy :: Proxy b)
135+-- -- new = _arbitrary :: Gen [c]
120136
121137-- instance (KnownSymbol h, HasContextualGenRequest b st, ToHttpApiData c)
122138-- => HasContextualGenRequest (Header' mods h c :> b) st where